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