xu před 4 měsíci
rodič
revize
11adb0b468
8 změnil soubory, kde provedl 105 přidání a 15 odebrání
  1. 24 0
      api/device.go
  2. 8 0
      dao/Sun.go
  3. 1 0
      dao/device.go
  4. 20 0
      initialize/myData.go
  5. 2 1
      modbus/instruction.go
  6. 2 0
      router/router.go
  7. 22 0
      service/device.go
  8. 26 14
      static/data.json

+ 24 - 0
api/device.go

@@ -88,3 +88,27 @@ func GetOnlineDevice(c *gin.Context) {
 		"online": online,
 	}, "ok", c)
 }
+
+func GetSunDevices(c *gin.Context) {
+	devices, err := service.GetSunDevices()
+	if err != nil {
+		model.FailWithMessage(err.Error(), c)
+		return
+	}
+	model.OkWithData(devices, c)
+}
+
+func UpdateRegisterAddress(c *gin.Context) {
+	var id string
+	err := c.ShouldBindJSON(&id)
+	if err != nil {
+		model.FailWithMessage(err.Error(), c)
+		return
+	}
+	err = service.UpdateRegisterAddress(id)
+	if err != nil {
+		model.FailWithMessage(err.Error(), c)
+		return
+	}
+	model.Ok(c)
+}

+ 8 - 0
dao/Sun.go

@@ -0,0 +1,8 @@
+package dao
+
+type Sun struct {
+	DeviceId            string  `json:"deviceId"`
+	BatteryVoltage      float64 `json:"batteryVoltage"`
+	BatteryCurrent      int     `json:"batteryCurrent"`
+	BatteryPlateVoltage float64 `json:"batteryPlateVoltage"`
+}

+ 1 - 0
dao/device.go

@@ -13,4 +13,5 @@ type Device struct {
 	LoopNumber  int          `json:"loopNumber"`
 	OnlineTime  time.Time    `json:"onlineTime"`
 	DeviceLoops []DeviceLoop `json:"deviceLoops"`
+	Sun         Sun          `json:"sun"`
 }

+ 20 - 0
initialize/myData.go

