Commit 36535723 by 李小龙

fixbug:支持一台设备绑定多个租户

parent 26762860
......@@ -11,6 +11,7 @@ import com.makeit.entity.platform.device.PlatDevice;
import com.makeit.global.aspect.tenant.TenantIdIgnore;
import com.makeit.service.platform.device.PlatDeviceService;
import com.makeit.service.saas.SaasOperationLogService;
import com.makeit.vo.platform.device.PlatDeviceListVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -31,7 +32,7 @@ public class SaasDeviceController {
@ApiOperation("列表")
@PostMapping("page")
@TenantIdIgnore
public ApiResponseEntity<PageVO<PlatDevice>> page(@RequestBody PageReqDTO<PlatDevice> pageReqDTO) {
public ApiResponseEntity<PageVO<PlatDeviceListVO>> page(@RequestBody PageReqDTO<PlatDevice> pageReqDTO) {
return ApiResponseUtils.success(platDeviceService.pageSaas(pageReqDTO));
}
......
......@@ -76,7 +76,7 @@ public class BehaviorAlarm implements IAlarm {
Integer duration = ruleConfig.getDuration();//分钟
PlatDayDurationRecord platDayDurationRecord = dayDurationUtil.get(platDevice.getOriDeviceId());
if (platDayDurationRecord == null) {
log.error("行为告警未找到行为异常平均时长,设备id:" + platDevice.getId());
log.error("行为告警未找到行为异常平均时长,设备plat_id:" + platDevice.getId());
return;
}
Long duration1 = platDayDurationRecord.getDuration();
......@@ -99,13 +99,13 @@ public class BehaviorAlarm implements IAlarm {
alarmRedisDTO.setStartLong(endLong);
alarmRedisDTO.setStart(now);
RedisUtil.set(RedisConst.ALARM_DEVICE_BEHAVIOR_ID + deviceId, alarmRedisDTO);
log.error("空间雷达上报进入房间,设备id:" + platDevice.getId());
log.error("空间雷达上报进入房间,设备plat_id:" + platDevice.getId());
return;
}
long count = endLong - startLong;
//进入空间时间满足告警时长,判断是否已经告警过了
if (count / 1000 >= duration && StringUtils.equals(alarmRedisDTO.getAlarm(), CommonEnum.NO.getValue())) {
log.error("空间雷达发出告警,设备id:" + platDevice.getId());
log.error("空间雷达发出告警,设备plat_id:" + platDevice.getId());
notice(platAlarmCheckDTO);
alarmRedisDTO.setAlarm(CommonEnum.YES.getValue());
RedisUtil.set(RedisConst.ALARM_DEVICE_BEHAVIOR_ID + deviceId, alarmRedisDTO);
......
......@@ -85,14 +85,14 @@ public class BreathAlarm implements IAlarm {
alarmRedisDTO.setStart(now);
alarmRedisDTO.setStartLong(endLong);
RedisUtil.set(RedisConst.ALARM_DEVICE_BR_ID + deviceId,alarmRedisDTO);
log.error("发现长者呼吸异常,设备id:"+deviceId);
log.error("发现长者呼吸异常,设备plat_id:"+deviceId);
return;
}
Long startLong = alarmRedisDTO.getStartLong();
long count = endLong - startLong;
if (count/1000 >= duration) {
if (StringUtils.equals(alarmRedisDTO.getAlarm(), CommonEnum.YES.getValue())) {
log.error("呼吸已告警,设备id:" + platDevice.getId());
log.error("呼吸已告警,设备plat_id:" + platDevice.getId());
return;
}
fillRemark(platAlarmCheckDTO, br, start, end);
......
......@@ -66,7 +66,7 @@ public class FallAlarm implements IAlarm {
platAlarmRecordService.getElderListByDeviceId(platAlarmCheckDTO);
List<PlatElder> platElderList = platAlarmCheckDTO.getPlatElderList();
if (CollectionUtils.isEmpty(platElderList)) {
log.error("跌倒设备未关联长者,设备id:" + platDevice.getId());
log.error("跌倒设备未关联长者,设备plat_id:" + platDevice.getId());
return;
}
PlatRoom platRoom = platAlarmCheckDTO.getPlatRoom();
......
......@@ -75,7 +75,7 @@ public class HeartAlarm implements IAlarm {
alarmRedisDTO.setStart(now);
alarmRedisDTO.setStartLong(endLong);
RedisUtil.set(RedisConst.ALARM_DEVICE_HR_ID + deviceId,alarmRedisDTO);
log.error("发现长者心率异常,设备id:"+deviceId);
log.error("发现长者心率异常,设备iot_id:"+deviceId);
return;
}
Long startLong = alarmRedisDTO.getStartLong();
......@@ -84,7 +84,7 @@ public class HeartAlarm implements IAlarm {
//todo 如果每台服务器都能收到上报消息 加redis锁
//RLock lock = RedisLockUtil.lock(RedisConst.LOCK_ALARM + deviceId);
if (StringUtils.equals(alarmRedisDTO.getAlarm(), CommonEnum.YES.getValue())) {
log.error("心率已告警,设备id:" + platDevice.getId());
log.error("心率已告警,设备plat_id:" + platDevice.getId());
return;
}
fillRemark(platAlarmCheckDTO, hr, start, end);
......@@ -116,7 +116,7 @@ public class HeartAlarm implements IAlarm {
PlatDevice platDevice = platAlarmCheckDTO.getPlatDevice();
List<PlatElder> platElderList = platAlarmCheckDTO.getPlatElderList();
if (CollectionUtils.isEmpty(platElderList)) {
log.error("心率设备未关联长者,设备id:" + platAlarmCheckDTO.getPlatDevice().getId());
log.error("心率设备未关联长者,设备plat_id:" + platAlarmCheckDTO.getPlatDevice().getId());
return;
}
for (PlatElder platElder : platElderList) {
......@@ -128,7 +128,7 @@ public class HeartAlarm implements IAlarm {
platAlarmRecord.setElderIds(platElder.getId());
platAlarmRecord.setElderName(platElder.getName());
platAlarmRecordService.noticeDeviceAlarm(platAlarmCheckDTO.getPlatAlarmConfig(), platAlarmRecord);
log.error("长者心率异常,发出告警,设备id:"+platDevice.getId()+", 长者名称:"+platElder.getName());
log.error("长者心率异常,发出告警,设备plat_id:"+platDevice.getId()+", 长者名称:"+platElder.getName());
}
}
}
......@@ -42,7 +42,7 @@ public interface PlatDeviceService extends IService<PlatDevice> {
* @param pageReqDTO
* @return
*/
PageVO<PlatDevice> pageSaas(PageReqDTO<PlatDevice> pageReqDTO);
PageVO<PlatDeviceListVO> pageSaas(PageReqDTO<PlatDevice> pageReqDTO);
/**
* 设备信息
......
......@@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.makeit.common.dto.BaseTenantDTO;
import com.makeit.common.entity.BaseBusEntity;
import com.makeit.common.entity.BaseEntity;
import com.makeit.common.page.PageReqDTO;
......@@ -25,6 +26,7 @@ import com.makeit.module.iot.service.IotDevicePropertiesOperateService;
import com.makeit.service.platform.auth.PlatOrgService;
import com.makeit.service.platform.device.PlatDeviceOtherService;
import com.makeit.service.platform.device.PlatDeviceService;
import com.makeit.service.saas.PlatTenantService;
import com.makeit.utils.DeviceCacheUtil;
import com.makeit.utils.data.convert.BeanDtoVoUtils;
import com.makeit.utils.data.convert.PageUtil;
......@@ -60,6 +62,8 @@ public class PlatDeviceServiceImpl extends ServiceImpl<PlatDeviceMapper, PlatDev
private DeviceCacheUtil deviceCacheUtil;
@Autowired
private IotDevicePropertiesOperateService devicePropertiesOperateService;
@Autowired
private PlatTenantService platTenantService;
@Override
public PageVO<PlatDeviceListVO> page(PageReqDTO<PlatDeviceQueryDTO> pageReqDTO) {
......@@ -195,12 +199,17 @@ public class PlatDeviceServiceImpl extends ServiceImpl<PlatDeviceMapper, PlatDev
}
@Override
public PageVO<PlatDevice> pageSaas(PageReqDTO<PlatDevice> pageReqDTO) {
public PageVO<PlatDeviceListVO> pageSaas(PageReqDTO<PlatDevice> pageReqDTO) {
PlatDevice param = pageReqDTO.getData();
Page<PlatDevice> page = PageUtil.toMpPage(pageReqDTO);
LambdaQueryWrapper<PlatDevice> lambdaQueryWrapper = getLambdaQueryWrapper(param);
Page<PlatDevice> devicePage = page(page, lambdaQueryWrapper);
return PageUtil.toPageVO(devicePage);
List<PlatDevice> records = devicePage.getRecords();
List<PlatDeviceListVO> platDeviceListVOS = BeanDtoVoUtils.listVo(records, PlatDeviceListVO.class);
JoinUtil.join(platDeviceListVOS,platTenantService, BaseTenantDTO::getTenantId,BaseEntity::getId,(d,o)->{
d.setTenantName(o.getName());
});
return PageUtil.toPageVO(platDeviceListVOS,devicePage);
}
@Override
......
......@@ -2,10 +2,12 @@ package com.makeit.utils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.makeit.entity.platform.device.PlatDevice;
import com.makeit.entity.saas.PlatTenant;
import com.makeit.enums.redis.RedisConst;
import com.makeit.global.aspect.tenant.TenantIdIgnore;
import com.makeit.module.iot.enums.DeviceState;
import com.makeit.service.platform.device.PlatDeviceService;
import com.makeit.service.saas.PlatTenantService;
import com.makeit.utils.redis.RedisUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -21,6 +23,8 @@ public class DeviceCacheUtil implements ApplicationRunner {
@Autowired
private PlatDeviceService platDeviceService;
@Autowired
private PlatTenantService platTenantService;
public List<PlatDevice> getAll() {
List<PlatDevice> list = platDeviceService.list(new LambdaQueryWrapper<PlatDevice>().eq(PlatDevice::getStatus,DeviceState.online.getValue()));
......@@ -31,19 +35,20 @@ public class DeviceCacheUtil implements ApplicationRunner {
}
public void put(PlatDevice platDevice) {
PlatTenant platTenant = platTenantService.getById(platDevice.getTenantId());
if(StringUtils.equals(platDevice.getStatus(),DeviceState.online.getValue())) {
RedisUtil.set(RedisConst.PLAT_IOT_DEVICE_PREFIX + platDevice.getOriDeviceId(), platDevice);
RedisUtil.set(RedisConst.PLAT_IOT_DEVICE_PREFIX + platDevice.getOriDeviceId()+":"+platTenant.getIotOrgId(), platDevice);
}else {
RedisUtil.delete(RedisConst.PLAT_IOT_DEVICE_PREFIX + platDevice.getOriDeviceId());
RedisUtil.delete(RedisConst.PLAT_IOT_DEVICE_PREFIX + platDevice.getOriDeviceId()+":"+platTenant.getIotOrgId());
}
}
public PlatDevice get(String oriDeviceId) {
PlatDevice platDevice = RedisUtil.get(RedisConst.PLAT_IOT_DEVICE_PREFIX + oriDeviceId);
public PlatDevice get(String oriDeviceIdAndIotOrgId) {
PlatDevice platDevice = RedisUtil.get(RedisConst.PLAT_IOT_DEVICE_PREFIX + oriDeviceIdAndIotOrgId);
if(platDevice==null){
getAll();
}
return RedisUtil.get(RedisConst.PLAT_IOT_DEVICE_PREFIX + oriDeviceId);
return RedisUtil.get(RedisConst.PLAT_IOT_DEVICE_PREFIX + oriDeviceIdAndIotOrgId);
}
public void putAll(Collection<PlatDevice> platDeviceList) {
platDeviceList.forEach(vo -> {
......
......@@ -74,4 +74,7 @@ public class PlatDeviceListVO extends BaseTenantDTO {
private LocalDateTime endDate;
private String tenantName;
}
......@@ -7,6 +7,7 @@ import com.makeit.entity.platform.alarm.PlatAlarmConfig;
import com.makeit.entity.platform.device.PlatDevice;
import com.makeit.global.aspect.tenant.TenantIdIgnore;
import com.makeit.module.iot.vo.DeviceInfo;
import com.makeit.module.iot.vo.HeaderInfo;
import com.makeit.service.platform.alarm.alarmStrategy.IAlarm;
import com.makeit.utils.AlarmConfigCacheUtil;
import com.makeit.utils.DeviceCacheUtil;
......@@ -79,6 +80,10 @@ public class PushCallback implements MqttCallback {
@TenantIdIgnore
@Async
public void checkAlarm(DeviceInfo device) {
HeaderInfo headers = device.getHeaders();
List<HeaderInfo.Bind> bindings = headers.getBindings();
for (HeaderInfo.Bind binding : bindings) {
String iot_tenantId = binding.getId();
StopWatch stopWatch = new StopWatch();
stopWatch.start("checkAlarm-1");
String deviceId = device.getDeviceId();
......@@ -86,15 +91,15 @@ public class PushCallback implements MqttCallback {
JSONObject properties = device.getProperties();
//iot设备id
PlatDevice platDevice = deviceCacheUtil.get(deviceId);
PlatDevice platDevice = deviceCacheUtil.get(deviceId+":"+iot_tenantId);
if(platDevice==null){
logger.error("设备信息异常,设备iot-id:"+deviceId);
logger.error("获取设备信息异常,设备iot-id:"+deviceId);
return;
}
List<PlatAlarmConfig> deviceAlarmConfigList = alarmConfigCacheUtil.getDeviceAlarmConfigMap(platDevice);
deviceAlarmConfigList.removeIf(Objects::isNull);
if (CollectionUtils.isEmpty(deviceAlarmConfigList)) {
logger.error("该设备没有告警配置:"+deviceId);
logger.error("该设备没有告警配置,设备iot-id:"+deviceId);
return;
}
for (PlatAlarmConfig config : deviceAlarmConfigList) {
......@@ -116,6 +121,8 @@ public class PushCallback implements MqttCallback {
}
stopWatch.stop();
logger.info(stopWatch.prettyPrint());
}
}
......
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