Procházet zdrojové kódy

离线文件保存为空bug修改

xu před 2 měsíci
rodič
revize
33884284b7
3 změnil soubory, kde provedl 118 přidání a 87 odebrání
  1. 1 1
      initialize/myData.go
  2. 33 2
      modbus/operate.go
  3. 84 84
      static/data.json

+ 1 - 1
initialize/myData.go

@@ -210,7 +210,7 @@ func parseData(data *model.QueueData) {
 			regions, err := utils.SaveRegionOnData(reg)
 			err = service.SaveData(regions)
 			if err != nil {
-				logger.Get().Errorln(err)
+				logger.Get().Errorln("设备回路状态" + err.Error())
 				return
 			}
 		}

+ 33 - 2
modbus/operate.go

@@ -1,6 +1,9 @@
 package modbus
 
 import (
+	"encoding/json"
+	"os"
+	"server/dao"
 	"server/logger"
 	"server/model"
 	"server/utils"
@@ -36,16 +39,44 @@ func DealWithOffline() {
 		logger.Get().Errorln("离线" + err.Error())
 		return
 	}
+
+	datafb, err := os.ReadFile("static/data-fb.json")
+	var regionsfb []dao.Region
+	if err := json.Unmarshal(datafb, &regionsfb); err != nil {
+		// 提供更多的上下文信息来帮助调试
+		logger.Get().Errorf("解析 JSON 失败: %v, 原始数据: %s", err, regions)
+	}
+
+	// 检查文件是否为空
+	if len(regions) == 0 {
+		regions = regionsfb // 如果文件为空,返回空数组
+	}
+
+	// 确保 regions 不为 nil
+	if regions == nil {
+		regions = regionsfb
+	}
+
+	now := time.Now()
 	for i, region := range regions {
+		// 确保 region.Devices 不为 nil
+		if region.Devices == nil {
+			continue
+		}
+
 		for i2, device := range region.Devices {
-			if time.Now().After(device.OnlineTime.Add(8*time.Minute)) && regions[i].Devices[i2].State != 0 {
+			if now.After(device.OnlineTime.Add(13*time.Minute)) && device.State != 0 {
 				regions[i].Devices[i2].State = 0
 			}
 		}
 	}
+
 	if len(regions) == 0 {
 		return
 	}
+
 	err = utils.SaveData("static/data.json", regions)
-	logger.Get().Errorln("离线保存" + err.Error())
+	if err != nil {
+		logger.Get().Errorf("离线保存失败: %v", err)
+	}
 }

+ 84 - 84
static/data.json

@@ -10,18 +10,18 @@
         "regionId": 1,
         "name": "维修车间",
         "genre": "八回路控制",
-        "state": 0,
+        "state": 1,
         "isSun": true,
         "loopNumber": 8,
-        "onlineTime": "2025-01-19T17:19:59.057003056+08:00",
+        "onlineTime": "2025-01-19T09:43:59.91502361+08:00",
         "deviceLoops": [
           {
             "id": 1,
             "deviceId": "JM36xWRZq6PiwmKV",
             "name": "回路12",
             "state": 0,
-            "timeCondition1OnTime": "17:30",
-            "timeCondition1OffTime": "07:00",
+            "timeCondition1OnTime": "关闭",
+            "timeCondition1OffTime": "关闭",
             "timeCondition2OnTime": "关闭",
             "timeCondition2OffTime": "关闭"
           },
@@ -30,8 +30,8 @@
             "deviceId": "JM36xWRZq6PiwmKV",
             "name": "回路2",
             "state": 0,
-            "timeCondition1OnTime": "17:30",
-            "timeCondition1OffTime": "07:00",
+            "timeCondition1OnTime": "关闭",
+            "timeCondition1OffTime": "关闭",
             "timeCondition2OnTime": "关闭",
             "timeCondition2OffTime": "关闭"
           },
@@ -40,8 +40,8 @@
             "deviceId": "JM36xWRZq6PiwmKV",
             "name": "回路3",
             "state": 0,
-            "timeCondition1OnTime": "17:30",
-            "timeCondition1OffTime": "07:00",
+            "timeCondition1OnTime": "关闭",
+            "timeCondition1OffTime": "关闭",
             "timeCondition2OnTime": "关闭",
             "timeCondition2OffTime": "关闭"
           },
@@ -50,8 +50,8 @@
             "deviceId": "JM36xWRZq6PiwmKV",
             "name": "回路4",
             "state": 0,
