1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
|
@Service @Slf4j public class DeviceCommandService {
@Autowired private DeviceCommandMapper deviceCommandMapper; @Autowired private KafkaTemplate<String, String> kafkaTemplate; @Autowired private RedisTemplate<String, Object> redisTemplate;
public DeviceCommandResult sendCommand(DeviceCommandRequest request) { try { DeviceCommand command = createDeviceCommand(request); deviceCommandMapper.insert(command); sendCommandToKafka(command); cacheCommandStatus(command); DeviceCommandResult result = new DeviceCommandResult(); result.setSuccess(true); result.setCommandId(command.getId()); result.setCommandNo(command.getCommandNo()); result.setStatus("SENT"); result.setMessage("指令下发成功"); log.info("设备指令下发成功: commandId={}, deviceId={}, commandType={}", command.getId(), request.getDeviceId(), request.getCommandType()); return result; } catch (Exception e) { log.error("设备指令下发失败: deviceId={}, commandType={}, error={}", request.getDeviceId(), request.getCommandType(), e.getMessage(), e); return DeviceCommandResult.failed("指令下发失败: " + e.getMessage()); } }
public DeviceCommandStatus getCommandStatus(String commandId) { try { String cacheKey = "device:command:status:" + commandId; DeviceCommandStatus status = (DeviceCommandStatus) redisTemplate.opsForValue() .get(cacheKey); if (status != null) { return status; } DeviceCommand command = deviceCommandMapper.selectById(Long.parseLong(commandId)); if (command == null) { throw new BusinessException("指令不存在"); } status = new DeviceCommandStatus(); status.setCommandId(command.getId()); status.setCommandNo(command.getCommandNo()); status.setStatus(command.getStatus()); status.setResponseData(command.getResponseData()); status.setErrorMessage(command.getErrorMessage()); return status; } catch (Exception e) { log.error("查询指令状态失败: commandId={}, error={}", commandId, e.getMessage(), e); throw new BusinessException("查询指令状态失败: " + e.getMessage()); } }
private DeviceCommand createDeviceCommand(DeviceCommandRequest request) { DeviceCommand command = new DeviceCommand(); command.setCommandNo(generateCommandNo()); command.setDeviceId(request.getDeviceId()); command.setCommandType(request.getCommandType()); command.setCommandData(request.getCommandData()); command.setStatus("PENDING"); command.setCreateTime(LocalDateTime.now()); command.setUpdateTime(LocalDateTime.now()); return command; }
private void sendCommandToKafka(DeviceCommand command) { try { DeviceCommandMessage message = new DeviceCommandMessage(); message.setCommandId(command.getId()); message.setCommandNo(command.getCommandNo()); message.setDeviceId(command.getDeviceId()); message.setCommandType(command.getCommandType()); message.setCommandData(command.getCommandData()); message.setCreateTime(command.getCreateTime()); kafkaTemplate.send("device.command.send", command.getDeviceId(), JSON.toJSONString(message)); } catch (Exception e) { log.error("发送指令到Kafka失败: commandId={}, error={}", command.getId(), e.getMessage(), e); } }
private void cacheCommandStatus(DeviceCommand command) { try { DeviceCommandStatus status = new DeviceCommandStatus(); status.setCommandId(command.getId()); status.setCommandNo(command.getCommandNo()); status.setStatus(command.getStatus()); String cacheKey = "device:command:status:" + command.getId(); redisTemplate.opsForValue().set(cacheKey, status, 24, TimeUnit.HOURS); } catch (Exception e) { log.error("缓存指令状态失败: commandId={}, error={}", command.getId(), e.getMessage(), e); } }
private String generateCommandNo() { return "CMD_" + System.currentTimeMillis() + "_" + UUID.randomUUID().toString().substring(0, 8); } }
@Component @Slf4j public class RealtimeDataConsumer {
@Autowired private RealtimeSubscriptionService subscriptionService;
@KafkaListener(topics = "realtime.data.subscription", groupId = "monitor-service-group") public void consumeRealtimeData(String message) { try { RealtimeDataMessage dataMessage = JSON.parseObject(message, RealtimeDataMessage.class); DeviceRealtimeData data = dataMessage.getData(); subscriptionService.pushRealtimeData(dataMessage.getDeviceId(), data); log.debug("消费实时信息成功: deviceId={}", dataMessage.getDeviceId()); } catch (Exception e) { log.error("消费实时信息失败: error={}", e.getMessage(), e); } } }
@Component @Slf4j public class MonitorApplicationEventConsumer {
@Autowired private KafkaTemplate<String, String> kafkaTemplate;
@KafkaListener(topics = "application.event", groupId = "monitor-service-group") public void consumeApplicationEvent(String message) { try { ApplicationEvent event = JSON.parseObject(message, ApplicationEvent.class); switch (event.getEventType()) { case "DEVICE_ONLINE": handleDeviceOnline(event); break; case "DEVICE_OFFLINE": handleDeviceOffline(event); break; default: log.warn("未知的应用事件类型: eventType={}", event.getEventType()); } } catch (Exception e) { log.error("消费应用事件失败: error={}", e.getMessage(), e); } }
private void handleDeviceOnline(ApplicationEvent event) { try { Map<String, Object> eventData = event.getEventData(); String deviceId = (String) eventData.get("deviceId"); sendDeviceOnlineNotification(deviceId); log.info("处理设备上线事件成功: deviceId={}", deviceId); } catch (Exception e) { log.error("处理设备上线事件失败: error={}", e.getMessage(), e); } }
private void handleDeviceOffline(ApplicationEvent event) { try { Map<String, Object> eventData = event.getEventData(); String deviceId = (String) eventData.get("deviceId"); sendDeviceOfflineNotification(deviceId); log.info("处理设备离线事件成功: deviceId={}", deviceId); } catch (Exception e) { log.error("处理设备离线事件失败: error={}", e.getMessage(), e); } }
private void sendDeviceOnlineNotification(String deviceId) { try { DeviceNotificationEvent event = new DeviceNotificationEvent(); event.setDeviceId(deviceId); event.setNotificationType("DEVICE_ONLINE"); event.setNotificationTime(LocalDateTime.now()); kafkaTemplate.send("device.notification", deviceId, JSON.toJSONString(event)); } catch (Exception e) { log.error("发送设备上线通知失败: deviceId={}, error={}", deviceId, e.getMessage(), e); } }
private void sendDeviceOfflineNotification(String deviceId) { try { DeviceNotificationEvent event = new DeviceNotificationEvent(); event.setDeviceId(deviceId); event.setNotificationType("DEVICE_OFFLINE"); event.setNotificationTime(LocalDateTime.now()); kafkaTemplate.send("device.notification", deviceId, JSON.toJSONString(event)); } catch (Exception e) { log.error("发送设备离线通知失败: deviceId={}, error={}", deviceId, e.getMessage(), e); } } }
|