Forráskód Böngészése

新建电信物联网平台上传北向数据

zhuoning 4 éve
szülő
commit
45b0b8c1d9

+ 15 - 0
huimv-test-platform/huimv-ctwing/pom.xml

@@ -0,0 +1,15 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <parent>
+        <artifactId>huimv-test-platform</artifactId>
+        <groupId>com.huimv</groupId>
+        <version>0.0.1-SNAPSHOT</version>
+    </parent>
+    <modelVersion>4.0.0</modelVersion>
+
+    <artifactId>huimv-ctwing</artifactId>
+
+
+</project>

+ 13 - 0
huimv-test-platform/huimv-ctwing/src/main/java/com/huimv/devicedata/DevicedataApplication.java

@@ -0,0 +1,13 @@
+package com.huimv.devicedata;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+
+@SpringBootApplication
+public class DevicedataApplication {
+
+    public static void main(String[] args) {
+        SpringApplication.run(DevicedataApplication.class, args);
+    }
+
+}

+ 64 - 0
huimv-test-platform/huimv-ctwing/src/main/java/com/huimv/devicedata/controller/DeviceDataController.java

@@ -0,0 +1,64 @@
+package com.huimv.devicedata.controller;
+
+import com.alibaba.fastjson.JSONObject;
+import com.huimv.devicedata.service.IDeviceDataService;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
+import org.springframework.web.bind.annotation.RestController;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.BufferedReader;
+import java.io.InputStreamReader;
+import java.util.Calendar;
+import java.util.Date;
+
+/**
+ * @Project : huimv.shiwan
+ * @Package : com.huimv.biosafety.uface.controller
+ * @Description : TODO
+ * @Version : 1.0
+ * @Author : ZhuoNing
+ * @Create : 2020-12-25
+ **/
+@RestController
+@RequestMapping(value = "/deviceData",method = RequestMethod.POST)
+public class DeviceDataController {
+    public static Logger logger = LoggerFactory.getLogger(DeviceDataController.class);
+    @Autowired
+    private IDeviceDataService deviceDataService;
+
+    @RequestMapping(value = "/deviceChange",method = {RequestMethod.POST,RequestMethod.GET})
+    public String deviceUpAndDown(HttpServletRequest request, HttpServletResponse response){
+        long startTime = Calendar.getInstance().getTimeInMillis();
+        Date startDate = new Date(startTime);
+        java.text.SimpleDateFormat f = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+        logger.info("开始接收时间:"+f.format(startDate));
+
+        StringBuffer resultStr = new StringBuffer("");
+        try{
+            BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream(),"utf-8"));
+            StringBuffer sb = new StringBuffer("");
+            String temp;
+            while ((temp = br.readLine()) != null){
+                sb.append(temp);
+            }
+            br.close();
+            //真正接收到的数据
+            String result = sb.toString();
+            System.out.println("接收到数据result>>"+result);
+//          result = "{'upPacketSN':-1,'upDataSN':-1,'topic':'v1/up/ad19','timestamp':1617172207604,'tenantId':'2000031842','serviceId':12,'protocol':'lwm2m','productId':'15039192','payload':{'VER':1,'TIME':15,'TEMP':201,'STC':94,'RSSI':18,'HUMI':564,'BAT':3600},'messageType':'dataReport','deviceType':'','deviceId':'488200bd80e14a97826c95834dee135e','assocAssetId':'','IMSI':'undefined','IMEI':'864333043632975'}";
+            JSONObject deviceUpDataJson = JSONObject.parseObject(result);
+            // 保存设备上传数据
+            deviceDataService.saveDeviceChangedUpData(deviceUpDataJson);
+        }catch (Exception e){
+            e.printStackTrace();
+        }
+//        return resultStr;
+
+        return "200";
+    }
+}

+ 235 - 0
huimv-test-platform/huimv-ctwing/src/main/java/com/huimv/devicedata/entity/DeviceUpdataEntity.java

