|
|
@@ -5,6 +5,7 @@ import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONArray;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+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;
|
|
|
@@ -189,41 +190,54 @@ public class IotDeviceStatusServiceImpl extends ServiceImpl<IotDeviceStatusMappe
|
|
|
|
|
|
final JSONArray data = (JSONArray) req.getData();
|
|
|
final List<AlarmDataReq> dataReqList = data.toJavaList(AlarmDataReq.class);
|
|
|
- if (dataReqList == null || dataReqList.isEmpty()){
|
|
|
+ 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;
|
|
|
}
|
|
|
- final IotAlarmData iotAlarmData = BeanHelper.copyProperties(dataReq, IotAlarmData.class);
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+ iotAlarmData.setUpdateTime(date);
|
|
|
iotAlarmData.setOrgId(sysOrg.getId());
|
|
|
iotAlarmData.setDeviceId(String.valueOf(deviceInfo.getId()));
|
|
|
iotAlarmData.setTime(dataReq.getStartTime());
|
|
|
- //final String sourceType = SensorType.getCodeByProduct(dataReq.getProductType());
|
|
|
- fieldList.stream()
|
|
|
+ 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()
|
|
|
- .ifPresent(r -> {
|
|
|
- iotAlarmData.setSourceType(r.getProductType());
|
|
|
- iotAlarmData.setSourceTypeDes(r.getProductName() + "告警");
|
|
|
- iotAlarmData.setFieldCode(r.getProductProperty());
|
|
|
- });
|
|
|
- iotAlarmDataService.saveOrUpdate(iotAlarmData);
|
|
|
+ .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);
|
|
|
+ websocketNoticeLogService.noticeAlarm(deviceInfo, iotAlarmData, false);
|
|
|
}
|
|
|
// 报警结束
|
|
|
if (ObjectUtil.isNotNull(dataReq.getStartTime()) && ObjectUtil.isNotNull(dataReq.getEndTime())) {
|
|
|
updateStatusByDevicePrimaryId(deviceInfo.getId(), DeviceStatusEnum.NORMAL.getValue());
|
|
|
//告警结束需要结束通知
|
|
|
- websocketNoticeLogService.endNoticeAlarm(deviceInfo,iotAlarmData);
|
|
|
+ websocketNoticeLogService.endNoticeAlarm(deviceInfo, iotAlarmData);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -231,6 +245,35 @@ public class IotDeviceStatusServiceImpl extends ServiceImpl<IotDeviceStatusMappe
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
+ private void otherLogic(AlarmDataReq dataReq, Long deviceId) {
|
|
|
+ //如果历史没有产生告警 那么这次不管上报什么都不做操作
|
|
|
+ LambdaQueryWrapper<IotAlarmData> queryWrapper = new LambdaQueryWrapper<>();
|
|
|
+ queryWrapper.eq(IotAlarmData::getDeviceId, deviceId);
|
|
|
+ queryWrapper.eq(IotAlarmData::getFieldCode, dataReq.getProductProperty());
|
|
|
+ queryWrapper.orderByDesc(IotAlarmData::getTime);
|
|
|
+ queryWrapper.last(Constants.LIMIT1);
|
|
|
+ final IotAlarmData alarmData = iotAlarmDataService.getOne(queryWrapper);
|
|
|
+ if (alarmData == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ //处理平台设备正在告警中,又上报了同设备的告警开始数据的情况(丢失了中间的告警结束数据)
|
|
|
+ if (ObjectUtil.isNull(dataReq.getEndTime())) {
|
|
|
+ LambdaUpdateWrapper<IotAlarmData> wrapper = new LambdaUpdateWrapper<>();
|
|
|
+ wrapper.eq(IotAlarmData::getDeviceId, deviceId);
|
|
|
+ wrapper.eq(IotAlarmData::getFieldCode, dataReq.getProductProperty());
|
|
|
+ wrapper.isNull(IotAlarmData::getTime);
|
|
|
+ wrapper.ne(IotAlarmData::getId, dataReq.getId());
|
|
|
+ wrapper.set(IotAlarmData::getEndTime, LocalDateTime.now());
|
|
|
+ iotAlarmDataService.update(wrapper);
|
|
|
+ }
|
|
|
+ //当前设备正在告警,却收到设备恢复的数据,说明丢失了设备的告警恢复与告警开始数据,那么将当前设备历史告警的结束时间设置为当前时间
|
|
|
+ if (alarmData.getEndTime() == null && !alarmData.getId().equals(dataReq.getId())){
|
|
|
+ alarmData.setEndTime(LocalDateTime.now());
|
|
|
+ LogUtils.SOCKET_DEVICE_ALARM_DATA.warn("收到设备告警恢复数据,但存在历史告警未能正常结束告警,修改历史告警的恢复时间为当前时间,设备id:{},告警数据id:{},告警数据:{}", deviceId, dataReq.getId(), JSON.toJSONString(alarmData));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
private void updateStatusByDevicePrimaryId(Long deviceId, int status) {
|
|
|
|