|
|
@@ -8,17 +8,26 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
import com.xunmei.common.core.constant.Constants;
|
|
|
+import com.xunmei.common.core.domain.mediator.domain.IotAlarmData;
|
|
|
import com.xunmei.common.core.domain.mediator.domain.IotAlarmSystemField;
|
|
|
+import com.xunmei.common.core.enums.iot.BaseDeviceTypeEnum;
|
|
|
import com.xunmei.common.core.enums.iot.DeviceTypeEnum;
|
|
|
import com.xunmei.common.core.enums.iot.SensorType;
|
|
|
+import com.xunmei.common.core.util.BeanHelper;
|
|
|
import com.xunmei.host.alarm.mapper.IotAlarmSystemFieldMapper;
|
|
|
import com.xunmei.host.alarm.service.IotAlarmDataService;
|
|
|
+import com.xunmei.host.iot.dto.AlarmDataReq;
|
|
|
import com.xunmei.host.iot.mapper.IotDeviceStatusMapper;
|
|
|
import com.xunmei.host.iot.service.IIotDeviceInfoService;
|
|
|
import com.xunmei.host.iot.service.IotDeviceStatusService;
|
|
|
+import com.xunmei.host.server.service.IotServerInfoService;
|
|
|
+import com.xunmei.host.websocket.constant.WebSocketConstants;
|
|
|
import com.xunmei.host.websocket.dto.WebsocketExecuteReq;
|
|
|
import com.xunmei.host.websocket.dto.WebsocketResult;
|
|
|
import com.xunmei.host.websocket.enums.DeviceNetStatusEnum;
|
|
|
+import com.xunmei.host.websocket.enums.ProductEnums;
|
|
|
+import com.xunmei.host.websocket.service.RouterService;
|
|
|
+import com.xunmei.system.api.domain.SysOrg;
|
|
|
import com.xunmei.system.api.domain.iot.IotDeviceInfo;
|
|
|
import com.xunmei.system.api.domain.iot.IotDeviceStatus;
|
|
|
import com.xunmei.system.api.enums.ElectricityMeterAttributes;
|
|
|
@@ -28,13 +37,11 @@ import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.BeanUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
-import org.springframework.transaction.annotation.Transactional;
|
|
|
-import org.springframework.transaction.support.TransactionSynchronization;
|
|
|
-import org.springframework.transaction.support.TransactionSynchronizationManager;
|
|
|
|
|
|
import java.util.Date;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
+import java.util.StringJoiner;
|
|
|
|
|
|
|
|
|
/**
|
|
|
@@ -45,16 +52,17 @@ import java.util.Map;
|
|
|
*/
|
|
|
@Slf4j
|
|
|
@Service
|
|
|
-public class IotDeviceStatusServiceImpl extends ServiceImpl<IotDeviceStatusMapper, IotDeviceStatus> implements IotDeviceStatusService {
|
|
|
+public class IotDeviceStatusServiceImpl extends ServiceImpl<IotDeviceStatusMapper, IotDeviceStatus> implements IotDeviceStatusService, RouterService {
|
|
|
@Autowired
|
|
|
private IotDeviceStatusMapper iotDeviceStatusMapper;
|
|
|
@Autowired
|
|
|
private IIotDeviceInfoService iotDeviceInfoService;
|
|
|
@Autowired
|
|
|
- private IotAlarmDataService alarmDataService;
|
|
|
+ private IotServerInfoService serverInfoService;
|
|
|
@Autowired
|
|
|
private IotAlarmSystemFieldMapper alarmSystemFieldMapper;
|
|
|
-
|
|
|
+ @Autowired
|
|
|
+ private IotAlarmDataService iotAlarmDataService;
|
|
|
|
|
|
@Override
|
|
|
public IotDeviceStatus getByDeviceId(Long deviceId) {
|
|
|
@@ -64,94 +72,6 @@ public class IotDeviceStatusServiceImpl extends ServiceImpl<IotDeviceStatusMappe
|
|
|
return iotDeviceStatusMapper.selectOne(wrapper);
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * {
|
|
|
- * "topic": "/things/FSU_TemperatureAndHumidity/XXDevice/property/post",
|
|
|
- * "id":"xxx", // Guid,消息IoT生成。
|
|
|
- * "timestamp": "2023-01-11T18:00:00 +08:00",
|
|
|
- * "headers":{
|
|
|
- * "topicType":"",// Topic类型,相对于topicCategory的细分
|
|
|
- * "topicCategory": "",// Topic分类
|
|
|
- * "productName": "FSU_TemperatureAndHumidity",
|
|
|
- * "deviceName": "XXDevice"
|
|
|
- * },
|
|
|
- * "payload":[
|
|
|
- * {
|
|
|
- * "propertyName": "temperature",// 应用方调用的属性名称
|
|
|
- * "args": 23.6,
|
|
|
- * "dataSpec": {
|
|
|
- * "defaultValue": 0,
|
|
|
- * "max": 5000,
|
|
|
- * "min": -300,
|
|
|
- * "step": 0.01,
|
|
|
- * "unit": "℃",
|
|
|
- * "unitName": "摄氏度",
|
|
|
- * "dataType": "Int"
|
|
|
- * }
|
|
|
- * },
|
|
|
- * {
|
|
|
- * "propertyName": "humidity",// 应用方调用的属性名称
|
|
|
- * "args": 23.6,
|
|
|
- * "dataSpec": {
|
|
|
- * "defaultValue": 0,
|
|
|
- * "max": 5000,
|
|
|
- * "min": -300,
|
|
|
- * "step": 0.01,
|
|
|
- * "unit": "%RH",
|
|
|
- * "unitName": "",
|
|
|
- * "dataType": "Int"
|
|
|
- * }
|
|
|
- * }
|
|
|
- * ]
|
|
|
- * }
|
|
|
- *
|
|
|
- * @param req
|
|
|
- * @return
|
|
|
- */
|
|
|
-
|
|
|
- @Override
|
|
|
- @Transactional
|
|
|
- public WebsocketResult dealDeviceStatusChange(WebsocketExecuteReq req) {
|
|
|
- final JSONArray data = (JSONArray) req.getData();
|
|
|
- final String productName = req.getProductName();
|
|
|
- final DeviceTypeEnum deviceTypeEnum = DeviceTypeEnum.valueOf(productName);
|
|
|
-
|
|
|
- final IotDeviceInfo code = iotDeviceInfoService.selectByTypeAndCode(req.getToken(), deviceTypeEnum.getCode(), req.getProductName(), req.getDeviceName());
|
|
|
- if (code == null) {
|
|
|
- log.error("动环设备状态同步时,未能找到对应设备,token:{},productName:{},deviceNane:{}", req.getToken(), req.getProductName(), req.getDeviceName());
|
|
|
- return new WebsocketResult();
|
|
|
- }
|
|
|
- String deviceType = SensorType.getCodeByProduct(productName);
|
|
|
- List<IotAlarmSystemField> fieldList = alarmSystemFieldMapper.selectList(new LambdaQueryWrapper<>());
|
|
|
- final JSONArray array = dealStatusData(data, fieldList, deviceType);
|
|
|
- IotDeviceStatus status = getByDeviceId(code.getId());
|
|
|
- if (status == null) {
|
|
|
- status = new IotDeviceStatus();
|
|
|
- BeanUtils.copyProperties(code, status, "id");
|
|
|
- status.setDeviceId(code.getId());
|
|
|
- status.setId(IdWorker.getId());
|
|
|
- status.setUniqueCode();
|
|
|
- status.setDeviceType(deviceType);
|
|
|
- status.setInfo(JSON.toJSONString(array));
|
|
|
- save(status);
|
|
|
- } else {
|
|
|
- status.setInfo(JSON.toJSONString(array));
|
|
|
- updateById(status);
|
|
|
- }
|
|
|
- IotDeviceStatus finalStatus = status;
|
|
|
- TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
|
|
|
- @Override
|
|
|
- public void afterCommit() {
|
|
|
- try {
|
|
|
- alarmDataService.dealDeviceStatusData(finalStatus);
|
|
|
- } catch (Exception e) {
|
|
|
- throw new RuntimeException(e);
|
|
|
- }
|
|
|
- }
|
|
|
- });
|
|
|
-
|
|
|
- return new WebsocketResult();
|
|
|
- }
|
|
|
|
|
|
private static JSONArray dealStatusData(JSONArray data, List<IotAlarmSystemField> fieldList, String deviceType) {
|
|
|
final JSONArray array = new JSONArray();
|
|
|
@@ -167,7 +87,7 @@ public class IotDeviceStatusServiceImpl extends ServiceImpl<IotDeviceStatusMappe
|
|
|
.findFirst()
|
|
|
.orElse(StringUtil.EMPTY_STRING);
|
|
|
String val = DeviceTypeEnum.getStatusText(jsb.get("args"));
|
|
|
- if (ObjectUtil.hasEmpty(name,val)) {
|
|
|
+ if (ObjectUtil.hasEmpty(name, val)) {
|
|
|
continue;
|
|
|
}
|
|
|
object.put("name", name);
|
|
|
@@ -220,4 +140,104 @@ public class IotDeviceStatusServiceImpl extends ServiceImpl<IotDeviceStatusMappe
|
|
|
|
|
|
|
|
|
}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public ProductEnums product() {
|
|
|
+ return ProductEnums.DETECTION_HOST;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String routerKey() {
|
|
|
+ StringJoiner sj = new StringJoiner(",");
|
|
|
+ //设备状态变更
|
|
|
+ sj.add(WebSocketConstants.SUB_DEVICES_STATUS);
|
|
|
+ //设备发生报警
|
|
|
+ sj.add(WebSocketConstants.SUB_DEVICE_ALARM);
|
|
|
+
|
|
|
+ return sj.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Object execute(WebsocketExecuteReq req) {
|
|
|
+ final String event = req.getEvent();
|
|
|
+ switch (event) {
|
|
|
+ case WebSocketConstants.SUB_DEVICES_STATUS:
|
|
|
+ return dealDeviceStatus(req);
|
|
|
+ case WebSocketConstants.SUB_DEVICE_ALARM:
|
|
|
+ return dealDeviceAlarm(req);
|
|
|
+ default:
|
|
|
+ throw new RuntimeException("未知的消息类型");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private Object dealDeviceAlarm(WebsocketExecuteReq req) {
|
|
|
+ final SysOrg sysOrg = serverInfoService.selectOrgByToken(req.getToken());
|
|
|
+ if (ObjectUtil.isNull(sysOrg)) {
|
|
|
+ LogUtils.WEBSOCKET_MSG.error("收到设备报警消息,根据token:{}未查询到机构信息", req.getToken());
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ List<IotAlarmSystemField> fieldList = alarmSystemFieldMapper.selectList(new LambdaQueryWrapper<>());
|
|
|
+
|
|
|
+ final JSONArray data = (JSONArray) req.getData();
|
|
|
+ final List<AlarmDataReq> dataReqList = data.toJavaList(AlarmDataReq.class);
|
|
|
+ for (AlarmDataReq dataReq : dataReqList) {
|
|
|
+ IotDeviceInfo deviceInfo = iotDeviceInfoService.selectByTokenProductAndDeviceCode(req.getToken(), req.getProductName(), req.getDeviceName());
|
|
|
+ if (ObjectUtil.isNull(deviceInfo)) {
|
|
|
+ LogUtils.WEBSOCKET_MSG.error("收到设备报警消息,根据token:{},productName:{},deviceName:{}未查询到设备信息", req.getToken(), req.getProductName(), req.getDeviceName());
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ final IotAlarmData iotAlarmData = BeanHelper.copyProperties(dataReq, IotAlarmData.class);
|
|
|
+ iotAlarmData.setOrgId(sysOrg.getId());
|
|
|
+ iotAlarmData.setDeviceId(String.valueOf(deviceInfo.getId()));
|
|
|
+ final String sourceType = SensorType.getCodeByProduct(dataReq.getProductType());
|
|
|
+ fieldList.stream()
|
|
|
+ .filter(r->ObjectUtil.equal(r.getSourceType(),sourceType))
|
|
|
+ .filter(r->ObjectUtil.equal(r.getPropertyName(),dataReq.getProductProperty()))
|
|
|
+ .findAny()
|
|
|
+ .ifPresent(r->{
|
|
|
+ iotAlarmData.setSourceType(r.getSourceType());
|
|
|
+ iotAlarmData.setSourceTypeDes(r.getSourceTypeDes());
|
|
|
+ iotAlarmData.setFieldCode(r.getSysFieldCode());
|
|
|
+ });
|
|
|
+ iotAlarmDataService.save(iotAlarmData);
|
|
|
+ if (Boolean.TRUE.equals(dataReq.getSmsType())) {
|
|
|
+ //todo 发送短信
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ private WebsocketResult dealDeviceStatus(WebsocketExecuteReq req) {
|
|
|
+ final JSONArray data = (JSONArray) req.getData();
|
|
|
+ final String productName = req.getProductName();
|
|
|
+ final BaseDeviceTypeEnum deviceTypeEnum = BaseDeviceTypeEnum.valueOf(productName);
|
|
|
+
|
|
|
+ final IotDeviceInfo code = iotDeviceInfoService.selectByTypeAndCode(req.getToken(), deviceTypeEnum.getCode(), req.getProductName(), req.getDeviceName());
|
|
|
+ if (code == null) {
|
|
|
+ log.error("动环设备状态同步时,未能找到对应设备,token:{},productName:{},deviceNane:{}", req.getToken(), req.getProductName(), req.getDeviceName());
|
|
|
+ return new WebsocketResult();
|
|
|
+ }
|
|
|
+ String deviceType = SensorType.getCodeByProduct(productName);
|
|
|
+ List<IotAlarmSystemField> fieldList = alarmSystemFieldMapper.selectList(new LambdaQueryWrapper<>());
|
|
|
+ final JSONArray array = dealStatusData(data, fieldList, deviceType);
|
|
|
+ IotDeviceStatus status = getByDeviceId(code.getId());
|
|
|
+ if (status == null) {
|
|
|
+ status = new IotDeviceStatus();
|
|
|
+ BeanUtils.copyProperties(code, status, "id");
|
|
|
+ status.setDeviceId(code.getId());
|
|
|
+ status.setId(IdWorker.getId());
|
|
|
+ status.setUniqueCode();
|
|
|
+ status.setDeviceType(deviceType);
|
|
|
+ status.setInfo(JSON.toJSONString(array));
|
|
|
+ save(status);
|
|
|
+ } else {
|
|
|
+ status.setInfo(JSON.toJSONString(array));
|
|
|
+ updateById(status);
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
}
|