|
|
@@ -1,8 +1,5 @@
|
|
|
package com.yunfeiyun.agmp.iotm.web.service.impl;
|
|
|
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.List;
|
|
|
-
|
|
|
import com.alibaba.fastjson2.JSONArray;
|
|
|
import com.yunfeiyun.agmp.common.constant.ErrorCode;
|
|
|
import com.yunfeiyun.agmp.common.utils.DateUtils;
|
|
|
@@ -13,7 +10,7 @@ import com.yunfeiyun.agmp.iot.common.domain.IotDeviceconn;
|
|
|
import com.yunfeiyun.agmp.iot.common.domain.TosDevicetype;
|
|
|
import com.yunfeiyun.agmp.iot.common.enums.IotDeviceconnTypeEnum;
|
|
|
import com.yunfeiyun.agmp.iot.common.exception.IotBizException;
|
|
|
-import com.yunfeiyun.agmp.iot.common.service.DeviceconnCacheService;
|
|
|
+import com.yunfeiyun.agmp.iot.common.model.mq.IotDeviceEditMqModel;
|
|
|
import com.yunfeiyun.agmp.iotm.mq.service.SendToIotsMsgService;
|
|
|
import com.yunfeiyun.agmp.iotm.web.domain.IotDevconnConfigInfoVo;
|
|
|
import com.yunfeiyun.agmp.iotm.web.domain.reqvo.IotDeviceconnAddReqVo;
|
|
|
@@ -21,15 +18,17 @@ import com.yunfeiyun.agmp.iotm.web.domain.reqvo.IotDeviceconnEditReqVo;
|
|
|
import com.yunfeiyun.agmp.iotm.web.domain.reqvo.IotDeviceconnListReqVo;
|
|
|
import com.yunfeiyun.agmp.iotm.web.domain.resvo.IotDeviceconnInfoResVo;
|
|
|
import com.yunfeiyun.agmp.iotm.web.domain.resvo.IotDeviceconnListResVo;
|
|
|
+import com.yunfeiyun.agmp.iotm.web.mapper.IotDeviceMapper;
|
|
|
import com.yunfeiyun.agmp.iotm.web.mapper.IotDeviceconnMapper;
|
|
|
import com.yunfeiyun.agmp.iotm.web.service.IIotDeviceService;
|
|
|
import com.yunfeiyun.agmp.iotm.web.service.IIotDeviceconnService;
|
|
|
import com.yunfeiyun.agmp.iotm.web.service.ITosDevicetypeService;
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
-
|
|
|
-import javax.annotation.PostConstruct;
|
|
|
import javax.annotation.Resource;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
|
|
|
/**
|
|
|
* 设备连接配置Service业务层处理
|
|
|
@@ -42,24 +41,14 @@ public class IotDeviceconnServiceImpl implements IIotDeviceconnService
|
|
|
{
|
|
|
@Autowired
|
|
|
private IotDeviceconnMapper iotDeviceconnMapper;
|
|
|
-
|
|
|
@Autowired
|
|
|
private ITosDevicetypeService tosDevicetypeService;
|
|
|
-
|
|
|
@Autowired
|
|
|
private IIotDeviceService iotDeviceService;
|
|
|
@Resource
|
|
|
- private DeviceconnCacheService deviceconnCacheService;
|
|
|
- @Resource
|
|
|
private SendToIotsMsgService sendToIotsMsgService;
|
|
|
-
|
|
|
- @PostConstruct
|
|
|
- public void init(){
|
|
|
- List<IotDeviceconn> iotDeviceconnList = iotDeviceconnMapper.selectIotDeviceconnAllList();
|
|
|
- for(IotDeviceconn iotDeviceconn :iotDeviceconnList){
|
|
|
- deviceconnCacheService.setCache(iotDeviceconn);
|
|
|
- }
|
|
|
- }
|
|
|
+ @Resource
|
|
|
+ private IotDeviceMapper iotDeviceMapper;
|
|
|
|
|
|
@Override
|
|
|
public int addDeviceconn(IotDeviceconnAddReqVo reqVo, boolean isCreateConnect) {
|
|
|
@@ -89,7 +78,6 @@ public class IotDeviceconnServiceImpl implements IIotDeviceconnService
|
|
|
iotDeviceconn.setDevconnConfig(tosDevicetype.getDevTypeConfig());
|
|
|
iotDeviceconn.setDevconnCreator(SecurityUtils.getUserId());
|
|
|
iotDeviceconn.setDevconnCreateddate(DateUtils.dateTimeNow());
|
|
|
-
|
|
|
insertIotDeviceconnList.add(iotDeviceconn);
|
|
|
}else{
|
|
|
List<IotDevconnConfigInfoVo> devconnConfigList = reqVo.getDevconnConfigList();
|
|
|
@@ -120,14 +108,14 @@ public class IotDeviceconnServiceImpl implements IIotDeviceconnService
|
|
|
}
|
|
|
|
|
|
IotDeviceconn iotDeviceconn = new IotDeviceconn();
|
|
|
- iotDeviceconn.setDevconnBid(iotDeviceconn.getUUId());
|
|
|
+ String bid = iotDeviceconn.getDevconnBid();
|
|
|
+ iotDeviceconn.setDevconnBid(StringUtils.isEmpty(bid)?iotDeviceconn.getUUId():bid);
|
|
|
iotDeviceconn.setDevtypeBid(devtypeBid);
|
|
|
iotDeviceconn.setDevconnType(devconnType);
|
|
|
iotDeviceconn.setDevconnName(devconnName);
|
|
|
iotDeviceconn.setDevconnConfig(devconnConfig);
|
|
|
iotDeviceconn.setDevconnCreator(SecurityUtils.getUserId());
|
|
|
iotDeviceconn.setDevconnCreateddate(DateUtils.dateTimeNow());
|
|
|
-
|
|
|
insertIotDeviceconnList.add(iotDeviceconn);
|
|
|
diffName = devconnName;
|
|
|
}
|
|
|
@@ -147,6 +135,14 @@ public class IotDeviceconnServiceImpl implements IIotDeviceconnService
|
|
|
public int addDeviceconn(IotDeviceconnAddReqVo reqVo) {
|
|
|
return addDeviceconn(reqVo, true);
|
|
|
}
|
|
|
+ private int addDeviceconn(String devtypeBid,String connType,List<IotDevconnConfigInfoVo> list,String commonConnBid,boolean isCreateConnect){
|
|
|
+ IotDeviceconnAddReqVo iotDeviceconnAddReqVo = new IotDeviceconnAddReqVo();
|
|
|
+ iotDeviceconnAddReqVo.setDevtypeBid(devtypeBid);
|
|
|
+ iotDeviceconnAddReqVo.setDevconnType(connType);
|
|
|
+ iotDeviceconnAddReqVo.setDevconnConfigList(list);
|
|
|
+ iotDeviceconnAddReqVo.setCommonConnBid(commonConnBid);
|
|
|
+ return addDeviceconn(iotDeviceconnAddReqVo,isCreateConnect);
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
* 查询设备连接配置列表
|
|
|
@@ -196,33 +192,140 @@ public class IotDeviceconnServiceImpl implements IIotDeviceconnService
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 修改设备连接配置
|
|
|
+ * 修改设备连接配置,该接口不允许修改设备类型标识
|
|
|
*
|
|
|
* @param reqVo@return 结果
|
|
|
*/
|
|
|
@Override
|
|
|
public int editIotDeviceconn(IotDeviceconnEditReqVo reqVo) {
|
|
|
- String devconnType = reqVo.getDevconnType();
|
|
|
String oldDevtypeBid = reqVo.getOldDevtypeBid();
|
|
|
- String newDevtypeBid = reqVo.getNewDevtypeBid();
|
|
|
-
|
|
|
IotDeviceconn selectIotDeviceconn = new IotDeviceconn();
|
|
|
selectIotDeviceconn.setDevtypeBid(oldDevtypeBid);
|
|
|
+ // 查询出当前租户下,旧类型的配置信息
|
|
|
List<IotDeviceconn> iotDeviceconnList = selectIotDeviceconnList(selectIotDeviceconn);
|
|
|
if(iotDeviceconnList == null || iotDeviceconnList.isEmpty()){
|
|
|
throw new IotBizException(ErrorCode.INVALID_PARAMETER.getCode(),"连接配置不存在");
|
|
|
}
|
|
|
+ if(iotDeviceconnList.size() == 1){
|
|
|
+ // 若只有一条配置信息
|
|
|
+ IotDeviceconn iotDeviceconn = iotDeviceconnList.get(0);
|
|
|
+ if(IotDeviceconnTypeEnum.COMMON.getCode().equals(iotDeviceconn.getDevconnType())) {
|
|
|
+ return updateCommonConn(reqVo,iotDeviceconnList);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return updatePrivateConn(reqVo,iotDeviceconnList);
|
|
|
+ }
|
|
|
+ // 编辑通用连接
|
|
|
+ private int updateCommonConn(IotDeviceconnEditReqVo reqVo,List<IotDeviceconn> oldConnList){
|
|
|
+ int result = 0;
|
|
|
+ // 连接配置之前为通用连接
|
|
|
+ if(IotDeviceconnTypeEnum.COMMON.getCode().equals(reqVo.getDevconnType())){
|
|
|
+ // 如果编辑后仍为通用连接,则不做任何处理
|
|
|
+ result++;
|
|
|
+ return result;
|
|
|
+ }else {
|
|
|
+ // 编辑后变为私有连接
|
|
|
+ if(reqVo.getDevconnConfigList().size() == 1){
|
|
|
+ // 如果新的私有连接信息只有1条
|
|
|
+ IotDeviceconn iotDeviceconn = oldConnList.get(0);
|
|
|
+ IotDevice iotDeviceQuery = new IotDevice();
|
|
|
+ iotDeviceQuery.setDevconnBid(iotDeviceconn.getDevconnBid());
|
|
|
+ // 查询旧设备信息
|
|
|
+ List<IotDevice> iotDeviceList = iotDeviceService.selectIotDeviceList(iotDeviceQuery);
|
|
|
+ // 创建新连接
|
|
|
+ IotDevconnConfigInfoVo newConfig = reqVo.getDevconnConfigList().get(0);
|
|
|
+ newConfig.setDevconnBid(iotDeviceconn.getUUId());
|
|
|
+ // 先删除该租户的旧的通用配置信息
|
|
|
+ result = result + iotDeviceconnMapper.deleteIotDeviceconnByBids(new String[]{iotDeviceconn.getDevconnBid()});
|
|
|
+ // 再创建该租户的新的私有连接信息
|
|
|
+ result = result + addDeviceconn(reqVo.getNewDevtypeBid(),reqVo.getDevconnType(),reqVo.getDevconnConfigList(),null,true);
|
|
|
+ // TODO: 此处用于解绑设备的旧配置以及创建新配置,后续可以优化
|
|
|
+ for(IotDevice iotDevice : iotDeviceList){
|
|
|
+ IotDeviceEditMqModel iotDeviceEditMqModel = new IotDeviceEditMqModel();
|
|
|
+ iotDeviceEditMqModel.setOldIotDevice(iotDevice);
|
|
|
+ IotDevice newIotDevice = new IotDevice();
|
|
|
+ BeanUtils.copyProperties(iotDevice,newIotDevice);
|
|
|
+ newIotDevice.setDevconnBid(newConfig.getDevconnBid());
|
|
|
+ iotDeviceEditMqModel.setNewIotDevice(newIotDevice);
|
|
|
+ iotDeviceMapper.updateIotDevice(newIotDevice);
|
|
|
+ sendToIotsMsgService.sendIotDeviceUpdateMsg(iotDeviceEditMqModel);
|
|
|
+ }
|
|
|
+ // 删除旧连接
|
|
|
+ return result;
|
|
|
+ }else {
|
|
|
+ // 如果新的私有连接信息有多条
|
|
|
+ // 先删除该租户的旧的连接配置,但不重新建立连接
|
|
|
+ result = result+ deleteIotDeviceconnByBids(oldConnList,false);
|
|
|
+ // 创建私有连接
|
|
|
+ IotDeviceconnAddReqVo iotDeviceconnAddReqVo = new IotDeviceconnAddReqVo();
|
|
|
+ iotDeviceconnAddReqVo.setDevtypeBid(reqVo.getOldDevtypeBid());
|
|
|
+ iotDeviceconnAddReqVo.setDevconnType(reqVo.getDevconnType());
|
|
|
+ iotDeviceconnAddReqVo.setDevconnConfigList(reqVo.getDevconnConfigList());
|
|
|
+ result = result + addDeviceconn(iotDeviceconnAddReqVo);
|
|
|
+ // 新增私有连接
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- TosDevicetype tosDevicetype = tosDevicetypeService.selectTosDevicetypeByDevtypeBid(newDevtypeBid);
|
|
|
- if(tosDevicetype == null){
|
|
|
- throw new IotBizException(ErrorCode.INVALID_PARAMETER.getCode(),"设备型号不存在");
|
|
|
+ // 编辑私有连接
|
|
|
+ private int updatePrivateConn(IotDeviceconnEditReqVo reqVo,List<IotDeviceconn> iotDeviceconnList){
|
|
|
+ int result = 0;
|
|
|
+ // 连接配置之前为私有连接
|
|
|
+ if(IotDeviceconnTypeEnum.COMMON.getCode().equals(reqVo.getDevconnType())){
|
|
|
+ // 编辑后变为通用连接
|
|
|
+ IotDevice iotDeviceQuery = new IotDevice();
|
|
|
+ iotDeviceQuery.setDevtypeBid(reqVo.getOldDevtypeBid());
|
|
|
+ List<IotDevice> iotDeviceList = iotDeviceService.selectIotDeviceList(iotDeviceQuery);
|
|
|
+ // 先删除该租户的旧的
|
|
|
+ result = result + deleteIotDeviceconnByBids(iotDeviceconnList,true);
|
|
|
+ String newConnBid = iotDeviceQuery.getUUId();
|
|
|
+ // 创建该租户新的通用连接
|
|
|
+ result = result + addDeviceconn(reqVo.getNewDevtypeBid(),reqVo.getDevconnType(),reqVo.getDevconnConfigList(),newConnBid,true);
|
|
|
+ for(IotDevice iotDevice : iotDeviceList){
|
|
|
+ IotDeviceEditMqModel iotDeviceEditMqModel = new IotDeviceEditMqModel();
|
|
|
+ iotDeviceEditMqModel.setOldIotDevice(iotDevice);
|
|
|
+ IotDevice newIotDevice = new IotDevice();
|
|
|
+ BeanUtils.copyProperties(iotDevice,newIotDevice);
|
|
|
+ newIotDevice.setDevconnBid(newConnBid);
|
|
|
+ iotDeviceEditMqModel.setNewIotDevice(newIotDevice);
|
|
|
+ iotDeviceMapper.updateIotDevice(newIotDevice);
|
|
|
+ sendToIotsMsgService.sendIotDeviceUpdateMsg(iotDeviceEditMqModel);
|
|
|
+ }
|
|
|
+ }else {
|
|
|
+ // 编辑后仍为私有连接
|
|
|
+ // 编辑后变为通用连接
|
|
|
+ IotDevice iotDeviceQuery = new IotDevice();
|
|
|
+ iotDeviceQuery.setDevtypeBid(reqVo.getOldDevtypeBid());
|
|
|
+ List<IotDevice> iotDeviceList = iotDeviceService.selectIotDeviceList(iotDeviceQuery);
|
|
|
+ // 先删除该租户的旧的
|
|
|
+ result = result + deleteIotDeviceconnByBids(iotDeviceconnList,true);
|
|
|
+ // 创建该租户新的通用连接
|
|
|
+ result = result + addDeviceconn(reqVo.getNewDevtypeBid(),reqVo.getDevconnType(),reqVo.getDevconnConfigList(),null,true);
|
|
|
+ for(IotDevice iotDevice : iotDeviceList){
|
|
|
+ iotDevice.setDevconnBid(null);
|
|
|
+ iotDeviceMapper.updateIotDevice(iotDevice);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ private int updateIotDeviceconnByBids(List<IotDevconnConfigInfoVo> needUpdate, boolean isResetConnect) {
|
|
|
+ List<IotDeviceconn> iotDeviceconnList = new ArrayList<>();
|
|
|
+ for(IotDevconnConfigInfoVo devconnConfigInfoVo : needUpdate){
|
|
|
+ IotDeviceconn iotDeviceconn = new IotDeviceconn();
|
|
|
+ iotDeviceconn.setDevconnBid(devconnConfigInfoVo.getDevconnBid());
|
|
|
+ iotDeviceconn.setDevconnName(devconnConfigInfoVo.getDevconnName());
|
|
|
+ iotDeviceconn.setDevconnConfig(devconnConfigInfoVo.getDevconnConfig());
|
|
|
+ iotDeviceconnList.add(iotDeviceconn);
|
|
|
+ }
|
|
|
+ int result = iotDeviceconnMapper.updateBatchByBid(iotDeviceconnList);
|
|
|
+ if(isResetConnect){
|
|
|
+ for(IotDeviceconn iotDeviceconn : iotDeviceconnList){
|
|
|
+ sendToIotsMsgService.sendIotDeviceconnUpdateMsg(iotDeviceconn);
|
|
|
+ }
|
|
|
}
|
|
|
- deleteIotDeviceconnByBids(iotDeviceconnList, true);
|
|
|
- IotDeviceconnAddReqVo addReqVo = new IotDeviceconnAddReqVo();
|
|
|
- addReqVo.setDevtypeBid(newDevtypeBid);
|
|
|
- addReqVo.setDevconnType(devconnType);
|
|
|
- addReqVo.setDevconnConfigList(reqVo.getDevconnConfigList());
|
|
|
- return addDeviceconn(addReqVo, true);
|
|
|
+ return result;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -256,7 +359,7 @@ public class IotDeviceconnServiceImpl implements IIotDeviceconnService
|
|
|
/**
|
|
|
* 批量删除设备连接配置
|
|
|
*
|
|
|
- * @param devconnBids 需要删除的数据主键集合
|
|
|
+ * @param iotDeviceconnList 需要删除的数据
|
|
|
* @return 结果
|
|
|
*/
|
|
|
@Override
|
|
|
@@ -277,7 +380,7 @@ public class IotDeviceconnServiceImpl implements IIotDeviceconnService
|
|
|
/**
|
|
|
* 批量删除设备连接配置
|
|
|
*
|
|
|
- * @param devconnBids 需要删除的数据主键集合
|
|
|
+ * @param iotDeviceconnList 需要删除的数据
|
|
|
* @param
|
|
|
* @return 结果
|
|
|
*/
|