|
|
@@ -9,6 +9,7 @@ import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
|
|
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.constant.ErrorMsgConstants;
|
|
|
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;
|
|
|
@@ -36,8 +37,11 @@ import com.xunmei.system.api.domain.iot.IotDeviceStatus;
|
|
|
import com.xunmei.system.api.util.LogUtils;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.BeanUtils;
|
|
|
+import org.springframework.beans.BeansException;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Propagation;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
import java.time.LocalDateTime;
|
|
|
@@ -167,6 +171,7 @@ public class IotDeviceStatusServiceImpl extends ServiceImpl<IotDeviceStatusMappe
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
+ @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRES_NEW)
|
|
|
public WebsocketResult execute(WebsocketExecuteReq req) {
|
|
|
final String event = req.getEvent();
|
|
|
switch (event) {
|
|
|
@@ -175,76 +180,80 @@ public class IotDeviceStatusServiceImpl extends ServiceImpl<IotDeviceStatusMappe
|
|
|
case WebSocketConstants.SUB_DEVICE_ALARM:
|
|
|
return dealDeviceAlarm(req);
|
|
|
default:
|
|
|
- throw new RuntimeException("未知的消息类型");
|
|
|
+ return WebsocketResult.replyError(req, ErrorMsgConstants.ERROR_ROUTE);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private WebsocketResult dealDeviceAlarm(WebsocketExecuteReq req) {
|
|
|
- final String iotCode = req.getServerInfo().getIotCode();
|
|
|
- final SysOrg sysOrg = serverInfoService.selectOrgByToken(iotCode);
|
|
|
- if (ObjectUtil.isNull(sysOrg)) {
|
|
|
- LogUtils.SOCKET_DEVICE_ALARM_DATA.error("收到设备报警消息,根据token:{}未查询到机构信息", iotCode);
|
|
|
- return null;
|
|
|
- }
|
|
|
- List<IotAlarmSystemField> fieldList = alarmSystemFieldMapper.selectList(new LambdaQueryWrapper<>());
|
|
|
-
|
|
|
- final JSONArray data = (JSONArray) req.getData();
|
|
|
- final List<AlarmDataReq> dataReqList = data.toJavaList(AlarmDataReq.class);
|
|
|
- if (dataReqList == null || dataReqList.isEmpty()) {
|
|
|
- return null;
|
|
|
- }
|
|
|
- final Date date = new Date();
|
|
|
- for (AlarmDataReq dataReq : dataReqList) {
|
|
|
- IotDeviceInfo deviceInfo = iotDeviceInfoService.selectByTokenProductAndDeviceCode(iotCode, req.getProductName(), req.getDeviceName());
|
|
|
- if (ObjectUtil.isNull(deviceInfo)) {
|
|
|
- LogUtils.SOCKET_DEVICE_ALARM_DATA.error("收到设备报警消息,根据token:{},productName:{},deviceName:{}未查询到设备信息", iotCode, req.getProductName(), req.getDeviceName());
|
|
|
- continue;
|
|
|
- }
|
|
|
- otherLogic(dataReq, deviceInfo.getId());
|
|
|
- IotAlarmData iotAlarmData = iotAlarmDataService.getById(dataReq.getId());
|
|
|
- boolean insert = false;
|
|
|
- if (iotAlarmData == null) {
|
|
|
- insert = true;
|
|
|
- iotAlarmData = BeanHelper.copyProperties(dataReq, IotAlarmData.class);
|
|
|
- iotAlarmData.setCreateTime(date);
|
|
|
- }else {
|
|
|
- BeanUtils.copyProperties(dataReq, iotAlarmData);
|
|
|
- }
|
|
|
- iotAlarmData.setUpdateTime(date);
|
|
|
- iotAlarmData.setOrgId(sysOrg.getId());
|
|
|
- iotAlarmData.setDeviceId(String.valueOf(deviceInfo.getId()));
|
|
|
- iotAlarmData.setTime(dataReq.getStartTime());
|
|
|
- final Optional<IotAlarmSystemField> optional = fieldList.stream()
|
|
|
- .filter(r -> ObjectUtil.equal(String.valueOf(r.getProductType()), dataReq.getProductType()))
|
|
|
- .filter(r -> ObjectUtil.equal(r.getProductProperty(), dataReq.getProductProperty()))
|
|
|
- .findAny();
|
|
|
- if (optional.isPresent()) {
|
|
|
- final IotAlarmSystemField field = optional.get();
|
|
|
- iotAlarmData.setSourceType(field.getProductType());
|
|
|
- iotAlarmData.setSourceTypeDes(field.getProductName() + "告警");
|
|
|
- iotAlarmData.setFieldCode(field.getProductProperty());
|
|
|
- }
|
|
|
- if (insert) {
|
|
|
- iotAlarmDataService.save(iotAlarmData);
|
|
|
- } else {
|
|
|
- iotAlarmDataService.updateById(iotAlarmData);
|
|
|
+ try {
|
|
|
+ final String iotCode = req.getServerInfo().getIotCode();
|
|
|
+ final SysOrg sysOrg = serverInfoService.selectOrgByToken(iotCode);
|
|
|
+ if (ObjectUtil.isNull(sysOrg)) {
|
|
|
+ String str=String.format("收到设备报警消息,根据token:%s未查询到机构信息",iotCode);
|
|
|
+ LogUtils.SOCKET_DEVICE_ALARM_DATA.error(str);
|
|
|
+ return WebsocketResult.replyError(req,str);
|
|
|
}
|
|
|
- // 发生报警
|
|
|
- if (ObjectUtil.isNotNull(dataReq.getStartTime()) && ObjectUtil.isNull(dataReq.getEndTime())) {
|
|
|
- updateStatusByDevicePrimaryId(deviceInfo.getId(), DeviceStatusEnum.ALARM.getValue());
|
|
|
- //判断是否通知app
|
|
|
- websocketNoticeLogService.noticeAlarm(deviceInfo, iotAlarmData, false);
|
|
|
+ List<IotAlarmSystemField> fieldList = alarmSystemFieldMapper.selectList(new LambdaQueryWrapper<>());
|
|
|
+
|
|
|
+ final JSONArray data = (JSONArray) req.getData();
|
|
|
+ final List<AlarmDataReq> dataReqList = data.toJavaList(AlarmDataReq.class);
|
|
|
+ if (dataReqList == null || dataReqList.isEmpty()) {
|
|
|
+ return null;
|
|
|
}
|
|
|
- // 报警结束
|
|
|
- if (ObjectUtil.isNotNull(dataReq.getStartTime()) && ObjectUtil.isNotNull(dataReq.getEndTime())) {
|
|
|
- updateStatusByDevicePrimaryId(deviceInfo.getId(), DeviceStatusEnum.NORMAL.getValue());
|
|
|
- //告警结束需要结束通知
|
|
|
- websocketNoticeLogService.endNoticeAlarm(deviceInfo, iotAlarmData);
|
|
|
+ final Date date = new Date();
|
|
|
+ for (AlarmDataReq dataReq : dataReqList) {
|
|
|
+ IotDeviceInfo deviceInfo = iotDeviceInfoService.selectByTokenProductAndDeviceCode(iotCode, req.getProductName(), req.getDeviceName());
|
|
|
+ if (ObjectUtil.isNull(deviceInfo)) {
|
|
|
+ String str=String.format("收到设备报警消息,根据token:%s,productName:%s,deviceName:%s未查询到设备信息",iotCode,req.getProductName(),req.getDeviceName());
|
|
|
+ throw new RuntimeException(str);
|
|
|
+ }
|
|
|
+ otherLogic(dataReq, deviceInfo.getId());
|
|
|
+ IotAlarmData iotAlarmData = iotAlarmDataService.getById(dataReq.getId());
|
|
|
+ boolean insert = false;
|
|
|
+ if (iotAlarmData == null) {
|
|
|
+ insert = true;
|
|
|
+ iotAlarmData = BeanHelper.copyProperties(dataReq, IotAlarmData.class);
|
|
|
+ iotAlarmData.setCreateTime(date);
|
|
|
+ }else {
|
|
|
+ BeanUtils.copyProperties(dataReq, iotAlarmData);
|
|
|
+ }
|
|
|
+ iotAlarmData.setUpdateTime(date);
|
|
|
+ iotAlarmData.setOrgId(sysOrg.getId());
|
|
|
+ iotAlarmData.setDeviceId(String.valueOf(deviceInfo.getId()));
|
|
|
+ iotAlarmData.setTime(dataReq.getStartTime());
|
|
|
+ final Optional<IotAlarmSystemField> optional = fieldList.stream()
|
|
|
+ .filter(r -> ObjectUtil.equal(String.valueOf(r.getProductType()), dataReq.getProductType()))
|
|
|
+ .filter(r -> ObjectUtil.equal(r.getProductProperty(), dataReq.getProductProperty()))
|
|
|
+ .findAny();
|
|
|
+ if (optional.isPresent()) {
|
|
|
+ final IotAlarmSystemField field = optional.get();
|
|
|
+ iotAlarmData.setSourceType(field.getProductType());
|
|
|
+ iotAlarmData.setSourceTypeDes(field.getProductName() + "告警");
|
|
|
+ iotAlarmData.setFieldCode(field.getProductProperty());
|
|
|
+ }
|
|
|
+ if (insert) {
|
|
|
+ iotAlarmDataService.save(iotAlarmData);
|
|
|
+ } else {
|
|
|
+ iotAlarmDataService.updateById(iotAlarmData);
|
|
|
+ }
|
|
|
+ // 发生报警
|
|
|
+ if (ObjectUtil.isNotNull(dataReq.getStartTime()) && ObjectUtil.isNull(dataReq.getEndTime())) {
|
|
|
+ updateStatusByDevicePrimaryId(deviceInfo.getId(), DeviceStatusEnum.ALARM.getValue());
|
|
|
+ //判断是否通知app
|
|
|
+ websocketNoticeLogService.noticeAlarm(deviceInfo, iotAlarmData, false);
|
|
|
+ }
|
|
|
+ // 报警结束
|
|
|
+ if (ObjectUtil.isNotNull(dataReq.getStartTime()) && ObjectUtil.isNotNull(dataReq.getEndTime())) {
|
|
|
+ updateStatusByDevicePrimaryId(deviceInfo.getId(), DeviceStatusEnum.NORMAL.getValue());
|
|
|
+ //告警结束需要结束通知
|
|
|
+ websocketNoticeLogService.endNoticeAlarm(deviceInfo, iotAlarmData);
|
|
|
+ }
|
|
|
}
|
|
|
+ } catch (BeansException e) {
|
|
|
+ LogUtils.SOCKET_DEVICE_ALARM_DATA.error("收到设备报警消息,处理发生异常", e);
|
|
|
+ throw new RuntimeException(e);
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
- return null;
|
|
|
+ return WebsocketResult.replySuccess(req);
|
|
|
}
|
|
|
|
|
|
private void otherLogic(AlarmDataReq dataReq, Long deviceId) {
|
|
|
@@ -284,46 +293,52 @@ public class IotDeviceStatusServiceImpl extends ServiceImpl<IotDeviceStatusMappe
|
|
|
}
|
|
|
|
|
|
private WebsocketResult dealDeviceStatus(WebsocketExecuteReq req) {
|
|
|
- final String iotCode = req.getServerInfo().getIotCode();
|
|
|
- final JSONObject reqDataObj = (JSONObject) req.getData();
|
|
|
- final Object reqStatus = reqDataObj.get(WebSocketConstants.STATUS);
|
|
|
- if (ObjectUtil.isNull(reqStatus)) {
|
|
|
- LogUtils.SOCKET_DEVICE_STATUS_INFO.error("设备状态同步时,参数异常,status为空,token:{}", iotCode);
|
|
|
- return new WebsocketResult();
|
|
|
- }
|
|
|
- final JSONArray data = (JSONArray) reqStatus;
|
|
|
- final String productName = req.getProductName();
|
|
|
- final BaseDeviceTypeEnum deviceTypeEnum = BaseDeviceTypeEnum.valueOf(productName);
|
|
|
+ try {
|
|
|
+ final String iotCode = req.getServerInfo().getIotCode();
|
|
|
+ final JSONObject reqDataObj = (JSONObject) req.getData();
|
|
|
+ final Object reqStatus = reqDataObj.get(WebSocketConstants.STATUS);
|
|
|
+ if (ObjectUtil.isNull(reqStatus)) {
|
|
|
+ String str=String.format("设备状态同步时,参数异常,status为空,token:%s",iotCode);
|
|
|
+ LogUtils.SOCKET_DEVICE_STATUS_INFO.error(str);
|
|
|
+ return WebsocketResult.replyError(req,str);
|
|
|
+ }
|
|
|
+ final JSONArray data = (JSONArray) reqStatus;
|
|
|
+ final String productName = req.getProductName();
|
|
|
+ final BaseDeviceTypeEnum deviceTypeEnum = BaseDeviceTypeEnum.valueOf(productName);
|
|
|
|
|
|
- final IotDeviceInfo code = iotDeviceInfoService.selectByTypeAndCode(iotCode, deviceTypeEnum.getCode(), req.getProductName(), req.getDeviceName());
|
|
|
- if (code == null) {
|
|
|
- LogUtils.SOCKET_DEVICE_STATUS_INFO.error("设备状态同步时,未能找到对应设备,token:{},productName:{},deviceNane:{}", iotCode, 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, deviceTypeEnum.name());
|
|
|
- IotDeviceStatus status = getByDeviceId(code.getId());
|
|
|
- final LocalDateTime now = LocalDateTime.now();
|
|
|
- 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));
|
|
|
- status.setStateUpdateTime(now);
|
|
|
- status.setStateStartTime(now);
|
|
|
- status.setState(0);
|
|
|
- save(status);
|
|
|
- } else {
|
|
|
- status.setInfo(JSON.toJSONString(array));
|
|
|
- status.setStateStartTime(now);
|
|
|
- status.setStateUpdateTime(now);
|
|
|
- updateById(status);
|
|
|
+ final IotDeviceInfo code = iotDeviceInfoService.selectByTypeAndCode(iotCode, deviceTypeEnum.getCode(), req.getProductName(), req.getDeviceName());
|
|
|
+ if (code == null) {
|
|
|
+ String str=String.format("设备状态同步时,未能找到对应设备,token:%s,productName:%s,deviceNane:%s", iotCode, req.getProductName(), req.getDeviceName());
|
|
|
+ LogUtils.SOCKET_DEVICE_STATUS_INFO.error(str);
|
|
|
+ return WebsocketResult.replyError(req,str);
|
|
|
+ }
|
|
|
+ String deviceType = SensorType.getCodeByProduct(productName);
|
|
|
+ List<IotAlarmSystemField> fieldList = alarmSystemFieldMapper.selectList(new LambdaQueryWrapper<>());
|
|
|
+ final JSONArray array = dealStatusData(data, fieldList, deviceTypeEnum.name());
|
|
|
+ IotDeviceStatus status = getByDeviceId(code.getId());
|
|
|
+ final LocalDateTime now = LocalDateTime.now();
|
|
|
+ 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));
|
|
|
+ status.setStateUpdateTime(now);
|
|
|
+ status.setStateStartTime(now);
|
|
|
+ status.setState(0);
|
|
|
+ save(status);
|
|
|
+ } else {
|
|
|
+ status.setInfo(JSON.toJSONString(array));
|
|
|
+ status.setStateStartTime(now);
|
|
|
+ status.setStateUpdateTime(now);
|
|
|
+ updateById(status);
|
|
|
+ }
|
|
|
+ deviceStatusLogService.saveLog(status);
|
|
|
+ return WebsocketResult.replySuccess(req);
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException("设备状态同步,处理失败",e);
|
|
|
}
|
|
|
- deviceStatusLogService.saveLog(status);
|
|
|
- return null;
|
|
|
}
|
|
|
}
|