From 1c217d27f7333f8f99035300e5d4f1785beec1f9 Mon Sep 17 00:00:00 2001 From: Tony Gilkerson Date: Wed, 24 Jan 2024 22:06:57 -0500 Subject: [PATCH] fix: look for pipe --- README.md | 2 +- cmd/serial/main.go | 107 ++++++++++++++++++++++++++------------------- 2 files changed, 62 insertions(+), 47 deletions(-) diff --git a/README.md b/README.md index d6f3cae..66997e2 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ The serial-gateway has a `/pub` endpoint that can be used for testing. A `POST` ```sh ssh -D 9995 weeble -kubectl ctx weeble +kubectl ctx weeble-admin kubectl -n iot port-forward svc/serial-gateway 8080:8080 curl -X POST "http://localhost:8080/pub" -d "a-message" ``` diff --git a/cmd/serial/main.go b/cmd/serial/main.go index 50203f1..2b2dafd 100644 --- a/cmd/serial/main.go +++ b/cmd/serial/main.go @@ -159,59 +159,74 @@ func serialServer(port *serial.Port) { var err error var n int - var msg string + var partialMsg string - log.Println("Pause for buffer ZZZzzz...") - time.Sleep(time.Millisecond * 50) - n, err = port.Read(buf) if err != nil { log.Panicf("Error trying to read serial port %v\n", err) } - msg = string(buf[:n]) - - switch { - - case strings.Contains(msg, string(iot.MbxTemperature)): - parts := strings.Split(msg, ":") - f, err := strconv.ParseFloat(parts[1], 64) - if err != nil { - log.Printf("Error converting temperature reading to a float, original input message: %v, error: %v", msg, err) - } else { - mbxTemperatureFahrenheit.Set(f) - log.Printf("set MailboxTemperature to: %v", f) - } - - case msg == iot.MbxMuleAlarm: - mbxMuleAlarmCount.Inc() - log.Println("increment mbxMuleAlarmCount") - - case msg == iot.MbxDoorOpened: - mbxMailboxDoorOpenedCount.Inc() - log.Println("increment mbxMailboxDoorOpenedCount") - - case msg == iot.MbxChargerChargeStatusOn: - mbxChargerChargeStatus.Set(1) - log.Println("set mbxChargerChargeStatus to ON") - - case msg == iot.MbxChargerChargeStatusOff: - mbxChargerChargeStatus.Set(0) - log.Println("set mbxChargerChargeStatus to OFF") - case msg == iot.MbxChargerPowerSourceGood: - mbxChargerPowerStatus.Set(1) - log.Println("set mbxChargerPowerStatus to GOOD") - - case msg == iot.MbxChargerPowerSourceBad: - mbxChargerPowerStatus.Set(0) - log.Println("set mbxChargerPowerStatus to BAD") - - case msg == iot.MbxRoadMainLoopHeartbeat: - mbxRoadMainLoopHeartbeatCount.Inc() - log.Println("increment mbxRoadMainLoopHeartbeatCount") + // + // messages should looks like "msg1|msg2|msg3|" and end in a | + // + messages := partialMsg + string(buf[:n]) + + // prepend the partial message from last time to the message we got this time + // if we don't find a | then we still have a partial message + // Add to the partial message and keep reading + if !strings.HasSuffix(messages, "|") { + partialMsg = messages + continue + } - default: - log.Printf("No-op: %s\n", msg) + // + // Split + msgs := strings.Split(messages, "|") + for _, msg := range msgs { + + switch { + + case strings.Contains(msg, string(iot.MbxTemperature)): + parts := strings.Split(msg, ":") + f, err := strconv.ParseFloat(parts[1], 64) + if err != nil { + log.Printf("Error converting temperature reading to a float, original input message: %v, error: %v", msg, err) + } else { + mbxTemperatureFahrenheit.Set(f) + log.Printf("set MailboxTemperature to: %v", f) + } + + case msg == iot.MbxMuleAlarm: + mbxMuleAlarmCount.Inc() + log.Println("increment mbxMuleAlarmCount") + + case msg == iot.MbxDoorOpened: + mbxMailboxDoorOpenedCount.Inc() + log.Println("increment mbxMailboxDoorOpenedCount") + + case msg == iot.MbxChargerChargeStatusOn: + mbxChargerChargeStatus.Set(1) + log.Println("set mbxChargerChargeStatus to ON") + + case msg == iot.MbxChargerChargeStatusOff: + mbxChargerChargeStatus.Set(0) + log.Println("set mbxChargerChargeStatus to OFF") + + case msg == iot.MbxChargerPowerSourceGood: + mbxChargerPowerStatus.Set(1) + log.Println("set mbxChargerPowerStatus to GOOD") + + case msg == iot.MbxChargerPowerSourceBad: + mbxChargerPowerStatus.Set(0) + log.Println("set mbxChargerPowerStatus to BAD") + + case msg == iot.MbxRoadMainLoopHeartbeat: + mbxRoadMainLoopHeartbeatCount.Inc() + log.Println("increment mbxRoadMainLoopHeartbeatCount") + + default: + log.Printf("No-op: %s\n", msg) + } } } }