From fbae58e8f6e6159325ef41b85917ddb468d1b98a Mon Sep 17 00:00:00 2001
From: zhangqian <zhangqian@123.com>
Date: 星期六, 16 九月 2023 18:17:23 +0800
Subject: [PATCH] 读写plc连续失败20次认为失去心跳

---
 pkg/plc/modbusx/connection_manager.go |   16 ++++++++++++----
 1 files changed, 12 insertions(+), 4 deletions(-)

diff --git a/pkg/plc/modbusx/connection_manager.go b/pkg/plc/modbusx/connection_manager.go
index 898968f..3757d34 100644
--- a/pkg/plc/modbusx/connection_manager.go
+++ b/pkg/plc/modbusx/connection_manager.go
@@ -3,7 +3,6 @@
 import (
 	"github.com/goburrow/modbus"
 	"sync"
-	"time"
 )
 
 type ConnectionManager struct {
@@ -37,10 +36,11 @@
 	cm.connections[address] = connection
 }
 
-func (cm *ConnectionManager) CheckConnect(conn modbus.Client, timeout time.Duration) (bool, error) {
-	return true, nil
+func (cm *ConnectionManager) RemoveConnection(address string) {
+	cm.mu.Lock()
+	defer cm.mu.Unlock()
+	delete(cm.connections, address)
 }
-
 func getModbusConnection(ipAddr string) modbus.Client {
 	if conn, ok := connectionManager.GetConnection(ipAddr); ok {
 		return conn
@@ -50,6 +50,14 @@
 	return conn
 }
 
+func unsetModbusConnection(ipAddr string) {
+	_, ok := connectionManager.GetConnection(ipAddr)
+	if !ok {
+		return
+	}
+	connectionManager.RemoveConnection(ipAddr)
+}
+
 func newGetModbusConnection(ipAddr string) modbus.Client {
 	return modbus.TCPClient(ipAddr)
 }

--
Gitblit v1.8.0