Commit 481ec1ef by 李小龙

整理代码

parent 454ccf95
Showing with 423 additions and 296 deletions
......@@ -6,6 +6,7 @@ import com.makeit.common.page.PageVO;
import com.makeit.common.response.ApiResponseEntity;
import com.makeit.common.response.ApiResponseUtils;
import com.makeit.dto.platform.device.PlatDeviceDetailDTO;
import com.makeit.dto.platform.device.PlatDeviceEditDTO;
import com.makeit.entity.platform.device.PlatDevice;
import com.makeit.service.platform.device.PlatDeviceService;
import io.swagger.annotations.Api;
......@@ -37,8 +38,8 @@ public class SaasDeviceController {
@ApiOperation("设备编辑")
@PostMapping("edit")
public ApiResponseEntity<Void> edit(@RequestBody PlatDevice platDevice) {
platDeviceService.edit(platDevice);
public ApiResponseEntity<Void> edit(@RequestBody PlatDeviceEditDTO dto) {
platDeviceService.edit(dto);
return ApiResponseUtils.success();
}
......
......@@ -95,6 +95,8 @@ public class RedisConst {
public static final String ALARM_DEVICE_ID = "alarm:device:id:";
public static final String ALARM_CONFIG_ORG_ID = "alarm:config:org:id";
public static final String PLAT_IOT_DEVICE_PREFIX = "plat:iot:device:";
......
package com.makeit.dto.platform.alarm;
import com.alibaba.fastjson.JSONObject;
import com.makeit.entity.platform.alarm.PlatAlarmConfig;
import com.makeit.entity.platform.device.PlatDevice;
import com.makeit.entity.platform.elder.PlatElder;
import com.makeit.entity.platform.space.PlatRoom;
import lombok.Data;
......@@ -8,9 +11,18 @@ import java.util.ArrayList;
import java.util.List;
@Data
public class PlatAlaramCheckDTO {
public class PlatAlarmCheckDTO {
private List<PlatElder> platElderList = new ArrayList<>();
private PlatRoom platRoom;
private PlatDevice platDevice;
private PlatAlarmConfig platAlarmConfig;
private List<String> param = new ArrayList<>();
private JSONObject properties;
}
......@@ -3,6 +3,7 @@ package com.makeit.service.platform.alarm;
import com.baomidou.mybatisplus.extension.service.IService;
import com.makeit.common.page.PageReqDTO;
import com.makeit.common.page.PageVO;
import com.makeit.dto.platform.alarm.PlatAlarmCheckDTO;
import com.makeit.dto.platform.alarm.PlatAlarmRecordQueryDTO;
import com.makeit.entity.platform.alarm.PlatAlarmConfig;
import com.makeit.entity.platform.alarm.PlatAlarmRecord;
......@@ -52,4 +53,6 @@ public interface PlatAlarmRecordService extends IService<PlatAlarmRecord> {
* 设备告警调用 发送消息
*/
void noticeDeviceAlarm(PlatAlarmConfig alarmConfig, PlatAlarmRecord alarmRecord);
PlatAlarmRecord createPlatAlarmRecord(PlatAlarmCheckDTO platAlarmCheckDTO);
}
package com.makeit.service.platform.alarm.alarmStrategy;
public interface AlarmStrategy {
}
package com.makeit.service.platform.alarm.alarmStrategy;
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.device.PlatDevice;
import com.makeit.enums.platform.alarm.PlatAlarmConfigEnum;
import com.makeit.enums.redis.RedisConst;
import com.makeit.service.platform.alarm.PlatAlarmRecordService;
import com.makeit.utils.data.convert.JsonUtil;
import com.makeit.utils.redis.RedisUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
@Component
public class BehaviorAlarm implements IAlarm{
@Autowired
private PlatAlarmRecordService platAlarmRecordService;
private PlatAlarmConfigEnum.AlarmTypeEnum alarmTypeEnum = PlatAlarmConfigEnum.AlarmTypeEnum.BEHAVIOR;
@Override
public boolean support(String alarmType) {
return StringUtils.equals(alarmType,alarmTypeEnum.getValue());
}
@Override
public void createPlatAlarmRecord(PlatAlarmCheckDTO platAlarmCheckDTO) {
PlatAlarmConfig config = platAlarmCheckDTO.getPlatAlarmConfig();
String ruleConfigStr = config.getRuleConfig();
JSONObject properties = platAlarmCheckDTO.getProperties();
PlatDevice platDevice = platAlarmCheckDTO.getPlatDevice();
String deviceId = platDevice.getOriDeviceId();
if (StringUtils.isBlank(ruleConfigStr)) {
return;
}
String personState = (String) properties.get("personState");
PlatAlarmConfigBehaviorDTOVO ruleConfig = JsonUtil.toObj(ruleConfigStr, PlatAlarmConfigBehaviorDTOVO.class);
Integer duration = ruleConfig.getDuration();
duration = duration*60;
//todo duration+平均停留时长
List<String> personStateList = Arrays.asList("1", "2", "3");
//有人
//计数
long endLong = new Date().getTime();
Long startLong = RedisUtil.get(RedisConst.ALARM_DEVICE_ID + deviceId);
if (personStateList.contains(personState)) {
long count = endLong - startLong;
if(startLong == null ){
RedisUtil.set(RedisConst.ALARM_DEVICE_ID + deviceId,endLong);
return;
}
if (count >= duration) {
platAlarmCheckDTO.getParam().add("房间名");
PlatAlarmRecord platAlarmRecord =platAlarmRecordService.createPlatAlarmRecord(platAlarmCheckDTO);
platAlarmRecordService.noticeDeviceAlarm(platAlarmCheckDTO.getPlatAlarmConfig(),platAlarmRecord);
RedisUtil.set(RedisConst.ALARM_DEVICE_ID + deviceId,startLong);
}
}else {
//
RedisUtil.set(RedisConst.ALARM_DEVICE_ID + deviceId,null);
}
}
}
package com.makeit.service.platform.alarm.alarmStrategy;
import com.alibaba.fastjson.JSONObject;
import com.makeit.dto.platform.alarm.PlatAlarmCheckDTO;
import com.makeit.dto.platform.alarm.PlatAlarmConfigRespiratoryDTOVO;
import com.makeit.entity.platform.alarm.PlatAlarmConfig;
import com.makeit.entity.platform.alarm.PlatAlarmRecord;
import com.makeit.entity.platform.device.PlatDevice;
import com.makeit.enums.platform.alarm.PlatAlarmConfigEnum;
import com.makeit.enums.redis.RedisConst;
import com.makeit.service.platform.alarm.PlatAlarmRecordService;
import com.makeit.utils.data.convert.JsonUtil;
import com.makeit.utils.redis.RedisUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Date;
@Component
public class BreathAlarm implements IAlarm{
@Autowired
private PlatAlarmRecordService platAlarmRecordService;
private PlatAlarmConfigEnum.AlarmTypeEnum alarmTypeEnum = PlatAlarmConfigEnum.AlarmTypeEnum.BREATHE;
@Override
public boolean support(String alarmType) {
return StringUtils.equals(alarmType,alarmTypeEnum.getValue());
}
@Override
public void createPlatAlarmRecord(PlatAlarmCheckDTO platAlarmCheckDTO) {
PlatAlarmConfig config = platAlarmCheckDTO.getPlatAlarmConfig();
String ruleConfigStr = config.getRuleConfig();
JSONObject properties = platAlarmCheckDTO.getProperties();
PlatDevice platDevice = platAlarmCheckDTO.getPlatDevice();
String deviceId = platDevice.getOriDeviceId();
if (StringUtils.isBlank(ruleConfigStr)) {
return;
}
String personState = (String) properties.get("personState");
//呼吸率
int br = (int) properties.get("br");
PlatAlarmConfigRespiratoryDTOVO ruleConfig = JsonUtil.toObj(ruleConfigStr, PlatAlarmConfigRespiratoryDTOVO.class);
Integer start = ruleConfig.getRespiratoryRateStart();
Integer end = ruleConfig.getRespiratoryRateEnd();
Integer duration = ruleConfig.getDuration();
long endLong = new Date().getTime();
//计数
Long startLong = RedisUtil.get(RedisConst.ALARM_DEVICE_ID + deviceId);
if (StringUtils.equals(personState, "0")|| startLong == null) {
RedisUtil.set(RedisConst.ALARM_DEVICE_ID + deviceId,endLong/1000);
return;
}
long count = endLong - startLong;
if (br > end || br < start) {
if (count >= duration) {
platAlarmCheckDTO.getParam().add("呼吸状态111");
PlatAlarmRecord platAlarmRecord = platAlarmRecordService.createPlatAlarmRecord(platAlarmCheckDTO);
platAlarmRecordService.noticeDeviceAlarm(platAlarmCheckDTO.getPlatAlarmConfig(),platAlarmRecord);
RedisUtil.set(RedisConst.ALARM_DEVICE_ID + deviceId,endLong/1000);
}
} else {
RedisUtil.set(RedisConst.ALARM_DEVICE_ID + deviceId,endLong/1000);
}
return;
}
}
package com.makeit.service.platform.alarm.alarmStrategy;
import com.alibaba.fastjson.JSONObject;
import com.makeit.dto.platform.alarm.PlatAlarmCheckDTO;
import com.makeit.entity.platform.alarm.PlatAlarmRecord;
import com.makeit.enums.platform.alarm.PlatAlarmConfigEnum;
import com.makeit.service.platform.alarm.PlatAlarmRecordService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class FallAlarm implements IAlarm{
@Autowired
private PlatAlarmRecordService platAlarmRecordService;
private PlatAlarmConfigEnum.AlarmTypeEnum alarmTypeEnum = PlatAlarmConfigEnum.AlarmTypeEnum.FALL;
@Override
public boolean support(String alarmType) {
return StringUtils.equals(alarmType,alarmTypeEnum.getValue());
}
@Override
public void createPlatAlarmRecord(PlatAlarmCheckDTO platAlarmCheckDTO) {
JSONObject properties = platAlarmCheckDTO.getProperties();
String personState = (String) properties.get("personState");
if (StringUtils.equals(personState, "1")) {
platAlarmCheckDTO.getParam().add("房间名111");
PlatAlarmRecord platAlarmRecord = platAlarmRecordService.createPlatAlarmRecord(platAlarmCheckDTO);
platAlarmRecordService.noticeDeviceAlarm(platAlarmCheckDTO.getPlatAlarmConfig(),platAlarmRecord);
}
}
}
package com.makeit.service.platform.alarm.alarmStrategy;
import com.alibaba.fastjson.JSONObject;
import com.makeit.dto.platform.alarm.PlatAlarmCheckDTO;
import com.makeit.dto.platform.alarm.PlatAlarmConfigHeartDTOVO;
import com.makeit.entity.platform.alarm.PlatAlarmConfig;
import com.makeit.entity.platform.alarm.PlatAlarmRecord;
import com.makeit.entity.platform.device.PlatDevice;
import com.makeit.enums.platform.alarm.PlatAlarmConfigEnum;
import com.makeit.enums.redis.RedisConst;
import com.makeit.service.platform.alarm.PlatAlarmRecordService;
import com.makeit.utils.data.convert.JsonUtil;
import com.makeit.utils.redis.RedisUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Date;
@Component
public class HeartAlarm implements IAlarm {
@Autowired
private PlatAlarmRecordService platAlarmRecordService;
private PlatAlarmConfigEnum.AlarmTypeEnum alarmTypeEnum = PlatAlarmConfigEnum.AlarmTypeEnum.HEART;
@Override
public boolean support(String alarmType) {
return StringUtils.equals(alarmType,alarmTypeEnum.getValue());
}
@Override
public void createPlatAlarmRecord(PlatAlarmCheckDTO platAlarmCheckDTO) {
PlatAlarmConfig config = platAlarmCheckDTO.getPlatAlarmConfig();
String ruleConfigStr = config.getRuleConfig();
JSONObject properties = platAlarmCheckDTO.getProperties();
PlatDevice platDevice = platAlarmCheckDTO.getPlatDevice();
String deviceId = platDevice.getOriDeviceId();
if (StringUtils.isBlank(ruleConfigStr)) {
return;
}
String personState = (String) properties.get("personState");
//心率
int hr = (int) properties.get("hr");
PlatAlarmConfigHeartDTOVO ruleConfig = JsonUtil.toObj(ruleConfigStr, PlatAlarmConfigHeartDTOVO.class);
Integer start = ruleConfig.getHeartRateStart();
Integer end = ruleConfig.getHeartRateEnd();
Integer duration = ruleConfig.getDuration();
long endLong = new Date().getTime();
//计数
Long startLong = RedisUtil.get(RedisConst.ALARM_DEVICE_ID + deviceId);
if (StringUtils.equals(personState, "0")|| startLong == null) {
RedisUtil.set(RedisConst.ALARM_DEVICE_ID + deviceId,endLong/1000);
return;
}
long count = endLong - startLong;
if (hr > end || hr < start) {
if (count >= duration) {
platAlarmCheckDTO.getParam().add("呼吸状态111");
PlatAlarmRecord platAlarmRecord =platAlarmRecordService. createPlatAlarmRecord(platAlarmCheckDTO);
platAlarmRecordService.noticeDeviceAlarm(platAlarmCheckDTO.getPlatAlarmConfig(),platAlarmRecord);
RedisUtil.set(RedisConst.ALARM_DEVICE_ID + deviceId,endLong/1000);
}
} else {
RedisUtil.set(RedisConst.ALARM_DEVICE_ID + deviceId,endLong/1000);
}
}
}
package com.makeit.service.platform.alarm.alarmStrategy;
import com.makeit.dto.platform.alarm.PlatAlarmCheckDTO;
public interface IAlarm {
boolean support(String alarmType);
void createPlatAlarmRecord(PlatAlarmCheckDTO platAlarmCheckDTO);
}
......@@ -7,11 +7,16 @@ import com.makeit.common.entity.BaseBusEntity;
import com.makeit.common.entity.BaseEntity;
import com.makeit.common.page.PageReqDTO;
import com.makeit.common.page.PageVO;
import com.makeit.dto.platform.alarm.PlatAlarmCheckDTO;
import com.makeit.dto.platform.alarm.PlatAlarmRecordQueryDTO;
import com.makeit.entity.platform.alarm.PlatAlarmConfig;
import com.makeit.entity.platform.alarm.PlatAlarmRecord;
import com.makeit.entity.platform.auth.PlatUser;
import com.makeit.entity.platform.device.PlatDevice;
import com.makeit.entity.platform.elder.PlatElder;
import com.makeit.entity.platform.elder.PlatElderChildrenInfo;
import com.makeit.entity.platform.space.PlatRoom;
import com.makeit.entity.platform.space.PlatRoomBedDevice;
import com.makeit.enums.CommonEnum;
import com.makeit.exception.BusinessException;
import com.makeit.global.aspect.tenant.TenantIdIgnore;
......@@ -20,6 +25,9 @@ import com.makeit.service.platform.alarm.PlatAlarmConfigService;
import com.makeit.service.platform.alarm.PlatAlarmRecordService;
import com.makeit.service.platform.auth.PlatUserService;
import com.makeit.service.platform.elder.PlatElderChildrenInfoService;
import com.makeit.service.platform.elder.PlatElderService;
import com.makeit.service.platform.space.PlatRoomBedDeviceService;
import com.makeit.service.platform.space.PlatRoomService;
import com.makeit.utils.data.convert.BeanDtoVoUtils;
import com.makeit.utils.data.convert.PageUtil;
import com.makeit.utils.msg.MsgSendUtil;
......@@ -40,9 +48,12 @@ import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
/**
......@@ -65,6 +76,14 @@ public class PlatAlarmRecordServiceImpl extends ServiceImpl<PlatAlarmRecordMappe
@Autowired
private MsgSendUtil msgUtil;
@Autowired
private PlatRoomBedDeviceService platRoomBedDeviceService;
@Autowired
private PlatElderService platElderService;
@Autowired
private PlatRoomService platRoomService;
@Override
public PageVO<PlatAlarmRecordVO> page(PageReqDTO<PlatAlarmRecordQueryDTO> dto) {
PlatAlarmRecordQueryDTO param = dto.getData();
......@@ -276,5 +295,78 @@ public class PlatAlarmRecordServiceImpl extends ServiceImpl<PlatAlarmRecordMappe
}
}
@Override
public PlatAlarmRecord createPlatAlarmRecord(PlatAlarmCheckDTO platAlarmCheckDTO) {
getElderListByDeviceId(platAlarmCheckDTO);
PlatAlarmConfig config = platAlarmCheckDTO.getPlatAlarmConfig();
PlatDevice platDevice = platAlarmCheckDTO.getPlatDevice();
List<String> param = platAlarmCheckDTO.getParam();
List<PlatElder> elderList = platAlarmCheckDTO.getPlatElderList();
String elderNameJoin = elderList.stream().map(PlatElder::getName).collect(Collectors.joining(" "));
PlatAlarmRecord platAlarmRecord = new PlatAlarmRecord();
platAlarmRecord.setAlarmId(config.getId());
platAlarmRecord.setAlarmType(config.getAlarmType());
platAlarmRecord.setAlarmDate(new Date());
platAlarmRecord.setStatus("0");
//todo
platAlarmRecord.setContent(replaceParam(elderNameJoin, param));
platAlarmRecord.setOrgId(platDevice.getOrgId());
platAlarmRecord.setNotifyUser(config.getNotifyUser());
platAlarmRecord.setDeviceId(platDevice.getId());
platAlarmRecord.setElderIds(elderList.stream().map(BaseEntity::getId).collect(Collectors.joining(",")));
platAlarmRecord.setTenantId(config.getTenantId());
return platAlarmRecord;
}
private PlatAlarmCheckDTO getElderListByDeviceId(PlatAlarmCheckDTO platAlarmCheckDTO) {
PlatDevice platDevice = platAlarmCheckDTO.getPlatDevice();
LambdaQueryWrapper<PlatRoomBedDevice> roomBedDeviceLambdaQueryWrapper = new LambdaQueryWrapper<>();
roomBedDeviceLambdaQueryWrapper.eq(PlatRoomBedDevice::getDeviceId, platDevice.getId());
PlatRoomBedDevice platRoomBedDevice = platRoomBedDeviceService.getOne(roomBedDeviceLambdaQueryWrapper, false);
PlatRoom platRoom = platRoomService.getById(platRoomBedDevice.getRoomId());
platAlarmCheckDTO.setPlatRoom(platRoom);
if (platRoomBedDevice == null) {
return platAlarmCheckDTO;
}
String bedId = platRoomBedDevice.getBedId();
if (StringUtils.isNotBlank(bedId)) {
LambdaQueryWrapper<PlatElder> elderLambdaQueryWrapper = new LambdaQueryWrapper<>();
elderLambdaQueryWrapper.eq(PlatElder::getBedId, bedId);
List<PlatElder> list = platElderService.list(elderLambdaQueryWrapper);
platAlarmCheckDTO.setPlatElderList(list);
return platAlarmCheckDTO;
}
String roomId = platRoomBedDevice.getRoomId();
if (StringUtils.isNotBlank(roomId)) {
LambdaQueryWrapper<PlatElder> elderLambdaQueryWrapper = new LambdaQueryWrapper<>();
elderLambdaQueryWrapper.eq(PlatElder::getRoomId, roomId);
List<PlatElder> list = platElderService.list(elderLambdaQueryWrapper);
platAlarmCheckDTO.setPlatElderList(list);
return platAlarmCheckDTO;
}
return platAlarmCheckDTO;
}
private String replaceParam(String oriContent,List<String> param) {
Pattern p = Pattern.compile("\\[#\\d+\\]|\\[#[\\p{IsHan}]+\\]|\\[#[^\\]]*\\]");
Matcher m = p.matcher(oriContent);
StringBuffer sb = new StringBuffer();
if (!m.find()) {
return oriContent;
}
m.reset();
int i = 0;
while (m.find()) {
m.appendReplacement(sb, param.get(i));
i++;
}
m.appendTail(sb);
return sb.toString();
}
}
......@@ -49,11 +49,4 @@ public interface PlatDeviceService extends IService<PlatDevice> {
* @return
*/
PlatDeviceDetailDTO getDetailDTO(String deviceId);
/**
* 编辑
*
* @param platDevice
*/
void edit(PlatDevice platDevice);
}
......@@ -2,8 +2,6 @@ package com.makeit.service.platform.device.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.makeit.common.entity.BaseBusEntity;
......@@ -25,6 +23,7 @@ import com.makeit.mapper.platform.device.PlatDeviceMapper;
import com.makeit.service.platform.auth.PlatOrgService;
import com.makeit.service.platform.device.PlatDeviceOtherService;
import com.makeit.service.platform.device.PlatDeviceService;
import com.makeit.utils.DeviceCacheUtil;
import com.makeit.utils.data.convert.BeanDtoVoUtils;
import com.makeit.utils.data.convert.PageUtil;
import com.makeit.utils.data.convert.StreamUtil;
......@@ -55,6 +54,8 @@ public class PlatDeviceServiceImpl extends ServiceImpl<PlatDeviceMapper, PlatDev
@Autowired
private PlatOrgService platOrgService;
@Autowired
private DeviceCacheUtil deviceCacheUtil;
@Override
public PageVO<PlatDeviceListVO> page(PageReqDTO<PlatDeviceQueryDTO> pageReqDTO) {
......@@ -105,6 +106,8 @@ public class PlatDeviceServiceImpl extends ServiceImpl<PlatDeviceMapper, PlatDev
updateById(db);
deviceCacheUtil.put(db);
}
@Override
......@@ -144,6 +147,8 @@ public class PlatDeviceServiceImpl extends ServiceImpl<PlatDeviceMapper, PlatDev
updateById(db);
deviceCacheUtil.put(db);
platDeviceOtherService.saveOrUpdate(other);
......@@ -169,6 +174,8 @@ public class PlatDeviceServiceImpl extends ServiceImpl<PlatDeviceMapper, PlatDev
updateById(db);
deviceCacheUtil.put(db);
platDeviceOtherService.saveOrUpdate(other);
}
......@@ -214,20 +221,4 @@ public class PlatDeviceServiceImpl extends ServiceImpl<PlatDeviceMapper, PlatDev
.orderByDesc(BaseEntity::getUpdateDate);
}
/**
* 编辑
*
* @param platDevice
*/
@Override
@Transactional
public void edit(PlatDevice platDevice) {
LambdaUpdateWrapper<PlatDevice> updateWrapper = Wrappers.lambdaUpdate(PlatDevice.class)
.eq(BaseEntity::getId, platDevice.getId())
.set(PlatDevice::getOriDeviceId, platDevice.getOriDeviceId())
.set(PlatDevice::getName, platDevice.getName())
.set(PlatDevice::getProductName, platDevice.getProductName())
.set(PlatDevice::getDescription, platDevice.getDescription());
this.update(updateWrapper);
}
}
......@@ -14,6 +14,7 @@ import com.makeit.module.system.service.SysDictionaryCategoryService;
import com.makeit.module.system.vo.DictionaryVo;
import com.makeit.service.platform.device.PlatDeviceService;
import com.makeit.service.saas.PlatTenantService;
import com.makeit.utils.DeviceCacheUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
......@@ -41,6 +42,8 @@ public class IotSyncTask {
private PlatDeviceService platDeviceService;
@Autowired
private SysDictionaryCategoryService sysDictionaryCategoryService;
@Autowired
private DeviceCacheUtil deviceCacheUtil;
/**
* 一小时同步一次
......@@ -75,6 +78,8 @@ public class IotSyncTask {
//更新平台设备
Collection<PlatDevice> platDevices = convertToPlatDevice(iotDeviceList, deviceList, platTenant.getId(), dicNameIdMap);
platDeviceService.saveOrUpdateBatch(platDevices);
deviceCacheUtil.putAll(platDevices);
}
log.info("结束执行同步设备信息接口");
......
......@@ -3,8 +3,10 @@ package com.makeit.utils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.makeit.entity.platform.alarm.PlatAlarmConfig;
import com.makeit.enums.CommonEnum;
import com.makeit.enums.redis.RedisConst;
import com.makeit.global.aspect.tenant.TenantIdIgnore;
import com.makeit.service.platform.alarm.PlatAlarmConfigService;
import com.makeit.utils.redis.RedisUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
......@@ -12,30 +14,30 @@ import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
@Component
public class AlarmConfigCacheUtil implements ApplicationRunner {
/**
* key orgId|type
* todo 放redis
* key orgId:type
*/
private static final ConcurrentHashMap<String, PlatAlarmConfig> CACHE = new ConcurrentHashMap<>(16);
@Autowired
private PlatAlarmConfigService platAlarmConfigService;
public List<PlatAlarmConfig> getAll() {
List<PlatAlarmConfig> list = platAlarmConfigService.list(new LambdaQueryWrapper<PlatAlarmConfig>().eq(PlatAlarmConfig::getStatus, CommonEnum.YES.getValue()));
list.stream().forEach(vo -> {
put(vo);
});
return list;
}
public void put(PlatAlarmConfig alarmConfig) {
if (StringUtils.equals(CommonEnum.YES.getValue(),alarmConfig.getStatus())) {
CACHE.put(alarmConfig.getOrgId() + "|" + alarmConfig.getAlarmType(),alarmConfig);
RedisUtil.set(RedisConst.ALARM_CONFIG_ORG_ID+alarmConfig.getOrgId() + ":" + alarmConfig.getAlarmType(),alarmConfig);
} else {
CACHE.remove(alarmConfig.getOrgId() + "|" + alarmConfig.getAlarmType());
RedisUtil.delete(RedisConst.ALARM_CONFIG_ORG_ID+alarmConfig.getOrgId() + ":" + alarmConfig.getAlarmType());
}
}
......@@ -46,7 +48,7 @@ public class AlarmConfigCacheUtil implements ApplicationRunner {
}
public PlatAlarmConfig get(String orgIdType) {
return CACHE.get(orgIdType);
return RedisUtil.get(RedisConst.ALARM_CONFIG_ORG_ID+orgIdType);
}
......@@ -59,9 +61,7 @@ public class AlarmConfigCacheUtil implements ApplicationRunner {
@Override
@TenantIdIgnore
public void run(ApplicationArguments args) throws Exception {
List<PlatAlarmConfig> list = getAll();
list.stream().forEach(vo -> {
put(vo);
});
getAll();
}
}
......@@ -2,46 +2,49 @@ package com.makeit.utils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.makeit.entity.platform.device.PlatDevice;
import com.makeit.enums.redis.RedisConst;
import com.makeit.global.aspect.tenant.TenantIdIgnore;
import com.makeit.service.platform.device.PlatDeviceService;
import com.makeit.utils.redis.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
@Component
public class DeviceCacheUtil implements ApplicationRunner {
/**
* key
* todo 放redis
*/
private static final ConcurrentHashMap<String, PlatDevice> CACHE = new ConcurrentHashMap<>(16);
@Autowired
private PlatDeviceService platDeviceService;
public List<PlatDevice> getAll() {
List<PlatDevice> list = platDeviceService.list(new LambdaQueryWrapper<PlatDevice>());
list.stream().forEach(vo -> {
put(vo);
});
return list;
}
public void put(PlatDevice platDevice) {
CACHE.put(platDevice.getOriDeviceId(), platDevice);
RedisUtil.set(RedisConst.PLAT_IOT_DEVICE_PREFIX+platDevice.getOriDeviceId(), platDevice);
}
public void putAll(List<PlatDevice> platDevices) {
platDevices.forEach(vo -> {
public PlatDevice get(String oriDeviceId) {
PlatDevice platDevice = RedisUtil.get(RedisConst.PLAT_IOT_DEVICE_PREFIX + oriDeviceId);
if(platDevice==null){
getAll();
}
return RedisUtil.get(RedisConst.PLAT_IOT_DEVICE_PREFIX + oriDeviceId);
}
public void putAll(Collection<PlatDevice> platDeviceList) {
platDeviceList.forEach(vo -> {
put(vo);
});
}
public PlatDevice get(String oriDeviceId) {
return CACHE.get(oriDeviceId);
}
/**
* Callback used to run the bean.
*
......@@ -51,9 +54,7 @@ public class DeviceCacheUtil implements ApplicationRunner {
@Override
@TenantIdIgnore
public void run(ApplicationArguments args) throws Exception {
List<PlatDevice> list = getAll();
list.stream().forEach(vo -> {
put(vo);
});
getAll();
}
}
......@@ -2,30 +2,15 @@ package com.makeit.mqtt;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.makeit.common.entity.BaseEntity;
import com.makeit.dto.platform.alarm.PlatAlaramCheckDTO;
import com.makeit.dto.platform.alarm.PlatAlarmConfigBehaviorDTOVO;
import com.makeit.dto.platform.alarm.PlatAlarmConfigHeartDTOVO;
import com.makeit.dto.platform.alarm.PlatAlarmConfigRespiratoryDTOVO;
import com.makeit.dto.platform.alarm.PlatAlarmCheckDTO;
import com.makeit.entity.platform.alarm.PlatAlarmConfig;
import com.makeit.entity.platform.alarm.PlatAlarmRecord;
import com.makeit.entity.platform.device.PlatDevice;
import com.makeit.entity.platform.elder.PlatElder;
import com.makeit.entity.platform.space.PlatRoom;
import com.makeit.entity.platform.space.PlatRoomBedDevice;
import com.makeit.enums.platform.alarm.PlatAlarmConfigEnum;
import com.makeit.enums.platform.device.PlatDeviceEnum;
import com.makeit.enums.redis.RedisConst;
import com.makeit.module.iot.vo.DeviceInfo;
import com.makeit.service.platform.alarm.PlatAlarmRecordService;
import com.makeit.service.platform.elder.PlatElderService;
import com.makeit.service.platform.space.PlatRoomBedDeviceService;
import com.makeit.service.platform.space.PlatRoomService;
import com.makeit.service.platform.alarm.alarmStrategy.IAlarm;
import com.makeit.utils.AlarmConfigCacheUtil;
import com.makeit.utils.DeviceCacheUtil;
import com.makeit.utils.data.convert.JsonUtil;
import com.makeit.utils.redis.RedisUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
......@@ -38,12 +23,7 @@ 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;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
@Component
......@@ -55,13 +35,12 @@ public class PushCallback implements MqttCallback {
private static MqttClient client;
//todo 放到缓存
@Autowired
private PlatAlarmRecordService platAlarmRecordService;
@Autowired
private AlarmConfigCacheUtil alarmConfigCacheUtil;
@Autowired
private DeviceCacheUtil deviceCacheUtil;
@Autowired
private List<IAlarm> alarmList;
@Override
......@@ -95,7 +74,7 @@ public class PushCallback implements MqttCallback {
public void checkAlarm(DeviceInfo device) {
String deviceId = device.getDeviceId();
String propertiesStr = device.getProperties();
if(StringUtils.isBlank(propertiesStr)){
if (StringUtils.isBlank(propertiesStr)) {
return;
}
JSONObject properties = JSON.parseObject(propertiesStr);
......@@ -106,164 +85,22 @@ public class PushCallback implements MqttCallback {
if (CollectionUtils.isEmpty(deviceAlarmConfigList)) {
return;
}
//RedissonClient redissonClient = RedisUtil.getClient();
for (PlatAlarmConfig config : deviceAlarmConfigList) {
String alarmType = config.getAlarmType();
String ruleConfigStr = config.getRuleConfig();
//跌倒
if (StringUtils.equals(alarmType, PlatAlarmConfigEnum.AlarmTypeEnum.FALL.getValue())) {
String personState = (String) properties.get("personState");
if (StringUtils.equals(personState, "1")) {
PlatAlarmRecord platAlarmRecord = createFallPlatAlarmRecord(platDevice, config);
PlatAlarmCheckDTO platAlarmCheckDTO = new PlatAlarmCheckDTO();
platAlarmCheckDTO.setPlatAlarmConfig(config);
platAlarmCheckDTO.setPlatDevice(platDevice);
platAlarmCheckDTO.setProperties(properties);
for (IAlarm alarm : alarmList) {
if (alarm.support(alarmType)) {
alarm.createPlatAlarmRecord(platAlarmCheckDTO);
}
}
if (StringUtils.isBlank(ruleConfigStr)) {
continue;
}
//呼吸
if (StringUtils.equals(alarmType, PlatAlarmConfigEnum.AlarmTypeEnum.BREATHE.getValue())) {
String personState = (String) properties.get("personState");
//呼吸率
int br = (int) properties.get("br");
PlatAlarmConfigRespiratoryDTOVO ruleConfig = JsonUtil.toObj(ruleConfigStr, PlatAlarmConfigRespiratoryDTOVO.class);
Integer start = ruleConfig.getRespiratoryRateStart();
Integer end = ruleConfig.getRespiratoryRateEnd();
Integer duration = ruleConfig.getDuration();
long endLong = new Date().getTime();
//计数
Long startLong = RedisUtil.get(RedisConst.ALARM_DEVICE_ID + deviceId);
if (StringUtils.equals(personState, "0")|| startLong == null) {
RedisUtil.set(RedisConst.ALARM_DEVICE_ID + deviceId,endLong/1000);
continue;
}
long count = endLong - startLong;
if (br > end || br < start) {
if (count >= duration) {
PlatAlarmRecord platAlarmRecord = createRespiratoryPlatAlarmRecord(platDevice, config);
RedisUtil.set(RedisConst.ALARM_DEVICE_ID + deviceId,endLong/1000);
}
} else {
RedisUtil.set(RedisConst.ALARM_DEVICE_ID + deviceId,endLong/1000);
}
}
//心率
if (StringUtils.equals(alarmType, PlatAlarmConfigEnum.AlarmTypeEnum.HEART.getValue())) {
String personState = (String) properties.get("personState");
//心率
int hr = (int) properties.get("hr");
PlatAlarmConfigHeartDTOVO ruleConfig = JsonUtil.toObj(ruleConfigStr, PlatAlarmConfigHeartDTOVO.class);
Integer start = ruleConfig.getHeartRateStart();
Integer end = ruleConfig.getHeartRateEnd();
Integer duration = ruleConfig.getDuration();
long endLong = new Date().getTime();
//计数
Long startLong = RedisUtil.get(RedisConst.ALARM_DEVICE_ID + deviceId);
if (StringUtils.equals(personState, "0")|| startLong == null) {
RedisUtil.set(RedisConst.ALARM_DEVICE_ID + deviceId,endLong/1000);
continue;
}
long count = endLong - startLong;
if (hr > end || hr < start) {
if (count >= duration) {
PlatAlarmRecord platAlarmRecord = createHeartPlatAlarmRecord(platDevice, config);
RedisUtil.set(RedisConst.ALARM_DEVICE_ID + deviceId,endLong/1000);
}
} else {
RedisUtil.set(RedisConst.ALARM_DEVICE_ID + deviceId,endLong/1000);
}
}
//行为
if (StringUtils.equals(alarmType, PlatAlarmConfigEnum.AlarmTypeEnum.BEHAVIOR.getValue())) {
String personState = (String) properties.get("personState");
PlatAlarmConfigBehaviorDTOVO ruleConfig = JsonUtil.toObj(ruleConfigStr, PlatAlarmConfigBehaviorDTOVO.class);
Integer duration = ruleConfig.getDuration();
duration = duration*60;
//todo duration+平均停留时长
List<String> personStateList = Arrays.asList("1", "2", "3");
//有人
//计数
long endLong = new Date().getTime();
Long startLong = RedisUtil.get(RedisConst.ALARM_DEVICE_ID + deviceId);
if (personStateList.contains(personState)) {
long count = endLong - startLong;
if(startLong == null ){
RedisUtil.set(RedisConst.ALARM_DEVICE_ID + deviceId,endLong);
continue;
}
if (count >= duration) {
PlatAlarmRecord platAlarmRecord = createBehaviorPlatAlarmRecord(platDevice, config);
RedisUtil.set(RedisConst.ALARM_DEVICE_ID + deviceId,startLong);
}
}else {
//
RedisUtil.set(RedisConst.ALARM_DEVICE_ID + deviceId,null);
}
}
// else { //没人 更新停留时长
// long stayLong = atomicLong.get();
// //保存
// /**
// * 老人id
// * 日期
// * 区域名称
// * 当天总时长
// * 当天最后一次进入该区域的开始时间
// * 当天最后一次进入该区域的结束时间
// * 当天最后一次进入该区域的时长
// */
// }
//todo
}
}
private PlatAlarmRecord createFallPlatAlarmRecord(PlatDevice platDevice, PlatAlarmConfig config) {
PlatAlarmRecord platAlarmRecord = createPlatAlarmRecord(platDevice, config, "房间名111");
platAlarmRecordService.noticeDeviceAlarm(config,platAlarmRecord);
return platAlarmRecord;
}
private PlatAlarmRecord createRespiratoryPlatAlarmRecord(PlatDevice platDevice, PlatAlarmConfig config) {
PlatAlarmRecord platAlarmRecord = createPlatAlarmRecord(platDevice, config, "呼吸状态111");
platAlarmRecordService.noticeDeviceAlarm(config,platAlarmRecord);
return platAlarmRecord;
}
private PlatAlarmRecord createHeartPlatAlarmRecord(PlatDevice platDevice, PlatAlarmConfig config) {
PlatAlarmRecord platAlarmRecord = createPlatAlarmRecord(platDevice, config, "呼吸状态111");
platAlarmRecordService.noticeDeviceAlarm(config,platAlarmRecord);
return platAlarmRecord;
}
private PlatAlarmRecord createBehaviorPlatAlarmRecord(PlatDevice platDevice, PlatAlarmConfig config) {
PlatAlarmRecord platAlarmRecord = createPlatAlarmRecord(platDevice, config, "房间名");
platAlarmRecordService.noticeDeviceAlarm(config,platAlarmRecord);
return platAlarmRecord;
}
private PlatAlarmRecord createPlatAlarmRecord(PlatDevice platDevice, PlatAlarmConfig config, String... param) {
PlatAlaramCheckDTO platAlaramCheckDTO = getElderListByDeviceId(platDevice.getId());
List<PlatElder> elderList = platAlaramCheckDTO.getPlatElderList();
String elderNameJoin = elderList.stream().map(PlatElder::getName).collect(Collectors.joining(" "));
PlatAlarmRecord platAlarmRecord = new PlatAlarmRecord();
platAlarmRecord.setAlarmId(config.getId());
platAlarmRecord.setAlarmType(config.getAlarmType());
platAlarmRecord.setAlarmDate(new Date());
platAlarmRecord.setStatus("0");
//todo
platAlarmRecord.setContent(replaceParam(elderNameJoin, param));
platAlarmRecord.setOrgId(platDevice.getOrgId());
platAlarmRecord.setNotifyUser(config.getNotifyUser());
platAlarmRecord.setDeviceId(platDevice.getId());
platAlarmRecord.setElderIds(elderList.stream().map(BaseEntity::getId).collect(Collectors.joining(",")));
platAlarmRecord.setTenantId(config.getTenantId());
return platAlarmRecord;
}
public List<PlatAlarmConfig> getDeviceAlarmConfigMap(PlatDevice platDevice) {
if (StringUtils.equals(platDevice.getCategory(), PlatDeviceEnum.CategoryEnum.HEART.getValue())) {
List<PlatAlarmConfig> set = new ArrayList<>();
......@@ -280,64 +117,5 @@ public class PushCallback implements MqttCallback {
}
return new ArrayList<>();
}
private String replaceParam(String oriContent, String... param) {
Pattern p = Pattern.compile("\\[#\\d+\\]|\\[#[\\p{IsHan}]+\\]|\\[#[^\\]]*\\]");
Matcher m = p.matcher(oriContent);
StringBuffer sb = new StringBuffer();
if (!m.find()) {
return oriContent;
}
m.reset();
int i = 0;
while (m.find()) {
m.appendReplacement(sb, param[i]);
i++;
}
m.appendTail(sb);
return sb.toString();
}
@Autowired
private PlatRoomBedDeviceService platRoomBedDeviceService;
@Autowired
private PlatElderService platElderService;
@Autowired
private PlatRoomService platRoomService;
private PlatAlaramCheckDTO getElderListByDeviceId(String deviceId) {
PlatAlaramCheckDTO result = new PlatAlaramCheckDTO();
LambdaQueryWrapper<PlatRoomBedDevice> roomBedDeviceLambdaQueryWrapper = new LambdaQueryWrapper<>();
roomBedDeviceLambdaQueryWrapper.eq(PlatRoomBedDevice::getDeviceId, deviceId);
PlatRoomBedDevice platRoomBedDevice = platRoomBedDeviceService.getOne(roomBedDeviceLambdaQueryWrapper, false);
PlatRoom platRoom = platRoomService.getById(platRoomBedDevice.getRoomId());
result.setPlatRoom(platRoom);
if (platRoomBedDevice == null) {
return result;
}
String bedId = platRoomBedDevice.getBedId();
if (StringUtils.isNotBlank(bedId)) {
LambdaQueryWrapper<PlatElder> elderLambdaQueryWrapper = new LambdaQueryWrapper<>();
elderLambdaQueryWrapper.eq(PlatElder::getBedId, bedId);
List<PlatElder> list = platElderService.list(elderLambdaQueryWrapper);
result.setPlatElderList(list);
return result;
}
String roomId = platRoomBedDevice.getRoomId();
if (StringUtils.isNotBlank(roomId)) {
LambdaQueryWrapper<PlatElder> elderLambdaQueryWrapper = new LambdaQueryWrapper<>();
elderLambdaQueryWrapper.eq(PlatElder::getRoomId, roomId);
List<PlatElder> list = platElderService.list(elderLambdaQueryWrapper);
result.setPlatElderList(list);
return result;
}
return result;
}
public static void main(String[] args) {
Date date = new Date();
System.out.println(date.toInstant());
}
}
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