-            "timeCondition1OnTime": "17:30",
-            "timeCondition1OffTime": "07:00",
+            "timeCondition1OnTime": "关闭",
+            "timeCondition1OffTime": "关闭",
             "timeCondition2OnTime": "关闭",
             "timeCondition2OffTime": "关闭"
           },
@@ -60,8 +60,8 @@
             "deviceId": "JM36xWRZq6PiwmKV",
             "name": "回路5",
             "state": 0,
-            "timeCondition1OnTime": "17:30",
-            "timeCondition1OffTime": "07:00",
+            "timeCondition1OnTime": "关闭",
+            "timeCondition1OffTime": "关闭",
             "timeCondition2OnTime": "关闭",
             "timeCondition2OffTime": "关闭"
           },
@@ -70,8 +70,8 @@
             "deviceId": "JM36xWRZq6PiwmKV",
             "name": "回路6",
             "state": 0,
-            "timeCondition1OnTime": "17:30",
-            "timeCondition1OffTime": "07:00",
+            "timeCondition1OnTime": "关闭",
+            "timeCondition1OffTime": "关闭",
             "timeCondition2OnTime": "关闭",
             "timeCondition2OffTime": "关闭"
           },
@@ -80,8 +80,8 @@
             "deviceId": "JM36xWRZq6PiwmKV",
             "name": "回路7",
             "state": 0,
-            "timeCondition1OnTime": "17:30",
-            "timeCondition1OffTime": "07:00",
+            "timeCondition1OnTime": "关闭",
+            "timeCondition1OffTime": "关闭",
             "timeCondition2OnTime": "关闭",
             "timeCondition2OffTime": "关闭"
           },
@@ -90,8 +90,8 @@
             "deviceId": "JM36xWRZq6PiwmKV",
             "name": "回路8",
             "state": 0,
-            "timeCondition1OnTime": "17:30",
-            "timeCondition1OffTime": "07:00",
+            "timeCondition1OnTime": "关闭",
+            "timeCondition1OffTime": "关闭",
             "timeCondition2OnTime": "关闭",
             "timeCondition2OffTime": "关闭"
           }
@@ -109,18 +109,18 @@
         "regionId": 1,
         "name": "洗车房",
         "genre": "四回路控制",
-        "state": 0,
+        "state": 1,
         "isSun": false,
         "loopNumber": 4,
-        "onlineTime": "2025-01-19T18:22:00.035072721+08:00",
+        "onlineTime": "2025-01-19T09:46:00.028035846+08:00",
         "deviceLoops": [
           {
             "id": 1,
             "deviceId": "JM453Sa4a8pdYdgV",
             "name": "回路1",
-            "state": 1,
-            "timeCondition1OnTime": "17:30",
-            "timeCondition1OffTime": "07:00",
+            "state": 0,
+            "timeCondition1OnTime": "关闭",
+            "timeCondition1OffTime": "关闭",
             "timeCondition2OnTime": "关闭",
             "timeCondition2OffTime": "关闭"
           },
@@ -128,9 +128,9 @@
             "id": 2,
             "deviceId": "JM453Sa4a8pdYdgV",
             "name": "回路2",
-            "state": 1,
-            "timeCondition1OnTime": "17:30",
-            "timeCondition1OffTime": "07:00",
+            "state": 0,
+            "timeCondition1OnTime": "关闭",
+            "timeCondition1OffTime": "关闭",
             "timeCondition2OnTime": "关闭",
             "timeCondition2OffTime": "关闭"
           },
@@ -138,9 +138,9 @@
             "id": 3,
             "deviceId": "JM453Sa4a8pdYdgV",
             "name": "回路3",
-            "state": 1,
-            "timeCondition1OnTime": "17:30",
-            "timeCondition1OffTime": "07:00",
+            "state": 0,
+            "timeCondition1OnTime": "关闭",
+            "timeCondition1OffTime": "关闭",
             "timeCondition2OnTime": "关闭",
             "timeCondition2OffTime": "关闭"
           },
@@ -148,9 +148,9 @@
             "id": 4,
             "deviceId": "JM453Sa4a8pdYdgV",
             "name": "回路4",
-            "state": 1,
-            "timeCondition1OnTime": "17:30",
-            "timeCondition1OffTime": "07:00",
+            "state": 0,
+            "timeCondition1OnTime": "关闭",
+            "timeCondition1OffTime": "关闭",
             "timeCondition2OnTime": "关闭",
             "timeCondition2OffTime": "关闭"
           }
