Explorar el Código

停用主机服务时 删除主机下设备数据

jingyuanchao hace 1 año
padre
commit
8b9f2b924a

+ 6 - 1
soc-api/soc-api-system/src/main/java/com/xunmei/system/api/RemoteHostService.java

@@ -9,12 +9,13 @@ import com.xunmei.common.core.web.domain.AjaxResult;
 import com.xunmei.system.api.domain.SysConfig;
 import com.xunmei.system.api.dto.SysJobDTO;
 import com.xunmei.system.api.factory.RemoteConfigFallbackFactory;
+import com.xunmei.system.api.factory.RemoteHostFallbackFactory;
 import org.springframework.cloud.openfeign.FeignClient;
 import org.springframework.web.bind.annotation.*;
 
 import java.util.List;
 
-@FeignClient(contextId = "remoteHostService", value = ServiceNameConstants.HOST, fallbackFactory = RemoteConfigFallbackFactory.class)
+@FeignClient(contextId = "remoteHostService", value = ServiceNameConstants.HOST, fallbackFactory = RemoteHostFallbackFactory.class)
 public interface RemoteHostService {
     @PostMapping("/getRecordInfo")
      AjaxResult getRecordInfo(@RequestBody VideoIntegrityGetReq req);
@@ -27,4 +28,8 @@ public interface RemoteHostService {
 
     @GetMapping("/closeSession")
     AjaxResult closeSession(@RequestParam(value = "iotCode") String iotCode);
+
+    @GetMapping("/delData")
+    AjaxResult delData(@RequestParam(value = "iotCode") String iotCode);
+
 }

+ 5 - 0
soc-api/soc-api-system/src/main/java/com/xunmei/system/api/factory/RemoteHostFallbackFactory.java

@@ -41,6 +41,11 @@ public class RemoteHostFallbackFactory implements FallbackFactory<RemoteHostServ
             public AjaxResult closeSession(String iotCode) {
                 return null;
             }
+
+            @Override
+            public AjaxResult delData(String iotCode) {
+                return null;
+            }
         };
     }
 }

+ 2 - 0
soc-modules/soc-modules-host/src/main/java/com/xunmei/host/iot/service/IIotDeviceInfoService.java

@@ -78,4 +78,6 @@ public interface IIotDeviceInfoService extends IService<IotDeviceInfo> {
     IotDeviceInfo selectByTokenProductAndDeviceCode(String token, String productName, String deviceType);
 
     void deviceStatusChange(List<DeviceStatusInfo> statusInfos, IotServerInfo serverInfo);
+
+    void delData(String iotCode);
 }

+ 10 - 16
soc-modules/soc-modules-host/src/main/java/com/xunmei/host/iot/service/impl/IotDeviceInfoServiceImpl.java

@@ -275,6 +275,15 @@ public class IotDeviceInfoServiceImpl extends ServiceImpl<IotDeviceInfoMapper, I
     }
 
     @Override
+    public void delData(String iotCode) {
+        if (ObjectUtil.isEmpty(iotCode)){
+            return;
+        }
+        deleteDeviceByToken(iotCode);
+        LogUtils.SOCKET_BASE_DEVICE_INFO.info("删除设备数据,token:{}", iotCode);
+    }
+
+    @Override
     public ProductEnums product() {
         return ProductEnums.DETECTION_HOST;
     }
@@ -290,12 +299,6 @@ public class IotDeviceInfoServiceImpl extends ServiceImpl<IotDeviceInfoMapper, I
         return result.toString();
     }
 
-    private static final String LOCK_DVS_BASE_INFO = "websocket:lock:dvs_base_info";
-
-    private static final String LOCK_DEVICE_BASE_INFO = "websocket:lock:device_base_info";
-
-    private static final String LOCK_ALARM_HOST_BASE_INFO = "websocket:lock:alarmHost_base_info";
-
     @Override
     @Transactional(rollbackFor = Exception.class)
     public Object execute(WebsocketExecuteReq req) {
@@ -309,19 +312,15 @@ public class IotDeviceInfoServiceImpl extends ServiceImpl<IotDeviceInfoMapper, I
         IotServerInfo serverInfo = iotServerInfoService.selectByToken(token);
         if (ObjectUtil.isEmpty(serverInfo)) {
             LogUtils.WEBSOCKET_MSG.error("未查询到token为 {} 的服务器信息", token);
-            return "";
+            return null;
         }
         String event = req.getEvent();
         switch (event) {
             case WebSocketConstants.DEVICES_EVENT:
             case WebSocketConstants.GET_DEVICES_SERVICES:
                 List<DvsBaseInfo> dataList = dataArray.toJavaList(DvsBaseInfo.class);
-                String lockKey = LOCK_DEVICE_BASE_INFO + token;
-                String key = LOCK_ALARM_HOST_BASE_INFO + token;
                 handleDvsAndFSUDeviceInfo(dataList, serverInfo);
                 handleAlarmHostDeviceInfo(dataList, serverInfo);
-                /*lockAndExecute(lockKey, () -> handleDvsAndFSUDeviceInfo(dataList, serverInfo));
-                lockAndExecute(key, () -> handleAlarmHostDeviceInfo(dataList, serverInfo));*/
                 return WebsocketResult.replySuccess(req.getTopic(), req.getId(), req.getProductName(), req.getDeviceName());
             case WebSocketConstants.DEVICES_STATUS_EVENT:
                 List<DeviceStatusInfo> statusInfos = dataArray.toJavaList(DeviceStatusInfo.class);
@@ -330,11 +329,6 @@ public class IotDeviceInfoServiceImpl extends ServiceImpl<IotDeviceInfoMapper, I
             default:
                 break;
         }
-        /*final RedissonClient client = RedisUtils.getClient();
-        final boolean lock = client.getLock(event + req.getToken()).tryLock();
-        if (lock) {
-
-        }*/
         return null;
     }
 