@@ -0,0 +1,235 @@
+package com.huimv.devicedata.entity;
+
+import javax.persistence.*;
+import java.io.Serializable;
+
+@Entity
+@Table(name = "device_up_data")
+public class DeviceUpdataEntity implements Serializable {
+
+    private static final long serialVersionUID = 1L;
+
+    @Id
+    @GeneratedValue(strategy = GenerationType.IDENTITY)
+    @Column(name = "id", nullable = false)
+    private Integer id;
+
+    @Column(name = "product_id")
+    private String productId;
+
+    @Column(name = "device_id")
+    private String deviceId;
+
+    @Column(name = "timestamp")
+    private String timestamp;
+
+    @Column(name = "service_id")
+    private String serviceId;
+
+    @Column(name = "protocol")
+    private String protocol;
+
+    @Column(name = "imsi")
+    private String imsi;
+
+    @Column(name = "imei")
+    private String imei;
+
+    @Column(name = "device_type")
+    private String deviceType;
+
+    @Column(name = "message_type")
+    private String messageType;
+
+    @Column(name = "data_ver")
+    private Integer dataVer;
+
+    @Column(name = "data_time")
+    private Integer dataTime;
+
+    @Column(name = "data_temp")
+    private Integer dataTemp;
+
+    @Column(name = "data_stc")
+    private Integer dataStc;
+
+    @Column(name = "data_rssi")
+    private Integer dataRssi;
+
+    @Column(name = "data_humi")
+    private Integer dataHumi;
+
+    @Column(name = "data_bat")
+    private Integer dataBat;
+
+    @Column(name = "tenant_id")
+    private String tenantId;
+
+    public void setId(Integer id) {
+        this.id = id;
+    }
+
+    public Integer getId() {
+        return id;
+    }
+
+    public void setProductId(String productId) {
+        this.productId = productId;
+    }
+
+    public String getProductId() {
+        return productId;
+    }
+
+    public void setDeviceId(String deviceId) {
+        this.deviceId = deviceId;
+    }
+
+    public String getDeviceId() {
+        return deviceId;
+    }
+
+    public void setTimestamp(String timestamp) {
+        this.timestamp = timestamp;
+    }
+
+    public String getTimestamp() {
+        return timestamp;
+    }
+
+    public void setServiceId(String serviceId) {
+        this.serviceId = serviceId;
+    }
+
+    public String getServiceId() {
+        return serviceId;
+    }
+
+    public void setProtocol(String protocol) {
+        this.protocol = protocol;
+    }
+
+    public String getProtocol() {
+        return protocol;
+    }
+
+    public void setImsi(String imsi) {
+        this.imsi = imsi;
+    }
+
+    public String getImsi() {
+        return imsi;
+    }
+
+    public void setImei(String imei) {
+        this.imei = imei;
+    }
+
+    public String getImei() {
+        return imei;
+    }
+
+    public void setDeviceType(String deviceType) {
+        this.deviceType = deviceType;
+    }
+
+    public String getDeviceType() {
+        return deviceType;
+    }
+
+    public void setMessageType(String messageType) {
+        this.messageType = messageType;
+    }
+
+    public String getMessageType() {
+        return messageType;
+    }
+
+    public void setDataVer(Integer dataVer) {
+        this.dataVer = dataVer;
+    }
+
+    public Integer getDataVer() {
+        return dataVer;
+    }
+
+    public void setDataTime(Integer dataTime) {
+        this.dataTime = dataTime;
+    }
+
+    public Integer getDataTime() {
+        return dataTime;
+    }
+
+    public void setDataTemp(Integer dataTemp) {
+        this.dataTemp = dataTemp;
+    }
+
+    public Integer getDataTemp() {
+        return dataTemp;
+    }
+
+    public void setDataStc(Integer dataStc) {
+        this.dataStc = dataStc;
+    }
+
+    public Integer getDataStc() {
+        return dataStc;
+    }
+
+    public void setDataRssi(Integer dataRssi) {
+        this.dataRssi = dataRssi;
+    }
+
+    public Integer getDataRssi() {
+        return dataRssi;
+    }
+
+    public void setDataHumi(Integer dataHumi) {
+        this.dataHumi = dataHumi;
+    }
+
+    public Integer getDataHumi() {
+        return dataHumi;
+    }
+
+    public void setDataBat(Integer dataBat) {
+        this.dataBat = dataBat;
+    }
+
+    public Integer getDataBat() {
+        return dataBat;
+    }
+
+    public String getTenantId() {
+        return tenantId;
+    }
+
+    public void setTenantId(String tenantId) {
+        this.tenantId = tenantId;
+    }
+
+    @Override
+    public String toString() {
+        return "DeviceUpdataEntity{" +
+                "id=" + id +
+                ", productId='" + productId + '\'' +
+                ", deviceId='" + deviceId + '\'' +
+                ", timestamp=" + timestamp +
+                ", serviceId='" + serviceId + '\'' +
+                ", protocol='" + protocol + '\'' +
+                ", imsi='" + imsi + '\'' +
+                ", imei='" + imei + '\'' +
+                ", deviceType='" + deviceType + '\'' +
+                ", messageType='" + messageType + '\'' +
+                ", dataVer=" + dataVer +
+                ", dataTime=" + dataTime +
+                ", dataTemp=" + dataTemp +
+                ", dataStc=" + dataStc +
+                ", dataRssi=" + dataRssi +
+                ", dataHumi=" + dataHumi +
+                ", dataBat=" + dataBat +
+                ", tenantId='" + tenantId + '\'' +
+                '}';
+    }
+}