@@ -171,15 +171,15 @@
         "state": 1,
         "isSun": false,
         "loopNumber": 4,
-        "onlineTime": "2025-01-19T18:34:00.037774419+08:00",
+        "onlineTime": "2025-01-19T09:46:00.027293637+08:00",
         "deviceLoops": [
           {
             "id": 1,
             "deviceId": "JM45U0zGDCMhgrxc",
             "name": "回路1",
-            "state": 1,
-            "timeCondition1OnTime": "17:30",
-            "timeCondition1OffTime": "07:00",
+            "state": 0,
+            "timeCondition1OnTime": "关闭",
+            "timeCondition1OffTime": "关闭",
             "timeCondition2OnTime": "关闭",
             "timeCondition2OffTime": "关闭"
           },
@@ -187,9 +187,9 @@
             "id": 2,
             "deviceId": "JM45U0zGDCMhgrxc",
             "name": "回路2",
-            "state": 1,
-            "timeCondition1OnTime": "17:30",
-            "timeCondition1OffTime": "07:00",
+            "state": 0,
+            "timeCondition1OnTime": "关闭",
+            "timeCondition1OffTime": "关闭",
             "timeCondition2OnTime": "关闭",
             "timeCondition2OffTime": "关闭"
           },
@@ -197,9 +197,9 @@
             "id": 3,
             "deviceId": "JM45U0zGDCMhgrxc",
             "name": "回路3",
-            "state": 1,
-            "timeCondition1OnTime": "17:30",
-            "timeCondition1OffTime": "07:00",
+            "state": 0,
+            "timeCondition1OnTime": "关闭",
+            "timeCondition1OffTime": "关闭",
             "timeCondition2OnTime": "关闭",
             "timeCondition2OffTime": "关闭"
           },
@@ -207,9 +207,9 @@
             "id": 4,
             "deviceId": "JM45U0zGDCMhgrxc",
             "name": "回路4",
-            "state": 1,
-            "timeCondition1OnTime": "17:30",
-            "timeCondition1OffTime": "07:00",
+            "state": 0,
+            "timeCondition1OnTime": "关闭",
+            "timeCondition1OffTime": "关闭",
             "timeCondition2OnTime": "关闭",
             "timeCondition2OffTime": "关闭"
           }
@@ -230,15 +230,15 @@
         "state": 1,
         "isSun": false,
         "loopNumber": 4,
-        "onlineTime": "2025-01-19T18:34:00.05470463+08:00",
+        "onlineTime": "2025-01-19T09:44:00.051568026+08:00",
         "deviceLoops": [
           {
             "id": 1,
             "deviceId": "JM45S67eqnSKueAK",
             "name": "回路1",
-            "state": 1,
-            "timeCondition1OnTime": "17:30",
-            "timeCondition1OffTime": "07:00",
+            "state": 0,
+            "timeCondition1OnTime": "关闭",
+            "timeCondition1OffTime": "关闭",
             "timeCondition2OnTime": "关闭",
             "timeCondition2OffTime": "关闭"
           },
@@ -246,9 +246,9 @@
             "id": 2,
             "deviceId": "JM45S67eqnSKueAK",
             "name": "回路2",
-            "state": 1,
-            "timeCondition1OnTime": "17:30",
-            "timeCondition1OffTime": "07:00",
+            "state": 0,
+            "timeCondition1OnTime": "关闭",
+            "timeCondition1OffTime": "关闭",
             "timeCondition2OnTime": "关闭",
             "timeCondition2OffTime": "关闭"
           },
@@ -256,9 +256,9 @@
             "id": 3,
             "deviceId": "JM45S67eqnSKueAK",
             "name": "回路3",
-            "state": 1,
-            "timeCondition1OnTime": "17:30",
-            "timeCondition1OffTime": "07:00",
+            "state": 0,
+            "timeCondition1OnTime": "关闭",
+            "timeCondition1OffTime": "关闭",
             "timeCondition2OnTime": "关闭",
             "timeCondition2OffTime": "关闭"
           },
@@ -266,9 +266,9 @@
             "id": 4,
             "deviceId": "JM45S67eqnSKueAK",
             "name": "回路4",
-            "state": 1,
-            "timeCondition1OnTime": "17:30",
-            "timeCondition1OffTime": "07:00",
+            "state": 0,
+            "timeCondition1OnTime": "关闭",
+            "timeCondition1OffTime": "关闭",
             "timeCondition2OnTime": "关闭",
             "timeCondition2OffTime": "关闭"
           }
