feat(device): 新增报警类型枚举并优化设备指令处理逻辑
- 新增 AlarmTypeEnum 枚举类,定义 SOS 和静止报警类型 -优化 AppAuthController 中版本信息解析逻辑,增强空值处理 - 统一设备指令接口参数类型为 DeviceXinghanInstructDto - 在 DeviceXinghanBizService 中实现 SOS 报警创建逻辑 -重构 XinghanDeviceDataRule 报警处理流程,使用统一枚举类型- 添加蓝牙模式下 SOS 指令的特殊处理逻辑- 完善报警 Redis 缓存键构建和续期机制
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
package com.fuyuanshen.web.service.device;
|
||||
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
@ -28,17 +29,21 @@ import com.fuyuanshen.common.redis.utils.RedisUtils;
|
||||
import com.fuyuanshen.common.satoken.utils.AppLoginHelper;
|
||||
import com.fuyuanshen.equipment.domain.Device;
|
||||
import com.fuyuanshen.equipment.domain.DeviceType;
|
||||
import com.fuyuanshen.equipment.domain.bo.DeviceAlarmBo;
|
||||
import com.fuyuanshen.equipment.domain.dto.AppDeviceSendMsgBo;
|
||||
import com.fuyuanshen.equipment.enums.LightModeEnum;
|
||||
import com.fuyuanshen.equipment.mapper.DeviceLogMapper;
|
||||
import com.fuyuanshen.equipment.mapper.DeviceMapper;
|
||||
import com.fuyuanshen.equipment.mapper.DeviceTypeMapper;
|
||||
import com.fuyuanshen.equipment.service.IDeviceAlarmService;
|
||||
import com.fuyuanshen.global.mqtt.base.MqttXinghanJson;
|
||||
import com.fuyuanshen.global.mqtt.config.MqttGateway;
|
||||
import com.fuyuanshen.global.mqtt.constants.DeviceRedisKeyConstants;
|
||||
import com.fuyuanshen.global.mqtt.constants.MqttConstants;
|
||||
import com.fuyuanshen.web.domain.Dto.DeviceDebugLogoUploadDto;
|
||||
import com.fuyuanshen.web.domain.Dto.DeviceXinghanInstructDto;
|
||||
import com.fuyuanshen.web.domain.vo.DeviceXinghanDetailVo;
|
||||
import com.fuyuanshen.web.enums.AlarmTypeEnum;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@ -68,6 +73,7 @@ public class DeviceXinghanBizService {
|
||||
private final MqttGateway mqttGateway;
|
||||
private final DeviceLogMapper deviceLogMapper;
|
||||
private final AppPersonnelInfoRecordsMapper appPersonnelInfoRecordsMapper;
|
||||
private final IDeviceAlarmService deviceAlarmService;
|
||||
@Autowired
|
||||
private ObjectMapper objectMapper;
|
||||
|
||||
@ -95,28 +101,39 @@ public class DeviceXinghanBizService {
|
||||
/**
|
||||
* 设置静电预警档位
|
||||
*/
|
||||
public void upDetectGradeSettings(DeviceInstructDto dto) {
|
||||
public void upDetectGradeSettings(DeviceXinghanInstructDto dto) {
|
||||
sendCommand(dto, "ins_DetectGrade","静电预警档位");
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置照明档位
|
||||
*/
|
||||
public void upLightGradeSettings(DeviceInstructDto dto) {
|
||||
public void upLightGradeSettings(DeviceXinghanInstructDto dto) {
|
||||
sendCommand(dto, "ins_LightGrade","照明档位");
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置SOS档位
|
||||
*/
|
||||
public void upSOSGradeSettings(DeviceInstructDto dto) {
|
||||
sendCommand(dto, "ins_SOSGrade","SOS档位");
|
||||
public void upSOSGradeSettings(DeviceXinghanInstructDto dto) {
|
||||
if(dto.getIsBluetooth()){
|
||||
long deviceId = dto.getDeviceId();
|
||||
|
||||
// 1. 使用Optional简化空值检查,使代码更简洁
|
||||
Device device = Optional.ofNullable(deviceMapper.selectById(deviceId))
|
||||
.orElseThrow(() -> new ServiceException("设备不存在"));
|
||||
int sosGrade = Integer.parseInt(dto.getInstructValue());
|
||||
// 6. 新建报警信息
|
||||
createAlarm(device.getId(),device.getDeviceImei(),"ins_SOSGrade",sosGrade);
|
||||
}else {
|
||||
sendCommand(dto, "ins_SOSGrade","SOS档位");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置强制报警
|
||||
*/
|
||||
public void upShakeBitSettings(DeviceInstructDto dto) {
|
||||
public void upShakeBitSettings(DeviceXinghanInstructDto dto) {
|
||||
sendCommand(dto, "ins_ShakeBit","强制报警");
|
||||
}
|
||||
|
||||
@ -465,7 +482,7 @@ public class DeviceXinghanBizService {
|
||||
* @param payloadKey 指令负载数据的键名
|
||||
* @param deviceAction 设备操作类型描述
|
||||
*/
|
||||
private void sendCommand(DeviceInstructDto dto, String payloadKey, String deviceAction) {
|
||||
private void sendCommand(DeviceXinghanInstructDto dto, String payloadKey, String deviceAction) {
|
||||
long deviceId = dto.getDeviceId();
|
||||
|
||||
// 1. 使用Optional简化空值检查,使代码更简洁
|
||||
@ -509,6 +526,9 @@ public class DeviceXinghanBizService {
|
||||
deviceAction,
|
||||
content,
|
||||
AppLoginHelper.getUserId());
|
||||
|
||||
// 6. 新建报警信息
|
||||
createAlarm(device.getId(),deviceImei,payloadKey,value);
|
||||
}
|
||||
|
||||
// private boolean isDeviceOffline(String imei) {
|
||||
@ -516,6 +536,48 @@ public class DeviceXinghanBizService {
|
||||
// return getDeviceStatus(imei);
|
||||
// }
|
||||
|
||||
/**
|
||||
* 新建报警 Bo
|
||||
*/
|
||||
private void createAlarm(Long deviceId, String deviceImei,
|
||||
String payloadKey, int value) {
|
||||
// 这里直接放你原来的 createAlarmBo 全部逻辑
|
||||
if (!"ins_SOSGrade".equals(payloadKey) || value == 0) {
|
||||
return;
|
||||
}
|
||||
AlarmTypeEnum type = value == 1 ? AlarmTypeEnum.SOS : AlarmTypeEnum.SHAKE;
|
||||
String redisKey = buildAlarmRedisKey(deviceImei, type);
|
||||
Long alarmId = RedisUtils.getCacheObject(redisKey);
|
||||
// 已存在未结束报警 -> 什么都不做(同一条报警)
|
||||
if (alarmId != null) {
|
||||
// key 还在 -> 同一条报警,只续期
|
||||
RedisUtils.setCacheObject(redisKey, alarmId, Duration.ofMinutes(10));
|
||||
return;
|
||||
}
|
||||
// 不存在 -> 新建
|
||||
DeviceAlarmBo bo = new DeviceAlarmBo();
|
||||
bo.setDeviceId(deviceId);
|
||||
bo.setDeviceImei(deviceImei);
|
||||
bo.setDeviceAction(0); // 强制报警
|
||||
bo.setStartTime(new Date());
|
||||
bo.setTreatmentState(1); // 未处理
|
||||
bo.setContent("强制报警:" + type.getDesc());
|
||||
String location = RedisUtils.getCacheObject(GLOBAL_REDIS_KEY + DEVICE_KEY_PREFIX + deviceImei + DEVICE_LOCATION_KEY_PREFIX);
|
||||
if (StrUtil.isNotBlank(location)) {
|
||||
bo.setLocation(JSONObject.parseObject(location).getString("address"));
|
||||
}
|
||||
deviceAlarmService.insertByBo(bo);
|
||||
RedisUtils.setCacheObject(redisKey, bo.getId(), Duration.ofMinutes(10));
|
||||
}
|
||||
|
||||
/**
|
||||
* key 构建
|
||||
*/
|
||||
private String buildAlarmRedisKey(String deviceImei, AlarmTypeEnum type) {
|
||||
return StrUtil.format("{}{}{}{}:alarm_id",
|
||||
GLOBAL_REDIS_KEY, DEVICE_KEY_PREFIX, deviceImei, type.getSuffix());
|
||||
}
|
||||
|
||||
/**
|
||||
* 记录设备操作日志
|
||||
* @param deviceId 设备ID
|
||||
|
||||
Reference in New Issue
Block a user