Commit 2ad734d0 by 汪志阳

fix:离床时间范围临界值处理

parent bee9903b
...@@ -25,8 +25,7 @@ import org.springframework.stereotype.Component; ...@@ -25,8 +25,7 @@ import org.springframework.stereotype.Component;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.text.DateFormat; import java.text.DateFormat;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.time.LocalDate; import java.time.*;
import java.time.LocalDateTime;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
...@@ -88,27 +87,105 @@ public class OffBedAlarm implements IAlarm { ...@@ -88,27 +87,105 @@ public class OffBedAlarm implements IAlarm {
String deviceId = platDevice.getId(); String deviceId = platDevice.getId();
List<LocalDateTime> timeRange = getOffTimeRange(ruleConfig); List<LocalDateTime> timeRange = getOffTimeRange(ruleConfig);
if (CollUtil.isEmpty(timeRange)) { if (CollUtil.isEmpty(timeRange)) {
log.warn("离床告警配置时间段解析失败,config:{}", JSONUtil.toJsonStr(ruleConfig)); log.error("离床告警配置时间段解析失败,config:{}", JSONUtil.toJsonStr(ruleConfig));
return; return;
} }
LocalDateTime startTime = timeRange.get(0); LocalDateTime startTime = timeRange.get(0);
LocalDateTime endTime = timeRange.get(1); LocalDateTime endTime = timeRange.get(1);
Integer duration = ruleConfig.getDuration(); Integer duration = ruleConfig.getDuration();
String personState = Convert.toStr(properties.get("person")); String personState = Convert.toStr(properties.get("person"));
boolean isOffBed = "0".equals(personState);
// 1-有人 0-无人 // 1-有人 0-无人
log.warn("离床config:{}",JSONUtil.toJsonStr(properties)); log.info("离床config:{}", JSONUtil.toJsonStr(properties));
if ("1".equals(personState)) { AlarmRedisDTO alarmRedisDTO = RedisUtil.get(RedisConst.ALARM_DEVICE_OFF_BED_ID + deviceId);
if (!isOffBed && alarmRedisDTO != null) {
log.warn("离床有人状态下,删除redis!"); log.warn("离床有人状态下,删除redis!");
RedisUtil.delete(RedisConst.ALARM_DEVICE_OFF_BED_ID + deviceId); RedisUtil.delete(RedisConst.ALARM_DEVICE_OFF_BED_ID + deviceId);
return;
} }
// 设备无人,在范围内上报存储redis,超过离床持续时间,则预警 // 设备无人,在范围内上报存储redis,超过离床持续时间,则预警
String messageType = platAlarmCheckDTO.getMessageType();
if (StringUtils.equalsAnyIgnoreCase(messageType, "OFFLINE", "DISCONNECT")) {
log.warn("设备下线,删除redis off_bed 预警");
RedisUtil.delete(RedisConst.ALARM_DEVICE_OFF_BED_ID + deviceId);
}
if (isOffBed) {
// 离床推送redis
sendToRedis(alarmRedisDTO, deviceId);
alarmRedisDTO = RedisUtil.get(RedisConst.ALARM_DEVICE_OFF_BED_ID + deviceId);
long localDateLong = System.currentTimeMillis();
Long firstOffBedLong = alarmRedisDTO.getStartLong();
boolean isOverTime = (localDateLong - firstOffBedLong) / 1000 >= duration * 60;
LocalDateTime firstOffBedTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(firstOffBedLong), ZoneOffset.of("+8"));
log.info("第一次离床时间为:" + firstOffBedTime);
// 第一次离床时间在范围前,则已范围开始时间起始算
if (firstOffBedTime.isBefore(startTime)) {
boolean over = (Duration.between(LocalDateTime.now(), startTime).toMillis()) / 1000 >= duration * 60;
if (over) {
platAlarmCheckDTO.setAbnormalValue(String.valueOf(localDateLong - firstOffBedLong));
log.info("离床时间在范围前,发送预警");
noticeAlarm(alarmRedisDTO, platAlarmCheckDTO, deviceId);
return;
}
}
LocalDateTime validEndTime = endTime.plusMinutes(duration);
// 离床时间在范围时间内(小于范围结束时间-持续分钟)
boolean isOffBedValid = firstOffBedTime.isAfter(startTime) && firstOffBedTime.isBefore(validEndTime);
boolean isInRange = LocalDateTime.now().isAfter(startTime) && LocalDateTime.now().isBefore(endTime); boolean isInRange = LocalDateTime.now().isAfter(startTime) && LocalDateTime.now().isBefore(endTime);
if ("0".equals(personState) && isInRange) { if (isOffBedValid && isInRange && isOverTime) {
platAlarmCheckDTO.setAbnormalValue(String.valueOf(localDateLong - firstOffBedLong));
log.info("离床时间在范围时间内(小于范围结束时间-持续分钟)");
noticeAlarm(alarmRedisDTO, platAlarmCheckDTO, deviceId);
return;
}
AlarmRedisDTO alarmRedisDTO = RedisUtil.get(RedisConst.ALARM_DEVICE_OFF_BED_ID + deviceId); if (firstOffBedTime.isAfter(endTime)) {
if (alarmRedisDTO == null) { log.warn("离床时间超出时间范围!");
}
}
// if (isOffBed && isInRange) {
//
// // AlarmRedisDTO alarmRedisDTO = RedisUtil.get(RedisConst.ALARM_DEVICE_OFF_BED_ID + deviceId);
// if (alarmRedisDTO == null) {
// Date now = new Date();
// long endLong = now.getTime();
// alarmRedisDTO = new AlarmRedisDTO();
// alarmRedisDTO.setAlarm(CommonEnum.NO.getValue());
// alarmRedisDTO.setStart(now);
// alarmRedisDTO.setStartLong(endLong);
// RedisUtil.set(RedisConst.ALARM_DEVICE_OFF_BED_ID + deviceId, alarmRedisDTO);
// log.warn("离床设备存储redis,第一次上报时间:{},开始上报时间:{},是否上报:{}",
// getDateTime(alarmRedisDTO.getStartLong()), alarmRedisDTO.getStart(), alarmRedisDTO.getAlarm());
// return;
// }
// Date now = new Date();
// long endLong = now.getTime();
// Long startLong = alarmRedisDTO.getStartLong();
// long count = endLong - startLong;
// // 持续时间满足、且未预警过
// if (count / 1000 >= duration * 60 && StringUtils.equals(alarmRedisDTO.getAlarm(), CommonEnum.NO.getValue())) {
// log.warn("离床持续时间:{} 秒,当前时间:{},duration:{}", count / 1000, now, duration);
// // alarmRedisDTO.setStartLong(endLong);
// alarmRedisDTO.setStart(now);
// alarmRedisDTO.setAlarm(CommonEnum.YES.getValue());
// platAlarmCheckDTO.setAbnormalValue(String.valueOf(count));
// RedisUtil.set(RedisConst.ALARM_DEVICE_OFF_BED_ID + deviceId, alarmRedisDTO);
// log.warn("推送离床消息,第一次上报时间:{},开始上报时间:{},是否上报:{}",
// getDateTime(alarmRedisDTO.getStartLong()), alarmRedisDTO.getStart(), alarmRedisDTO.getAlarm());
// notice(platAlarmCheckDTO);
// }
// }
}
private void sendToRedis(AlarmRedisDTO alarmRedisDTO, String deviceId) {
Date now = new Date(); Date now = new Date();
long endLong = now.getTime(); long endLong = now.getTime();
// 第一次上报
if (alarmRedisDTO == null) {
alarmRedisDTO = new AlarmRedisDTO(); alarmRedisDTO = new AlarmRedisDTO();
alarmRedisDTO.setAlarm(CommonEnum.NO.getValue()); alarmRedisDTO.setAlarm(CommonEnum.NO.getValue());
alarmRedisDTO.setStart(now); alarmRedisDTO.setStart(now);
...@@ -116,32 +193,24 @@ public class OffBedAlarm implements IAlarm { ...@@ -116,32 +193,24 @@ public class OffBedAlarm implements IAlarm {
RedisUtil.set(RedisConst.ALARM_DEVICE_OFF_BED_ID + deviceId, alarmRedisDTO); RedisUtil.set(RedisConst.ALARM_DEVICE_OFF_BED_ID + deviceId, alarmRedisDTO);
log.warn("离床设备存储redis,第一次上报时间:{},开始上报时间:{},是否上报:{}", log.warn("离床设备存储redis,第一次上报时间:{},开始上报时间:{},是否上报:{}",
getDateTime(alarmRedisDTO.getStartLong()), alarmRedisDTO.getStart(), alarmRedisDTO.getAlarm()); getDateTime(alarmRedisDTO.getStartLong()), alarmRedisDTO.getStart(), alarmRedisDTO.getAlarm());
return; } else {
}
Date now = new Date();
long endLong = now.getTime();
Long startLong = alarmRedisDTO.getStartLong();
long count = endLong - startLong;
// 持续时间满足、且未预警过
if (count / 1000 >= duration * 60 && StringUtils.equals(alarmRedisDTO.getAlarm(), CommonEnum.NO.getValue())) {
log.warn("离床持续时间:{} 秒,当前时间:{},duration:{}", count / 1000, now, duration);
// alarmRedisDTO.setStartLong(endLong);
alarmRedisDTO.setStart(now); alarmRedisDTO.setStart(now);
log.info("离床更新redis,第一次上报时间:{},更新时间:{},是否上报:{}",
getDateTime(alarmRedisDTO.getStartLong()), alarmRedisDTO.getStart(), alarmRedisDTO.getAlarm());
RedisUtil.set(RedisConst.ALARM_DEVICE_OFF_BED_ID + deviceId, alarmRedisDTO);
}
}
private void noticeAlarm(AlarmRedisDTO alarmRedisDTO, PlatAlarmCheckDTO platAlarmCheckDTO, String deviceId) {
notice(platAlarmCheckDTO);
alarmRedisDTO.setStart(new Date());
alarmRedisDTO.setAlarm(CommonEnum.YES.getValue()); alarmRedisDTO.setAlarm(CommonEnum.YES.getValue());
platAlarmCheckDTO.setAbnormalValue(String.valueOf(count));
RedisUtil.set(RedisConst.ALARM_DEVICE_OFF_BED_ID + deviceId, alarmRedisDTO); RedisUtil.set(RedisConst.ALARM_DEVICE_OFF_BED_ID + deviceId, alarmRedisDTO);
log.warn("推送离床消息,第一次上报时间:{},开始上报时间:{},是否上报:{}", log.warn("推送离床消息,第一次上报时间:{},开始上报时间:{},是否上报:{}",
getDateTime(alarmRedisDTO.getStartLong()), alarmRedisDTO.getStart(), alarmRedisDTO.getAlarm()); getDateTime(alarmRedisDTO.getStartLong()), alarmRedisDTO.getStart(), alarmRedisDTO.getAlarm());
notice(platAlarmCheckDTO);
}
}
String messageType = platAlarmCheckDTO.getMessageType();
if (StringUtils.equalsAnyIgnoreCase(messageType,"OFFLINE", "DISCONNECT")) {
log.warn("删除redisoffbed预警");
RedisUtil.delete(RedisConst.ALARM_DEVICE_OFF_BED_ID + deviceId);
}
} }
private List<LocalDateTime> getOffTimeRange(PlatAlarmConfigOffBedDTOVO ruleConfig) { private List<LocalDateTime> getOffTimeRange(PlatAlarmConfigOffBedDTOVO ruleConfig) {
List<LocalDateTime> result = new ArrayList<>(); List<LocalDateTime> result = new ArrayList<>();
LocalDate localDate = LocalDate.now(); LocalDate localDate = LocalDate.now();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment