From 4bee56623dde2b4ec268e978526aed6ce15f9386 Mon Sep 17 00:00:00 2001
From: gaoyoulong <774597026@qq.com>
Date: Mon, 29 Apr 2024 11:22:45 +0800
Subject: [PATCH] =?UTF-8?q?fix:=20=E6=9B=B4=E6=96=B0=E7=8A=B6=E6=80=81?=
=?UTF-8?q?=E6=A3=80=E6=9F=A5=E7=89=A9=E6=A8=A1=E5=9E=8B=E6=B6=88=E6=81=AF?=
=?UTF-8?q?=EF=BC=8C=E6=9B=B4=E6=96=B0=E4=B8=8A=E4=B8=8B=E7=BA=BF=E8=A7=84?=
=?UTF-8?q?=E5=88=99=E5=BC=95=E6=93=8Ebug?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../manager/service/DeviceStateCheckTask.java | 21 +++++++---
.../iotkit/plugin/main/ThingServiceImpl.java | 32 ++++++++++----
iot-module/iot-rule-engine/pom.xml | 4 ++
.../handler/sys/DeviceStateCheckHandler.java | 42 +++++++++----------
4 files changed, 64 insertions(+), 35 deletions(-)
diff --git a/iot-module/iot-manager/src/main/java/cc/iotkit/manager/service/DeviceStateCheckTask.java b/iot-module/iot-manager/src/main/java/cc/iotkit/manager/service/DeviceStateCheckTask.java
index f51cd912..3e1ea3c0 100644
--- a/iot-module/iot-manager/src/main/java/cc/iotkit/manager/service/DeviceStateCheckTask.java
+++ b/iot-module/iot-manager/src/main/java/cc/iotkit/manager/service/DeviceStateCheckTask.java
@@ -11,10 +11,14 @@ package cc.iotkit.manager.service;
import cc.iotkit.common.api.PageRequest;
import cc.iotkit.common.api.Paging;
+import cc.iotkit.common.utils.UniqueIdUtil;
import cc.iotkit.data.manager.IDeviceInfoData;
import cc.iotkit.data.manager.IProductData;
import cc.iotkit.model.device.DeviceInfo;
import cc.iotkit.model.product.Product;
+import cc.iotkit.plugin.core.thing.IThingService;
+import cc.iotkit.plugin.core.thing.actions.DeviceState;
+import cc.iotkit.plugin.core.thing.actions.up.DeviceStateChange;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
@@ -38,6 +42,9 @@ public class DeviceStateCheckTask {
@Qualifier("productDataCache")
private IProductData productData;
+ @Autowired
+ private IThingService thingService;
+
@Scheduled(fixedDelay = 10, initialDelay = 20, timeUnit = TimeUnit.SECONDS)
public void syncState() {
int pn = 1;
@@ -61,11 +68,15 @@ public class DeviceStateCheckTask {
continue;
}
log.info("device state check offline,{}", deviceId);
- //更新为离线
- DeviceInfo.State state = realTimeDevice.getState();
- state.setOnline(false);
- state.setOfflineTime(System.currentTimeMillis());
- deviceInfoData.save(realTimeDevice);
+
+ // 发送设备离线物模型消息
+ thingService.post("NONE", DeviceStateChange.builder()
+ .id(UniqueIdUtil.newRequestId())
+ .productKey(realTimeDevice.getProductKey())
+ .deviceName(realTimeDevice.getDeviceName())
+ .state(DeviceState.OFFLINE)
+ .time(System.currentTimeMillis())
+ .build());
}
}
diff --git a/iot-module/iot-plugin/iot-plugin-main/src/main/java/cc/iotkit/plugin/main/ThingServiceImpl.java b/iot-module/iot-plugin/iot-plugin-main/src/main/java/cc/iotkit/plugin/main/ThingServiceImpl.java
index 04580f4a..e412b841 100644
--- a/iot-module/iot-plugin/iot-plugin-main/src/main/java/cc/iotkit/plugin/main/ThingServiceImpl.java
+++ b/iot-module/iot-plugin/iot-plugin-main/src/main/java/cc/iotkit/plugin/main/ThingServiceImpl.java
@@ -74,14 +74,7 @@ public class ThingServiceImpl implements IThingService {
subRegisterDevice(pluginId, device, (SubDeviceRegister) action);
break;
case STATE_CHANGE:
- publishMsg(
- device, action,
- ThingModelMessage.builder()
- .type(ThingModelMessage.TYPE_STATE)
- .identifier(((DeviceStateChange) action).getState().getState())
- .time(System.currentTimeMillis())
- .build()
- );
+ deviceStateChange(device, (DeviceStateChange) action);
break;
case EVENT_REPORT:
EventReport eventReport = (EventReport) action;
@@ -190,6 +183,29 @@ public class ThingServiceImpl implements IThingService {
return device.getProperty();
}
+
+ private void deviceStateChange(DeviceInfo device, DeviceStateChange action) {
+ DeviceState state = action.getState();
+ if (state == DeviceState.ONLINE) {
+ device.getState().setOnline(true);
+ device.getState().setOnlineTime(System.currentTimeMillis());
+ } else {
+ device.getState().setOnline(false);
+ device.getState().setOfflineTime(System.currentTimeMillis());
+ }
+ deviceInfoData.save(device);
+
+ publishMsg(
+ device, action,
+ ThingModelMessage.builder()
+ .type(ThingModelMessage.TYPE_STATE)
+ .identifier(action.getState().getState())
+ .time(System.currentTimeMillis())
+ .build()
+ );
+ }
+
+
private String registerDevice(DeviceInfo device, DeviceRegister register, String parentId) {
String productKey = register.getProductKey();
//指定了pk需验证
diff --git a/iot-module/iot-rule-engine/pom.xml b/iot-module/iot-rule-engine/pom.xml
index def04b47..bf30a5e2 100644
--- a/iot-module/iot-rule-engine/pom.xml
+++ b/iot-module/iot-rule-engine/pom.xml
@@ -108,6 +108,10 @@
io.vertx
vertx-kafka-client
+
+ cc.iotkit
+ iot-plugin-core
+
diff --git a/iot-module/iot-rule-engine/src/main/java/cc/iotkit/ruleengine/handler/sys/DeviceStateCheckHandler.java b/iot-module/iot-rule-engine/src/main/java/cc/iotkit/ruleengine/handler/sys/DeviceStateCheckHandler.java
index 339009e7..613a3b85 100644
--- a/iot-module/iot-rule-engine/src/main/java/cc/iotkit/ruleengine/handler/sys/DeviceStateCheckHandler.java
+++ b/iot-module/iot-rule-engine/src/main/java/cc/iotkit/ruleengine/handler/sys/DeviceStateCheckHandler.java
@@ -10,14 +10,17 @@
package cc.iotkit.ruleengine.handler.sys;
import cc.iotkit.common.thing.ThingModelMessage;
+import cc.iotkit.common.utils.UniqueIdUtil;
import cc.iotkit.data.manager.IDeviceInfoData;
import cc.iotkit.model.device.DeviceInfo;
+import cc.iotkit.plugin.core.thing.actions.DeviceState;
+import cc.iotkit.plugin.core.thing.actions.up.DeviceStateChange;
import cc.iotkit.ruleengine.handler.DeviceMessageHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
-
+import cc.iotkit.plugin.core.thing.IThingService;
/**
* 设备状态检查
@@ -30,6 +33,8 @@ public class DeviceStateCheckHandler implements DeviceMessageHandler {
@Qualifier("deviceInfoDataCache")
private IDeviceInfoData deviceInfoData;
+ @Autowired
+ private IThingService thingService;
@Override
public void handle(ThingModelMessage msg) {
@@ -58,32 +63,25 @@ public class DeviceStateCheckHandler implements DeviceMessageHandler {
//过滤oat消息
if (ThingModelMessage.TYPE_CONFIG.equals(type) ||
ThingModelMessage.TYPE_OTA.equals(type) ||
- ThingModelMessage.TYPE_LIFETIME.equals(type)) {
+ ThingModelMessage.TYPE_LIFETIME.equals(type) ||
+ ThingModelMessage.TYPE_STATE.equals(type)
+ ) {
return;
}
- //当前设备状态
- boolean online = false;
- //在离线消息
- if (ThingModelMessage.TYPE_STATE.equals(type)) {
- online = ThingModelMessage.ID_ONLINE.equals(identifier);
- } else {
- //其它消息都认作为在线
- online = true;
+ // 如果在线,则不处理
+ if( deviceInfo.getState().isOnline() ) {
+ return;
}
- DeviceInfo.State state = deviceInfo.getState();
- if (state != null && state.isOnline() != online) {
- //状态有变更
- state.setOnline(online);
- if (online) {
- state.setOnlineTime(System.currentTimeMillis());
- } else {
- state.setOfflineTime(System.currentTimeMillis());
- }
- }
- deviceInfoData.save(deviceInfo);
-
+ // 其他消息, 发送设备在线物模型消息
+ thingService.post("NONE", DeviceStateChange.builder()
+ .id(UniqueIdUtil.newRequestId())
+ .productKey(deviceInfo.getProductKey())
+ .deviceName(deviceInfo.getDeviceName())
+ .state(DeviceState.ONLINE)
+ .time(System.currentTimeMillis())
+ .build());
}
}