+ 13 - 2
soc-modules/soc-modules-host/src/main/java/com/xunmei/host/north/controller/HostController.java

@@ -4,6 +4,7 @@ package com.xunmei.host.north.controller;
 import com.xunmei.common.core.domain.host.remote.DiskInfoGetReq;
 import com.xunmei.common.core.domain.host.remote.VideoIntegrityGetReq;
 import com.xunmei.common.core.web.domain.AjaxResult;
+import com.xunmei.host.iot.service.IIotDeviceInfoService;
 import com.xunmei.host.protection.service.IIotAlarmHostService;
 import com.xunmei.host.video.service.IVideoIntegrityCheckService;
 import com.xunmei.host.video.service.IotDvrHardDiskDetectionService;
@@ -41,6 +42,9 @@ public class HostController {
     @Resource
     IIotAlarmHostService alarmHostService;
 
+    @Resource
+    IIotDeviceInfoService deviceInfoService;
+
 
     @ApiOperation("主动获取所有的主机基本信息")
     @GetMapping({"/getDevices"})
@@ -51,14 +55,14 @@ public class HostController {
 
     @ApiOperation("主动获取指定通道的录像完整性数据")
     @GetMapping({"/getRecordInfo"})
-    public AjaxResult videoIntegrityCheck(@RequestBody VideoIntegrityGetReq req ) {
+    public AjaxResult videoIntegrityCheck(@RequestBody VideoIntegrityGetReq req) {
         this.videoIntegrityCheckService.getRecordInfo(req);
         return AjaxResult.success();
     }
 
     @ApiOperation("主动获取指定主机的磁盘数据")
     @GetMapping({"/getDiskInfos"})
-    public AjaxResult getDiskInfos(@RequestBody DiskInfoGetReq req ) {
+    public AjaxResult getDiskInfos(@RequestBody DiskInfoGetReq req) {
         this.diskDetectionService.getDiskInfos(req);
         return AjaxResult.success();
     }
@@ -81,4 +85,11 @@ public class HostController {
         WebSocketSessionHolder.closeServe(iotCode);
         return AjaxResult.success();
     }
+
+    @ApiOperation("删除设备数据")
+    @GetMapping({"/delData"})
+    public AjaxResult delData(String iotCode) {
+        deviceInfoService.delData(iotCode);
+        return AjaxResult.success();
+    }
 }

+ 1 - 0
soc-modules/soc-modules-host/src/main/java/com/xunmei/host/websocket/handler/SocWebSocketHandler.java

@@ -130,6 +130,7 @@ public class SocWebSocketHandler extends AbstractWebSocketHandler {
     protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
         // 从WebSocket会话中获取登录用户信息
         String token = WebSocketSessionHolder.updateToken(session);
+        //String token = "de672e88-c120-41d8-8f5e-4a882d13b894";
         if (StringUtils.isEmpty(token)) {
             LogUtils.WEBSOCKET_MSG.error("会话消息不存在,ip:{}", WebSocketUtils.getIp(session));
             return;

+ 13 - 6
soc-modules/soc-modules-iot/src/main/java/com/xunmei/iot/service/impl/IotServerInfoServiceImpl.java

@@ -124,19 +124,26 @@ public class IotServerInfoServiceImpl extends ServiceImpl<IotServerInfoMapper, I
         if (1 == req.getEnable()) {
             CompletableFuture.runAsync(() -> {
                 try {
-                    final AjaxResult result = hostService.closeSession(info.getIotCode());
-                    if (result.isSuccess()) {
-                        info.setIotStatus(IotServerConnectStatus.OFFLINE.getIdx());
-                        updateById(info);
-                    }
+                    stopServerAndDelData(info);
                 } catch (Exception e) {
-                    throw new RuntimeException(e);
+                    log.error("iot服务断开失败", e);
                 }
             });
+
         }
 
     }
 
+    private void stopServerAndDelData(IotServerInfo info){
+        final AjaxResult result = hostService.closeSession(info.getIotCode());
+        if (result.isSuccess()) {
+            info.setIotStatus(IotServerConnectStatus.OFFLINE.getIdx());
+            updateById(info);
+        }
+        //删除iot服务对应的数据
+        hostService.delData(info.getIotCode());
+    }
+
     @Override
     public void export(IotServerInfoPageDto req, HttpServletResponse response) {
         req.setPageSize(Integer.MAX_VALUE);