+ 9 - 0
huimv-test-platform/huimv-ctwing/src/main/java/com/huimv/devicedata/repo/DeviceUpdataEntityRepository.java

@@ -0,0 +1,9 @@
+package com.huimv.devicedata.repo;
+
+import com.huimv.devicedata.entity.DeviceUpdataEntity;
+import org.springframework.data.jpa.repository.JpaRepository;
+import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
+
+public interface DeviceUpdataEntityRepository extends JpaRepository<DeviceUpdataEntity, Integer>, JpaSpecificationExecutor<DeviceUpdataEntity> {
+
+}

+ 8 - 0
huimv-test-platform/huimv-ctwing/src/main/java/com/huimv/devicedata/service/IDeviceDataService.java

@@ -0,0 +1,8 @@
+package com.huimv.devicedata.service;
+
+import com.alibaba.fastjson.JSONObject;
+
+public interface IDeviceDataService {
+
+    void saveDeviceChangedUpData(JSONObject deviceUpDataJson);
+}

+ 71 - 0
huimv-test-platform/huimv-ctwing/src/main/java/com/huimv/devicedata/service/impl/DeviceDataServiceImpl.java

@@ -0,0 +1,71 @@
+package com.huimv.devicedata.service.impl;
+
+import com.alibaba.fastjson.JSONObject;
+
+import com.huimv.devicedata.entity.DeviceUpdataEntity;
+import com.huimv.devicedata.repo.DeviceUpdataEntityRepository;
+import com.huimv.devicedata.service.IDeviceDataService;
+import com.huimv.devicedata.utils.DateUtil;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+/**
+ * @Project : huimv.shiwan
+ * @Package : com.huimv.biosafety.uface.controller
+ * @Description : TODO
+ * @Version : 1.0
+ * @Author : ZhuoNing
+ * @Create : 2020-12-25
+ **/
+@Service
+public class DeviceDataServiceImpl implements IDeviceDataService {
+
+    @Autowired
+    private DeviceUpdataEntityRepository deviceUpdataEntityRepo;
+    @Autowired
+    private DateUtil dateUtil;
+
+    @Override
+    public void saveDeviceChangedUpData(JSONObject deviceUpDataJo) {
+        String productId = deviceUpDataJo.getString("productId");
+        String deviceId = deviceUpDataJo.getString("deviceId");
+        long timestamp = deviceUpDataJo.getLong("timestamp");
+        String upDataDate = dateUtil.formatLongDate(timestamp);
+        String serviceId = deviceUpDataJo.getString("serviceId");
+        String imsi = deviceUpDataJo.getString("IMSI");
+        String imei = deviceUpDataJo.getString("IMEI");
+        String deviceType = deviceUpDataJo.getString("deviceType");
+        String messageType = deviceUpDataJo.getString("messageType");
+        String protocol = deviceUpDataJo.getString("protocol");
+        String tenantId = deviceUpDataJo.getString("tenantId");
+        JSONObject businessDataJo = deviceUpDataJo.getJSONObject("payload");
+        int dataVer = businessDataJo.getInteger("VER");
+        int dataTime = businessDataJo.getInteger("TIME");
+        int dataTemp = businessDataJo.getInteger("TEMP");
+        int dataStc = businessDataJo.getInteger("STC");
+        int dataRssi = businessDataJo.getInteger("RSSI");
+        int dataHumi = businessDataJo.getInteger("HUMI");
+        int dataBat = businessDataJo.getInteger("BAT");
+        DeviceUpdataEntity deviceUpdataEntity = new DeviceUpdataEntity();
+        deviceUpdataEntity.setProductId(productId);
+        deviceUpdataEntity.setDeviceId(deviceId);
+        deviceUpdataEntity.setTimestamp(upDataDate);
+        deviceUpdataEntity.setServiceId(serviceId);
+        deviceUpdataEntity.setImei(imei);
+        deviceUpdataEntity.setImsi(imsi);
+        deviceUpdataEntity.setDeviceType(deviceType);
+        deviceUpdataEntity.setMessageType(messageType);
+        deviceUpdataEntity.setProtocol(protocol);
+        deviceUpdataEntity.setDataVer(dataVer);
+        deviceUpdataEntity.setDataTime(dataTime);
+        deviceUpdataEntity.setDataTemp(dataTemp);
+        deviceUpdataEntity.setDataStc(dataStc);
+        deviceUpdataEntity.setDataRssi(dataRssi);
+        deviceUpdataEntity.setDataHumi(dataHumi);
+        deviceUpdataEntity.setDataBat(dataBat);
+        deviceUpdataEntity.setTenantId(tenantId);
+        //
+        deviceUpdataEntityRepo.saveAndFlush(deviceUpdataEntity);
+    }
+}
+

