Commit 5ae0224b553c30f05f3658d3ebe8bec0cfab56ee

Authored by YunaiV
1 parent c1657149

review:【iot 物联网】场景联动的缓存等

yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/rule/data/action/IotTcpDataRuleAction.java
... ... @@ -33,7 +33,7 @@ public class IotTcpDataRuleAction extends
33 33  
34 34 @Override
35 35 protected IotTcpClient initProducer(IotDataSinkTcpConfig config) throws Exception {
36   - // 1. 参数校验
  36 + // 1.1 参数校验
37 37 if (config.getHost() == null || config.getHost().trim().isEmpty()) {
38 38 throw new IllegalArgumentException("TCP 服务器地址不能为空");
39 39 }
... ... @@ -41,7 +41,7 @@ public class IotTcpDataRuleAction extends
41 41 throw new IllegalArgumentException("TCP 服务器端口无效");
42 42 }
43 43  
44   - // 2. 创建 TCP 客户端
  44 + // 2.1 创建 TCP 客户端
45 45 IotTcpClient tcpClient = new IotTcpClient(
46 46 config.getHost(),
47 47 config.getPort(),
... ... @@ -51,13 +51,10 @@ public class IotTcpDataRuleAction extends
51 51 config.getSslCertPath(),
52 52 config.getDataFormat()
53 53 );
54   -
55   - // 3. 连接服务器
  54 + // 2.2 连接服务器