@@ -7,6 +7,7 @@ import (
 	"net"
 	"runtime"
 	"runtime/debug"
+	"server/dao"
 	"server/modbus"
 	"server/model"
 	"server/service"
@@ -164,6 +165,25 @@ func parseData(data *model.QueueData) {
 		switch toString[2:6] {
 		case "0336":
 			batteryVoltage, _ := strconv.ParseInt(toString[6:10], 16, 64)
+			batteryCurrent, _ := strconv.ParseInt(toString[10:14], 16, 64)
+			batteryPlateVoltage, _ := strconv.ParseInt(toString[38:42], 16, 64)
+			sun := dao.Sun{
+				DeviceId:            dev.Sn,
+				BatteryVoltage:      float64(batteryVoltage) / 100,
+				BatteryCurrent:      int(batteryCurrent),
+				BatteryPlateVoltage: float64(batteryPlateVoltage) / 100,
+			}
+			for i, device := range reg.Devices {
+				if device.Sn == dev.Sn {
+					reg.Devices[i].Sun = sun
+				}
+			}
+			regions, err := utils.SaveRegionOnData(reg)
+			err = service.SaveData(regions)
+			if err != nil {
+				fmt.Println(err)
+				return
+			}
 			//电池
 			if float64(batteryVoltage)/100 < 5 {
 				data1 := modbus.DeviceSwitch(8, 1)

+ 2 - 1
modbus/instruction.go

@@ -57,7 +57,8 @@ func SolarEnergyData() (data []byte) {
 	return
 }
 
-func Xie() (data []byte) {
+// UpdateRegisterAddress 修改寄存器地址
+func UpdateRegisterAddress() (data []byte) {
 	data = append(data, 0x01)
 	data = append(data, 0x10)
 	data = append(data, 0x00)

+ 2 - 0
router/router.go

@@ -19,6 +19,8 @@ func InitRouter() *gin.Engine {
 		Group.POST("deviceSwitch", api.DeviceSwitch)
 		Group.POST("deviceBatchSwitch", api.BatchDeviceSwitch)
 		Group.GET("getOnlineDevice", api.GetOnlineDevice)
+		Group.GET("getSunDevices", api.GetSunDevices)
+		Group.PUT("updateRegisterAddress", api.UpdateRegisterAddress)
 	}
 	return router
 }

+ 22 - 0
service/device.go

@@ -82,3 +82,25 @@ func GetOnlineDevice() (total, online int) {
 	}
 	return len(allDevices), len(devices)
 }
+
+func GetSunDevices() (devices []dao.Device, err error) {
+	allDevices, err := utils.GetAllDevices()
+	if err != nil {
+		return nil, err
+	}
+	for _, device := range allDevices {
+		if device.IsSun {
+			devices = append(devices, device)
+		}
+	}
+	return
+}
+
+func UpdateRegisterAddress(deviceId string) error {
+	err := utils.WriteDevice(modbus.UpdateRegisterAddress(), model.ConnectionMap[deviceId])
+	if err != nil {
+		fmt.Printf("UpdateRegisterAddress error: %v\n", err)
+		return err
+	}
+	return nil
+}

+ 26 - 14
static/data.json

@@ -11,16 +11,16 @@
         "name": "测试",
         "genre": "八回路控制",
         "state": 1,
-        "isSun": false,
+        "isSun": true,
         "loopNumber": 8,
-        "onlineTime": "2024-11-13T16:10:22.6196115+08:00",
+        "onlineTime": "2024-11-26T10:19:59.7839971+08:00",
         "deviceLoops": [
           {
             "id": 1,
             "deviceId": "JM36g0RM0ivegAGK",
             "name": "回路12",
             "state": 0,
-            "timeCondition1OnTime": "11:18",
+            "timeCondition1OnTime": "关闭",
             "timeCondition1OffTime": "关闭",
             "timeCondition2OnTime": "关闭",
             "timeCondition2OffTime": "关闭"
@@ -30,7 +30,7 @@
             "deviceId": "JM36g0RM0ivegAGK",
             "name": "回路2",
             "state": 0,
-            "timeCondition1OnTime": "11:18",
+            "timeCondition1OnTime": "关闭",
             "timeCondition1OffTime": "关闭",
             "timeCondition2OnTime": "关闭",
             "timeCondition2OffTime": "关闭"
@@ -40,7 +40,7 @@
             "deviceId": "JM36g0RM0ivegAGK",
             "name": "回路3",
             "state": 0,
-            "timeCondition1OnTime": "11:18",
+            "timeCondition1OnTime": "关闭",
             "timeCondition1OffTime": "关闭",
             "timeCondition2OnTime": "关闭",
             "timeCondition2OffTime": "关闭"
@@ -50,7 +50,7 @@
             "deviceId": "JM36g0RM0ivegAGK",
             "name": "回路4",
             "state": 0,
-            "timeCondition1OnTime": "11:18",
+            "timeCondition1OnTime": "关闭",
             "timeCondition1OffTime": "关闭",
             "timeCondition2OnTime": "关闭",
             "timeCondition2OffTime": "关闭"
@@ -60,7 +60,7 @@
             "deviceId": "JM36g0RM0ivegAGK",
             "name": "回路5",
             "state": 0,
-            "timeCondition1OnTime": "11:18",
+            "timeCondition1OnTime": "关闭",
             "timeCondition1OffTime": "关闭",
             "timeCondition2OnTime": "关闭",
             "timeCondition2OffTime": "关闭"
@@ -70,7 +70,7 @@
             "deviceId": "JM36g0RM0ivegAGK",
             "name": "回路6",
             "state": 0,
-            "timeCondition1OnTime": "10:50",
+            "timeCondition1OnTime": "关闭",
             "timeCondition1OffTime": "关闭",
             "timeCondition2OnTime": "关闭",
             "timeCondition2OffTime": "关闭"
@@ -80,7 +80,7 @@
             "deviceId": "JM36g0RM0ivegAGK",
             "name": "回路7",
             "state": 0,
-            "timeCondition1OnTime": "11:18",
+            "timeCondition1OnTime": "关闭",
             "timeCondition1OffTime": "关闭",
             "timeCondition2OnTime": "关闭",
             "timeCondition2OffTime": "关闭"
@@ -90,12 +90,18 @@
             "deviceId": "JM36g0RM0ivegAGK",
             "name": "回路8",
             "state": 0,
-            "timeCondition1OnTime": "10:50",
+            "timeCondition1OnTime": "关闭",
             "timeCondition1OffTime": "关闭",
             "timeCondition2OnTime": "关闭",
             "timeCondition2OffTime": "关闭"
           }
-        ]
+        ],
+        "sun": {
+          "deviceId": "JM36g0RM0ivegAGK",
+          "batteryVoltage": 0,
+          "batteryCurrent": 0,
+          "batteryPlateVoltage": 0
+        }
       },
       {
         "id": 2,
@@ -103,10 +109,10 @@
         "regionId": 1,
         "name": "测试3",
         "genre": "四回路控制",
-        "state": 1,
+        "state": 0,
         "isSun": false,
         "loopNumber": 4,
-        "onlineTime": "2024-11-13T16:10:23.1872104+08:00",
+        "onlineTime": "2024-11-20T17:25:08.988889+08:00",
         "deviceLoops": [
           {
             "id": 1,
@@ -148,7 +154,13 @@
             "timeCondition2OnTime": "关闭",
             "timeCondition2OffTime": "关闭"
           }
-        ]
+        ],
+        "sun": {
+          "deviceId": "",
+          "batteryVoltage": 0,
+          "batteryCurrent": 0,
+          "batteryPlateVoltage": 0
+        }
       }
     ]
   },