瀏覽代碼

更新!

TRX 1 年之前
父節點
當前提交
b84b0fe34b

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

@@ -82,14 +82,14 @@ public class GateWayUserInfoController {
     @ResourceAuth(value = "user", type = AuthType.User)
     @Operation(summary = "删除连接用户")
     @RequestMapping(value = "deleteMqttUser", method = {RequestMethod.POST})
-    public ResultContent deleteMqttUser(GateWayUserInfoNameParam param) {
+    public ResultContent deleteMqttUser(@RequestBody GateWayUserInfoNameParam param) {
         return gateWayUserInfoService.deleteMqttUser(param);
     }
 
     @ResourceAuth(value = "user", type = AuthType.User)
     @Operation(summary = "启动-停用连接账号")
     @RequestMapping(value = "updateState", method = {RequestMethod.POST})
-    public ResultContent updateState(GateWayUserInfoNameParam param) {
+    public ResultContent updateState(@RequestBody GateWayUserInfoNameParam param) {
         Assert.hasText(param.getUserName(), "连接账号不能为空");
         return gateWayUserInfoService.updateState(param);
     }
@@ -97,7 +97,7 @@ public class GateWayUserInfoController {
     @ResourceAuth(value = "user", type = AuthType.User)
     @Operation(summary = "查询连接账号")
     @RequestMapping(value = "getUserByUserName", method = {RequestMethod.POST})
-    public ResultContent<GateWayUserInfoModel> getUserByUserName(GateWayUserInfoNameParam param) {
+    public ResultContent<GateWayUserInfoModel> getUserByUserName(@RequestBody GateWayUserInfoNameParam param) {
         Assert.hasText(param.getUserName(), "连接账号不能为空");
         return gateWayUserInfoService.getUserByUserName(param.getUserName());
     }

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

@@ -1,6 +1,5 @@
 package com.zhongshu.iot.server.core.service.device;
 
-import com.github.microservice.models.type.DeviceType;
 import com.github.microservice.models.type.OnLineState;
 import com.github.microservice.net.ResultContent;
 import com.github.microservice.types.FunctionType;
@@ -20,7 +19,6 @@ import com.zhongshu.iot.server.core.domain.iot.mqtt.ProjectInfo;
 import com.zhongshu.iot.server.core.service.iot.IotServiceImpl;
 import com.zhongshu.iot.server.core.service.sync.DeviceSyncFullCardService;
 import com.zhongshu.iot.server.core.service.user.OperationLogsService;
-import com.zhongshu.iot.server.core.util.CommonUtil;
 import com.zhongshu.iot.server.core.util.bean.BeanUtils;
 import com.zhongshu.iot.server.core.util.page.PageEntityUtil;
 import org.apache.commons.lang3.ObjectUtils;
@@ -65,6 +63,9 @@ public class DeviceInfoService {
     @Autowired
     private GateWayUserInfoService gateWayUserInfoService;
 
+    @Autowired
+    private JMXSyncService jmxSyncService;
+
     public ResultContent initSyncAllDevice() {
         // 同步所有的设备
         List<DeviceInfo> deviceInfos = deviceInfoDao.findAll();
@@ -109,6 +110,9 @@ public class DeviceInfoService {
             operationLogsService.addLogs(String.format("添加了设备;%s", finalDeviceInfo.getDeviceName()), LogsLevel.Middle, finalDeviceInfo);
             // 跟新账号的绑定设备数量
             gateWayUserInfoService.updateBindNumber(finalDeviceInfo.getMqttUserName());
+
+            // 同步设备权限
+            jmxSyncService.syncDevicesSecurity(List.of(finalDeviceInfo));
         });
 
         return ResultContent.buildSuccess(deviceInfo);

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

@@ -409,6 +409,107 @@ public class JMXSyncService {
         }
     }
 
+    /**
+     * 同步设备权限
+     *
+     * @param deviceInfos
+     */
+    public void syncDevicesSecurity(List<DeviceInfo> deviceInfos) {
+        if (ObjectUtils.isNotEmpty(deviceInfos)) {
+            List<MqttInfo> list = mqttInfoDao.findAll();
+            log.info("syncDevicesSecurity {}", list.size());
+            if (ObjectUtils.isNotEmpty(list)) {
+                list.parallelStream().forEach(mqttInfo -> {
+                    syncMqttSecuritySettingsByDevices(mqttInfo, deviceInfos);
+                });
+            }
+        }
+    }
+
+    public void syncMqttSecuritySettingsByDevices(MqttInfo mqttInfo, List<DeviceInfo> deviceInfos) {
+        if (ObjectUtils.isNotEmpty(mqttInfo) && ObjectUtils.isNotEmpty(deviceInfos)) {
+            try {
+                String urlStr = JMXUtil.buildServiceURL(mqttInfo);
+                JMXServiceURL url = new JMXServiceURL(urlStr);
+                @Cleanup JMXConnector connector = JMXConnectorFactory.connect(url, null);
+                connector.connect();
+                log.info("syncMqttSecuritySettings JMX {}:{} 连接成功...", mqttInfo.getJmxHost(), mqttInfo.getJmxPort());
+                MBeanServerConnection connection = connector.getMBeanServerConnection();
+                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 (DeviceInfo deviceInfo : deviceInfos) {
+                    GateWayUserInfo gateWayUserInfo = gateWayUserInfoDao.findTopByUserName(deviceInfo.getMqttUserName());
+                    if (ObjectUtils.isEmpty(gateWayUserInfo)) {
+                        continue;
+                    }
+
+
+//                    // 以前的权限
+//                    List<GateWayMqttSecurity> list = gateWayMqttSecurityDao.findByMqttInfoAndGateWayInfo(mqttInfo, gateWayInfo);
+//                    gateWayMqttSecurityDao.deleteByMqttInfoAndGateWayInfo(mqttInfo, gateWayInfo);
+//
+//                    // 查询网关关联的用户
+//                    GateWay2User gateWay2User = gateWay2UserDao.findTopByGateWayInfo(gateWayInfo);
+//                    if (ObjectUtils.isEmpty(gateWay2User)) {
+//                        log.error("网关对应用户信息为不存在");
+//                        continue;
+//                    }
+//                    GateWayUserInfo gateWayUserInfo = gateWay2User.getGateWayUserInfo();
+//                    if (ObjectUtils.isEmpty(gateWayUserInfo)) {
+//                        log.error("网关对应用户信息为空");
+//                        continue;
+//                    }
+//                    // 查询网关对应的设备
+//                    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();
+//                            gateWayMqttSecurity.setUserName(userName);
+//                            gateWayMqttSecurity.setRoleName(roleName);
+//                            gateWayMqttSecurity.setGateWayInfo(gateWayInfo);
+//                            gateWayMqttSecurity.setGateWayId(gateWayInfo.getGateWayId());
+//                            gateWayMqttSecurity.setMqttInfo(mqttInfo);
+//                            gateWayMqttSecurity.setMqttName(mqttInfo.getName());
+//                            gateWayMqttSecurity.setAddressMatch(addressMatch);
+//                            try {
+//                                addressControl.addSecuritySettings(addressMatch, roleName, roleName, roleName, roleName, roleName, roleName, roleName, roleName, roleName, roleName);
+//
+//                                gateWayMqttSecurity.setIsSync(Boolean.TRUE);
+//                                gateWayMqttSecurity.setMsg("同步成功");
+//                            } catch (Exception e) {
+//                                e.printStackTrace();
+//                                gateWayMqttSecurity.setIsSync(Boolean.FALSE);
+//                                gateWayMqttSecurity.setMsg(String.format("同步失败:%s", e.getMessage()));
+//                            }
+//                            gateWayMqttSecurity.setTimeStr(DateUtils.paresTime(System.currentTimeMillis(), DateUtils.FORMAT_LONG));
+//                            securities.add(gateWayMqttSecurity);
+//                        }
+//                        gateWayMqttSecurityDao.saveAll(securities);
+//                    }
+                }
+            } catch (Exception e) {
+                e.printStackTrace();
+            }
+        }
+    }
+
     /**
      * 同步所有的用户
      *

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

@@ -65,7 +65,7 @@ public class JMXUtil {
         }
         if (ObjectUtils.isNotEmpty(deviceInfoList)) {
             deviceInfoList.stream().forEach(deviceInfo -> {
-                list.add(String.format("/v1/device/%s/#", deviceInfo.getDeviceId()));
+                list.add(String.format("/device/%s/#", deviceInfo.getDeviceId()));
             });
         }
         return list;
@@ -74,7 +74,7 @@ public class JMXUtil {
     public static List<String> buildGateWayAddressMatch(GateWayInfo gateWayInfo) {
         List<String> list = new ArrayList<String>();
         if (ObjectUtils.isNotEmpty(gateWayInfo)) {
-            list.add(String.format("/v1/gateway/%s/#", gateWayInfo.getGateWayId()));
+            list.add(String.format("/device/%s/#", gateWayInfo.getGateWayId()));
         }
         return list;
     }

+ 1 - 1
OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/util/test/TestReplace.java

@@ -16,7 +16,7 @@ import java.util.regex.Pattern;
  */
 public class TestReplace {
     public static void main(String[] args) {
-        String str = "/v1/gateway/${deviceId}/on";
+        String str = "/gateway/${deviceId}/on";
         String str1 = str.replaceAll(Pattern.quote("${deviceId}"), "aaa");
 
         System.out.println(str1);