Browse Source

更新!

TRX 1 year ago
parent
commit
30ba928c93

+ 7 - 9
OneCardIotClient/src/main/java/com/zhongshu/iot/client/model/mqtt/DeviceInfoRegistParam.java

@@ -14,12 +14,18 @@ import lombok.Data;
 @Data
 public class DeviceInfoRegistParam {
 
-    @Schema(description = "产品code")
+    @Schema(description = "所属产品code")
     private String productCode;
 
+    @Schema(description = "所属项目")
+    private String projectInfoCode;
+
     @Schema(description = "mqtt账号名称")
     private String mqttUserName;
 
+    @Schema(description = "是网关还是设备")
+    private DeviceCategory deviceCategory;
+
     @Schema(description = "网关ID")
     private String gateWayId;
 
@@ -29,18 +35,12 @@ public class DeviceInfoRegistParam {
     @Schema(description = "设备名称")
     private String deviceName;
 
-    @Schema(description = "设备类型:消费机 闸机")
-    private DeviceType deviceType;
-
     @Schema(description = "设备型号")
     private DeviceSpecType specType;
 
     @Schema(description = "ip地址")
     private String ip;
 
-    @Schema(description = "所属项目")
-    private String projectInfoCode;
-
     @Schema(description = "固件版本")
     private String firmwareVersion;
 
@@ -50,6 +50,4 @@ public class DeviceInfoRegistParam {
     @Schema(description = "心跳间隔,单位:秒")
     private Long hbInterval = 30l;
 
-    @Schema(description = "是网关还是设备")
-    private DeviceCategory deviceCategory;
 }

+ 1 - 0
OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/domain/iot/mqtt/GateWay2Device.java

@@ -25,6 +25,7 @@ public class GateWay2Device extends SuperEntity {
     @Schema(description = "硬件设备网关信息")
     private GateWayInfo gateWayInfo;
 
+    @Schema(description = "网关id")
     private String gateWayId;
 
     @DBRef(lazy = true)

+ 2 - 0
OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/service/device/DeviceInfoService.java

@@ -111,6 +111,8 @@ public class DeviceInfoService {
         if (ObjectUtils.isNotEmpty(iotTemplate)) {
             deviceInfo.setDeviceType(iotTemplate.getDeviceType());
             deviceInfo.setSpecType(iotTemplate.getSpecType());
+            deviceInfo.setDeviceCategory(iotTemplate.getDeviceCategory());
+            deviceInfo.setProductCode(iotTemplate.getProductCode());
         }
 
         // 项目

+ 51 - 23
OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/service/device/GateWayInfoService.java

@@ -5,6 +5,7 @@ import com.github.microservice.http.APIResponseModel;
 import com.github.microservice.models.common.CommonResult;
 import com.github.microservice.models.project.ProjectConfigQueryParam;
 import com.github.microservice.net.ResultContent;
+import com.github.microservice.types.deviceUse.DeviceCategory;
 import com.github.microservice.types.deviceUse.DeviceState;
 import com.github.microservice.types.deviceUse.OnLineState;
 import com.github.microservice.types.deviceUse.RegistType;
@@ -231,6 +232,7 @@ public class GateWayInfoService extends SuperService {
             commonResult.setFailed("数据不存在");
             return ResultContent.buildSuccess(commonResult);
         }
+        // 找到连接账号的信息
         ResultContent<GateWayUserInfo> resultContent = gateWayUserInfoService.verifyMqttUser(operationMessage.getTopic());
         if (resultContent.isFailed()) {
             commonResult.setFailed(resultContent.getMsg());
@@ -272,9 +274,11 @@ public class GateWayInfoService extends SuperService {
         if (StringUtils.isEmpty(param.getProductCode())) {
             return ResultContent.buildFail("productCode不能为空");
         }
+        // 设备deviceId
         if (StringUtils.isEmpty(param.getDeviceId())) {
             return ResultContent.buildFail("deviceId不能为空");
         }
+        // 设备名称
         if (StringUtils.isEmpty(param.getDeviceName())) {
             return ResultContent.buildFail("deviceName不能为空");
         }
@@ -324,8 +328,11 @@ public class GateWayInfoService extends SuperService {
         List<DeviceInfoAddParam> devices = new ArrayList<>();
         DeviceInfoAddParam deviceInfoAddParam = new DeviceInfoAddParam();
         BeanUtils.copyProperties(param, deviceInfoAddParam);
+
         deviceInfoAddParam.setRegistType(registType);
         deviceInfoAddParam.setDeviceCategory(iotTemplate.getDeviceCategory());
+        deviceInfoAddParam.setDeviceType(iotTemplate.getDeviceType());
+        deviceInfoAddParam.setSpecType(iotTemplate.getSpecType());
         devices.add(deviceInfoAddParam);
 
         // 设备列表
@@ -336,30 +343,8 @@ public class GateWayInfoService extends SuperService {
                 // 保存设备信息
                 ResultContent<DeviceInfo> resultContent = deviceInfoService.addDeviceInfo(device);
                 DeviceInfo deviceInfo = resultContent.getContent();
-
-                if (registType == RegistType.DirectConnection) {
-                    // 直连
-                    List<GateWay2Device> _list = gateWay2DeviceDao.findByDeviceInfo(deviceInfo);
-                    gateWay2DeviceDao.deleteAll(_list);
-                } else if (registType == RegistType.Gateway) {
-                    // 网关注册
-                    // 设备可以绑定到多个网关,一个网关只能绑定设备一次
-                    GateWay2Device gateWay2Device = gateWay2DeviceDao.findTopByDeviceInfoOrderByUpdateTimeDesc(deviceInfo);
-                    if (ObjectUtils.isEmpty(gateWay2Device)) {
-                        gateWay2Device = new GateWay2Device();
-                        gateWay2Device.setState(OnLineState.OnLine);
-                    } else {
-                    }
-                    gateWay2Device.setGateWayInfo(gateWayInfo);
-                    gateWay2Device.setGateWayId(gateWayInfo.getGateWayId());
-                    gateWay2Device.setDeviceInfo(deviceInfo);
-                    gateWay2Device.setDeviceId(deviceInfo.getDeviceId());
-
-                    gateWay2Device.setBindTimeStr(DateUtils.paresTime(System.currentTimeMillis(), DateUtils.FORMAT_LONG));
-                    gateWay2DeviceDao.save(gateWay2Device);
-                }
+                deviceBindGateWay(deviceInfo, gateWayInfo);
                 deviceInfos.add(deviceInfo);
-
                 // 更新设备的topic
                 iotService.updateAllDeviceIotMainGateWayInfo(deviceInfo);
             }
@@ -374,6 +359,49 @@ public class GateWayInfoService extends SuperService {
         return ResultContent.buildSuccess();
     }
 
+    /**
+     * 设备绑定 网关
+     *
+     * @param deviceInfo
+     * @param gateWayInfo
+     * @return
+     */
+    public ResultContent deviceBindGateWay(DeviceInfo deviceInfo, GateWayInfo gateWayInfo) {
+        if (ObjectUtils.isEmpty(deviceInfo)) {
+            return ResultContent.buildFail("设备信息为空");
+        }
+        // 如果设备时网关
+        if (deviceInfo.getDeviceCategory() == DeviceCategory.GW) {
+            List<GateWay2Device> _list = gateWay2DeviceDao.findByDeviceInfo(deviceInfo);
+            gateWay2DeviceDao.deleteAll(_list);
+            return ResultContent.buildSuccess();
+        }
+
+        RegistType _registType = deviceInfo.getRegistType();
+        if (_registType == RegistType.DirectConnection) {
+            // 直连
+            List<GateWay2Device> _list = gateWay2DeviceDao.findByDeviceInfo(deviceInfo);
+            gateWay2DeviceDao.deleteAll(_list);
+        } else if (_registType == RegistType.Gateway) {
+            // 网关注册
+            // 设备可以绑定到多个网关,一个网关只能绑定设备一次
+            GateWay2Device gateWay2Device = gateWay2DeviceDao.findTopByDeviceInfoOrderByUpdateTimeDesc(deviceInfo);
+            if (ObjectUtils.isEmpty(gateWay2Device)) {
+                gateWay2Device = new GateWay2Device();
+                gateWay2Device.setState(OnLineState.OnLine);
+            } else {
+            }
+            gateWay2Device.setGateWayInfo(gateWayInfo);
+            gateWay2Device.setGateWayId(gateWayInfo.getGateWayId());
+            gateWay2Device.setDeviceInfo(deviceInfo);
+            gateWay2Device.setDeviceId(deviceInfo.getDeviceId());
+
+            gateWay2Device.setBindTimeStr(DateUtils.paresTime(System.currentTimeMillis(), DateUtils.FORMAT_LONG));
+            gateWay2DeviceDao.save(gateWay2Device);
+        }
+        return ResultContent.buildSuccess();
+    }
+
     /**
      * 注册网关
      *