From e8c97695dd6930465e66b8fac819301f03624512 Mon Sep 17 00:00:00 2001 From: zhangzengfei <zhangzengfei@smartai.com> Date: 星期二, 01 八月 2023 11:19:37 +0800 Subject: [PATCH] 完善连接过程. 添加重连 --- collector/collector.go | 89 +++++++++++++++++++++++++++++++++----------- 1 files changed, 66 insertions(+), 23 deletions(-) diff --git a/collector/collector.go b/collector/collector.go index dafcb04..e70421d 100644 --- a/collector/collector.go +++ b/collector/collector.go @@ -7,6 +7,8 @@ "plc-recorder/logger" "plc-recorder/msg" + + plc4go "github.com/apache/plc4x/plc4go/pkg/api" ) var mapTask sync.Map @@ -19,7 +21,7 @@ func InitTask() { device := msg.PLCDevice{ Id: "0", - Ip: "192.168.20.188", + Ip: "192.168.1.188", Address: []int{17021}, Interval: 1, } @@ -32,32 +34,73 @@ mapTask.Store(device.Id, &proc) - go runCollectionTask(ctx, &device) + go connectingDevice(ctx, &device) } -func runCollectionTask(ctx context.Context, device *msg.PLCDevice) { - // 鍒涘缓modbusTCP杩炴帴, 寰幆鏌ヨ鏁版嵁骞朵笂鎶� +func connectingDevice(ctx context.Context, dev *msg.PLCDevice) { + plcResponse := msg.PLCResponse{ + Id: dev.Id, + Name: dev.Name, + Ip: dev.Ip, + Online: false, + } + for { - err, plcConnection := NewModbusConnection(device.Ip) - if err != nil { - logger.Warn("Error connecting to PLC: %s, ip:", device.Name, device.Ip) - - time.Sleep(30 * time.Second) - continue - } - - for { - select { - case <-ctx.Done(): - logger.Warn("Plc device %s, ip: %s, end of collection.", device.Name, device.Ip) - plcConnection.Close() - return - default: - - // 鏍规嵁璁剧疆鐨勫湴鍧�鏌ヨ鏁版嵁锛屼笂鎶� - // 鏆傚仠 - time.Sleep(time.Duration(device.Interval) * time.Second) + select { + case <-ctx.Done(): + logger.Warn("plc device %s, ip: %s, end of connecting.", dev.Name, dev.Ip) + return + default: + plcConnection, err := NewModbusConnection(dev.Ip) + if err != nil { + logger.Warn("error connecting to PLC: %s, ip: %s", dev.Name, dev.Ip) + plcResponse.Online = false + msg.SendDeviceLiveData(&plcResponse) + time.Sleep(30 * time.Second) + } else { + // 杩炴帴鎴愬姛鍚�, 寮�濮嬮噰闆嗘暟鎹�, 浼氬垽鏂繛鎺ユ槸鍚︽湁鏁�, 鏂紑鍚庝細閲囬泦浠诲姟浼氶��鍑�, 缁х画閲嶆柊灏濊瘯杩炴帴璁惧 + runCollectionTask(ctx, dev, plcConnection) } } } } + +func runCollectionTask(ctx context.Context, dev *msg.PLCDevice, conn plc4go.PlcConnection) { + // 鍒涘缓modbusTCP杩炴帴, 寰幆鏌ヨ鏁版嵁骞朵笂鎶� + plcResponse := msg.PLCResponse{ + Id: dev.Id, + Name: dev.Name, + Ip: dev.Ip, + Online: true, + Data: nil, + } + + for { + select { + case <-ctx.Done(): + logger.Warn("plc device %s, ip: %s, end of collection.", dev.Name, dev.Ip) + conn.Close() + return + default: + if !conn.IsConnected() { + logger.Warn("plc device %s, ip: %s, disconnected.", dev.Name, dev.Ip) + return + } + + // 鏍规嵁璁剧疆鐨勫湴鍧�鏌ヨ鏁版嵁锛屼笂鎶� + plcResponse.Data = make(map[int][]byte, 0) + for _, addr := range dev.Address { + result, err := ReadHoldingRegister(conn, addr) + if err != nil { + logger.Warn("plc device Read Holding Register error, %s", err.Error()) + } else { + plcResponse.Data[addr] = result + } + } + + msg.SendDeviceLiveData(&plcResponse) + // 闂撮殧鏃堕棿 + time.Sleep(time.Duration(dev.Interval) * time.Second) + } + } +} -- Gitblit v1.8.0