瀏覽代碼

设备命令响应

yinhao 4 年之前
父節點
當前提交
ead227d07c

+ 42 - 12
huimv-test-platform/huimv-ctwing/src/main/java/com/huimv/devicedata/controller/DeviceDataController.java

@@ -5,14 +5,14 @@ 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 org.springframework.web.bind.annotation.*;
 
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import java.io.BufferedReader;
+import java.io.IOException;
 import java.io.InputStreamReader;
+import java.nio.charset.StandardCharsets;
 import java.util.Calendar;
 import java.util.Date;
 
@@ -25,40 +25,70 @@ import java.util.Date;
  * @Create : 2020-12-25
  **/
 @RestController
-@RequestMapping(value = "/deviceData",method = RequestMethod.POST)
+@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){
+    @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));
+        logger.info("开始接收时间:" + f.format(startDate));
 
         StringBuffer resultStr = new StringBuffer("");
-        try{
-            BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream(),"utf-8"));
+        try {
+            BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream(), "utf-8"));
             StringBuffer sb = new StringBuffer("");
             String temp;
-            while ((temp = br.readLine()) != null){
+            while ((temp = br.readLine()) != null) {
                 sb.append(temp);
             }
             br.close();
             //真正接收到的数据
             String result = sb.toString();
-            System.out.println("接收到数据result>>"+result);
+            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){
+        } catch (Exception e) {
             e.printStackTrace();
         }
 //        return resultStr;
 
         return "200";
     }
+
+
+    /**
+     * 设备命令响应Controller
+     *
+     * @param request
+     * @param response
+     * @return
+     */
+    @RequestMapping("/deviceResponse")
+    public String deviceResponse(HttpServletRequest request, HttpServletResponse response) {
+        try {
+            //字符缓冲流接收数据
+            BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream(), StandardCharsets.UTF_8));
+            StringBuilder sb = new StringBuilder();
+            String lineData;
+            while ((lineData = br.readLine()) != null) {
+                sb.append(lineData);
+            }
+            br.close();
+            String result = sb.toString();
+            System.out.println("设备指令响应通知数据-->" + result);
+            JSONObject deviceResponseData = JSONObject.parseObject(result);
+            deviceDataService.saveDeviceResponseData(deviceResponseData);
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        return "200";
+
+    }
 }

+ 131 - 0
huimv-test-platform/huimv-ctwing/src/main/java/com/huimv/devicedata/entity/DeviceResponseEntity.java

@@ -0,0 +1,131 @@
+package com.huimv.devicedata.entity;
+
+import javax.persistence.*;
+import java.io.Serializable;
+
+/**
+ * @Author yinhao
+ * @Date 2021/3/31 17:51
+ * @Description
+ */
+@Entity
+@Table(name = "device_response")
+public class DeviceResponseEntity implements Serializable {
+    private static final long serialVersionUID = 1L;
+
+    @Id
+    @GeneratedValue(strategy = GenerationType.IDENTITY)
+    @Column(name = "id", nullable = false)
+    private Integer id;
+
+    @Column(name = "tenan_id")
+    private String tenanId;
+
+    @Column(name = "product_id")
+    private String productId;
+
+    @Column(name = "message_type")
+    private String messageType;
+
+    @Column(name = "device_id")
+    private String deviceId;
+
+    @Column(name = "task_id")
+    private Integer taskId;
+
+    @Column(name = "result_code")
+    private String resultCode;
+
+    @Column(name = "result_detail_length")
+    private Integer resultDetailLength;
+
+    @Column(name = "result_detail_rsa_data")
+    private String resultDetailRspData;
+
+    public Integer getId() {
+        return id;
+    }
+
+    public void setId(Integer id) {
+        this.id = id;
+    }
+
+    public String getProductId() {
+        return productId;
+    }
+
+    public void setProductId(String productId) {
+        this.productId = productId;
+    }
+
+    public String getTenanId() {
+        return tenanId;
+    }
+
+    public void setTenanId(String tenanId) {
+        this.tenanId = tenanId;
+    }
+
+    public String getMessageType() {
+        return messageType;
+    }
+
+    public void setMessageType(String messageType) {
+        this.messageType = messageType;
+    }
+
+    public String getDeviceId() {
+        return deviceId;
+    }
+
+    public void setDeviceId(String deviceId) {
+        this.deviceId = deviceId;
+    }
+
+    public Integer getTaskId() {
+        return taskId;
+    }
+
+    public void setTaskId(Integer taskId) {
+        this.taskId = taskId;
+    }
+
+    public String getResultCode() {
+        return resultCode;
+    }
+
+    public void setResultCode(String resultCode) {
+        this.resultCode = resultCode;
+    }
+
+    public Integer getResultDetailLength() {
+        return resultDetailLength;
+    }
+
+    public void setResultDetailLength(Integer resultDetailLength) {
+        this.resultDetailLength = resultDetailLength;
+    }
+
+    public String getResultDetailRspData() {
+        return resultDetailRspData;
+    }
+
+    public void setResultDetailRspData(String resultDetailRspData) {
+        this.resultDetailRspData = resultDetailRspData;
+    }
+
+    @Override
+    public String toString() {
+        return "DeviceResponseEntity{" +
+                "id=" + id +
+                ", tenanId=" + tenanId +
+                ", productId='" + productId + '\'' +
+                ", messageType='" + messageType + '\'' +
+                ", deviceId='" + deviceId + '\'' +
+                ", taskId=" + taskId +
+                ", resultCode='" + resultCode + '\'' +
+                ", resultDetailLength=" + resultDetailLength +
+                ", resultDetailRspData='" + resultDetailRspData + '\'' +
+                '}';
+    }
+}

