瀏覽代碼

更新!

TRX 1 年之前
父節點
當前提交
75cbf8fe12

+ 6 - 0
OneCardIotClient/src/main/java/com/zhongshu/iot/client/model/mqtt/GateWayInfoAddParam.java

@@ -16,6 +16,9 @@ import lombok.NoArgsConstructor;
 @NoArgsConstructor
 public class GateWayInfoAddParam extends SuperParam {
 
+    @Schema(description = "mqtt账号名称")
+    private String mqttUserName;
+
     @Schema(description = "网关ID")
     private String gateWayId;
 
@@ -28,6 +31,9 @@ public class GateWayInfoAddParam extends SuperParam {
     @Schema(description = "ip")
     private String ip;
 
+    @Schema(description = "心跳间隔,单位:秒")
+    private Long hbInterval = 30l;
+
     @Schema(description = "设备在线状态", hidden = true)
     private OnLineState state = OnLineState.OffLine;
 

+ 4 - 0
OneCardIotClient/src/main/java/com/zhongshu/iot/client/model/mqtt/GateWayUserInfoParam.java

@@ -14,9 +14,13 @@ import lombok.NoArgsConstructor;
 @NoArgsConstructor
 public class GateWayUserInfoParam {
 
+    private String tempId;
+
     @Schema(description = "最大的可绑定的设备数")
     private Long number;
 
     @Schema(description = "分组code")
     private String projectCode;
+
+    private String remark;
 }

+ 25 - 0
OneCardIotClient/src/main/java/com/zhongshu/iot/client/model/mqtt/GateWayUserInfoTempModel.java

@@ -0,0 +1,25 @@
+package com.zhongshu.iot.client.model.mqtt;
+
+import io.swagger.v3.oas.annotations.media.Schema;
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+/**
+ * @author TRX
+ * @date 2025/1/14
+ */
+@Data
+@AllArgsConstructor
+@NoArgsConstructor
+public class GateWayUserInfoTempModel {
+
+    @Schema(description = "")
+    private String id;
+
+    @Schema(description = "mqtt连接账号")
+    private String userName;
+
+    @Schema(description = "mqtt连接密码")
+    private String passWord;
+}

+ 14 - 0
OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/controller/hardware/GateWayUserInfoController.java

@@ -35,6 +35,13 @@ public class GateWayUserInfoController {
     @Autowired
     GateWayUserInfoService gateWayUserInfoService;
 
+    @ResourceAuth(value = "user", type = AuthType.User)
+    @Operation(summary = "创建临时连接账号")
+    @RequestMapping(value = "createTempUserCount", method = {RequestMethod.GET})
+    public ResultContent createUser() {
+        return gateWayUserInfoService.createTempUserCount();
+    }
+
     @ResourceAuth(value = "user", type = AuthType.User)
     @Operation(summary = "创建连接用户")
     @RequestMapping(value = "createUser", method = {RequestMethod.POST})
@@ -42,6 +49,13 @@ public class GateWayUserInfoController {
         return gateWayUserInfoService.createUser(param);
     }
 
+    @ResourceAuth(value = "user", type = AuthType.User)
+    @Operation(summary = "创建连接用户(模版来)")
+    @RequestMapping(value = "createUserByTemp", method = {RequestMethod.POST})
+    public ResultContent createUserByTemp(@RequestBody GateWayUserInfoParam param) {
+        return gateWayUserInfoService.createUserByTemp(param);
+    }
+
     @ResourceAuth(value = "user", type = AuthType.User)
     @Operation(summary = "编辑连接用户信息")
     @RequestMapping(value = "update", method = {RequestMethod.POST})

+ 13 - 0
OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/dao/mqtt/GateWayUserInfoTempDao.java

@@ -0,0 +1,13 @@
+package com.zhongshu.iot.server.core.dao.mqtt;
+
+import com.zhongshu.iot.server.core.domain.iot.mqtt.GateWayUserInfoTemp;
+
+/**
+ * @author TRX
+ * @date 2024/3/21
+ */
+public interface GateWayUserInfoTempDao extends org.springframework.data.mongodb.repository.MongoRepository<GateWayUserInfoTemp, String> {
+
+    GateWayUserInfoTemp findTopById(String id);
+
+}

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

@@ -78,4 +78,7 @@ public class DeviceInfo extends SuperEntity {
     @Schema(description = "物联网模版信息")
     @DBRef(lazy = true)
     private IotTemplate iotTemplate;
+
+    @Schema(description = "心跳间隔,单位:秒")
+    private Long hbInterval = 30l;
 }

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

@@ -51,4 +51,7 @@ public class GateWayInfo extends SuperEntity {
 
     @Schema(description = "最大可连接失败数量")
     private Long maxConnectDevices = 10L;
+
+    @Schema(description = "心跳间隔,单位:秒")
+    private Long hbInterval = 30l;
 }

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

@@ -39,6 +39,12 @@ public class GateWayUserInfo extends SuperEntity {
     @Schema(description = "已绑定的设备数量")
     private Long bindNumber = 0L;
 
+    @Schema(description = "网关数量")
+    private Long gatewayNumber = 0l;
+
+    @Schema(description = "连接的设备数量")
+    private Long deviceNumber = 0L;
+
     @Schema(description = "是否显示")
     private Boolean isShow = Boolean.TRUE;
 }

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

@@ -0,0 +1,33 @@
+package com.zhongshu.iot.server.core.domain.iot.mqtt;
+
+import com.zhongshu.iot.server.core.domain.base.SuperEntity;
+import io.swagger.v3.oas.annotations.media.Schema;
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+import org.springframework.data.mongodb.core.index.Indexed;
+import org.springframework.data.mongodb.core.mapping.Document;
+
+import java.util.Date;
+
+/**
+ * @author TRX
+ * @date 2025/1/14
+ */
+@Data
+@Document
+@NoArgsConstructor
+@AllArgsConstructor
+public class GateWayUserInfoTemp extends SuperEntity {
+    @Schema(description = "mqtt连接账号")
+    private String userName;
+
+    @Schema(description = "mqtt连接密码")
+    private String passWord;
+
+    @Schema(description = "用户权限角色名称,一般和 userName 相同")
+    private String roleName;
+
+    @Indexed(expireAfterSeconds = 0)
+    private Date ttl;
+}

+ 2 - 1
OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/service/artemis/OperationMessageService.java

@@ -26,7 +26,6 @@ import com.zhongshu.iot.server.core.domain.iot.mqtt.OperationMessage;
 import com.zhongshu.iot.server.core.domain.iot.mqtt.OperationMessageResult;
 import com.zhongshu.iot.server.core.domain.other.ExecuteMethodInfo;
 import com.zhongshu.iot.server.core.httpRequest.ApiRequestService;
-import com.zhongshu.iot.server.core.service.base.SuperService;
 import com.zhongshu.iot.server.core.service.device.DeviceInfoService;
 import com.zhongshu.iot.server.core.service.device.GateWayInfoService;
 import com.zhongshu.iot.server.core.service.iot.IotDataVerifyService;
@@ -372,6 +371,7 @@ public class OperationMessageService {
 
             // 本地处理的事件
             if (event != null && MqttConfig.localHandelEvent.contains(event)) {
+                operationMessageDao.save(entity);
                 localHandler(entity, requestData);
             } else {
                 // 业务处理失败
@@ -409,6 +409,7 @@ public class OperationMessageService {
                 String beanName = executeMethodInfo.getBeanName();
                 String methodName = executeMethodInfo.getMethodName();
                 Class c = applicationContext.getBean(beanName).getClass();
+
                 PlatformTopic t = (PlatformTopic) applicationContext.getBean(beanName);
                 Method method = c.getMethod(methodName, String.class, String.class);
                 ResultContent<Object> resultContent = (ResultContent<Object>) method.invoke(t, entity.getDataId(), dataStr);

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

@@ -1,5 +1,6 @@
 package com.zhongshu.iot.server.core.service.device;
 
+import cn.hutool.json.JSONUtil;
 import com.github.microservice.http.APIResponseModel;
 import com.github.microservice.models.common.CommonResult;
 import com.github.microservice.models.project.ProjectConfigQueryParam;
@@ -87,6 +88,9 @@ public class GateWayInfoService extends SuperService {
     @Autowired
     private GateWayUserInfoService gateWayUserInfoService;
 
+    @Autowired
+    private GateWayUserInfoDao gateWayUserInfoDao;
+
     /**
      * 注册 网关
      *
@@ -97,6 +101,7 @@ public class GateWayInfoService extends SuperService {
         log.info("gateWayRegister: {}", dataId);
         CommonResult commonResult = new CommonResult();
         OperationMessage operationMessage = operationMessageDao.findTopByDataId(dataId);
+        commonResult.setMessageId(dataId);
         if (ObjectUtils.isEmpty(operationMessage)) {
             commonResult.setFailed("数据不存在");
             return ResultContent.buildSuccess(commonResult);
@@ -107,6 +112,23 @@ public class GateWayInfoService extends SuperService {
             return ResultContent.buildSuccess(commonResult);
         }
 
+        GateWayInfoAddParam param = null;
+        try {
+            param = JSONUtil.toBean(dataStr, GateWayInfoAddParam.class);
+        } catch (Exception e) {
+            log.error(e.getMessage());
+            commonResult.setFailed(resultContent.getMsg());
+            return ResultContent.buildSuccess(commonResult);
+        }
+        GateWayUserInfo gateWayUserInfo = resultContent.getContent();
+        param.setMqttUserName(gateWayUserInfo.getUserName());
+        ResultContent<GateWayInfo> content = addGateWayInfo(param);
+        if (content.isSuccess()) {
+        } else {
+            commonResult.setFailed(content.getMsg());
+            return ResultContent.buildSuccess(commonResult);
+        }
+        commonResult.setSuccess("注册成功");
         return ResultContent.buildSuccess(commonResult);
     }
 
@@ -117,24 +139,48 @@ public class GateWayInfoService extends SuperService {
      * @return
      */
     public ResultContent<GateWayInfo> addGateWayInfo(GateWayInfoAddParam param) {
-        initDefaultUser(param);
+        if (StringUtils.isEmpty(param.getGateWayId())) {
+            return ResultContent.buildFail("gateWayId不能为空");
+        }
+        if (StringUtils.isEmpty(param.getGateWayName())) {
+            return ResultContent.buildFail("gateWayName不能为空");
+        }
+        if (StringUtils.isEmpty(param.getProjectInfoCode())) {
+            return ResultContent.buildFail("projectInfoCode不能为空");
+        }
+        ProjectInfo projectInfo = null;
+        if (StringUtils.isNotEmpty(param.getProjectInfoCode())) {
+            projectInfo = projectInfoDao.findTopByCode(param.getProjectInfoCode());
+        }
+        if (ObjectUtils.isEmpty(projectInfo)) {
+            return ResultContent.buildFail("projectInfoCode不存在");
+        }
+        GateWayUserInfo gateWayUserInfo = gateWayUserInfoDao.findTopByUserName(param.getMqttUserName());
+        if (StringUtils.isNotEmpty(gateWayUserInfo.getProjectCode()) &&
+                !gateWayUserInfo.getProjectCode().equals(param.getProjectInfoCode())) {
+            return ResultContent.buildFail("projectInfoCode不匹配");
+        }
+
         GateWayInfo gateWayInfo = gateWayInfoDao.findTopByGateWayId(param.getGateWayId());
         if (ObjectUtils.isEmpty(gateWayInfo)) {
             gateWayInfo = new GateWayInfo();
             gateWayInfo.setActivityTime(System.currentTimeMillis());
         }
+
+        BeanUtils.copyProperties(param, gateWayInfo, "id");
         gateWayInfo.setOnLineState(OnLineState.OnLine);
         gateWayInfo.setLastOnlineTime(System.currentTimeMillis());
-        BeanUtils.copyProperties(param, gateWayInfo, "id");
-
-        gateWayInfoDao.save(gateWayInfo);
-        if (StringUtils.isNotEmpty(param.getProjectInfoCode())) {
-            ProjectInfo projectInfo = projectInfoDao.findTopByCode(param.getProjectInfoCode());
+        if (ObjectUtils.isNotEmpty(projectInfo)) {
             gateWayInfo.setProjectInfo(projectInfo);
         }
+
+        gateWayInfoDao.save(gateWayInfo);
         log.info("网关注册成功");
         // 通知同步 (网关信息)
         deviceSyncFullCardService.noticeSyncGateWay(gateWayInfo);
+
+        // 更新连接账号关联的设备数量
+        gateWayUserInfoService.updateBindNumber(gateWayInfo.getMqttUserName());
         return ResultContent.buildSuccess(gateWayInfo);
     }
 

+ 84 - 15
OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/service/device/GateWayUserInfoService.java

@@ -6,10 +6,7 @@ import com.zhongshu.iot.client.model.mqtt.*;
 import com.zhongshu.iot.client.type.type.MqttUserState;
 import com.zhongshu.iot.server.core.dao.mqtt.*;
 import com.zhongshu.iot.server.core.dataConfig.MqttConfig;
-import com.zhongshu.iot.server.core.domain.iot.mqtt.GateWay2User;
-import com.zhongshu.iot.server.core.domain.iot.mqtt.GateWayInfo;
-import com.zhongshu.iot.server.core.domain.iot.mqtt.GateWayUserInfo;
-import com.zhongshu.iot.server.core.domain.iot.mqtt.ProjectInfo;
+import com.zhongshu.iot.server.core.domain.iot.mqtt.*;
 import com.zhongshu.iot.server.core.util.CommonUtil;
 import com.zhongshu.iot.server.core.util.DateUtils;
 import com.zhongshu.iot.server.core.util.bean.BeanUtils;
@@ -22,6 +19,8 @@ import org.springframework.data.domain.Page;
 import org.springframework.data.domain.Pageable;
 import org.springframework.stereotype.Service;
 
+import java.util.Date;
+
 /**
  * 账号管理
  *
@@ -62,6 +61,9 @@ public class GateWayUserInfoService {
     @Autowired
     private DeviceInfoDao deviceInfoDao;
 
+    @Autowired
+    private GateWayUserInfoTempDao gateWayUserInfoTempDao;
+
     /**
      * 刷新系统所有的用户 和 用户权限数据
      */
@@ -151,8 +153,40 @@ public class GateWayUserInfoService {
         userParam.setUserName(userName);
         userParam.setPassWord(CommonUtil.generateRandomString(16));
         userParam.setRoleName(roleName);
+        userParam.setRemark(param.getRemark());
+
+        // 最大连接数
+        userParam.setNumber(number);
+        userParam.setProjectCode(param.getProjectCode());
+        ResultContent<GateWayUserInfo> resultContent = addGateWayUser(userParam);
+        if (resultContent.isFailed()) {
+            return ResultContent.buildFail(resultContent.getMsg());
+        }
+        return ResultContent.buildSuccess();
+    }
+
+    public ResultContent createUserByTemp(GateWayUserInfoParam param) {
+        Long number = param.getNumber();
+        if (number == null || number == 0) {
+            return ResultContent.buildFail("number不符合要求");
+        }
+        if (StringUtils.isEmpty(param.getTempId())) {
+            return ResultContent.buildFail("tempId不能为空");
+        }
+        GateWayUserInfoTemp temp = gateWayUserInfoTempDao.findTopById(param.getTempId());
+        if (ObjectUtils.isEmpty(temp)) {
+            return ResultContent.buildFail("tempId数据不存在");
+        }
+
+        GateWayUserInfoAddParam userParam = new GateWayUserInfoAddParam();
+        userParam.setUserName(temp.getUserName());
+        userParam.setPassWord(temp.getPassWord());
+        userParam.setRoleName(temp.getRoleName());
+        // 最大连接数
         userParam.setNumber(number);
         userParam.setProjectCode(param.getProjectCode());
+        userParam.setRemark(param.getRemark());
+
         ResultContent<GateWayUserInfo> resultContent = addGateWayUser(userParam);
         if (resultContent.isFailed()) {
             return ResultContent.buildFail(resultContent.getMsg());
@@ -160,6 +194,21 @@ public class GateWayUserInfoService {
         return ResultContent.buildSuccess();
     }
 
+    public ResultContent<GateWayUserInfoTempModel> createTempUserCount() {
+        GateWayUserInfoTemp temp = new GateWayUserInfoTemp();
+        String userName = CommonUtil.generateRandomString(8);
+        String roleName = userName;
+        temp.setUserName(userName);
+        temp.setPassWord(CommonUtil.generateRandomString(16));
+        temp.setRoleName(roleName);
+        temp.setTtl(new Date(System.currentTimeMillis() + 60 * 1000));
+        gateWayUserInfoTempDao.save(temp);
+
+        GateWayUserInfoTempModel model = new GateWayUserInfoTempModel();
+        BeanUtils.copyProperties(temp, model);
+        return ResultContent.buildSuccess(model);
+    }
+
     /**
      * 编辑用户信息
      *
@@ -252,20 +301,24 @@ public class GateWayUserInfoService {
      */
     public ResultContent updateBindNumber(GateWayUserInfo entity) {
         if (ObjectUtils.isNotEmpty(entity)) {
-            entity.setBindNumber(countBindNumber(entity));
-        }
-        return ResultContent.buildSuccess();
-    }
-
-    public long countBindNumber(GateWayUserInfo entity) {
-        if (ObjectUtils.isNotEmpty(entity) && StringUtils.isNotEmpty(entity.getUserName())) {
             // 网关数量
             long gatewayNumber = gateWayInfoDao.countByMqttUserName(entity.getUserName());
             // 设备数量
             long deviceNumber = deviceInfoDao.countByMqttUserName(entity.getUserName());
-            return gatewayNumber + deviceNumber;
+            long bindNumber = gatewayNumber + deviceNumber;
+            entity.setGatewayNumber(gatewayNumber);
+            entity.setDeviceNumber(deviceNumber);
+            entity.setBindNumber(bindNumber);
         }
-        return 0;
+        return ResultContent.buildSuccess();
+    }
+
+    public ResultContent updateBindNumber(String userName) {
+        if (StringUtils.isNotEmpty(userName)) {
+            GateWayUserInfo entity = gateWayUserInfoDao.findTopByUserName(userName);
+            updateBindNumber(entity);
+        }
+        return ResultContent.buildSuccess();
     }
 
     /**
@@ -320,14 +373,30 @@ public class GateWayUserInfoService {
         if (StringUtils.isNotEmpty(topic)) {
             String[] arr = topic.split("/");
             if (arr.length >= 2) {
-                userName = arr[1];
+                userName = arr[2];
             }
         }
         if (StringUtils.isEmpty(userName)) {
             return ResultContent.buildFail("连接账号为空");
         }
         GateWayUserInfo entity = gateWayUserInfoDao.findTopByUserName(userName);
-
+        if (ObjectUtils.isEmpty(entity)) {
+            return ResultContent.buildFail("账号不存在");
+        }
+        if (entity.getState() != MqttUserState.Enable) {
+            return ResultContent.buildFail("账号不可用");
+        }
+        Long number = entity.getNumber();
+        Long bindNumber = entity.getBindNumber();
+        if (number == null) {
+            return ResultContent.buildFail("未设置最大连接数");
+        }
+        if (bindNumber == null) {
+            bindNumber = 0L;
+        }
+        if (bindNumber >= number) {
+            return ResultContent.buildFail("设备连接数已达最大");
+        }
         return ResultContent.buildSuccess(entity);
     }