forked from dyf/fys-Multi-tenant
Merge branch 'main' into dyf-device
This commit is contained in:
@ -39,8 +39,7 @@ import static com.fuyuanshen.common.core.constant.GlobalConstants.GLOBAL_REDIS_K
|
||||
import static com.fuyuanshen.common.core.utils.Bitmap80x12Generator.buildArr;
|
||||
import static com.fuyuanshen.common.core.utils.Bitmap80x12Generator.generateFixedBitmapData;
|
||||
import static com.fuyuanshen.common.core.utils.ImageToCArrayConverter.convertHexToDecimal;
|
||||
import static com.fuyuanshen.global.mqtt.constants.DeviceRedisKeyConstants.DEVICE_BOOT_LOGO_KEY_PREFIX;
|
||||
import static com.fuyuanshen.global.mqtt.constants.DeviceRedisKeyConstants.DEVICE_KEY_PREFIX;
|
||||
import static com.fuyuanshen.global.mqtt.constants.DeviceRedisKeyConstants.*;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
@ -62,7 +61,7 @@ public class DeviceXinghanBizService {
|
||||
"ins_DetectGrade", Map.of(1, "低档", 2, "中档", 3, "高档"),
|
||||
"ins_LightGrade", Map.of(1, "强光", 2, "弱光"),
|
||||
"ins_SOSGrade", Map.of(1, "爆闪模式", 2, "红蓝模式"),
|
||||
"ins_ShakeBit", Map.of(0, "未静止报警", 1, "正在静止报警")
|
||||
"ins_ShakeBit", Map.of(1, "开启报警")
|
||||
// 再加 4、5、6…… 档,直接往 Map 里塞即可
|
||||
);
|
||||
|
||||
@ -161,11 +160,12 @@ public class DeviceXinghanBizService {
|
||||
List<AppPersonnelInfoVo> appPersonnelInfoVos = appPersonnelInfoMapper.selectVoList(qw);
|
||||
|
||||
List<String> list = new ArrayList<>();
|
||||
list.add(bo.getUnitName());
|
||||
list.add(bo.getName());
|
||||
list.add(bo.getPosition());
|
||||
list.add(bo.getUnitName());
|
||||
list.add(bo.getCode());
|
||||
RedisUtils.setCacheList(GLOBAL_REDIS_KEY+ DEVICE_KEY_PREFIX + deviceObj.getDeviceImei() + ":app_send_message_data", list);
|
||||
RedisUtils.expire(GLOBAL_REDIS_KEY+ DEVICE_KEY_PREFIX + deviceObj.getDeviceImei() + ":app_send_message_data", Duration.ofSeconds(5 * 60L));
|
||||
|
||||
Map<String, Object> payload = Map.of("ins_TexTrans",
|
||||
Collections.singletonList(0));
|
||||
@ -195,38 +195,136 @@ public class DeviceXinghanBizService {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送报警信息
|
||||
* @param bo
|
||||
* @return
|
||||
*/
|
||||
public int sendAlarmMessage(AppDeviceSendMsgBo bo) {
|
||||
List<Long> deviceIds = bo.getDeviceIds();
|
||||
|
||||
// 1. 简化非空检查和抛出异常
|
||||
if (deviceIds == null || deviceIds.isEmpty()) {
|
||||
throw new ServiceException("请选择设备");
|
||||
}
|
||||
QueryWrapper<Device> queryWrapper = new QueryWrapper<>();
|
||||
// 使用 in 语句根据id集合查询
|
||||
queryWrapper.in("id", deviceIds);
|
||||
// 2. 将批量查询设备,减少数据库交互次数
|
||||
List<Device> devices = deviceMapper.selectList(queryWrapper);
|
||||
if (devices.size() != deviceIds.size()) {
|
||||
// 如果查询回来的设备数量不一致,说明有设备不存在,此处可以优化为更详细的提示
|
||||
throw new ServiceException("部分设备不存在");
|
||||
}
|
||||
|
||||
try {
|
||||
for (Device device : devices) {
|
||||
String deviceImei = device.getDeviceImei();
|
||||
String deviceName = device.getDeviceName();
|
||||
|
||||
// 3. 在循环中进行设备状态检查,快速失败
|
||||
if (isDeviceOffline(deviceImei)) {
|
||||
// 如果设备离线,可以选择继续处理下一个设备,或者抛出异常。这里选择抛出异常。
|
||||
throw new ServiceException(deviceName + ", 设备已断开连接");
|
||||
}
|
||||
|
||||
// 4. 将Redis和MQTT操作封装在一个方法中,提高可读性
|
||||
sendSingleAlarmMessage(device, bo.getSendMsg());
|
||||
|
||||
// 5. 批量更新设备状态,提高效率
|
||||
UpdateWrapper<Device> updateWrapper = new UpdateWrapper<>();
|
||||
updateWrapper.eq("id", device.getId())
|
||||
.eq("binding_user_id", AppLoginHelper.getUserId())
|
||||
.set("send_msg", bo.getSendMsg());
|
||||
deviceMapper.update(updateWrapper);
|
||||
|
||||
// 6. 记录操作日志
|
||||
recordDeviceLog(device.getId(), deviceName, "发送紧急通知", bo.getSendMsg(), AppLoginHelper.getUserId());
|
||||
}
|
||||
} catch (ServiceException e) {
|
||||
// 捕获并重新抛出自定义异常,避免内层异常被外层泛化捕获
|
||||
log.error("发送告警信息指令失败: {}", e.getMessage(), e);
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
log.error("发送告警信息指令发生未知错误", e);
|
||||
throw new ServiceException("发送告警信息指令失败");
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
/* ---------------------------------- 私有通用方法 ---------------------------------- */
|
||||
|
||||
private void sendCommand(DeviceInstructDto dto,
|
||||
String payloadKey,String deviceAction) {
|
||||
long deviceId = dto.getDeviceId();
|
||||
Device device = deviceMapper.selectById(deviceId);
|
||||
if (device == null) {
|
||||
throw new ServiceException("设备不存在");
|
||||
}
|
||||
if (isDeviceOffline(device.getDeviceImei())) {
|
||||
throw new ServiceException("设备已断开连接:" + device.getDeviceName());
|
||||
}
|
||||
/**
|
||||
* 封装单个设备发送告警信息的逻辑
|
||||
*/
|
||||
private void sendSingleAlarmMessage(Device device, String message) {
|
||||
String deviceImei = device.getDeviceImei();
|
||||
|
||||
Integer value = Integer.parseInt(dto.getInstructValue());
|
||||
// 缓存告警消息到Redis
|
||||
RedisUtils.setCacheObject(GLOBAL_REDIS_KEY + DEVICE_KEY_PREFIX + deviceImei + DEVICE_ALARM_MESSAGE_KEY_PREFIX, message, Duration.ofSeconds(5 * 60L));
|
||||
|
||||
Map<String, Object> payload = Map.of(payloadKey,
|
||||
Collections.singletonList(value));
|
||||
|
||||
String topic = MqttConstants.GLOBAL_PUB_KEY + device.getDeviceImei();
|
||||
// 构建并发送MQTT消息
|
||||
Map<String, Object> payload = Map.of("ins_BreakNews", Collections.singletonList(0));
|
||||
String topic = MqttConstants.GLOBAL_PUB_KEY + deviceImei;
|
||||
String json = JsonUtils.toJsonString(payload);
|
||||
|
||||
try {
|
||||
mqttGateway.sendMsgToMqtt(topic, 1, json);
|
||||
log.info("发送紧急通知成功 => topic:{}, payload:{}", topic, json);
|
||||
} catch (Exception e) {
|
||||
log.error("发送紧急通知失败, topic={}, payload={}", topic, json, e);
|
||||
throw new ServiceException("发送紧急通知失败:" + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送设备控制指令
|
||||
*
|
||||
* @param dto 设备指令数据传输对象,包含设备ID和指令值等信息
|
||||
* @param payloadKey 指令负载数据的键名
|
||||
* @param deviceAction 设备操作类型描述
|
||||
*/
|
||||
private void sendCommand(DeviceInstructDto dto, String payloadKey, String deviceAction) {
|
||||
long deviceId = dto.getDeviceId();
|
||||
|
||||
// 1. 使用Optional简化空值检查,使代码更简洁
|
||||
Device device = Optional.ofNullable(deviceMapper.selectById(deviceId))
|
||||
.orElseThrow(() -> new ServiceException("设备不存在"));
|
||||
|
||||
String deviceImei = device.getDeviceImei();
|
||||
String deviceName = device.getDeviceName();
|
||||
|
||||
// 2. 提前进行设备状态检查,逻辑更清晰
|
||||
if (isDeviceOffline(deviceImei)) {
|
||||
throw new ServiceException("设备已断开连接:" + deviceName);
|
||||
}
|
||||
|
||||
// 3. 统一处理类型转换异常,避免在业务逻辑中混杂try-catch
|
||||
int value;
|
||||
try {
|
||||
value = Integer.parseInt(dto.getInstructValue());
|
||||
} catch (NumberFormatException e) {
|
||||
throw new IllegalArgumentException("指令值格式不正确,必须为整数。", e);
|
||||
}
|
||||
|
||||
// 4. 使用Map.of()或Map.ofEntries()创建不可变Map,更简洁且线程安全
|
||||
Map<String, List<Integer>> payload = Map.of(payloadKey, List.of(value));
|
||||
|
||||
String topic = MqttConstants.GLOBAL_PUB_KEY + deviceImei;
|
||||
String json = JsonUtils.toJsonString(payload);
|
||||
|
||||
try {
|
||||
mqttGateway.sendMsgToMqtt(topic, 1, json);
|
||||
log.info("发送指令成功 => topic:{}, payload:{}", topic, json);
|
||||
} catch (Exception e) {
|
||||
log.error("发送指令失败, topic={}, payload={}", topic, json, e);
|
||||
throw new ServiceException("发送指令失败:" + e.getMessage());
|
||||
}
|
||||
|
||||
log.info("发送指令成功 => topic:{}, payload:{}", topic, json);
|
||||
// 5. 将日志记录和描述解析放在try-catch块之外,确保无论是否成功发送指令都能执行
|
||||
String content = resolveGradeDesc("ins_DetectGrade", value);
|
||||
recordDeviceLog(device.getId(),
|
||||
device.getDeviceName(),
|
||||
deviceName,
|
||||
deviceAction,
|
||||
content,
|
||||
AppLoginHelper.getUserId());
|
||||
|
||||
Reference in New Issue
Block a user