56 55 tcpClient.connect();
57   -
58 56 log.info("[initProducer][TCP 客户端创建并连接成功,服务器: {}:{},SSL: {},数据格式: {}]",
59 57 config.getHost(), config.getPort(), config.getSsl(), config.getDataFormat());
60   -
61 58 return tcpClient;
62 59 }
63 60  
... ... @@ -71,22 +68,19 @@ public class IotTcpDataRuleAction extends
71 68 @Override
72 69 protected void execute(IotDeviceMessage message, IotDataSinkTcpConfig config) throws Exception {
73 70 try {
74   - // 1. 获取或创建 TCP 客户端
  71 + // 1.1 获取或创建 TCP 客户端
75 72 IotTcpClient tcpClient = getProducer(config);
76   -
77   - // 2. 检查连接状态,如果断开则重新连接
  73 + // 1.2 检查连接状态,如果断开则重新连接
78 74 if (!tcpClient.isConnected()) {
79 75 log.warn("[execute][TCP 连接已断开,尝试重新连接,服务器: {}:{}]", config.getHost(), config.getPort());
80 76 tcpClient.connect();
81 77 }
82 78  
83   - // 3. 发送消息并等待结果
  79 + // 2.1 发送消息并等待结果
84 80 tcpClient.sendMessage(message);
85   -
86   - // 4. 记录发送成功日志
  81 + // 2.2 记录发送成功日志
87 82 log.info("[execute][message({}) config({}) 发送成功,TCP 服务器: {}:{}]",
88 83 message, config, config.getHost(), config.getPort());
89   -
90 84 } catch (Exception e) {
91 85 log.error("[execute][message({}) config({}) 发送失败,TCP 服务器: {}:{}]",
92 86 message, config, config.getHost(), config.getPort(), e);
... ...
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/rule/data/action/tcp/IotTcpClient.java
... ... @@ -38,6 +38,7 @@ public class IotTcpClient {
38 38 private BufferedReader reader;
39 39 private final AtomicBoolean connected = new AtomicBoolean(false);
40 40  
  41 + // TODO @puhui999:default 值,IotDataSinkTcpConfig.java 枚举起来哈;
41 42 public IotTcpClient(String host, Integer port, Integer connectTimeoutMs, Integer readTimeoutMs,
42 43 Boolean ssl, String sslCertPath, String dataFormat) {
43 44 this.host = host;
... ... @@ -76,9 +77,9 @@ public class IotTcpClient {
76 77 outputStream = socket.getOutputStream();
77 78 reader = new BufferedReader(new InputStreamReader(socket.getInputStream(), StandardCharsets.UTF_8));
78 79  
  80 + // 更新状态
79 81 connected.set(true);
80 82 log.info("[connect][TCP 客户端连接成功,服务器地址: {}:{}]", host, port);
81   -
82 83 } catch (Exception e) {
83 84 close();
84 85 log.error("[connect][TCP 客户端连接失败,服务器地址: {}:{}]", host, port, e);
... ... @@ -98,6 +99,7 @@ public class IotTcpClient {
98 99 }
99 100  
100 101 try {
  102 + // TODO @puhui999:枚举值
101 103 String messageData;
102 104 if ("JSON".equalsIgnoreCase(dataFormat)) {
103 105 // JSON 格式
... ... @@ -111,10 +113,8 @@ public class IotTcpClient {
111 113 outputStream.write(messageData.getBytes(StandardCharsets.UTF_8));
112 114 outputStream.write('\n'); // 添加换行符作为消息分隔符
113 115 outputStream.flush();
114   -
115 116 log.debug("[sendMessage][发送消息成功,设备 ID: {},消息长度: {}]",
116 117 message.getDeviceId(), messageData.length());
117   -
118 118 } catch (Exception e) {
119 119 log.error("[sendMessage][发送消息失败,设备 ID: {}]", message.getDeviceId(), e);
120 120 throw e;
... ... @@ -153,9 +153,9 @@ public class IotTcpClient {
153 153 }
154 154 }
155 155  
  156 + // 更新状态
156 157 connected.set(false);
157 158 log.info("[close][TCP 客户端连接已关闭,服务器地址: {}:{}]", host, port);
158   -
159 159 } catch (Exception e) {
160 160 log.error("[close][关闭 TCP 客户端连接异常]", e);
161 161 }
... ...
yudao-module-iot/yudao-module-iot-biz/src/test/java/cn/iocoder/yudao/module/iot/service/rule/data/action/IotTcpDataRuleActionTest.java
... ... @@ -25,13 +25,13 @@ class IotTcpDataRuleActionTest {
25 25 private IotTcpClient mockTcpClient;
26 26  
27 27 @BeforeEach
28   - void setUp() {
  28 + public void setUp() {
29 29 MockitoAnnotations.openMocks(this);
30 30 tcpDataRuleAction = new IotTcpDataRuleAction();
31 31 }
32 32  
33 33 @Test
34   - void testGetType() {
  34 + public void testGetType() {
35 35 // 准备参数
36 36 Integer expectedType = 2; // 数据接收类型枚举中 TCP 类型的值
37 37  
... ... @@ -42,8 +42,9 @@ class IotTcpDataRuleActionTest {
42 42 assertEquals(expectedType, actualType);
43 43 }
44 44  
  45 + // TODO @puhui999:_ 后面是小写哈,单测的命名规则。
45 46 @Test
46   - void testInitProducer_Success() throws Exception {
  47 + public void testInitProducer_Success() throws Exception {
47 48 // 准备参数
48 49 IotDataSinkTcpConfig config = new IotDataSinkTcpConfig();
49 50 config.setHost("localhost");
... ... @@ -59,7 +60,7 @@ class IotTcpDataRuleActionTest {
59 60 }
60 61  
61 62 @Test
62   - void testInitProducer_InvalidHost() {
  63 + public void testInitProducer_InvalidHost() {
63 64 // 准备参数
64 65 IotDataSinkTcpConfig config = new IotDataSinkTcpConfig();
65 66 config.setHost("");
... ... @@ -77,7 +78,7 @@ class IotTcpDataRuleActionTest {
77 78 }
78 79  
79 80 @Test
80   - void testInitProducer_InvalidPort() {
  81 + public void testInitProducer_InvalidPort() {
81 82 // 准备参数
82 83 IotDataSinkTcpConfig config = new IotDataSinkTcpConfig();
83 84 config.setHost("localhost");
... ... @@ -92,7 +93,7 @@ class IotTcpDataRuleActionTest {
92 93 }
93 94  
94 95 @Test
95   - void testCloseProducer() throws Exception {
  96 + public void testCloseProducer() throws Exception {
96 97 // 准备参数
97 98 IotTcpClient client = mock(IotTcpClient.class);
98 99  
... ... @@ -104,7 +105,7 @@ class IotTcpDataRuleActionTest {
104 105 }
105 106  
106 107 @Test
107   - void testExecute_WithValidConfig() {
  108 + public void testExecute_WithValidConfig() {
108 109 // 准备参数
109 110 IotDeviceMessage message = IotDeviceMessage.requestOf("thing.property.report",
110 111 "{\"temperature\": 25.5, \"humidity\": 60}");
... ... @@ -124,7 +125,7 @@ class IotTcpDataRuleActionTest {
124 125 }
125 126  
126 127 @Test
127   - void testConfig_DefaultValues() {
  128 + public void testConfig_DefaultValues() {
128 129 // 准备参数
129 130 IotDataSinkTcpConfig config = new IotDataSinkTcpConfig();
130 131  
... ... @@ -140,7 +141,7 @@ class IotTcpDataRuleActionTest {
140 141 }
141 142  
142 143 @Test
143   - void testMessageSerialization() {
  144 + public void testMessageSerialization() {
144 145 // 准备参数
145 146 IotDeviceMessage message = IotDeviceMessage.builder()
146 147 .deviceId(123L)
... ...