+ 33 - 0
huimv-test-platform/huimv-ctwing/src/main/java/com/huimv/devicedata/utils/DateUtil.java

@@ -0,0 +1,33 @@
+package com.huimv.devicedata.utils;
+
+import org.springframework.stereotype.Component;
+
+import java.util.Date;
+
+/**
+ * @Project : huimv.shiwan
+ * @Package : com.huimv.biosafety.uface.controller
+ * @Description : TODO
+ * @Version : 1.0
+ * @Author : ZhuoNing
+ * @Create : 2020-12-25
+ **/
+@Component
+public class DateUtil {
+
+    /**
+     * @Method      : formatLongDate
+     * @Description : 
+     * @Params      : [dateLong]
+     * @Return      : java.lang.String
+     * 
+     * @Author      : ZhuoNing
+     * @Date        : 2021/3/31       
+     * @Time        : 14:58
+     */
+    public String formatLongDate(long dateLong){
+        Date date = new Date(dateLong);
+        java.text.SimpleDateFormat f = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+        return f.format(date);
+    }
+}

+ 31 - 0
huimv-test-platform/huimv-ctwing/src/main/resources/application-dev.yml

@@ -0,0 +1,31 @@
+server:
+  port: 8075
+spring:
+  application:
+    name: huimv-ctwing
+
+  datasource:
+    url: jdbc:mysql://192.168.1.7:3306/test_platform_device?useUnicode=true&characterEncoding=UTF-8&useSSL=false&autoReconnect=true&serverTimezone=Asia/Shanghai
+    username: root
+    password: hm123456
+    driver-class-name: com.mysql.cj.jdbc.Driver
+  jpa:
+    show-sql: true
+    database: mysql
+    hibernate:
+      ddl-auto: update
+    database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
+    open-in-view: true
+
+    #  redis:
+#    database: 0
+#    host: 119.3.84.55
+#    port: 6379
+#    password: hm123456
+#    jedis:
+#      pool:
+#        max-active: 20
+#        max-wait: -1
+#        max-idle: 10
+#        min-idle: 0
+#    timeout: 5000

+ 1 - 0
huimv-test-platform/huimv-ctwing/src/main/resources/application.properties

@@ -0,0 +1 @@
+spring.profiles.active=dev