|
|
@@ -4,9 +4,12 @@ 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.DeviceType;
|
|
|
import com.github.microservice.models.type.OnLineState;
|
|
|
+import com.github.microservice.models.type.RegistType;
|
|
|
import com.github.microservice.net.ResultContent;
|
|
|
import com.zhongshu.iot.client.model.mqtt.*;
|
|
|
+import com.zhongshu.iot.client.type.type.LogsLevel;
|
|
|
import com.zhongshu.iot.server.core.dao.mqtt.*;
|
|
|
import com.zhongshu.iot.server.core.domain.iot.mqtt.*;
|
|
|
import com.zhongshu.iot.server.core.httpRequest.ApiRequestService;
|
|
|
@@ -14,6 +17,7 @@ import com.zhongshu.iot.server.core.httpRequest.conf.FullCardAPIConfig;
|
|
|
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.sync.DeviceSyncFullCardService;
|
|
|
+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;
|
|
|
import com.zhongshu.iot.server.core.util.mqtt.MqttTopicUtils;
|
|
|
@@ -37,6 +41,7 @@ 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
|
|
|
@@ -148,6 +153,9 @@ public class GateWayInfoService extends SuperService {
|
|
|
if (StringUtils.isEmpty(param.getProjectInfoCode())) {
|
|
|
return ResultContent.buildFail("projectInfoCode不能为空");
|
|
|
}
|
|
|
+ if (param.getHbInterval() != null && param.getHbInterval() <= 0) {
|
|
|
+ return ResultContent.buildFail("hbInterval不符合规范");
|
|
|
+ }
|
|
|
ProjectInfo projectInfo = null;
|
|
|
if (StringUtils.isNotEmpty(param.getProjectInfoCode())) {
|
|
|
projectInfo = projectInfoDao.findTopByCode(param.getProjectInfoCode());
|
|
|
@@ -156,8 +164,7 @@ public class GateWayInfoService extends SuperService {
|
|
|
return ResultContent.buildFail("projectInfoCode不存在");
|
|
|
}
|
|
|
GateWayUserInfo gateWayUserInfo = gateWayUserInfoDao.findTopByUserName(param.getMqttUserName());
|
|
|
- if (StringUtils.isNotEmpty(gateWayUserInfo.getProjectCode()) &&
|
|
|
- !gateWayUserInfo.getProjectCode().equals(param.getProjectInfoCode())) {
|
|
|
+ if (StringUtils.isNotEmpty(gateWayUserInfo.getProjectCode()) && !gateWayUserInfo.getProjectCode().equals(param.getProjectInfoCode())) {
|
|
|
return ResultContent.buildFail("projectInfoCode不匹配");
|
|
|
}
|
|
|
|
|
|
@@ -184,12 +191,160 @@ public class GateWayInfoService extends SuperService {
|
|
|
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) {
|
|
|
+ if (StringUtils.isEmpty(param.getDeviceId())) {
|
|
|
+ return ResultContent.buildFail("deviceId不能为空");
|
|
|
+ }
|
|
|
+ if (StringUtils.isEmpty(param.getDeviceName())) {
|
|
|
+ return ResultContent.buildFail("deviceName不能为空");
|
|
|
+ }
|
|
|
+ if (StringUtils.isEmpty(param.getDeviceCate())) {
|
|
|
+ return ResultContent.buildFail("deviceCate不能为空");
|
|
|
+ }
|
|
|
+ if (StringUtils.isEmpty(param.getProjectInfoCode())) {
|
|
|
+ return ResultContent.buildFail("projectInfoCode不能为空");
|
|
|
+ }
|
|
|
+ ProjectInfo projectInfo = projectInfoDao.findTopByCode(param.getProjectInfoCode());
|
|
|
+ if (ObjectUtils.isEmpty(projectInfo)) {
|
|
|
+ return ResultContent.buildFail("projectInfoCode不存在");
|
|
|
+ }
|
|
|
+
|
|
|
+ RegistType registType = RegistType.Gateway;
|
|
|
+ GateWayInfo gateWayInfo;
|
|
|
+ if (StringUtils.isNotEmpty(param.getGateWayId())) {
|
|
|
+ gateWayInfo = gateWayInfoDao.findTopByGateWayId(param.getGateWayId());
|
|
|
+ if (ObjectUtils.isEmpty(gateWayInfo)) {
|
|
|
+ return ResultContent.buildFail("gateWayId数据不存在");
|
|
|
+ }
|
|
|
+ registType = RegistType.Gateway;
|
|
|
+ } else {
|
|
|
+ gateWayInfo = null;
|
|
|
+ registType = RegistType.DirectConnection;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 封装参数
|
|
|
+ List<DeviceInfoAddParam> devices = new ArrayList<>();
|
|
|
+ DeviceInfoAddParam deviceInfoAddParam = new DeviceInfoAddParam();
|
|
|
+ BeanUtils.copyProperties(param, deviceInfoAddParam);
|
|
|
+ deviceInfoAddParam.setRegistType(registType);
|
|
|
+ if (StringUtils.isNotEmpty(param.getDeviceCate())) {
|
|
|
+ DeviceType deviceType = CommonUtil.getEnumByName(DeviceType.class, param.getDeviceCate());
|
|
|
+ if (deviceType == null) {
|
|
|
+ return ResultContent.buildFail("deviceCate不存在");
|
|
|
+ }
|
|
|
+ if (deviceType != null) {
|
|
|
+ deviceInfoAddParam.setDeviceType(deviceType);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ 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();
|
|
|
+
|
|
|
+ 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.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);
|
|
|
+
|
|
|
+ // 更新设备的topic
|
|
|
+ iotService.updateAllDeviceIotMainGateWayInfo(deviceInfo);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ CompletableFuture.runAsync(() -> {
|
|
|
+ // 更新网关的权限
|
|
|
+ jmxSyncService.syncSecurityToMQTTService(gateWayInfo);
|
|
|
+ log.info("设备注册成功:{}", deviceInfos.size());
|
|
|
+ // 同步设备
|
|
|
+ deviceSyncFullCardService.noticeSyncDevice(deviceInfos);
|
|
|
+ });
|
|
|
+
|
|
|
+ return ResultContent.buildSuccess();
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 注册网关
|
|
|
*
|
|
|
* @param param
|
|
|
* @return
|
|
|
*/
|
|
|
+ @Deprecated
|
|
|
public ResultContent<MqttInfoReturnModel> registerGateWay(GateWayInfoAddParam param) {
|
|
|
ProjectInfo projectInfo = projectInfoDao.findTopByCode(param.getProjectInfoCode());
|
|
|
if (ObjectUtils.isEmpty(projectInfo)) {
|