review:【IoT 物联网】OTA 相关实现

This commit is contained in:
haohao 2025-07-05 12:40:55 +08:00
parent f05470e68d
commit def9ff11dd
3 changed files with 12 additions and 0 deletions

View File

@ -46,6 +46,7 @@ public class IotOtaUpgradeJob implements JobHandler {
return null;
}
// TODO 芋艿可以优化成批量获取 原因是1. N+1 问题2. offline 的设备无需查询
// 2. 遍历推送记录
int successCount = 0;
int failureCount = 0;
@ -54,6 +55,10 @@ public class IotOtaUpgradeJob implements JobHandler {
try {
// 2.1 设备如果不在线直接跳过
IotDeviceDO device = deviceService.getDeviceFromCache(record.getDeviceId());
// TODO 芋艿优化当前逻辑跳过了离线的设备但未充分利用 MQTT 的离线消息能力
// 1. MQTT 协议本身支持持久化会话Clean Session=false QoS > 0 的消息允许 broker 为离线设备缓存消息
// 2. 对于 OTA 升级这类非实时性强的任务即使设备当前离线也应该可以推送升级指令设备在下次上线时即可收到
// 3. 后续可以考虑增加一个允许离线推送的选项如果开启即使设备状态为 OFFLINE也应尝试推送消息依赖 MQTT Broker 的能力进行离线缓存
if (device == null || IotDeviceStateEnum.isNotOnline(device.getState())) {
continue;
}

View File

@ -67,6 +67,7 @@ public class IotDeviceMessageSubscriber implements IotMessageSubscriber<IotDevic
IotDeviceDO device = deviceService.validateDeviceExistsFromCache(message.getDeviceId());
devicePropertyService.updateDeviceReportTimeAsync(device.getId(), LocalDateTime.now());
// 1.2 更新设备的连接 server
// TODO 芋艿HTTP 网关的上行消息不应该更新 serverId会覆盖掉 MQTT 等长连接的 serverId导致下行消息无法发送
devicePropertyService.updateDeviceServerIdAsync(device.getId(), message.getServerId());
// 2. 未上线的设备强制上线

View File

@ -117,6 +117,12 @@ public class IotDeviceMessageServiceImpl implements IotDeviceMessageService {
// 2.2 情况二发送下行消息
// 如果是下行消息需要校验 serverId 存在
// TODO 芋艿设计下行消息需要区分 PUSH PULL 模型
// 1. PUSH 模型适用于 MQTT 等长连接协议通过 serverId 将消息路由到指定网关实时推送
// 2. PULL 模型适用于 HTTP 等短连接协议设备无固定 serverId无法主动推送
// 解决方案
// serverId 不存在时将下行消息存入待拉取消息表例如 iot_device_pull_message
// 设备端通过定时轮询一个新增的 API例如 /iot/message/pull来拉取属于自己的消息
if (StrUtil.isEmpty(serverId)) {
serverId = devicePropertyService.getDeviceServerId(device.getId());
if (StrUtil.isEmpty(serverId)) {