|
|
@@ -28,9 +28,11 @@ import com.xunmei.mediator.api.enums.AlarmVideoTypeEnum;
|
|
|
import com.xunmei.mediator.api.enums.SmsNotifyType;
|
|
|
import com.xunmei.mediator.api.sensor.mapper.IotSensorMapper;
|
|
|
import com.xunmei.mediator.api.alarm.service.IotAlarmDataService;
|
|
|
+import com.xunmei.mediator.iot.mapper.IotDeviceStatusMapper;
|
|
|
import com.xunmei.system.api.RemoteConfigService;
|
|
|
import com.xunmei.system.api.RemoteSmsService;
|
|
|
import com.xunmei.system.api.domain.SysConfig;
|
|
|
+import com.xunmei.system.api.domain.iot.IotDeviceStatus;
|
|
|
import com.xunmei.system.api.util.LogUtils;
|
|
|
import lombok.AllArgsConstructor;
|
|
|
import lombok.Data;
|
|
|
@@ -71,6 +73,8 @@ public class IotAlarmDataServiceImpl extends ServiceImpl<IotAlarmDataMapper, Iot
|
|
|
|
|
|
@Autowired
|
|
|
private IotSensorMapper iotSensorMapper;
|
|
|
+ @Autowired
|
|
|
+ private IotDeviceStatusMapper deviceStatusMapper;;
|
|
|
|
|
|
@Autowired
|
|
|
private RemoteConfigService remoteConfigService;
|
|
|
@@ -78,17 +82,141 @@ public class IotAlarmDataServiceImpl extends ServiceImpl<IotAlarmDataMapper, Iot
|
|
|
@Autowired
|
|
|
private RemoteSmsService remoteSmsService;
|
|
|
|
|
|
+ @Override
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
+ public void dealDeviceStatusData(IotDeviceStatus deviceStatus) throws Exception {
|
|
|
+ Date now = new Date();
|
|
|
+ //判断是否有绑定规则
|
|
|
+ QueryWrapper<IotAlarmRuleSource> qw = new QueryWrapper<>();
|
|
|
+ qw.lambda().eq(IotAlarmRuleSource::getValue, deviceStatus.getUniqueCode())
|
|
|
+ .eq(IotAlarmRuleSource::getOrgId, deviceStatus.getOrgId());
|
|
|
+ IotAlarmRuleSource iotAlarmRuleSource = iotAlarmRuleSourceMapper.selectOne(qw);
|
|
|
+ if (iotAlarmRuleSource == null) {
|
|
|
+ //设备未绑定规则,不做处理
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ //获取该设备对应的规则
|
|
|
+ int weekDay = DateUtil.thisDayOfWeek() - 1;
|
|
|
+ // 0 代表星期天, 规则表中星期天存的为7,需要做一下转换
|
|
|
+ if (weekDay == 0) {
|
|
|
+ weekDay = 7;
|
|
|
+ }
|
|
|
+ System.out.println(weekDay);
|
|
|
+ QueryWrapper<IotAlarmRuleExpress> ruleExpress = new QueryWrapper<>();
|
|
|
+ ruleExpress.lambda().eq(IotAlarmRuleExpress::getRuleId, iotAlarmRuleSource.getRuleId())
|
|
|
+ .eq(IotAlarmRuleExpress::getSourceType, iotAlarmRuleSource.getSourceType())
|
|
|
+ .eq(IotAlarmRuleExpress::getWeekDay, weekDay);
|
|
|
+ List<IotAlarmRuleExpress> iotAlarmRuleExpresses = iotAlarmRuleExpressMapper.selectList(ruleExpress);
|
|
|
+
|
|
|
+ List<IotAlarmSystemField> fields = iotAlarmSystemFieldMapper.selectList(null);
|
|
|
+ Map<String, IotAlarmSystemField> fieldMap = fields.stream().collect(Collectors.toMap(IotAlarmSystemField::getSysFieldCode, Function.identity()));
|
|
|
+ String dateStr = DateUtil.format(new Date(), "yyyy-MM-dd");
|
|
|
+ boolean isAlarm = false;
|
|
|
+ boolean isExpress = false;
|
|
|
+ List<IotAlarmData> list = new ArrayList<>();
|
|
|
+ for (IotAlarmRuleExpress express : iotAlarmRuleExpresses) {
|
|
|
+ Boolean smsType = express.getSmsType();
|
|
|
+ boolean isOk = compareTime(express, dateStr, now);
|
|
|
+ if (!isOk) {
|
|
|
+ //不在时间段内
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ isExpress = true;
|
|
|
+ IotAlarmData alarmData = createAlarmData(deviceStatus, express, fieldMap);
|
|
|
+ if (alarmData != null) {
|
|
|
+ isAlarm = true;
|
|
|
+ alarmData.setSmsType(smsType);
|
|
|
+ list.add(alarmData);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ String deviceCode = deviceStatus.getUniqueCode();
|
|
|
+ QueryWrapper<IotAlarmData> queryWrapper = new QueryWrapper<>();
|
|
|
+ queryWrapper.lambda().eq(IotAlarmData::getDeviceId, deviceCode)
|
|
|
+ .eq(IotAlarmData::getOrgId, deviceStatus.getOrgId())
|
|
|
+ .isNull(IotAlarmData::getEndTime);
|
|
|
+ List<IotAlarmData> alarms = baseMapper.selectList(queryWrapper);
|
|
|
+ if (isAlarm && alarms.size() == 0) {
|
|
|
+ //报警中,且表中没有告警数据,则插入数据
|
|
|
+ this.saveBatch(list);
|
|
|
+ DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
|
|
+ for (IotAlarmData alarm : list) {
|
|
|
+ if (alarm.getSmsType()) {
|
|
|
+ LogUtils.DIRECT_HOST_SENSOR_STATUS.info("传感器【 {} 】 发生报警需要发送短信提醒", deviceStatus.getDeviceName());
|
|
|
+ CompletableFuture.runAsync(()->{
|
|
|
+ try {
|
|
|
+ //需要发送短信
|
|
|
+ LocalDateTime time = alarm.getTime();
|
|
|
+ String timeStr = time.format(formatter);
|
|
|
+ String alarmType = SmsNotifyType.getDesc(deviceStatus.getDeviceType());
|
|
|
+ LogUtils.DIRECT_HOST_SENSOR_STATUS.info("传感器【 {} 】 发生报警: {} ,准备异步发送短信提醒", deviceStatus.getDeviceName(), alarmType);
|
|
|
+ remoteSmsService.sendSmsIot(deviceStatus.getOrgId(), alarmType, alarm.getContent(), timeStr);
|
|
|
+ LogUtils.DIRECT_HOST_SENSOR_STATUS.info("传感器【 {} 】 发生报警: {} ,异步发送短信提醒完成", deviceStatus.getDeviceName(), alarmType);
|
|
|
+ } catch (Exception e) {
|
|
|
+ LogUtils.DIRECT_HOST_SENSOR_STATUS.info("传感器【 {} 】 发生报警,异步发送短信提醒时发生异常:{}", deviceStatus.getDeviceName(), e.getMessage());
|
|
|
+
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (ObjectUtil.notEqual(deviceStatus.getState(), 1)) {
|
|
|
+ deviceStatus.setState(1);
|
|
|
+ deviceStatus.setStateUpdateTime(LocalDateTime.now());
|
|
|
+ deviceStatus.setStateStartTime(LocalDateTime.now());
|
|
|
+ deviceStatusMapper.updateById(deviceStatus);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ // 本次上传的设备状态数据 没有生成告警
|
|
|
+ if (isExpress && !isAlarm) {
|
|
|
+ //报警恢复
|
|
|
+ if (alarms.size() != 0) {
|
|
|
+ // 温湿度一个设备 存在同时2条告警的情况
|
|
|
+ if (alarms.size() > 1) {
|
|
|
+ for (IotAlarmData oldAlarm : alarms) {
|
|
|
+ oldAlarm.setEndTime(LocalDateTime.now());
|
|
|
+ this.updateById(oldAlarm);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ IotAlarmData iotAlarmData = alarms.get(0);
|
|
|
+ iotAlarmData.setEndTime(LocalDateTime.now());
|
|
|
+ this.updateById(iotAlarmData);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (ObjectUtil.notEqual(deviceStatus.getState(), 0)) {
|
|
|
+ deviceStatus.setStateUpdateTime(LocalDateTime.now());
|
|
|
+ deviceStatus.setStateStartTime(LocalDateTime.now());
|
|
|
+ deviceStatus.setState(0);
|
|
|
+ deviceStatusMapper.updateById(deviceStatus);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 处理温湿度 一个设备 2 种告警, 可能会单个恢复的情况
|
|
|
+ else if (isAlarm && isExpress) {
|
|
|
+ if (ObjectUtil.notEqual(list.size(), alarms.size())) {
|
|
|
+ List<IotAlarmData> noAlarmData = alarms.stream().filter(x -> !list.stream().anyMatch(y -> ObjectUtil.equal(y.getFieldCode(), x.getFieldCode()))).collect(Collectors.toList());
|
|
|
+ for (IotAlarmData oldAlarm : noAlarmData) {
|
|
|
+ oldAlarm.setEndTime(LocalDateTime.now());
|
|
|
+ this.updateById(oldAlarm);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /* @Transactional(rollbackFor = Exception.class)
|
|
|
@Override
|
|
|
public void dealSensorData(IotSensor iotSensor) throws Exception {
|
|
|
- /**
|
|
|
+ *//**
|
|
|
* 生成告警数据逻辑梳理
|
|
|
* 1.根据传入的设备数据,判断设备类型。
|
|
|
* 2.根据设备类型获取设备绑定规则。
|
|
|
* 3.根据绑定规则判断适用于星期几,时间段。
|
|
|
* 4.根据绑定的规则判断是否需要生成告警数据。
|
|
|
* 5.如果结果是告警,判断是否已有告警数据,如果有则不生成告警数据。如果是告警结束,则修改告警数据的结束时间。
|
|
|
- */
|
|
|
+ *//*
|
|
|
Date now = new Date();
|
|
|
//判断是否有绑定规则
|
|
|
QueryWrapper<IotAlarmRuleSource> qw = new QueryWrapper<>();
|
|
|
@@ -209,17 +337,17 @@ public class IotAlarmDataServiceImpl extends ServiceImpl<IotAlarmDataMapper, Iot
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- }
|
|
|
+ }*/
|
|
|
|
|
|
@Override
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
public void dealSensorListData(List<IotSensor> iotSensor) throws Exception {
|
|
|
- if (ObjectUtil.isEmpty(iotSensor)) {
|
|
|
+ /*if (ObjectUtil.isEmpty(iotSensor)) {
|
|
|
return;
|
|
|
}
|
|
|
for (IotSensor sensor : iotSensor) {
|
|
|
dealSensorData(sensor);
|
|
|
- }
|
|
|
+ }*/
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -389,11 +517,11 @@ public class IotAlarmDataServiceImpl extends ServiceImpl<IotAlarmDataMapper, Iot
|
|
|
|
|
|
}
|
|
|
|
|
|
- private IotAlarmData createAlarmData(IotSensor iotSensor, IotAlarmRuleExpress express, Map<String, IotAlarmSystemField> fieldMap) {
|
|
|
+ private IotAlarmData createAlarmData(IotDeviceStatus deviceStatus, IotAlarmRuleExpress express, Map<String, IotAlarmSystemField> fieldMap) {
|
|
|
IotAlarmData iotAlarmData = null;
|
|
|
String operator = express.getOperator();
|
|
|
String settingValue = express.getValue();
|
|
|
- String infos = iotSensor.getInfos();
|
|
|
+ String infos = deviceStatus.getInfo();
|
|
|
Map<String, String> dataMap = dealInfos(infos);
|
|
|
IotAlarmSystemField field = fieldMap.get(express.getFieldcode());
|
|
|
String sensorValue = dataMap.get(field.getName());
|
|
|
@@ -406,7 +534,7 @@ public class IotAlarmDataServiceImpl extends ServiceImpl<IotAlarmDataMapper, Iot
|
|
|
// sensorValue 存在为null的情况, deviceType和 infos.name 不对应的情况
|
|
|
if (ObjectUtil.equal(sensorValue, settingValue)) {
|
|
|
//对上了
|
|
|
- iotAlarmData = builderAlarm(sensorValue, field, express, iotSensor);
|
|
|
+ iotAlarmData = builderAlarm(sensorValue, field, express, deviceStatus);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -415,7 +543,7 @@ public class IotAlarmDataServiceImpl extends ServiceImpl<IotAlarmDataMapper, Iot
|
|
|
final BigDecimal setValue = new BigDecimal(settingValue);
|
|
|
if (curValue.compareTo(setValue) > 0) {
|
|
|
//对上了
|
|
|
- iotAlarmData = builderAlarm(sensorValue, field, express, iotSensor);
|
|
|
+ iotAlarmData = builderAlarm(sensorValue, field, express, deviceStatus);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -424,7 +552,7 @@ public class IotAlarmDataServiceImpl extends ServiceImpl<IotAlarmDataMapper, Iot
|
|
|
final BigDecimal setValue = new BigDecimal(settingValue);
|
|
|
if (curValue.compareTo(setValue) > 0 || curValue.equals(setValue)) {
|
|
|
//对上了
|
|
|
- iotAlarmData = builderAlarm(sensorValue, field, express, iotSensor);
|
|
|
+ iotAlarmData = builderAlarm(sensorValue, field, express, deviceStatus);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -433,7 +561,7 @@ public class IotAlarmDataServiceImpl extends ServiceImpl<IotAlarmDataMapper, Iot
|
|
|
final BigDecimal setValue = new BigDecimal(settingValue);
|
|
|
if (curValue.compareTo(setValue) < 0) {
|
|
|
//对上了
|
|
|
- iotAlarmData = builderAlarm(sensorValue, field, express, iotSensor);
|
|
|
+ iotAlarmData = builderAlarm(sensorValue, field, express, deviceStatus);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -442,7 +570,7 @@ public class IotAlarmDataServiceImpl extends ServiceImpl<IotAlarmDataMapper, Iot
|
|
|
final BigDecimal setValue = new BigDecimal(settingValue);
|
|
|
if (curValue.compareTo(setValue) < 0 || curValue.equals(setValue)) {
|
|
|
//对上了
|
|
|
- iotAlarmData = builderAlarm(sensorValue, field, express, iotSensor);
|
|
|
+ iotAlarmData = builderAlarm(sensorValue, field, express, deviceStatus);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -472,11 +600,11 @@ public class IotAlarmDataServiceImpl extends ServiceImpl<IotAlarmDataMapper, Iot
|
|
|
return iotAlarmData;
|
|
|
}
|
|
|
|
|
|
- private IotAlarmData builderAlarm(String sensorValue, IotAlarmSystemField field, IotAlarmRuleExpress express, IotSensor iotSensor) {
|
|
|
+ private IotAlarmData builderAlarm(String sensorValue, IotAlarmSystemField field, IotAlarmRuleExpress express, IotDeviceStatus deviceStatus) {
|
|
|
IotAlarmData iotAlarmData = new IotAlarmData();
|
|
|
iotAlarmData.setAlarmValue(sensorValue + (field.getUnit() == null ? "" : field.getUnit()));
|
|
|
iotAlarmData.setTime(LocalDateTime.now());
|
|
|
- iotAlarmData.setDeviceName(iotSensor.getDeviceName());
|
|
|
+ iotAlarmData.setDeviceName(deviceStatus.getDeviceName());
|
|
|
iotAlarmData.setRuleId(express.getRuleId());
|
|
|
iotAlarmData.setSourceType(field.getSourceType());
|
|
|
iotAlarmData.setSourceTypeDes(field.getSourceTypeDes() + "告警");
|
|
|
@@ -484,10 +612,14 @@ public class IotAlarmDataServiceImpl extends ServiceImpl<IotAlarmDataMapper, Iot
|
|
|
iotAlarmData.setOperator(express.getOperator());
|
|
|
iotAlarmData.setValue(express.getValue());
|
|
|
iotAlarmData.setValueText(express.getValueText());
|
|
|
- iotAlarmData.setContent(iotSensor.getDeviceName() + "触发" + iotAlarmData.getSourceTypeDes());
|
|
|
- iotAlarmData.setOrgId(iotSensor.getOrgId());
|
|
|
+ iotAlarmData.setContent(deviceStatus.getDeviceName() + "触发" + iotAlarmData.getSourceTypeDes());
|
|
|
+ iotAlarmData.setOrgId(deviceStatus.getOrgId());
|
|
|
iotAlarmData.setDataType(AlarmDataTypeEnum.IOT_ALARM.getValue());
|
|
|
- iotAlarmData.setDeviceId(iotSensor.getDeviceCode());
|
|
|
+ iotAlarmData.setDeviceId(deviceStatus.getUniqueCode());
|
|
|
+ iotAlarmData.setCreateTime(new Date());
|
|
|
+ iotAlarmData.setUpdateTime(new Date());
|
|
|
+ iotAlarmData.setCreateBy("system");
|
|
|
+ iotAlarmData.setUpdateBy("system");
|
|
|
return iotAlarmData;
|
|
|
}
|
|
|
|