Commit 1ff7aafe by 杨伟程
parents 626d1558 b04680a3
......@@ -2,17 +2,23 @@ package com.makeit.service.platform.alarm;
import com.baomidou.mybatisplus.extension.service.IService;
import com.makeit.dto.platform.alarm.PlatAlarmCheckDTO;
import com.makeit.dto.platform.alarm.PlatDayDurationRecordDTO;
import com.makeit.entity.platform.alarm.PlatDayDurationRecord;
import com.makeit.utils.AlarmRedisDTO;
import java.util.List;
/**
* @author lixl
* @description 针对表【plat_day_duration_record(每天停留时长记录)】的数据库操作Service
* @createDate 2023-09-14 10:14:20
*/
* @author lixl
* @description 针对表【plat_day_duration_record(每天停留时长记录)】的数据库操作Service
* @createDate 2023-09-14 10:14:20
*/
public interface PlatDayDurationRecordService extends IService<PlatDayDurationRecord> {
List<PlatDayDurationRecord> getDayMaxDurationList(PlatDayDurationRecordDTO param);
void saveDayDurationRecord(PlatAlarmCheckDTO platAlarmCheckDTO, AlarmRedisDTO alarmRedisDTO);
}
......@@ -127,57 +127,11 @@ public class BehaviorAlarm implements IAlarm {
return;
}
//保存每次进入空间时长
saveDayDurationRecord(platAlarmCheckDTO, alarmRedisDTO);
platDayDurationRecordService.saveDayDurationRecord(platAlarmCheckDTO, alarmRedisDTO);
RedisUtil.delete(RedisConst.ALARM_DEVICE_BEHAVIOR_ID + deviceId);
}
}
/**
* 保存停留时长记录
* 进入雷达开始
* 走出雷达结束
*
* @param platAlarmCheckDTO
* @param alarmRedisDTO
*/
private void saveDayDurationRecord(PlatAlarmCheckDTO platAlarmCheckDTO, AlarmRedisDTO alarmRedisDTO) {
PlatAlarmConfig config = platAlarmCheckDTO.getPlatAlarmConfig();
PlatDevice platDevice = platAlarmCheckDTO.getPlatDevice();
Date now = new Date();
long endLong = now.getTime();
long count = endLong - alarmRedisDTO.getStartLong();
//save db
platAlarmRecordService.getElderListByDeviceId(platAlarmCheckDTO);
if(CollectionUtils.isEmpty(platAlarmCheckDTO.getPlatElderList())){
log.error("未关联长者,设备id:"+platDevice.getId());
return;
}
List<PlatElder> platElderList = platAlarmCheckDTO.getPlatElderList();
if(CollectionUtils.isNotEmpty(platElderList)){
platAlarmRecordService.getElderListByDeviceId(platAlarmCheckDTO);
platElderList = platAlarmCheckDTO.getPlatElderList();
}
for (PlatElder platElder : platElderList) {
if(StringUtils.isBlank(platElder.getRegionName())){
continue;
}
PlatDayDurationRecord durationRecord = new PlatDayDurationRecord();
durationRecord.setElderIds(platElder.getId());
durationRecord.setRegionName(platElder.getRegionName());
durationRecord.setDuration(count);
durationRecord.setOriDeviceId(platDevice.getOriDeviceId());
durationRecord.setDay(alarmRedisDTO.getStart());
durationRecord.setStartDate(alarmRedisDTO.getStart());
durationRecord.setEndDate(now);
durationRecord.setTenantId(config.getTenantId());
durationRecord.setRoomId(platElder.getRoomId());
durationRecord.setDeviceId(platDevice.getId());
platDayDurationRecordService.saveOrUpdate(durationRecord);
}
}
@Override
public void notice(PlatAlarmCheckDTO platAlarmCheckDTO) {
......
......@@ -3,7 +3,10 @@ package com.makeit.service.platform.alarm.alarmStrategy;
import cn.hutool.core.convert.Convert;
import com.alibaba.fastjson.JSONObject;
import com.makeit.dto.platform.alarm.PlatAlarmCheckDTO;
import com.makeit.dto.platform.alarm.PlatAlarmConfigBehaviorDTOVO;
import com.makeit.entity.platform.alarm.PlatAlarmConfig;
import com.makeit.entity.platform.alarm.PlatAlarmRecord;
import com.makeit.entity.platform.alarm.PlatDayDurationRecord;
import com.makeit.entity.platform.device.PlatDevice;
import com.makeit.entity.platform.elder.PlatElder;
import com.makeit.entity.platform.space.PlatRoom;
......@@ -11,7 +14,11 @@ import com.makeit.enums.CommonEnum;
import com.makeit.enums.platform.alarm.PlatAlarmConfigEnum;
import com.makeit.enums.redis.RedisConst;
import com.makeit.service.platform.alarm.PlatAlarmRecordService;
import com.makeit.service.platform.alarm.PlatDayDurationRecordService;
import com.makeit.utils.AlarmConfigCacheUtil;
import com.makeit.utils.AlarmRedisDTO;
import com.makeit.utils.DayDurationUtil;
import com.makeit.utils.data.convert.JsonUtil;
import com.makeit.utils.redis.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
......@@ -20,6 +27,8 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
@Component
......@@ -28,6 +37,15 @@ public class FallAlarm implements IAlarm {
@Autowired
private PlatAlarmRecordService platAlarmRecordService;
@Autowired
private PlatDayDurationRecordService platDayDurationRecordService;
@Autowired
private AlarmConfigCacheUtil alarmConfigCacheUtil;
@Autowired
private DayDurationUtil dayDurationUtil;
private final PlatAlarmConfigEnum.AlarmTypeEnum alarmTypeEnum = PlatAlarmConfigEnum.AlarmTypeEnum.FALL;
@Override
......@@ -46,25 +64,106 @@ public class FallAlarm implements IAlarm {
PlatDevice platDevice = platAlarmCheckDTO.getPlatDevice();
String deviceId = platDevice.getId();
String personState = Convert.toStr(properties.get("personState"));
//记录停留时长
recordDuration(platAlarmCheckDTO);
//跌倒告警
AlarmRedisDTO alarmRedisDTO = RedisUtil.get(RedisConst.ALARM_DEVICE_FALL_ID + deviceId);
if(alarmRedisDTO==null){
if (alarmRedisDTO == null) {
alarmRedisDTO = new AlarmRedisDTO();
alarmRedisDTO.setAlarm(CommonEnum.NO.getValue());
RedisUtil.set(RedisConst.ALARM_DEVICE_FALL_ID+ deviceId, alarmRedisDTO);
RedisUtil.set(RedisConst.ALARM_DEVICE_FALL_ID + deviceId, alarmRedisDTO);
}
if (StringUtils.equals(personState, CommonEnum.YES.getValue())) {
if (StringUtils.equals(alarmRedisDTO.getAlarm(),CommonEnum.YES.getValue())){
log.error("长者跌倒,已告警过,设备plat_id:"+deviceId);
if (StringUtils.equals(alarmRedisDTO.getAlarm(), CommonEnum.YES.getValue())) {
log.error("长者跌倒,已告警过,设备plat_id:" + deviceId);
return;
}
alarmRedisDTO.setAlarm(CommonEnum.YES.getValue());
RedisUtil.set(RedisConst.ALARM_DEVICE_FALL_ID+ deviceId, alarmRedisDTO);
RedisUtil.set(RedisConst.ALARM_DEVICE_FALL_ID + deviceId, alarmRedisDTO);
notice(platAlarmCheckDTO);
} else {
if(StringUtils.equals(alarmRedisDTO.getAlarm(),CommonEnum.YES.getValue())) {
if (StringUtils.equals(alarmRedisDTO.getAlarm(), CommonEnum.YES.getValue())) {
alarmRedisDTO.setAlarm(CommonEnum.NO.getValue());
RedisUtil.set(RedisConst.ALARM_DEVICE_FALL_ID+ deviceId, alarmRedisDTO);
RedisUtil.set(RedisConst.ALARM_DEVICE_FALL_ID + deviceId, alarmRedisDTO);
}
}
}
private void recordDuration(PlatAlarmCheckDTO platAlarmCheckDTO) {
PlatDevice platDevice = platAlarmCheckDTO.getPlatDevice();
PlatAlarmConfig config = alarmConfigCacheUtil.get(platDevice.getOrgId(), PlatAlarmConfigEnum.AlarmTypeEnum.BEHAVIOR.getValue());
String ruleConfigStr = config.getRuleConfig();
JSONObject properties = platAlarmCheckDTO.getProperties();
String deviceId = platDevice.getId();
if (StringUtils.isBlank(ruleConfigStr)) {
log.error("行为告警配置未配置,告警配置id:" + config.getId());
return;
}
String personState = Convert.toStr(properties.get("personState"));
String person = Convert.toStr(properties.get("person"));
PlatAlarmConfigBehaviorDTOVO ruleConfig = JsonUtil.toObj(ruleConfigStr, PlatAlarmConfigBehaviorDTOVO.class);
Integer duration = ruleConfig.getAverageDuration();//分钟
PlatDayDurationRecord platDayDurationRecord = dayDurationUtil.get(deviceId);
// if (platDayDurationRecord == null) {
// log.error("行为告警未找到行为异常平均时长,设备plat_id:" + platDevice.getId());
// return;
// }
Integer duration1 = 0;
if (platDayDurationRecord != null) {
duration1 = Math.toIntExact(platDayDurationRecord.getDuration());
}
//duration+平均停留时长
if (duration == null) {
duration = 0;
} else {
duration = duration * 60 + (duration1 / 1000);
}
List<String> personStateList = Arrays.asList("0");
//有人
//计数
Date now = new Date();
long endLong = now.getTime();
AlarmRedisDTO alarmRedisDTO = RedisUtil.get(RedisConst.ALARM_DEVICE_BEHAVIOR_ID + deviceId);
if (alarmRedisDTO == null) {
alarmRedisDTO = new AlarmRedisDTO();
}
//有人
if (!personStateList.contains(person)) {
//第一次进入空间
Long startLong = alarmRedisDTO.getStartLong();
if (startLong == null) {
alarmRedisDTO.setStartLong(endLong);
alarmRedisDTO.setStart(now);
RedisUtil.set(RedisConst.ALARM_DEVICE_BEHAVIOR_ID + deviceId, alarmRedisDTO);
log.error("空间雷达上报进入房间,设备plat_id:" + platDevice.getId());
return;
}
long count = endLong - startLong;
//进入空间时间满足告警时长,判断是否已经告警过了
if (duration!= 0 && count / 1000 >= duration && StringUtils.equals(alarmRedisDTO.getAlarm(), CommonEnum.NO.getValue())) {
log.error("空间雷达发出告警,设备plat_id:" + platDevice.getId());
if(duration1 != 0) {
alarmRedisDTO.setAlarm(CommonEnum.YES.getValue());
RedisUtil.set(RedisConst.ALARM_DEVICE_BEHAVIOR_ID + deviceId, alarmRedisDTO);
notice(platAlarmCheckDTO);
}else {
return;
}
}
} else {//没人
// 是否有第一次进入记录,有则保存db
Long startLong = alarmRedisDTO.getStartLong();
if (startLong == null) {
return;
}
//保存每次进入空间时长
platDayDurationRecordService.saveDayDurationRecord(platAlarmCheckDTO, alarmRedisDTO);
RedisUtil.delete(RedisConst.ALARM_DEVICE_BEHAVIOR_ID + deviceId);
}
}
......@@ -83,11 +182,11 @@ public class FallAlarm implements IAlarm {
param.add(platElder.getName());
param.add(platRoom.getName());
platAlarmCheckDTO.setParam(param);
PlatAlarmRecord platAlarmRecord = platAlarmRecordService.convertToPlatAlarmRecord(platAlarmCheckDTO,platElder);
PlatAlarmRecord platAlarmRecord = platAlarmRecordService.convertToPlatAlarmRecord(platAlarmCheckDTO, platElder);
platAlarmRecord.setElderIds(platElder.getId());
platAlarmRecord.setElderName(platElder.getName());
platAlarmRecordService.noticeDeviceAlarm(platAlarmCheckDTO.getPlatAlarmConfig(), platAlarmRecord);
log.error("长者跌倒,发出告警,设备id:"+platDevice.getId()+", 长者名称:"+platElder.getName());
log.error("长者跌倒,发出告警,设备id:" + platDevice.getId() + ", 长者名称:" + platElder.getName());
}
}
}
......@@ -21,6 +21,7 @@ import com.makeit.entity.platform.elder.PlatElderChildrenInfo;
import com.makeit.entity.platform.space.PlatRegionSetting;
import com.makeit.entity.platform.space.PlatRoom;
import com.makeit.entity.platform.space.PlatRoomBedDevice;
import com.makeit.entity.saas.PlatTenant;
import com.makeit.enums.CommonEnum;
import com.makeit.exception.BusinessException;
import com.makeit.global.aspect.tenant.TenantIdIgnore;
......@@ -33,6 +34,7 @@ import com.makeit.service.platform.elder.PlatElderService;
import com.makeit.service.platform.space.PlatRegionSettingService;
import com.makeit.service.platform.space.PlatRoomBedDeviceService;
import com.makeit.service.platform.space.PlatRoomService;
import com.makeit.service.saas.PlatTenantService;
import com.makeit.utils.data.convert.BeanDtoVoUtils;
import com.makeit.utils.data.convert.PageUtil;
import com.makeit.utils.msg.MsgSendUtil;
......@@ -89,6 +91,8 @@ public class PlatAlarmRecordServiceImpl extends ServiceImpl<PlatAlarmRecordMappe
private PlatRoomService platRoomService;
@Autowired
private PlatRegionSettingService platRegionSettingService;
@Autowired
private PlatTenantService platTenantService;
@Override
......@@ -315,8 +319,22 @@ public class PlatAlarmRecordServiceImpl extends ServiceImpl<PlatAlarmRecordMappe
if(StringUtils.isEmpty(notifyChannel)){
return;
}
PlatTenant platTenant = platTenantService.getById(alarmConfig.getTenantId());
if(platTenant == null){
return;
}
String alertChannel = platTenant.getAlertChannel();
if(StringUtils.isBlank(alertChannel)){
return;
}
String[] tenantChannels = alertChannel.split(",");
List<String> tenantChannelList = Arrays.asList(tenantChannels);
String[] split = notifyChannel.split(",");
for (String sendType : split) {
//租户告警渠道 没配置的 就不发送
if(!tenantChannelList.contains(sendType)){
continue;
}
SendTypeEnum sendTypeEnum = SendTypeEnum.getByValue(sendType);
boolean contains = notifyChannelList.contains(sendTypeEnum);
if (contains) {
......
......@@ -2,12 +2,22 @@ package com.makeit.service.platform.alarm.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.makeit.dto.platform.alarm.PlatAlarmCheckDTO;
import com.makeit.dto.platform.alarm.PlatDayDurationRecordDTO;
import com.makeit.entity.platform.alarm.PlatAlarmConfig;
import com.makeit.entity.platform.alarm.PlatDayDurationRecord;
import com.makeit.entity.platform.device.PlatDevice;
import com.makeit.entity.platform.elder.PlatElder;
import com.makeit.mapper.platform.alarm.PlatDayDurationRecordMapper;
import com.makeit.service.platform.alarm.PlatAlarmRecordService;
import com.makeit.service.platform.alarm.PlatDayDurationRecordService;
import com.makeit.utils.AlarmRedisDTO;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;
/**
......@@ -19,8 +29,59 @@ import java.util.List;
public class PlatDayDurationRecordServiceImpl extends ServiceImpl<PlatDayDurationRecordMapper, PlatDayDurationRecord>
implements PlatDayDurationRecordService {
@Autowired
private PlatAlarmRecordService platAlarmRecordService;
@Override
public List<PlatDayDurationRecord> getDayMaxDurationList(PlatDayDurationRecordDTO param){
return baseMapper.getDayMaxDurationList(param);
}
/**
* 保存停留时长记录
* 进入雷达开始
* 走出雷达结束
*
* @param platAlarmCheckDTO
* @param alarmRedisDTO
*/
@Override
public void saveDayDurationRecord(PlatAlarmCheckDTO platAlarmCheckDTO, AlarmRedisDTO alarmRedisDTO) {
PlatAlarmConfig config = platAlarmCheckDTO.getPlatAlarmConfig();
PlatDevice platDevice = platAlarmCheckDTO.getPlatDevice();
Date now = new Date();
long endLong = now.getTime();
long count = endLong - alarmRedisDTO.getStartLong();
//save db
platAlarmRecordService.getElderListByDeviceId(platAlarmCheckDTO);
if(CollectionUtils.isEmpty(platAlarmCheckDTO.getPlatElderList())){
log.error("未关联长者,设备id:"+platDevice.getId());
return;
}
List<PlatElder> platElderList = platAlarmCheckDTO.getPlatElderList();
if(CollectionUtils.isNotEmpty(platElderList)){
platAlarmRecordService.getElderListByDeviceId(platAlarmCheckDTO);
platElderList = platAlarmCheckDTO.getPlatElderList();
}
for (PlatElder platElder : platElderList) {
if(StringUtils.isBlank(platElder.getRegionName())){
continue;
}
PlatDayDurationRecord durationRecord = new PlatDayDurationRecord();
durationRecord.setElderIds(platElder.getId());
durationRecord.setRegionName(platElder.getRegionName());
durationRecord.setDuration(count);
durationRecord.setOriDeviceId(platDevice.getOriDeviceId());
durationRecord.setDay(alarmRedisDTO.getStart());
durationRecord.setStartDate(alarmRedisDTO.getStart());
durationRecord.setEndDate(now);
durationRecord.setTenantId(config.getTenantId());
durationRecord.setRoomId(platElder.getRoomId());
durationRecord.setDeviceId(platDevice.getId());
this.saveOrUpdate(durationRecord);
}
}
}
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