You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
4.5 KiB

5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
  1. package com.qyft.ms.device.handler;
  2. import cn.hutool.json.JSONObject;
  3. import cn.hutool.json.JSONUtil;
  4. import com.qyft.ms.app.common.command.CommandFuture;
  5. import com.qyft.ms.app.common.command.CurrentSendCmdMapInstance;
  6. import com.qyft.ms.app.service.WebSocketService;
  7. import com.qyft.ms.device.model.bo.DeviceFeedback;
  8. import com.qyft.ms.device.service.DeviceStatusService;
  9. import io.netty.buffer.ByteBuf;
  10. import io.netty.channel.ChannelHandler;
  11. import io.netty.channel.ChannelHandlerContext;
  12. import io.netty.channel.ChannelInboundHandlerAdapter;
  13. import io.netty.util.CharsetUtil;
  14. import lombok.RequiredArgsConstructor;
  15. import lombok.extern.slf4j.Slf4j;
  16. import org.springframework.stereotype.Component;
  17. import java.util.Map;
  18. import java.util.concurrent.CompletableFuture;
  19. import java.util.concurrent.ConcurrentHashMap;
  20. @Slf4j
  21. @Component
  22. @ChannelHandler.Sharable
  23. @RequiredArgsConstructor
  24. public class DeviceMessageHandler extends ChannelInboundHandlerAdapter {
  25. public final Map<String, CompletableFuture<DeviceFeedback>> responseMap = new ConcurrentHashMap<>();
  26. private final DeviceStatusService deviceStatusService;
  27. private final WebSocketService webSocketService;
  28. @Override
  29. public void channelRead(ChannelHandlerContext ctx, Object msg) {
  30. ByteBuf buf = (ByteBuf) msg;
  31. String serverMsg = buf.toString(CharsetUtil.UTF_8);
  32. try {
  33. // JsonRpcResponse jsonRpcResponse = JSONUtil.toBean(serverMsg, JsonRpcResponse.class);
  34. // if (TcpMessageType.STATUS.equals(jsonRpcResponse.getType())) {//设备状态
  35. // DeviceStatus deviceStatus = JSONUtil.toBean(jsonRpcResponse.getData(), DeviceStatus.class);
  36. // deviceStatusService.updateDeviceStatus(deviceStatus); // 更新设备状态
  37. // } else if (TcpMessageType.ALARM.equals(jsonRpcResponse.getType())) {//设备报警
  38. // log.error("设备报警: {}", serverMsg);
  39. // DeviceAlarm deviceAlarm = JSONUtil.toBean(jsonRpcResponse.getData(), DeviceAlarm.class);
  40. // webSocketService.pushMsg(WebSocketMessageType.WARN, deviceAlarm);
  41. // } else if (TcpMessageType.FEEDBACK.equals(jsonRpcResponse.getType())) {//设备指令反馈
  42. // DeviceFeedback deviceFeedback = JSONUtil.toBean(jsonRpcResponse.getData(), DeviceFeedback.class);
  43. // this.handleTcpResponse(deviceFeedback);
  44. // }
  45. JSONObject deviceResult = JSONUtil.parseObj(serverMsg);
  46. JSONObject payload = deviceResult.getJSONObject("payload");
  47. String tag = deviceResult.get("tag").toString();
  48. if ("event".equals(tag)) {
  49. //设备上报事件
  50. } else if ("ack".equals(tag)) {
  51. //设备指令反馈
  52. Integer cmdId = payload.getInt("cmdId");
  53. CommandFuture commandFuture = CurrentSendCmdMapInstance.getInstance().getCommand(cmdId);
  54. if(commandFuture!=null){
  55. commandFuture.setReceived(true);
  56. commandFuture.setCallbackResult(payload);
  57. commandFuture.commandContinue();
  58. }
  59. //
  60. // if (error != null) {
  61. // //指令执行错误,将错误放到CallbackResult中
  62. // commandFuture.setCallbackResult(error);
  63. // }
  64. // Object result = payload.get("result");
  65. // if (result instanceof Boolean) {
  66. // //ack 没携带数据
  67. // } else if (result instanceof JSONObject) {
  68. // //ack 携带了数据,将数据放到了CallbackResult中
  69. // commandFuture.setCallbackResult((JSONObject) result);
  70. // } else {
  71. // //TODO 没有定义的类型
  72. // }
  73. // commandFuture.setReceived(true);//已收到设备反馈
  74. // commandFuture.commandContinue();
  75. } else if ("status".equals(tag)) {
  76. //设备上报状态
  77. }
  78. } catch (Exception e) {
  79. log.error("TCP服务消息处理错误: {}, error: {}", serverMsg, e.getMessage(), e);
  80. } finally {
  81. buf.release();
  82. }
  83. }
  84. private void handleTcpResponse(DeviceFeedback deviceFeedback) {
  85. String requestId = deviceFeedback.getId();
  86. CompletableFuture<DeviceFeedback> future = responseMap.remove(requestId);
  87. if (future != null) {
  88. future.complete(deviceFeedback);
  89. } else {
  90. log.error("未找到 requestId: {} 对应的等待请求", requestId);
  91. }
  92. }
  93. }