From 2b1f7a47394363e95deb4dfa0f1c67d41e747f7f Mon Sep 17 00:00:00 2001
From: 648540858 <648540858@qq.com>
Date: 星期三, 01 二月 2023 10:56:40 +0800
Subject: [PATCH] Merge branch 'wvp-28181-2.0' into fix-269

---
 src/main/java/com/genersoft/iot/vmp/gb28181/session/CatalogDataCatch.java |  146 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 146 insertions(+), 0 deletions(-)

diff --git a/src/main/java/com/genersoft/iot/vmp/gb28181/session/CatalogDataCatch.java b/src/main/java/com/genersoft/iot/vmp/gb28181/session/CatalogDataCatch.java
new file mode 100644
index 0000000..38cdf7c
--- /dev/null
+++ b/src/main/java/com/genersoft/iot/vmp/gb28181/session/CatalogDataCatch.java
@@ -0,0 +1,146 @@
+package com.genersoft.iot.vmp.gb28181.session;
+
+import com.genersoft.iot.vmp.gb28181.bean.CatalogData;
+import com.genersoft.iot.vmp.gb28181.bean.Device;
+import com.genersoft.iot.vmp.gb28181.bean.DeviceChannel;
+import com.genersoft.iot.vmp.gb28181.bean.SyncStatus;
+import com.genersoft.iot.vmp.storager.IVideoManagerStorage;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.scheduling.annotation.Scheduled;
+import org.springframework.stereotype.Component;
+
+import java.time.Instant;
+import java.util.*;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.TimeUnit;
+
+@Component
+public class CatalogDataCatch {
+
+    public static Map<String, CatalogData> data = new ConcurrentHashMap<>();
+
+    @Autowired
+    private IVideoManagerStorage storager;
+
+    public void addReady(Device device, int sn ) {
+        CatalogData catalogData = data.get(device.getDeviceId());
+        if (catalogData == null || catalogData.getStatus().equals(CatalogData.CatalogDataStatus.end)) {
+            catalogData = new CatalogData();
+            catalogData.setChannelList(Collections.synchronizedList(new ArrayList<>()));
+            catalogData.setDevice(device);
+            catalogData.setSn(sn);
+            catalogData.setStatus(CatalogData.CatalogDataStatus.ready);
+            catalogData.setLastTime(Instant.now());
+            data.put(device.getDeviceId(), catalogData);
+        }
+    }
+
+    public void put(String deviceId, int sn, int total, Device device, List<DeviceChannel> deviceChannelList) {
+        CatalogData catalogData = data.get(deviceId);
+        if (catalogData == null) {
+            catalogData = new CatalogData();
+            catalogData.setSn(sn);
+            catalogData.setTotal(total);
+            catalogData.setDevice(device);
+            catalogData.setChannelList(deviceChannelList);
+            catalogData.setStatus(CatalogData.CatalogDataStatus.runIng);
+            catalogData.setLastTime(Instant.now());
+            data.put(deviceId, catalogData);
+        }else {
+            // 鍚屼竴涓澶囩殑閫氶亾鍚屾璇锋眰鍙�冭檻涓�涓紝鍏朵粬鐨勭洿鎺ュ拷鐣�
+            if (catalogData.getSn() != sn) {
+                return;
+            }
+            catalogData.setTotal(total);
+            catalogData.setDevice(device);
+            catalogData.setStatus(CatalogData.CatalogDataStatus.runIng);
+            catalogData.getChannelList().addAll(deviceChannelList);
+            catalogData.setLastTime(Instant.now());
+        }
+    }
+
+    public List<DeviceChannel> get(String deviceId) {
+        CatalogData catalogData = data.get(deviceId);
+        if (catalogData == null) {
+            return null;
+        }
+        return catalogData.getChannelList();
+    }
+
+    public int getTotal(String deviceId) {
+        CatalogData catalogData = data.get(deviceId);
+        if (catalogData == null) {
+            return 0;
+        }
+        return catalogData.getTotal();
+    }
+
+    public SyncStatus getSyncStatus(String deviceId) {
+        CatalogData catalogData = data.get(deviceId);
+        if (catalogData == null) {
+            return null;
+        }
+        SyncStatus syncStatus = new SyncStatus();
+        syncStatus.setCurrent(catalogData.getChannelList().size());
+        syncStatus.setTotal(catalogData.getTotal());
+        syncStatus.setErrorMsg(catalogData.getErrorMsg());
+        if (catalogData.getStatus().equals(CatalogData.CatalogDataStatus.end)) {
+            syncStatus.setSyncIng(false);
+        }else {
+            syncStatus.setSyncIng(true);
+        }
+        return syncStatus;
+    }
+
+    public boolean isSyncRunning(String deviceId) {
+        CatalogData catalogData = data.get(deviceId);
+        if (catalogData == null) {
+            return false;
+        }
+        return !catalogData.getStatus().equals(CatalogData.CatalogDataStatus.end);
+    }
+
+    @Scheduled(fixedRate = 5 * 1000)   //姣�5绉掓墽琛屼竴娆�, 鍙戠幇鏁版嵁5绉掓湭鏇存柊鍒欑Щ闄ゆ暟鎹苟璁や负鏁版嵁鎺ユ敹瓒呮椂
+    private void timerTask(){
+        Set<String> keys = data.keySet();
+
+        Instant instantBefore5S = Instant.now().minusMillis(TimeUnit.SECONDS.toMillis(5));
+        Instant instantBefore30S = Instant.now().minusMillis(TimeUnit.SECONDS.toMillis(30));
+
+        for (String deviceId : keys) {
+            CatalogData catalogData = data.get(deviceId);
+            if ( catalogData.getLastTime().isBefore(instantBefore5S)) {
+                // 瓒呰繃浜旂鏀朵笉鍒版秷鎭换鍔¤秴鏃讹紝 鍙洿鏂拌繖涓�閮ㄥ垎鏁版嵁, 鏀跺埌鏁版嵁涓庡0鏄庣殑鎬绘暟涓�鑷达紝鍒欓噸缃�氶亾鏁版嵁锛屾暟鎹笉鍏ㄥ垯鍙鏀跺埌鐨勬暟鎹仛鏇存柊鎿嶄綔
+                if (catalogData.getStatus().equals(CatalogData.CatalogDataStatus.runIng)) {
+                    if (catalogData.getTotal() == catalogData.getChannelList().size()) {
+                        storager.resetChannels(catalogData.getDevice().getDeviceId(), catalogData.getChannelList());
+                    }else {
+                        storager.updateChannels(catalogData.getDevice().getDeviceId(), catalogData.getChannelList());
+                    }
+                    String errorMsg = "鏇存柊鎴愬姛锛屽叡" + catalogData.getTotal() + "鏉★紝宸叉洿鏂�" + catalogData.getChannelList().size() + "鏉�";
+                    catalogData.setErrorMsg(errorMsg);
+                    if (catalogData.getTotal() != catalogData.getChannelList().size()) {
+
+                    }
+                }else if (catalogData.getStatus().equals(CatalogData.CatalogDataStatus.ready)) {
+                    String errorMsg = "鍚屾澶辫触锛岀瓑寰呭洖澶嶈秴鏃�";
+                    catalogData.setErrorMsg(errorMsg);
+                }
+                catalogData.setStatus(CatalogData.CatalogDataStatus.end);
+            }
+            if (catalogData.getStatus().equals(CatalogData.CatalogDataStatus.end) && catalogData.getLastTime().isBefore(instantBefore30S)) { // 瓒呰繃涓夊崄绉掞紝濡傛灉鏍囪涓篹nd鍒欏垹闄�
+                data.remove(deviceId);
+            }
+        }
+    }
+
+
+    public void setChannelSyncEnd(String deviceId, String errorMsg) {
+        CatalogData catalogData = data.get(deviceId);
+        if (catalogData == null) {
+            return;
+        }
+        catalogData.setStatus(CatalogData.CatalogDataStatus.end);
+        catalogData.setErrorMsg(errorMsg);
+    }
+}

--
Gitblit v1.8.0