Pārlūkot izejas kodu

feat: 设备离线在线状态切换处理

gaoyoulong 1 gadu atpakaļ
vecāks
revīzija
11bb9640e8

+ 8 - 22
iot-module/iot-plugin/iot-plugin-main/src/main/java/cc/iotkit/plugin/main/ThingServiceImpl.java

@@ -74,7 +74,14 @@ public class ThingServiceImpl implements IThingService {
                     subRegisterDevice(pluginId, device, (SubDeviceRegister) action);
                     break;
                 case STATE_CHANGE:
-                    deviceStateChange(device, (DeviceStateChange) action);
+                    publishMsg(
+                            device, action,
+                            ThingModelMessage.builder()
+                                    .type(ThingModelMessage.TYPE_STATE)
+                                    .identifier(((DeviceStateChange) action).getState().getState())
+                                    .time(System.currentTimeMillis())
+                                    .build()
+                    );
                     break;
                 case EVENT_REPORT:
                     EventReport eventReport = (EventReport) action;
@@ -258,27 +265,6 @@ public class ThingServiceImpl implements IThingService {
         }
     }
 
-    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 void deviceTopologyUpdate(DeviceInfo device, DeviceTopology topology) {
         //设备拓扑关系更新
         for (String deviceName : topology.getSubDevices()) {

+ 50 - 12
iot-module/iot-rule-engine/src/main/java/cc/iotkit/ruleengine/handler/sys/DeviceStateCheckHandler.java

@@ -18,7 +18,6 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Qualifier;
 import org.springframework.stereotype.Component;
 
-import javax.transaction.Transactional;
 
 /**
  * 设备状态检查
@@ -33,19 +32,58 @@ public class DeviceStateCheckHandler implements DeviceMessageHandler {
 
 
     @Override
-    @Transactional
     public void handle(ThingModelMessage msg) {
-        // 物模型->设备deviceName
-        String deviceName = msg.getDeviceName();
-
-        DeviceInfo realTimeDevice = deviceInfoData.findByDeviceName(deviceName);
-        if ( !realTimeDevice.getState().isOnline() ) {
-            // 切换状态,设备上线
-            DeviceInfo.State state = realTimeDevice.getState();
-            state.setOnline(true);
-            state.setOnlineTime(System.currentTimeMillis());
-            deviceInfoData.save(realTimeDevice);
+        DeviceInfo deviceInfo = deviceInfoData.findByDeviceId(msg.getDeviceId());
+        if (deviceInfo == null) {
+            return;
         }
+
+        String identifier = msg.getIdentifier();
+        String type = msg.getType();
+
+        //过滤下行消息
+        if (ThingModelMessage.TYPE_PROPERTY.equals(type)) {
+            if (ThingModelMessage.ID_PROPERTY_SET.equals(identifier)
+                    || ThingModelMessage.ID_PROPERTY_GET.equals(identifier)
+            ) {
+                return;
+            }
+        }
+
+        //过滤服务下发消息
+        if (ThingModelMessage.TYPE_SERVICE.equals(type) && !identifier.endsWith("_reply")) {
+            return;
+        }
+
+        //过滤oat消息
+        if (ThingModelMessage.TYPE_CONFIG.equals(type) ||
+                ThingModelMessage.TYPE_OTA.equals(type) ||
+                ThingModelMessage.TYPE_LIFETIME.equals(type)) {
+            return;
+        }
+
+        //当前设备状态
+        boolean online = false;
+        //在离线消息
+        if (ThingModelMessage.TYPE_STATE.equals(type)) {
+            online = ThingModelMessage.ID_ONLINE.equals(identifier);
+        } else {
+            //其它消息都认作为在线
+            online = true;
+        }
+
+        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);
+
     }
 
 }

BIN
iot-test-tool/.DS_Store