+ 15 - 0
huimv-test-platform/huimv-ctwing/src/main/java/com/huimv/devicedata/repo/DeviceResponseEntityRepository.java

@@ -0,0 +1,15 @@
+package com.huimv.devicedata.repo;
+
+import com.huimv.devicedata.entity.DeviceResponseEntity;
+import com.huimv.devicedata.entity.DeviceUpdataEntity;
+import org.springframework.data.jpa.repository.JpaRepository;
+import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
+
+/**
+ * @Author yinhao
+ * @Date 2021/3/31 18:18
+ * @Description
+ */
+public interface DeviceResponseEntityRepository extends JpaRepository<DeviceResponseEntity, Integer>, JpaSpecificationExecutor<DeviceResponseEntity> {
+
+}

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

@@ -2,7 +2,19 @@ package com.huimv.devicedata.service;
 
 import com.alibaba.fastjson.JSONObject;
 
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
 public interface IDeviceDataService {
 
     void saveDeviceChangedUpData(JSONObject deviceUpDataJson);
+
+
+    /**
+     * 设备命令响应
+     *
+     * @param deviceResponseData
+     */
+    void saveDeviceResponseData(JSONObject deviceResponseData);
+
 }

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

@@ -2,13 +2,18 @@ package com.huimv.devicedata.service.impl;
 
 import com.alibaba.fastjson.JSONObject;
 
+import com.huimv.devicedata.entity.DeviceResponseEntity;
 import com.huimv.devicedata.entity.DeviceUpdataEntity;
+import com.huimv.devicedata.repo.DeviceResponseEntityRepository;
 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;
 
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
 /**
  * @Project : huimv.shiwan
  * @Package : com.huimv.biosafety.uface.controller
@@ -25,6 +30,9 @@ public class DeviceDataServiceImpl implements IDeviceDataService {
     @Autowired
     private DateUtil dateUtil;
 
+    @Autowired
+    private DeviceResponseEntityRepository deviceResponseEntityRepo;
+
     @Override
     public void saveDeviceChangedUpData(JSONObject deviceUpDataJo) {
         String productId = deviceUpDataJo.getString("productId");
@@ -67,5 +75,40 @@ public class DeviceDataServiceImpl implements IDeviceDataService {
         //
         deviceUpdataEntityRepo.saveAndFlush(deviceUpdataEntity);
     }
+
+    /**
+     * 设备命令响应
+     *
+     * @param deviceResponseData
+     */
+    @Override
+    public void saveDeviceResponseData(JSONObject deviceResponseData) {
+
+        //json中取出数据
+        String tenantId = deviceResponseData.getString("tenantId");
+        String productId = deviceResponseData.getString("productId");
+        String messageType = deviceResponseData.getString("messageType");
+        String deviceId = deviceResponseData.getString("deviceId");
+        Integer taskId = deviceResponseData.getInteger("taskId");
+        JSONObject result = deviceResponseData.getJSONObject("result");
+        String resultCode = result.getString("resultCode");
+        JSONObject resultDetail = result.getJSONObject("resultDetail");
+        Integer length = resultDetail.getInteger("Length");
+        String rspdata = resultDetail.getString("Rspdata");
+
+        //封装到实体类
+        DeviceResponseEntity deviceResponseEntity = new DeviceResponseEntity();
+        deviceResponseEntity.setTenanId(tenantId);
+        deviceResponseEntity.setProductId(productId);
+        deviceResponseEntity.setMessageType(messageType);
+        deviceResponseEntity.setDeviceId(deviceId);
+        deviceResponseEntity.setTaskId(taskId);
+        deviceResponseEntity.setResultCode(resultCode);
+        deviceResponseEntity.setResultDetailLength(length);
+        deviceResponseEntity.setResultDetailRspData(rspdata);
+
+        //保存到数据库
+        deviceResponseEntityRepo.saveAndFlush(deviceResponseEntity);
+    }
 }