From 6bd2d18ce26adf8fc4a43ecbc75e16a5af5a150c Mon Sep 17 00:00:00 2001 From: hmelder Date: Fri, 15 Nov 2024 23:49:26 +0100 Subject: [PATCH] succd: Do not exit prematurely if modbus connections fails --- succbone/succd/main.go | 16 ++++++++++++++-- succbone/succd/modbus.go | 23 ----------------------- 2 files changed, 14 insertions(+), 25 deletions(-) diff --git a/succbone/succd/main.go b/succbone/succd/main.go index 52a10db..bb8aed7 100644 --- a/succbone/succd/main.go +++ b/succbone/succd/main.go @@ -6,7 +6,9 @@ import ( "net/http" "os" "os/signal" + "time" + "github.com/simonvetter/modbus" "k8s.io/klog" ) @@ -55,9 +57,19 @@ func main() { } d.adcPirani = adc - err = d.modbusConnect() + // Setup modbus client + d.modbusClient, err = modbus.NewClient(&modbus.ClientConfiguration{ + URL: "tcp://10.250.241.20:8887", + Timeout: 1 * time.Second, + }) if err != nil { - klog.Exitf("Failed to connect to modbus %v", err) + klog.Exitf("Failed to setup modbus client %v", err) + } + + // Connect to modbus client + err = d.modbusClient.Open() + if err != nil { + klog.Warningf("Failed to connect to modbus TCP %v", err) } } diff --git a/succbone/succd/modbus.go b/succbone/succd/modbus.go index 4aebd03..2db5a6b 100644 --- a/succbone/succd/modbus.go +++ b/succbone/succd/modbus.go @@ -12,29 +12,6 @@ func modbusValuesToFloat(v uint16) float32 { return float32(v) / 10.0 } -func (d *daemon) modbusConnect() error { - var err error - - d.mu.Lock() - defer d.mu.Unlock() - - // Setup modbus client - d.modbusClient, err = modbus.NewClient(&modbus.ClientConfiguration{ - URL: "tcp://10.250.241.20:8887", - Timeout: 1 * time.Second, - }) - if err != nil { - return err - } - // Connect to modbus client - err = d.modbusClient.Open() - if err != nil { - return err - } - - return nil -} - func (d *daemon) modbusRestart() error { d.modbusClient.Close() return d.modbusClient.Open()