From a9098372191b3c51995d41ee28404d1b71244d98 Mon Sep 17 00:00:00 2001
From: jiazx0107@163.com <jiazx0107@163.com>
Date: 星期二, 12 十二月 2023 18:41:04 +0800
Subject: [PATCH] 提交网关心跳和设备状态2

---
 src/main/java/com/fzzy/gateway/service/GatewayDeviceService.java |  114 ++++++++++++++++++++++++++++++++++++++++++++++++++++-----
 1 files changed, 104 insertions(+), 10 deletions(-)

diff --git a/src/main/java/com/fzzy/gateway/service/GatewayDeviceService.java b/src/main/java/com/fzzy/gateway/service/GatewayDeviceService.java
index e7a1784..96f40ed 100644
--- a/src/main/java/com/fzzy/gateway/service/GatewayDeviceService.java
+++ b/src/main/java/com/fzzy/gateway/service/GatewayDeviceService.java
@@ -3,21 +3,29 @@
 import com.bstek.dorado.annotation.DataProvider;
 import com.bstek.dorado.annotation.DataResolver;
 import com.bstek.dorado.annotation.Expose;
+import com.fzzy.api.Constant;
+import com.fzzy.api.data.ApiCommonDevice;
+import com.fzzy.api.data.GatewayDeviceType;
 import com.fzzy.api.utils.ContextUtil;
+import com.fzzy.gateway.GatewayUtils;
 import com.fzzy.gateway.entity.GatewayDevice;
 import com.fzzy.gateway.service.repository.GatewayDeviceRep;
+import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.BeanUtils;
+import org.springframework.data.domain.Sort;
 import org.springframework.stereotype.Component;
 
 import javax.annotation.Resource;
+import java.util.ArrayList;
+import java.util.Date;
 import java.util.List;
 
+@Slf4j
 @Component
 public class GatewayDeviceService {
 
     @Resource
     private GatewayDeviceRep gatewayDeviceRep;
-
 
     /**
      * gatewayDeviceService#listAll
@@ -26,26 +34,62 @@
      */
     @DataProvider
     public List<GatewayDevice> listAll() {
+        Sort sort = new Sort(Sort.Direction.ASC, "deviceId");
 
-        List<GatewayDevice> list = gatewayDeviceRep.findAll();
 
-        return list;
+        List<GatewayDevice> list = gatewayDeviceRep.findAll(sort);
+
+        if (null == list || list.isEmpty()) return list;
+
+        List<GatewayDevice> result = new ArrayList<>();
+
+        ApiCommonDevice commonDevice;
+        for (GatewayDevice device : list) {
+            commonDevice = Constant.getCommonDeviceCache(device.getDeviceSn());
+            if (null != commonDevice) {
+                device.setIp(commonDevice.getIp());
+                device.setPort(commonDevice.getPort());
+                device.setStatus(commonDevice.getStatus());
+                device.setOnlineTime(commonDevice.getOnlineTime());
+            }
+            result.add(device);
+        }
+        return result;
     }
 
     /**
      * gatewayDeviceService#updateSave
      *
-     * @param entity
+     * @param data
      */
     @DataResolver
-    public void updateSave(GatewayDevice entity) {
-        GatewayDevice data = new GatewayDevice();
-        BeanUtils.copyProperties(entity, data);
+    public void updateSave(GatewayDevice data) {
+        GatewayDevice data2 = new GatewayDevice();
+        BeanUtils.copyProperties(data, data2);
 
-        if (null == data.getId()) {
-            data.setId(ContextUtil.getUUID());
+
+        if (null == data2.getStatus()) {
+            data.setStatus(Constant.YN_Y);
+            if (GatewayDeviceType.TYPE_07.equals(data2.getType())) {
+                data.setStatus(Constant.YN_N);
+            }
         }
-        gatewayDeviceRep.save(data);
+
+        if (null == data2.getDeviceSn()) {
+            if (null != data2.getIp()) {
+                data.setDeviceSn(data2.getIp());
+            } else {
+                data.setDeviceSn(data2.getDeviceId());
+            }
+        }
+
+        if (null == data2.getId()) {
+            data2.setId(ContextUtil.getUUID());
+            gatewayDeviceRep.save(data2);
+        } else {
+            gatewayDeviceRep.save(data2);
+        }
+        flushCache();
     }
 
     /**
@@ -58,6 +102,56 @@
         GatewayDevice data2 = new GatewayDevice();
         BeanUtils.copyProperties(data, data2);
         gatewayDeviceRep.delete(data2);
+
+        GatewayUtils.removeCache(data2);
+
+        flushCache();
         return null;
     }
+
+    public void flushCache() {
+        List<GatewayDevice> list = listAll();
+        if (null == list || list.isEmpty()) return;
+        for (GatewayDevice device : list) {
+            GatewayUtils.add2Cache(device);
+        }
+    }
+
+
+    @Expose
+    public String test() {
+        log.info("-----------test-------------------");
+        return "SUCCESS";
+    }
+
+    /**
+     * 鏍规嵁瀹為檯閫氳鍒嗘満璁剧疆锛屽綋鍓嶅垎鏈哄湪绾�
+     *
+     * @param commonDevice 瀹為檯閫氳璁惧
+     */
+    public void onlineByCommonDevice(ApiCommonDevice commonDevice) {
+        List<GatewayDevice> list = GatewayUtils.getCacheByDeviceSn2(commonDevice.getSn());
+        if (null == list || list.isEmpty()) return;
+
+        for (GatewayDevice device : list) {
+            device.setIp(commonDevice.getIp());
+            device.setPort(commonDevice.getPort());
+            device.setOnlineTime(new Date());
+            device.setStatus(Constant.YN_Y);
+
+            GatewayUtils.add2Cache(device);
+        }
+    }
+
+    public void OfflineByCommonDevice(ApiCommonDevice commonDevice) {
+        List<GatewayDevice> list = GatewayUtils.getCacheByDeviceSn2(commonDevice.getSn());
+        if (null == list || list.isEmpty()) return;
+
+        for (GatewayDevice device : list) {
+            device.setIp(commonDevice.getIp());
+            device.setPort(commonDevice.getPort());
+            device.setStatus(Constant.YN_N);
+            GatewayUtils.add2Cache(device);
+        }
+    }
 }

--
Gitblit v1.9.3