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

This commit is contained in:
YunaiV
2025-09-20 16:15:33 +08:00
parent c1657149a8
commit 5ae0224b55
3 changed files with 21 additions and 26 deletions

View File

@@ -33,7 +33,7 @@ public class IotTcpDataRuleAction extends
@Override
protected IotTcpClient initProducer(IotDataSinkTcpConfig config) throws Exception {
// 1. 参数校验
// 1.1 参数校验
if (config.getHost() == null || config.getHost().trim().isEmpty()) {
throw new IllegalArgumentException("TCP 服务器地址不能为空");
}
@@ -41,7 +41,7 @@ public class IotTcpDataRuleAction extends
throw new IllegalArgumentException("TCP 服务器端口无效");
}
// 2. 创建 TCP 客户端
// 2.1 创建 TCP 客户端
IotTcpClient tcpClient = new IotTcpClient(
config.getHost(),
config.getPort(),
@@ -51,13 +51,10 @@ public class IotTcpDataRuleAction extends
config.getSslCertPath(),
config.getDataFormat()
);
// 3. 连接服务器
// 2.2 连接服务器
tcpClient.connect();
log.info("[initProducer][TCP 客户端创建并连接成功,服务器: {}:{}SSL: {},数据格式: {}]",
config.getHost(), config.getPort(), config.getSsl(), config.getDataFormat());
return tcpClient;
}
@@ -71,22 +68,19 @@ public class IotTcpDataRuleAction extends
@Override
protected void execute(IotDeviceMessage message, IotDataSinkTcpConfig config) throws Exception {
try {
// 1. 获取或创建 TCP 客户端
// 1.1 获取或创建 TCP 客户端
IotTcpClient tcpClient = getProducer(config);
// 2. 检查连接状态,如果断开则重新连接
// 1.2 检查连接状态,如果断开则重新连接
if (!tcpClient.isConnected()) {
log.warn("[execute][TCP 连接已断开,尝试重新连接,服务器: {}:{}]", config.getHost(), config.getPort());
tcpClient.connect();
}
// 3. 发送消息并等待结果
// 2.1 发送消息并等待结果
tcpClient.sendMessage(message);
// 4. 记录发送成功日志
// 2.2 记录发送成功日志
log.info("[execute][message({}) config({}) 发送成功TCP 服务器: {}:{}]",
message, config, config.getHost(), config.getPort());
} catch (Exception e) {
log.error("[execute][message({}) config({}) 发送失败TCP 服务器: {}:{}]",
message, config, config.getHost(), config.getPort(), e);

View File

@@ -38,6 +38,7 @@ public class IotTcpClient {
private BufferedReader reader;
private final AtomicBoolean connected = new AtomicBoolean(false);
// TODO @puhui999default 值IotDataSinkTcpConfig.java 枚举起来哈;
public IotTcpClient(String host, Integer port, Integer connectTimeoutMs, Integer readTimeoutMs,
Boolean ssl, String sslCertPath, String dataFormat) {
this.host = host;
@@ -76,9 +77,9 @@ public class IotTcpClient {
outputStream = socket.getOutputStream();
reader = new BufferedReader(new InputStreamReader(socket.getInputStream(), StandardCharsets.UTF_8));
// 更新状态
connected.set(true);
log.info("[connect][TCP 客户端连接成功,服务器地址: {}:{}]", host, port);
} catch (Exception e) {
close();
log.error("[connect][TCP 客户端连接失败,服务器地址: {}:{}]", host, port, e);
@@ -98,6 +99,7 @@ public class IotTcpClient {
}
try {
// TODO @puhui999枚举值
String messageData;
if ("JSON".equalsIgnoreCase(dataFormat)) {
// JSON 格式
@@ -111,10 +113,8 @@ public class IotTcpClient {
outputStream.write(messageData.getBytes(StandardCharsets.UTF_8));
outputStream.write('\n'); // 添加换行符作为消息分隔符
outputStream.flush();
log.debug("[sendMessage][发送消息成功,设备 ID: {},消息长度: {}]",
message.getDeviceId(), messageData.length());
} catch (Exception e) {
log.error("[sendMessage][发送消息失败,设备 ID: {}]", message.getDeviceId(), e);
throw e;
@@ -153,9 +153,9 @@ public class IotTcpClient {
}
}
// 更新状态
connected.set(false);
log.info("[close][TCP 客户端连接已关闭,服务器地址: {}:{}]", host, port);
} catch (Exception e) {
log.error("[close][关闭 TCP 客户端连接异常]", e);
}

View File

@@ -25,13 +25,13 @@ class IotTcpDataRuleActionTest {
private IotTcpClient mockTcpClient;
@BeforeEach
void setUp() {
public void setUp() {
MockitoAnnotations.openMocks(this);
tcpDataRuleAction = new IotTcpDataRuleAction();
}
@Test
void testGetType() {
public void testGetType() {
// 准备参数
Integer expectedType = 2; // 数据接收类型枚举中 TCP 类型的值
@@ -42,8 +42,9 @@ class IotTcpDataRuleActionTest {
assertEquals(expectedType, actualType);
}
// TODO @puhui999_ 后面是小写哈,单测的命名规则。
@Test
void testInitProducer_Success() throws Exception {
public void testInitProducer_Success() throws Exception {
// 准备参数
IotDataSinkTcpConfig config = new IotDataSinkTcpConfig();
config.setHost("localhost");
@@ -59,7 +60,7 @@ class IotTcpDataRuleActionTest {
}
@Test
void testInitProducer_InvalidHost() {
public void testInitProducer_InvalidHost() {
// 准备参数
IotDataSinkTcpConfig config = new IotDataSinkTcpConfig();
config.setHost("");
@@ -77,7 +78,7 @@ class IotTcpDataRuleActionTest {
}
@Test
void testInitProducer_InvalidPort() {
public void testInitProducer_InvalidPort() {
// 准备参数
IotDataSinkTcpConfig config = new IotDataSinkTcpConfig();
config.setHost("localhost");
@@ -92,7 +93,7 @@ class IotTcpDataRuleActionTest {
}
@Test
void testCloseProducer() throws Exception {
public void testCloseProducer() throws Exception {
// 准备参数
IotTcpClient client = mock(IotTcpClient.class);
@@ -104,7 +105,7 @@ class IotTcpDataRuleActionTest {
}
@Test
void testExecute_WithValidConfig() {
public void testExecute_WithValidConfig() {
// 准备参数
IotDeviceMessage message = IotDeviceMessage.requestOf("thing.property.report",
"{\"temperature\": 25.5, \"humidity\": 60}");
@@ -124,7 +125,7 @@ class IotTcpDataRuleActionTest {
}
@Test
void testConfig_DefaultValues() {
public void testConfig_DefaultValues() {
// 准备参数
IotDataSinkTcpConfig config = new IotDataSinkTcpConfig();
@@ -140,7 +141,7 @@ class IotTcpDataRuleActionTest {
}
@Test
void testMessageSerialization() {
public void testMessageSerialization() {
// 准备参数
IotDeviceMessage message = IotDeviceMessage.builder()
.deviceId(123L)