@@ -287,17 +287,17 @@
         "name": "入厂道路",
         "genre": "八回路控制",
         "state": 1,
-        "isSun": true,
+        "isSun": false,
         "loopNumber": 8,
-        "onlineTime": "2025-01-19T18:33:59.320571748+08:00",
+        "onlineTime": "2025-01-19T09:46:00.039018537+08:00",
         "deviceLoops": [
           {
             "id": 1,
             "deviceId": "JM36g0RM0ivegAGK",
             "name": "回路1",
-            "state": 1,
-            "timeCondition1OnTime": "17:30",
-            "timeCondition1OffTime": "07:00",
+            "state": 0,
+            "timeCondition1OnTime": "关闭",
+            "timeCondition1OffTime": "关闭",
             "timeCondition2OnTime": "关闭",
             "timeCondition2OffTime": "关闭"
           },
@@ -305,9 +305,9 @@
             "id": 2,
             "deviceId": "JM36g0RM0ivegAGK",
             "name": "回路2",
-            "state": 1,
-            "timeCondition1OnTime": "17:30",
-            "timeCondition1OffTime": "07:00",
+            "state": 0,
+            "timeCondition1OnTime": "关闭",
+            "timeCondition1OffTime": "关闭",
             "timeCondition2OnTime": "关闭",
             "timeCondition2OffTime": "关闭"
           },
@@ -315,9 +315,9 @@
             "id": 3,
             "deviceId": "JM36g0RM0ivegAGK",
             "name": "回路3",
-            "state": 1,
-            "timeCondition1OnTime": "17:30",
-            "timeCondition1OffTime": "07:00",
+            "state": 0,
+            "timeCondition1OnTime": "关闭",
+            "timeCondition1OffTime": "关闭",
             "timeCondition2OnTime": "关闭",
             "timeCondition2OffTime": "关闭"
           },
@@ -325,9 +325,9 @@
             "id": 4,
             "deviceId": "JM36g0RM0ivegAGK",
             "name": "回路4",
-            "state": 1,
-            "timeCondition1OnTime": "17:30",
-            "timeCondition1OffTime": "07:00",
+            "state": 0,
+            "timeCondition1OnTime": "关闭",
+            "timeCondition1OffTime": "关闭",
             "timeCondition2OnTime": "关闭",
             "timeCondition2OffTime": "关闭"
           },
@@ -335,9 +335,9 @@
             "id": 5,
             "deviceId": "JM36g0RM0ivegAGK",
             "name": "回路5",
-            "state": 1,
-            "timeCondition1OnTime": "17:30",
-            "timeCondition1OffTime": "07:00",
+            "state": 0,
+            "timeCondition1OnTime": "关闭",
+            "timeCondition1OffTime": "关闭",
             "timeCondition2OnTime": "关闭",
             "timeCondition2OffTime": "关闭"
           },
@@ -345,9 +345,9 @@
             "id": 6,
             "deviceId": "JM36g0RM0ivegAGK",
             "name": "回路6",
-            "state": 1,
-            "timeCondition1OnTime": "17:30",
-            "timeCondition1OffTime": "07:00",
+            "state": 0,
+            "timeCondition1OnTime": "关闭",
+            "timeCondition1OffTime": "关闭",
             "timeCondition2OnTime": "关闭",
             "timeCondition2OffTime": "关闭"
           },
@@ -355,9 +355,9 @@
             "id": 7,
             "deviceId": "JM36g0RM0ivegAGK",
             "name": "回路7",
-            "state": 1,
-            "timeCondition1OnTime": "17:30",
-            "timeCondition1OffTime": "07:00",
+            "state": 0,
+            "timeCondition1OnTime": "关闭",
+            "timeCondition1OffTime": "关闭",
             "timeCondition2OnTime": "关闭",
             "timeCondition2OffTime": "关闭"
           },
@@ -365,9 +365,9 @@
             "id": 8,
             "deviceId": "JM36g0RM0ivegAGK",
             "name": "回路8",
-            "state": 1,
-            "timeCondition1OnTime": "17:30",
-            "timeCondition1OffTime": "07:00",
+            "state": 0,
+            "timeCondition1OnTime": "关闭",
+            "timeCondition1OffTime": "关闭",
             "timeCondition2OnTime": "关闭",
             "timeCondition2OffTime": "关闭"
           }