TRX преди 1 година
родител
ревизия
db928dc1f0

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

@@ -1,5 +1,6 @@
 package com.zhongshu.iot.server.core.dao.mqtt;
 
+import com.github.microservice.models.type.RegistType;
 import com.zhongshu.iot.server.core.dao.mqtt.extend.DeviceInfoDaoExtend;
 import com.zhongshu.iot.server.core.domain.iot.mqtt.DeviceInfo;
 
@@ -21,4 +22,5 @@ public interface DeviceInfoDao extends DeviceInfoDaoExtend, org.springframework.
 
     long countByMqttUserNameAndIsDelete(String mqttUserName, Boolean isDelete);
 
+    List<DeviceInfo> findByRegistTypeIn(List<RegistType> registTypes);
 }

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

@@ -1,5 +1,6 @@
 package com.zhongshu.iot.server.core.domain.iot.mqtt;
 
+import com.github.microservice.models.type.DeviceState;
 import com.github.microservice.models.type.OnLineState;
 import com.zhongshu.iot.server.core.domain.base.SuperEntity;
 import io.swagger.v3.oas.annotations.media.Schema;
@@ -30,6 +31,9 @@ public class GateWayInfo extends SuperEntity {
     @Schema(description = "网关名称")
     private String gateWayName;
 
+    @Schema(description = "网关状态")
+    private DeviceState state = DeviceState.Enable;
+
     @Schema(description = "ip地址")
     private String ip;
 

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

@@ -4,6 +4,7 @@ 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;
+import com.github.microservice.models.type.DeviceState;
 import com.github.microservice.models.type.DeviceType;
 import com.github.microservice.models.type.OnLineState;
 import com.github.microservice.models.type.RegistType;
@@ -126,6 +127,8 @@ public class GateWayInfoService extends SuperService {
         }
         GateWayUserInfo gateWayUserInfo = resultContent.getContent();
         param.setMqttUserName(gateWayUserInfo.getUserName());
+
+        // 网关基本信息维护
         ResultContent<GateWayInfo> content = addGateWayInfo(param);
         if (content.isSuccess()) {
         } else {
@@ -171,6 +174,7 @@ public class GateWayInfoService extends SuperService {
         if (ObjectUtils.isEmpty(gateWayInfo)) {
             gateWayInfo = new GateWayInfo();
             gateWayInfo.setActivityTime(System.currentTimeMillis());
+            gateWayInfo.setState(DeviceState.Enable);
         }
 
         BeanUtils.copyProperties(param, gateWayInfo, "id");
@@ -187,6 +191,9 @@ public class GateWayInfoService extends SuperService {
 
         // 更新连接账号关联的设备数量
         gateWayUserInfoService.updateBindNumber(gateWayInfo.getMqttUserName());
+
+        // 同步权限
+        jmxSyncService.syncSecurityToMQTTService(gateWayInfo);
         return ResultContent.buildSuccess(gateWayInfo);
     }
 

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

@@ -74,6 +74,7 @@ public class GateWayUserInfoService {
 
         jmxSyncService.refreshAllGateWayUser();
         jmxSyncService.refreshAllGateWaySecurity();
+        jmxSyncService.refreshAllDirectDevice();
     }
 
     /**

+ 37 - 17
OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/service/device/JMXSyncService.java

@@ -1,6 +1,7 @@
 package com.zhongshu.iot.server.core.service.device;
 
 import com.github.microservice.models.type.DeviceState;
+import com.github.microservice.models.type.RegistType;
 import com.github.microservice.net.ResultContent;
 import com.zhongshu.iot.server.core.dao.mqtt.*;
 import com.zhongshu.iot.server.core.dataConfig.MqttConfig;
@@ -13,6 +14,7 @@ import lombok.extern.slf4j.Slf4j;
 import org.apache.activemq.artemis.api.core.management.ActiveMQServerControl;
 import org.apache.activemq.artemis.api.core.management.ObjectNameBuilder;
 import org.apache.commons.lang3.ObjectUtils;
+import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.context.ApplicationContext;
 import org.springframework.stereotype.Service;
@@ -61,6 +63,8 @@ public class JMXSyncService {
     @Autowired
     private ApplicationContext applicationContext;
 
+    @Autowired
+    private DeviceInfoDao deviceInfoDao;
 
     /**
      * 把用户同步到MQTT服务中
@@ -276,39 +280,40 @@ public class JMXSyncService {
                 ObjectName addressObjectName = ObjectNameBuilder.create("org.apache.activemq.artemis", mqttInfo.getBrokerName()).getActiveMQServerObjectName();
                 ActiveMQServerControl addressControl = MBeanServerInvocationHandler.newProxyInstance(connection, addressObjectName, ActiveMQServerControl.class, false);
 
-                final String _roleName = MqttConfig.adminRoleName;
                 for (GateWayInfo gateWayInfo : gateWayInfos) {
                     // 以前的权限
                     List<GateWayMqttSecurity> list = gateWayMqttSecurityDao.findByMqttInfoAndGateWayInfo(mqttInfo, gateWayInfo);
                     gateWayMqttSecurityDao.deleteByMqttInfoAndGateWayInfo(mqttInfo, gateWayInfo);
 
-                    // 查询网关关联的用户
-                    GateWay2User gateWay2User = gateWay2UserDao.findTopByGateWayInfo(gateWayInfo);
-                    if (ObjectUtils.isEmpty(gateWay2User)) {
+                    String userName = gateWayInfo.getMqttUserName();
+                    if (StringUtils.isEmpty(userName)) {
                         log.error("网关对应用户信息为不存在");
                         continue;
                     }
-                    GateWayUserInfo gateWayUserInfo = gateWay2User.getGateWayUserInfo();
+
+                    GateWayUserInfo gateWayUserInfo = gateWayUserInfoDao.findTopByUserName(userName);
                     if (ObjectUtils.isEmpty(gateWayUserInfo)) {
                         log.error("网关对应用户信息为空");
                         continue;
                     }
+                    String roleName = JMXUtil.buildSecurityRoleName(gateWayUserInfo.getRoleName());
+
+                    boolean gatewayIsUse = true;
+                    if (gateWayInfo.getState() == null || gateWayInfo.getState() == DeviceState.Enable) {
+                        gatewayIsUse = true;
+                    } else {
+                        gatewayIsUse = false;
+                    }
+
                     // 查询网关对应的设备
                     List<GateWay2Device> gateWay2Devices = gateWay2DeviceDao.findByGateWayInfo(gateWayInfo);
                     List<DeviceInfo> deviceInfos = new ArrayList<>();
                     if (ObjectUtils.isNotEmpty(gateWay2Devices)) {
                         deviceInfos = gateWay2Devices.stream().map(it -> it.getDeviceInfo()).collect(Collectors.toList());
                     }
+
                     List<String> addressMatchs = JMXUtil.buildAddressMatch(gateWayInfo, deviceInfos);
                     if (ObjectUtils.isNotEmpty(addressMatchs)) {
-                        String userName = gateWayUserInfo.getUserName();
-                        String roleName = gateWayUserInfo.getRoleName();
-
-                        List<String> roleNames = new ArrayList<>();
-                        roleNames.add(roleName);
-                        roleNames.add(_roleName);
-
-                        roleName = String.join(",", roleNames);
                         List<GateWayMqttSecurity> securities = new ArrayList<>();
                         for (String addressMatch : addressMatchs) {
                             GateWayMqttSecurity gateWayMqttSecurity = new GateWayMqttSecurity();
@@ -320,8 +325,11 @@ public class JMXSyncService {
                             gateWayMqttSecurity.setMqttName(mqttInfo.getName());
                             gateWayMqttSecurity.setAddressMatch(addressMatch);
                             try {
-                                addressControl.addSecuritySettings(addressMatch, roleName, roleName, roleName, roleName, roleName, roleName, roleName, roleName, roleName, roleName);
-
+                                if (gatewayIsUse) {
+                                    addressControl.addSecuritySettings(addressMatch, roleName, roleName, roleName, roleName, roleName, roleName, roleName, roleName, roleName, roleName);
+                                } else {
+                                    addressControl.removeSecuritySettings(addressMatch);
+                                }
                                 gateWayMqttSecurity.setIsSync(Boolean.TRUE);
                                 gateWayMqttSecurity.setMsg("同步成功");
                             } catch (Exception e) {
@@ -473,7 +481,7 @@ public class JMXSyncService {
     }
 
     /**
-     * 同步所有的用户
+     * 同步所有的用户 -- 用户
      *
      * @return
      */
@@ -486,7 +494,7 @@ public class JMXSyncService {
     }
 
     /**
-     * 同步所有的数据
+     * 同步所有的数据 -- 网关
      *
      * @return
      */
@@ -498,4 +506,16 @@ public class JMXSyncService {
         return ResultContent.buildSuccess();
     }
 
+    /**
+     * 同步所有直连设备的权限
+     *
+     * @return
+     */
+    public ResultContent refreshAllDirectDevice() {
+        List<DeviceInfo> deviceInfos = deviceInfoDao.findByRegistTypeIn(List.of(RegistType.DirectConnection));
+        if (ObjectUtils.isNotEmpty(deviceInfos)) {
+            syncDevicesSecurity(deviceInfos);
+        }
+        return ResultContent.buildSuccess();
+    }
 }

+ 10 - 0
OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/util/JMXUtil.java

@@ -73,6 +73,16 @@ public class JMXUtil {
         return list;
     }
 
+    public static List<String> buildAddressMatch(List<DeviceInfo> deviceInfoList) {
+        List<String> list = new ArrayList<String>();
+        if (ObjectUtils.isNotEmpty(deviceInfoList)) {
+            deviceInfoList.stream().forEach(deviceInfo -> {
+                list.add(String.format("/device/%s/#", deviceInfo.getDeviceId()));
+            });
+        }
+        return list;
+    }
+
     public static List<String> buildDeviceAddressMatch(DeviceInfo deviceInfo) {
         List<String> list = new ArrayList<String>();
         if (ObjectUtils.isNotEmpty(deviceInfo)) {