xu před 4 měsíci
rodič
revize
9c962d8f55
3 změnil soubory, kde provedl 42 přidání a 7 odebrání
  1. 36 1
      initialize/myData.go
  2. 1 1
      modbus/operate.go
  3. 5 5
      static/data.json

+ 36 - 1
initialize/myData.go

@@ -5,6 +5,7 @@ import (
 	"fmt"
 	"log"
 	"net"
+	"os"
 	"runtime"
 	"runtime/debug"
 	"server/dao"
@@ -13,7 +14,9 @@ import (
 	"server/service"
 	"server/utils"
 	"strconv"
+	"strings"
 	"sync"
+	"syscall"
 	"time"
 )
 
@@ -93,8 +96,19 @@ func (o *ModbusHandler) ReadAndHandle(conn net.Conn, deviceId string) {
 	for {
 		buffer := make([]byte, 1024)
 		n, err := conn.Read(buffer)
+		//if err != nil {
+		//	log.Println("Error reading from connection:", err)
+		//	return
+		//}
 		if err != nil {
-			log.Println("Error reading from connection:", err)
+			if isConnReset(err) {
+				fmt.Println("连接被远程主机强制关闭")
+			} else if os.IsTimeout(err) {
+				fmt.Println("读取操作超时")
+			} else {
+				// 处理其他类型的错误
+				fmt.Printf("读取错误: %s\n", err)
+			}
 			return
 		}
 		queueData := model.QueueData{
@@ -111,6 +125,27 @@ func (o *ModbusHandler) ReadAndHandle(conn net.Conn, deviceId string) {
 	}
 }
 
+func isConnReset(err error) bool {
+	if opErr, ok := err.(*net.OpError); ok {
+		if opErr.Err == syscall.ECONNRESET {
+			return true // Unix-like 系统上的 ECONNRESET
+		} else if runtime.GOOS == "windows" {
+			// Windows 上的 WSAECONNRESET 通常是通过错误消息识别的
+			if se, ok := opErr.Err.(*os.SyscallError); ok {
+				if errno, ok := se.Err.(syscall.Errno); ok {
+					if errno == 10054 { // 10054 对应 WSAECONNRESET
+						return true
+					}
+				}
+			} else if strings.Contains(opErr.Err.Error(), "WSAECONNRESET") {
+				// 如果错误消息包含 WSAECONNRESET,也认为是连接被重置
+				return true
+			}
+		}
+	}
+	return false
+}
+
 func (o *ModbusHandler) Handler() interface{} {
 	defer func() {
 		if err := recover(); err != nil {

+ 1 - 1
modbus/operate.go

@@ -10,7 +10,7 @@ import (
 func GetSunPowerInfo() error {
 	for id, conn := range model.ConnectionMap {
 		_, dev, err := utils.GetDataByDeviceId(id)
-		if dev.IsSun {
+		if dev.IsSun && dev.State == 1 { //打开  并且在线
 			err = utils.WriteDevice(SolarEnergyData(), conn)
 			if err != nil {
 				return err

+ 5 - 5
static/data.json

@@ -10,10 +10,10 @@
         "regionId": 1,
         "name": "测试",
         "genre": "八回路控制",
-        "state": 1,
+        "state": 0,
         "isSun": true,
         "loopNumber": 8,
-        "onlineTime": "2024-11-26T10:19:59.7839971+08:00",
+        "onlineTime": "2024-12-02T15:15:59.2168898+08:00",
         "deviceLoops": [
           {
             "id": 1,
@@ -105,14 +105,14 @@
       },
       {
         "id": 2,
-        "sn": "JM45zpMXCeW4SG7G",
+        "sn": "JM45hGVLg03gz1Kh",
         "regionId": 1,
-        "name": "测试3",
+        "name": "测试31",
         "genre": "四回路控制",
         "state": 0,
         "isSun": false,
         "loopNumber": 4,
-        "onlineTime": "2024-11-20T17:25:08.988889+08:00",
+        "onlineTime": "2024-12-02T15:13:28.6958017+08:00",
         "deviceLoops": [
           {
             "id": 1,