Browse Source

更新! 取消GateWay信息

TRX 1 year ago
parent
commit
97256787d3

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

@@ -27,7 +27,6 @@ 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.device.DeviceInfoService;
-import com.zhongshu.iot.server.core.service.device.GateWayInfoService;
 import com.zhongshu.iot.server.core.service.iot.IotDataVerifyService;
 import com.zhongshu.iot.server.core.service.iotPlatform.PlatformTopic;
 import com.zhongshu.iot.server.core.util.CommonUtil;
@@ -72,9 +71,6 @@ public class OperationMessageService {
     @Autowired
     private DeviceInfoService deviceInfoService;
 
-    @Autowired
-    private GateWayInfoService gateWayInfoService;
-
     @Autowired
     private MQClient mqClient;
 

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

@@ -14,10 +14,12 @@ import com.zhongshu.iot.client.type.IotDataType;
 import com.zhongshu.iot.client.type.type.LogsLevel;
 import com.zhongshu.iot.server.core.dao.iot.IotTemplateDao;
 import com.zhongshu.iot.server.core.dao.mqtt.DeviceInfoDao;
+import com.zhongshu.iot.server.core.dao.mqtt.GateWay2DeviceDao;
 import com.zhongshu.iot.server.core.dao.mqtt.ProjectInfoDao;
 import com.zhongshu.iot.server.core.dataConfig.ResultMessage;
 import com.zhongshu.iot.server.core.domain.iot.IotTemplate;
 import com.zhongshu.iot.server.core.domain.iot.mqtt.DeviceInfo;
+import com.zhongshu.iot.server.core.domain.iot.mqtt.GateWay2Device;
 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.iot.IotThingService;
@@ -74,6 +76,9 @@ public class DeviceInfoService {
     @Autowired
     private IotThingService iotThingService;
 
+    @Autowired
+    private GateWay2DeviceDao gateWay2DeviceDao;
+
     public ResultContent initSyncAllDevice() {
         // 同步所有的设备
         List<DeviceInfo> deviceInfos = deviceInfoDao.findAll();
@@ -360,18 +365,6 @@ public class DeviceInfoService {
         return ResultContent.buildSuccess(model);
     }
 
-    //---------------------------网关数据 start-----------------
-
-    /**
-     * 得到所有的网关数据
-     *
-     * @return
-     */
-    public List<DeviceInfo> getAllGateWayDevices() {
-        return deviceInfoDao.findByDeviceCategory(DeviceCategory.GW);
-    }
-    //---------------------------网关数据 end-------------------
-
     public DeviceInfoModel toModel(DeviceInfo deviceInfo) {
         DeviceInfoModel deviceInfoModel = new DeviceInfoModel();
         if (ObjectUtils.isNotEmpty(deviceInfo)) {

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

@@ -1,643 +0,0 @@
-package com.zhongshu.iot.server.core.service.device;
-
-import cn.hutool.json.JSONUtil;
-import com.github.microservice.models.common.CommonResult;
-import com.github.microservice.net.ResultContent;
-import com.github.microservice.types.deviceUse.DeviceCategory;
-import com.github.microservice.types.deviceUse.OnLineState;
-import com.github.microservice.types.deviceUse.RegistType;
-import com.zhongshu.iot.client.model.mqtt.DeviceInfoAddParam;
-import com.zhongshu.iot.client.model.mqtt.DeviceInfoRegistParam;
-import com.zhongshu.iot.client.type.IotDataType;
-import com.zhongshu.iot.server.core.dao.iot.IotTemplateDao;
-import com.zhongshu.iot.server.core.dao.mqtt.*;
-import com.zhongshu.iot.server.core.domain.iot.IotTemplate;
-import com.zhongshu.iot.server.core.domain.iot.mqtt.*;
-import com.zhongshu.iot.server.core.httpRequest.ApiRequestService;
-import com.zhongshu.iot.server.core.service.base.SuperService;
-import com.zhongshu.iot.server.core.service.iot.IotServiceImpl;
-import com.zhongshu.iot.server.core.service.iot.IotThingService;
-import com.zhongshu.iot.server.core.service.sync.DeviceSyncFullCardService;
-import com.zhongshu.iot.server.core.util.DateUtils;
-import com.zhongshu.iot.server.core.util.JMXUtil;
-import com.zhongshu.iot.server.core.util.bean.BeanUtils;
-import com.zhongshu.iot.server.core.util.mqtt.MqttTopicUtils;
-import lombok.Cleanup;
-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.stereotype.Service;
-
-import javax.management.MBeanServerConnection;
-import javax.management.MBeanServerInvocationHandler;
-import javax.management.ObjectName;
-import javax.management.remote.JMXConnector;
-import javax.management.remote.JMXConnectorFactory;
-import javax.management.remote.JMXServiceURL;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.concurrent.CompletableFuture;
-
-/**
- * @author TRX
- * @date 2024/5/17
- */
-@Slf4j
-@Service
-@Deprecated
-public class GateWayInfoService extends SuperService {
-
-    @Autowired
-    private DeviceInfoService deviceInfoService;
-
-    @Autowired
-    private GateWay2DeviceDao gateWay2DeviceDao;
-
-    @Autowired
-    private MqttInfoDao mqttInfoDao;
-
-    @Autowired
-    private DeviceSyncFullCardService deviceSyncFullCardService;
-
-    @Autowired
-    private ProjectInfoDao projectInfoDao;
-
-    @Autowired
-    private ProjectInfoService projectInfoService;
-
-    @Autowired
-    private IotServiceImpl iotService;
-
-    @Autowired
-    private MqttInfoService mqttInfoService;
-
-    @Autowired
-    private ApiRequestService apiRequestService;
-
-    @Autowired
-    private JMXSyncService jmxSyncService;
-
-    @Autowired
-    private OperationMessageDao operationMessageDao;
-
-    @Autowired
-    private GateWayUserInfoService gateWayUserInfoService;
-
-    @Autowired
-    private GateWayUserInfoDao gateWayUserInfoDao;
-
-    @Autowired
-    private IotTemplateDao iotTemplateDao;
-
-    @Autowired
-    private IotThingService iotThingService;
-
-    @Autowired
-    private DeviceInfoDao deviceInfoDao;
-
-//    /**
-//     * 注册 网关
-//     *
-//     * @param dataStr
-//     * @return
-//     */
-//    public ResultContent<Object> gateWayRegister(String dataId, String dataStr) {
-//        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);
-//        }
-//        ResultContent<GateWayUserInfo> resultContent = gateWayUserInfoService.verifyMqttUser(operationMessage.getTopic());
-//        if (resultContent.isFailed()) {
-//            commonResult.setFailed(resultContent.getMsg());
-//            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);
-//    }
-
-//    /**
-//     * 添加网关
-//     *
-//     * @param param
-//     * @return
-//     */
-//    public ResultContent<GateWayInfo> addGateWayInfo(GateWayInfoAddParam 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不能为空");
-//        }
-//        if (param.getHbInterval() != null && param.getHbInterval() <= 0) {
-//            return ResultContent.buildFail("hbInterval不符合规范");
-//        }
-//        ResultContent checkIdContent = JMXUtil.checkDeviceIdLength(param.getGateWayId(), "gateWayId");
-//        if (checkIdContent.isFailed()) {
-//            return ResultContent.buildFail(checkIdContent.getMsg());
-//        }
-//        ResultContent checkNameContent = JMXUtil.checkDeviceNameLength(param.getGateWayName(), "gateWayName");
-//        if (checkNameContent.isFailed()) {
-//            return ResultContent.buildFail(checkNameContent.getMsg());
-//        }
-//
-//        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());
-//            gateWayInfo.setState(DeviceState.Enable);
-//        }
-//
-//        BeanUtils.copyProperties(param, gateWayInfo, "id");
-//        gateWayInfo.setOnLineState(OnLineState.OnLine);
-//        gateWayInfo.setLastOnlineTime(System.currentTimeMillis());
-//        if (ObjectUtils.isNotEmpty(projectInfo)) {
-//            gateWayInfo.setProjectInfo(projectInfo);
-//        }
-//
-//        gateWayInfoDao.save(gateWayInfo);
-//
-//        log.info("网关注册成功");
-//        // 通知同步 (网关信息)
-//        deviceSyncFullCardService.noticeSyncGateWay(gateWayInfo);
-//
-//        // 更新连接账号关联的设备数量
-//        gateWayUserInfoService.updateBindNumber(gateWayInfo.getMqttUserName());
-//
-//        // 同步权限
-//        jmxSyncService.syncSecurityToMQTTService(gateWayInfo);
-//        return ResultContent.buildSuccess(gateWayInfo);
-//    }
-
-    /**
-     * 注册设备
-     *
-     * @param dataId
-     * @param dataStr
-     * @return
-     */
-    public ResultContent registDevice(String dataId, String dataStr) {
-        log.info("registDevice: {}", dataId);
-        CommonResult commonResult = new CommonResult();
-        OperationMessage operationMessage = operationMessageDao.findTopByDataId(dataId);
-        commonResult.setMessageId(dataId);
-        if (ObjectUtils.isEmpty(operationMessage)) {
-            commonResult.setFailed("数据不存在");
-            return ResultContent.buildSuccess(commonResult);
-        }
-        // 找到连接账号的信息
-        ResultContent<GateWayUserInfo> resultContent = gateWayUserInfoService.verifyMqttUser(operationMessage.getTopic());
-        if (resultContent.isFailed()) {
-            commonResult.setFailed(resultContent.getMsg());
-            return ResultContent.buildSuccess(commonResult);
-        }
-
-        // 注册设备参数
-        DeviceInfoRegistParam param = null;
-        try {
-            param = JSONUtil.toBean(dataStr, DeviceInfoRegistParam.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<DeviceInfo> content = addDevice(param);
-        if (content.isSuccess()) {
-        } else {
-            commonResult.setFailed(content.getMsg());
-            return ResultContent.buildSuccess(commonResult);
-        }
-        commonResult.setSuccess("注册成功");
-        return ResultContent.buildSuccess(commonResult);
-    }
-
-    /**
-     * 注册设备
-     *
-     * @param param
-     * @return
-     */
-    public ResultContent<DeviceInfo> addDevice(DeviceInfoRegistParam param) {
-        // 产品code
-        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不能为空");
-        }
-        // 分组code
-        if (StringUtils.isEmpty(param.getProjectInfoCode())) {
-            return ResultContent.buildFail("projectInfoCode不能为空");
-        }
-        ResultContent checkIdContent = JMXUtil.checkDeviceIdLength(param.getDeviceId(), "deviceId");
-        if (checkIdContent.isFailed()) {
-            return ResultContent.buildFail(checkIdContent.getMsg());
-        }
-        ResultContent checkNameContent = JMXUtil.checkDeviceNameLength(param.getDeviceName(), "deviceName");
-        if (checkNameContent.isFailed()) {
-            return ResultContent.buildFail(checkNameContent.getMsg());
-        }
-
-        // 检查产品是否存在
-        IotTemplate iotTemplate = iotTemplateDao.findTopByProductCodeAndIotDataType(param.getProductCode(), IotDataType.IotTemplate);
-        if (ObjectUtils.isEmpty(iotTemplate)) {
-            return ResultContent.buildFail(String.format("productCode不存在", param.getProductCode()));
-        }
-
-        // 检查分组是否存在
-        ProjectInfo projectInfo = projectInfoDao.findTopByCode(param.getProjectInfoCode());
-        if (ObjectUtils.isEmpty(projectInfo)) {
-            return ResultContent.buildFail("projectInfoCode不存在");
-        }
-
-        RegistType registType = RegistType.Gateway;
-        DeviceInfo gateWayInfo;
-        if (StringUtils.isNotEmpty(param.getGateWayId())) {
-            gateWayInfo = deviceInfoDao.findTopByDeviceId(param.getGateWayId());
-            if (ObjectUtils.isEmpty(gateWayInfo)) {
-                return ResultContent.buildFail("gateWayId数据不存在");
-            }
-            registType = RegistType.Gateway;
-
-            if (StringUtils.isEmpty(gateWayInfo.getMqttUserName()) || !gateWayInfo.getMqttUserName().equals(param.getMqttUserName())) {
-                return ResultContent.buildFail("设备关联的连接账号与所属网关不匹配,注册失败");
-            }
-        } else {
-            gateWayInfo = null;
-            registType = RegistType.DirectConnection;
-        }
-
-        // 封装参数
-        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);
-
-        // 设备列表
-        List<DeviceInfo> deviceInfos = new ArrayList<>();
-        if (ObjectUtils.isNotEmpty(devices)) {
-            // 绑定网关和设备的关系
-            for (DeviceInfoAddParam device : devices) {
-                // 保存设备信息
-                ResultContent<DeviceInfo> resultContent = deviceInfoService.addDeviceInfo(device);
-                DeviceInfo deviceInfo = resultContent.getContent();
-                deviceBindGateWay(deviceInfo, gateWayInfo);
-                deviceInfos.add(deviceInfo);
-                // 更新设备的topic
-                iotService.updateAllDeviceIotMainGateWayInfo(deviceInfo);
-            }
-        }
-
-        CompletableFuture.runAsync(() -> {
-            // 更新网关的权限
-            jmxSyncService.syncSecurityToMQTTService(gateWayInfo);
-            log.info("设备注册成功:{}", deviceInfos.size());
-        });
-
-        return ResultContent.buildSuccess();
-    }
-
-    /**
-     * 设备绑定 网关
-     *
-     * @param deviceInfo
-     * @param gateWayInfo
-     * @return
-     */
-    public ResultContent deviceBindGateWay(DeviceInfo deviceInfo, DeviceInfo 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 != null ? gateWayInfo.getDeviceId() : "");
-            gateWay2Device.setDeviceInfo(deviceInfo);
-            gateWay2Device.setDeviceId(deviceInfo.getDeviceId());
-
-            gateWay2Device.setBindTimeStr(DateUtils.paresTime(System.currentTimeMillis(), DateUtils.FORMAT_LONG));
-            gateWay2DeviceDao.save(gateWay2Device);
-        }
-        return ResultContent.buildSuccess();
-    }
-
-//    /**
-//     * 注册网关
-//     *
-//     * @param param
-//     * @return
-//     */
-//    @Deprecated
-//    public ResultContent<MqttInfoReturnModel> registerGateWay(GateWayInfoAddParam param) {
-//        ProjectInfo projectInfo = projectInfoDao.findTopByCode(param.getProjectInfoCode());
-//        if (ObjectUtils.isEmpty(projectInfo)) {
-//            return ResultContent.buildFail(String.format("分组不存在:%s", param.getProjectInfoCode()));
-//        }
-//        // 添加网关信息
-//        ResultContent<GateWayInfo> gateWayInfo = addGateWayInfo(param);
-//
-//        // 给网关分配个mqtt账号
-//        MqttInfoReturnModel mqttInfoSimpleModel = mqttInfoService.getCommonMqttInfo(gateWayInfo.getContent());
-//
-//        // 通知下发项目配置信息
-//        ProjectConfigQueryParam queryParam = new ProjectConfigQueryParam();
-//        queryParam.setProjectInfoCode(param.getProjectInfoCode());
-//        APIResponseModel api = apiRequestService.sendFullCardAPI(FullCardAPIConfig.iotQueryProjectConfig, queryParam);
-//        if (api.isSuccess()) {
-//            log.info("查询项目配置成功");
-//        } else {
-//            log.error("查询项目配置失败:{}", api.getMsg());
-//        }
-//        return ResultContent.buildSuccess(mqttInfoSimpleModel);
-//    }
-
-//    /**
-//     * 网关绑定设备、连接账号
-//     *
-//     * @param param
-//     * @return
-//     */
-//    public ResultContent gateWayBindDevice(GateWayBindDeviceParam param) {
-//        // 网关信息
-//        GateWayInfo gateWayInfo = gateWayInfoDao.findTopByGateWayId(param.getGateWayId());
-//        if (ObjectUtils.isEmpty(gateWayInfo)) {
-//            log.error("网关未注册");
-//            return ResultContent.buildFail(String.format("网关未注册,清先注册网关:%s", param.getGateWayId()));
-//        }
-//
-//        String projectInfoCode = param.getProjectInfoCode();
-//        if (ObjectUtils.isEmpty(projectInfoCode)) {
-//            projectInfoCode = gateWayInfo.getProjectInfoCode();
-//        }
-//
-//        ProjectInfo projectInfo = projectInfoDao.findTopByCode(projectInfoCode);
-//        if (ObjectUtils.isEmpty(projectInfo)) {
-//            log.error("分组不存在");
-//            return ResultContent.buildFail(String.format("分组不存在:%s", projectInfoCode));
-//        }
-//
-//        List<DeviceInfoAddParam> devices = param.getDevices();
-//        if (ObjectUtils.isNotEmpty(devices)) {
-//            // 检查设备数据合法性
-//            for (DeviceInfoAddParam device : devices) {
-//                if (StringUtils.isEmpty(device.getDeviceId())) {
-//                    return ResultContent.buildFail("deviceId不能为空");
-//                }
-//            }
-//        }
-//
-//        GateWay2User gateWay2User = gateWay2UserDao.findTopByGateWayInfo(gateWayInfo);
-//        if (ObjectUtils.isEmpty(gateWay2User)) {
-//            return ResultContent.buildFail(String.format("网关为分配连接账户: %S", gateWay2User.getGateWayId()));
-//        }
-//
-//        // 设备列表
-//        List<DeviceInfo> deviceInfos = new ArrayList<>();
-//        if (ObjectUtils.isNotEmpty(devices)) {
-//            // 绑定网关和设备的关系
-//            for (DeviceInfoAddParam device : devices) {
-//                device.setProjectInfoCode(projectInfoCode);
-//                // 保存设备信息
-//                ResultContent<DeviceInfo> resultContent = deviceInfoService.addDeviceInfo(device);
-//                DeviceInfo deviceInfo = resultContent.getContent();
-//
-//                // 设备可以绑定到多个网关,一个网关只能绑定设备一次
-//                GateWay2Device gateWay2Device = gateWay2DeviceDao.findTopByGateWayInfoAndDeviceInfo(gateWayInfo, deviceInfo);
-//                if (ObjectUtils.isEmpty(gateWay2Device)) {
-//                    gateWay2Device = new GateWay2Device();
-//                    gateWay2Device.setState(OnLineState.OffLine);
-//                } else {
-//                }
-//                gateWay2Device.setGateWayInfo(gateWayInfo);
-//                gateWay2Device.setDeviceInfo(deviceInfo);
-//                gateWay2Device.setGateWayId(gateWayInfo.getGateWayId());
-//                gateWay2Device.setDeviceId(deviceInfo.getDeviceId());
-//
-//                gateWay2Device.setBindTimeStr(DateUtils.paresTime(System.currentTimeMillis(), DateUtils.FORMAT_LONG));
-//                gateWay2DeviceDao.save(gateWay2Device);
-//                deviceInfos.add(deviceInfo);
-//
-//                iotService.updateAllDeviceIotMainGateWayInfo(deviceInfo);
-//            }
-//        }
-//
-//        // 更新网关的权限
-//        jmxSyncService.syncSecurityToMQTTService(gateWayInfo);
-//        log.info("设备注册成功:{}", deviceInfos.size());
-//
-//        // 同步设备
-//        deviceSyncFullCardService.noticeSyncDevice(deviceInfos);
-//        return ResultContent.buildSuccess();
-//    }
-
-//    /**
-//     * 删除网关
-//     *
-//     * @param gateWayId
-//     * @return
-//     */
-//    public ResultContent deleteGateWayInfo(String gateWayId) {
-//        GateWayInfo gateWayInfo = gateWayInfoDao.findTopByGateWayId(gateWayId);
-//        if (ObjectUtils.isEmpty(gateWayInfo)) {
-//            return ResultContent.buildFail(String.format("网关ID不存在:%s", gateWayId));
-//        }
-//        gateWayInfoDao.delete(gateWayInfo);
-//        return ResultContent.buildSuccess();
-//    }
-
-//    /**
-//     * 网关列表
-//     *
-//     * @param pageable
-//     * @param param
-//     * @return
-//     */
-//    public ResultContent<Page<GateWayInfoModel>> pageGateWay(Pageable pageable, GateWayInfoSearchParam param) {
-//        Page<GateWayInfo> page = gateWayInfoDao.page(pageable, param);
-//        return ResultContent.buildSuccess(PageEntityUtil.toPageModel(page, this::toModel));
-//    }
-
-//    /**
-//     * 查询网关详情
-//     *
-//     * @param gateWayId
-//     * @return
-//     */
-//    public ResultContent<GateWayInfoModel> getById(String gateWayId) {
-//        GateWayInfoModel model = null;
-//        GateWayInfo deviceInfo = gateWayInfoDao.findTopByGateWayId(gateWayId);
-//        if (ObjectUtils.isNotEmpty(deviceInfo)) {
-//            model = toModel(deviceInfo);
-//        }
-//        return ResultContent.buildSuccess(model);
-//    }
-
-//    public GateWayInfoModel toModel(GateWayInfo entity) {
-//        GateWayInfoModel model = new GateWayInfoModel();
-//        if (ObjectUtils.isNotEmpty(entity)) {
-//            BeanUtils.copyProperties(entity, model);
-//            model.setProjectInfo(projectInfoService.toModel(entity.getProjectInfo()));
-//        }
-//        return model;
-//    }
-
-    /**
-     * 绑定角色网关的权限
-     *
-     * @param roleName
-     * @param gateWayId
-     * @return
-     */
-    public ResultContent bindUserGateWayPermissions(String roleName, String gateWayId) {
-        try {
-            List<MqttInfo> list = mqttInfoDao.findAll();
-            if (ObjectUtils.isNotEmpty(list)) {
-                for (MqttInfo mqttInfo : list) {
-                    if (StringUtils.isNotEmpty(mqttInfo.getJmxHost()) && StringUtils.isNotEmpty(mqttInfo.getJmxPort())) {
-                        String urlStr = String.format("service:jmx:rmi:///jndi/rmi://%s:%s/jmxrmi", mqttInfo.getJmxHost(), mqttInfo.getJmxPort());
-                        JMXServiceURL url = new JMXServiceURL(urlStr);
-                        @Cleanup JMXConnector connector = JMXConnectorFactory.connect(url, null);
-                        connector.connect();
-                        log.info("JMX %s:%s 连接成功...", 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);
-
-                        addressControl.addSecuritySettings(MqttTopicUtils.buildGateWayAllTopic(gateWayId), roleName, roleName, roleName, roleName, roleName, roleName, roleName, roleName, roleName, roleName);
-                    } else {
-                        log.error("MQTT JMX 信息为空", mqttInfo.getName());
-                    }
-                }
-            }
-        } catch (Exception e) {
-            e.printStackTrace();
-            log.error("bindUserGateWayPermissions 出错:%s", e.getMessage());
-        }
-        return ResultContent.buildSuccess();
-    }
-
-    public ResultContent bindUserDevicesPermissions(String roleName, List<DeviceInfo> deviceInfos) {
-        try {
-            List<MqttInfo> list = mqttInfoDao.findAll();
-            if (ObjectUtils.isNotEmpty(list)) {
-                list.parallelStream().forEach(mqttInfo -> {
-                    try {
-                        if (StringUtils.isNotEmpty(mqttInfo.getJmxHost()) && StringUtils.isNotEmpty(mqttInfo.getJmxPort())) {
-                            String urlStr = String.format("service:jmx:rmi:///jndi/rmi://%s:%s/jmxrmi", mqttInfo.getJmxHost(), mqttInfo.getJmxPort());
-                            JMXServiceURL url = new JMXServiceURL(urlStr);
-                            @Cleanup JMXConnector connector = JMXConnectorFactory.connect(url, null);
-                            connector.connect();
-                            log.info("JMX %s:%s 连接成功...", 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);
-
-                            for (DeviceInfo deviceInfo : deviceInfos) {
-                                addressControl.addSecuritySettings(MqttTopicUtils.buildDeviceAllTopic(deviceInfo.getDeviceId()), roleName, roleName, roleName, roleName, roleName, roleName, roleName, roleName, roleName, roleName);
-                            }
-                        } else {
-                            log.error("MQTT JMX 信息为空:{}", mqttInfo.getName());
-                        }
-                    } catch (Exception e) {
-                        e.printStackTrace();
-                    }
-                });
-            }
-        } catch (Exception e) {
-            e.printStackTrace();
-            log.error("bindUserDevicesPermissions 出错:%s", e.getMessage());
-        }
-        return ResultContent.buildSuccess();
-    }
-
-    /**
-     * 得到设备的网关信息
-     *
-     * @param deviceInfo
-     * @return
-     */
-    public DeviceInfo getDeviceGateWayInfo(DeviceInfo deviceInfo) {
-        if (ObjectUtils.isEmpty(deviceInfo)) {
-            return null;
-        }
-        GateWay2Device gateWay2Device = gateWay2DeviceDao.findTopByDeviceInfoOrderByUpdateTimeDesc(deviceInfo);
-        if (gateWay2Device == null) {
-            return null;
-        }
-        return gateWay2Device.getGateWayInfo();
-    }
-}

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

@@ -0,0 +1,305 @@
+package com.zhongshu.iot.server.core.service.device;
+
+import cn.hutool.json.JSONUtil;
+import com.github.microservice.models.common.CommonResult;
+import com.github.microservice.net.ResultContent;
+import com.github.microservice.types.deviceUse.DeviceCategory;
+import com.github.microservice.types.deviceUse.OnLineState;
+import com.github.microservice.types.deviceUse.RegistType;
+import com.zhongshu.iot.client.model.mqtt.DeviceInfoAddParam;
+import com.zhongshu.iot.client.model.mqtt.DeviceInfoRegistParam;
+import com.zhongshu.iot.client.type.IotDataType;
+import com.zhongshu.iot.server.core.dao.iot.IotTemplateDao;
+import com.zhongshu.iot.server.core.dao.mqtt.*;
+import com.zhongshu.iot.server.core.domain.iot.IotTemplate;
+import com.zhongshu.iot.server.core.domain.iot.mqtt.*;
+import com.zhongshu.iot.server.core.httpRequest.ApiRequestService;
+import com.zhongshu.iot.server.core.service.base.SuperService;
+import com.zhongshu.iot.server.core.service.iot.IotServiceImpl;
+import com.zhongshu.iot.server.core.service.iot.IotThingService;
+import com.zhongshu.iot.server.core.service.sync.DeviceSyncFullCardService;
+import com.zhongshu.iot.server.core.util.DateUtils;
+import com.zhongshu.iot.server.core.util.JMXUtil;
+import com.zhongshu.iot.server.core.util.bean.BeanUtils;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.ObjectUtils;
+import org.apache.commons.lang3.StringUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.CompletableFuture;
+
+/**
+ * 网关管理服务
+ *
+ * @author TRX
+ * @date 2024/5/17
+ */
+@Slf4j
+@Service
+public class GateWayManagerService extends SuperService {
+
+    @Autowired
+    private DeviceInfoService deviceInfoService;
+
+    @Autowired
+    private GateWay2DeviceDao gateWay2DeviceDao;
+
+    @Autowired
+    private MqttInfoDao mqttInfoDao;
+
+    @Autowired
+    private DeviceSyncFullCardService deviceSyncFullCardService;
+
+    @Autowired
+    private ProjectInfoDao projectInfoDao;
+
+    @Autowired
+    private ProjectInfoService projectInfoService;
+
+    @Autowired
+    private IotServiceImpl iotService;
+
+    @Autowired
+    private MqttInfoService mqttInfoService;
+
+    @Autowired
+    private ApiRequestService apiRequestService;
+
+    @Autowired
+    private JMXSyncService jmxSyncService;
+
+    @Autowired
+    private OperationMessageDao operationMessageDao;
+
+    @Autowired
+    private GateWayUserInfoService gateWayUserInfoService;
+
+    @Autowired
+    private GateWayUserInfoDao gateWayUserInfoDao;
+
+    @Autowired
+    private IotTemplateDao iotTemplateDao;
+
+    @Autowired
+    private IotThingService iotThingService;
+
+    @Autowired
+    private DeviceInfoDao deviceInfoDao;
+
+    /**
+     * 注册设备
+     *
+     * @param dataId
+     * @param dataStr
+     * @return
+     */
+    public ResultContent registDevice(String dataId, String dataStr) {
+        log.info("registDevice: {}", dataId);
+        CommonResult commonResult = new CommonResult();
+        OperationMessage operationMessage = operationMessageDao.findTopByDataId(dataId);
+        commonResult.setMessageId(dataId);
+        if (ObjectUtils.isEmpty(operationMessage)) {
+            commonResult.setFailed("数据不存在");
+            return ResultContent.buildSuccess(commonResult);
+        }
+        // 找到连接账号的信息
+        ResultContent<GateWayUserInfo> resultContent = gateWayUserInfoService.verifyMqttUser(operationMessage.getTopic());
+        if (resultContent.isFailed()) {
+            commonResult.setFailed(resultContent.getMsg());
+            return ResultContent.buildSuccess(commonResult);
+        }
+
+        // 注册设备参数
+        DeviceInfoRegistParam param = null;
+        try {
+            param = JSONUtil.toBean(dataStr, DeviceInfoRegistParam.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<DeviceInfo> content = addDevice(param);
+        if (content.isSuccess()) {
+        } else {
+            commonResult.setFailed(content.getMsg());
+            return ResultContent.buildSuccess(commonResult);
+        }
+        commonResult.setSuccess("注册成功");
+        return ResultContent.buildSuccess(commonResult);
+    }
+
+    /**
+     * 注册设备
+     *
+     * @param param
+     * @return
+     */
+    public ResultContent<DeviceInfo> addDevice(DeviceInfoRegistParam param) {
+        // 产品code
+        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不能为空");
+        }
+        // 分组code
+        if (StringUtils.isEmpty(param.getProjectInfoCode())) {
+            return ResultContent.buildFail("projectInfoCode不能为空");
+        }
+        ResultContent checkIdContent = JMXUtil.checkDeviceIdLength(param.getDeviceId(), "deviceId");
+        if (checkIdContent.isFailed()) {
+            return ResultContent.buildFail(checkIdContent.getMsg());
+        }
+        ResultContent checkNameContent = JMXUtil.checkDeviceNameLength(param.getDeviceName(), "deviceName");
+        if (checkNameContent.isFailed()) {
+            return ResultContent.buildFail(checkNameContent.getMsg());
+        }
+
+        // 检查产品是否存在
+        IotTemplate iotTemplate = iotTemplateDao.findTopByProductCodeAndIotDataType(param.getProductCode(), IotDataType.IotTemplate);
+        if (ObjectUtils.isEmpty(iotTemplate)) {
+            return ResultContent.buildFail(String.format("productCode不存在", param.getProductCode()));
+        }
+
+        // 检查分组是否存在
+        ProjectInfo projectInfo = projectInfoDao.findTopByCode(param.getProjectInfoCode());
+        if (ObjectUtils.isEmpty(projectInfo)) {
+            return ResultContent.buildFail("projectInfoCode不存在");
+        }
+
+        RegistType registType = RegistType.Gateway;
+        DeviceInfo gateWayInfo;
+        if (StringUtils.isNotEmpty(param.getGateWayId())) {
+            gateWayInfo = deviceInfoDao.findTopByDeviceId(param.getGateWayId());
+            if (ObjectUtils.isEmpty(gateWayInfo)) {
+                return ResultContent.buildFail("gateWayId数据不存在");
+            }
+            registType = RegistType.Gateway;
+
+            if (StringUtils.isEmpty(gateWayInfo.getMqttUserName()) || !gateWayInfo.getMqttUserName().equals(param.getMqttUserName())) {
+                return ResultContent.buildFail("设备关联的连接账号与所属网关不匹配,注册失败");
+            }
+        } else {
+            gateWayInfo = null;
+            registType = RegistType.DirectConnection;
+        }
+
+        // 封装参数
+        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);
+
+        // 设备列表
+        List<DeviceInfo> deviceInfos = new ArrayList<>();
+        if (ObjectUtils.isNotEmpty(devices)) {
+            // 绑定网关和设备的关系
+            for (DeviceInfoAddParam device : devices) {
+                // 保存设备信息
+                ResultContent<DeviceInfo> resultContent = deviceInfoService.addDeviceInfo(device);
+                DeviceInfo deviceInfo = resultContent.getContent();
+                deviceBindGateWay(deviceInfo, gateWayInfo);
+                deviceInfos.add(deviceInfo);
+                // 更新设备的topic
+                iotService.updateAllDeviceIotMainGateWayInfo(deviceInfo);
+            }
+        }
+
+        CompletableFuture.runAsync(() -> {
+            // 更新网关的权限
+            jmxSyncService.syncSecurityToMQTTService(gateWayInfo);
+            log.info("设备注册成功:{}", deviceInfos.size());
+        });
+
+        return ResultContent.buildSuccess();
+    }
+
+    /**
+     * 设备绑定 网关
+     *
+     * @param deviceInfo
+     * @param gateWayInfo
+     * @return
+     */
+    public ResultContent deviceBindGateWay(DeviceInfo deviceInfo, DeviceInfo 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 != null ? gateWayInfo.getDeviceId() : "");
+            gateWay2Device.setDeviceInfo(deviceInfo);
+            gateWay2Device.setDeviceId(deviceInfo.getDeviceId());
+
+            gateWay2Device.setBindTimeStr(DateUtils.paresTime(System.currentTimeMillis(), DateUtils.FORMAT_LONG));
+            gateWay2DeviceDao.save(gateWay2Device);
+        }
+        return ResultContent.buildSuccess();
+    }
+
+    /**
+     * 得到所有的网关数据
+     *
+     * @return
+     */
+    public List<DeviceInfo> getAllGateWayDevices() {
+        return deviceInfoDao.findByDeviceCategory(DeviceCategory.GW);
+    }
+
+
+    /**
+     * 得到设备的网关信息
+     *
+     * @param deviceInfo
+     * @return
+     */
+    public DeviceInfo getDeviceGateWayInfo(DeviceInfo deviceInfo) {
+        if (ObjectUtils.isEmpty(deviceInfo)) {
+            return null;
+        }
+        GateWay2Device gateWay2Device = gateWay2DeviceDao.findTopByDeviceInfoOrderByUpdateTimeDesc(deviceInfo);
+        if (gateWay2Device == null) {
+            return null;
+        }
+        return gateWay2Device.getGateWayInfo();
+    }
+
+}

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

@@ -64,7 +64,7 @@ public class JMXSyncService {
     private DeviceInfoDao deviceInfoDao;
 
     @Autowired
-    private DeviceInfoService deviceInfoService;
+    private GateWayManagerService gateWayInfoService;
 
     /**
      * 把用户同步到MQTT服务中
@@ -499,7 +499,7 @@ public class JMXSyncService {
      * @return
      */
     public ResultContent refreshAllGateWaySecurity() {
-        List<DeviceInfo> gateWayInfos = deviceInfoService.getAllGateWayDevices();
+        List<DeviceInfo> gateWayInfos = gateWayInfoService.getAllGateWayDevices();
         if (ObjectUtils.isNotEmpty(gateWayInfos)) {
             syncSecurityToMQTTService(gateWayInfos);
         }

+ 2 - 2
OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/service/iot/IotTemplateManagerService.java

@@ -16,7 +16,7 @@ import com.zhongshu.iot.server.core.domain.iot.mqtt.DeviceInfo;
 import com.zhongshu.iot.server.core.service.base.CommonService;
 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.device.GateWayManagerService;
 import com.zhongshu.iot.server.core.util.bean.BeanUtils;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.ObjectUtils;
@@ -72,7 +72,7 @@ public class IotTemplateManagerService extends SuperService {
     private IotServiceImpl iotService;
 
     @Autowired
-    private GateWayInfoService gateWayInfoService;
+    private GateWayManagerService gateWayInfoService;
 
     /**
      * 自动绑定:更加物模型 绑定属性的信息 (添加或更新物模型)

+ 2 - 2
OneCardIotServer/src/main/java/com/zhongshu/iot/server/core/service/iotPlatform/impl/RegisterDeviceTopic.java

@@ -4,7 +4,7 @@ package com.zhongshu.iot.server.core.service.iotPlatform.impl;
 import com.github.microservice.net.ResultContent;
 import com.zhongshu.iot.server.core.domain.ExecuteAnnotationService;
 import com.zhongshu.iot.server.core.domain.ExecuteAnnotationServiceMethod;
-import com.zhongshu.iot.server.core.service.device.GateWayInfoService;
+import com.zhongshu.iot.server.core.service.device.GateWayManagerService;
 import com.zhongshu.iot.server.core.service.iotPlatform.PlatformTopic;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -20,7 +20,7 @@ import org.springframework.stereotype.Component;
 public class RegisterDeviceTopic implements PlatformTopic {
 
     @Autowired
-    private GateWayInfoService gateWayInfoService;
+    private GateWayManagerService gateWayInfoService;
 
     @Override
     public String topic() {