From fe9538c81755a5b79837a040bfa69caa663940ec Mon Sep 17 00:00:00 2001 From: Pierre-Olivier Mercier Date: Sat, 20 Aug 2022 20:20:46 +0200 Subject: [PATCH] In legacy mode, try to map to standard mode key --- main.go | 21 +++++++++++++++------ measurements.go | 19 +++++++++++++++++++ 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/main.go b/main.go index a0953d0..87f75b5 100644 --- a/main.go +++ b/main.go @@ -48,7 +48,7 @@ type Point struct { Horodate *time.Time } -func treatFrames(frames chan []byte, writer TICWriter) { +func treatFrames(frames chan []byte, writer TICWriter, legacyMode bool) { first := true fframe: for { @@ -65,13 +65,19 @@ fframe: var defaultHorodate time.Time for _, line := range bytes.Split(frame, []byte("\r\n")) { - key, horodate, data, err := treatLine(line) + key, horodate, data, err := treatLine(line, legacyMode) if err != nil { log.Println(err) continue } + // Replace legacy keys + if legacyMode { + if nkey, ok := Legacy2Std[key]; ok { + key = nkey + } + } // Skip ADCO, this is the Linky address, confidential and unrelevant if key == "ADCO" { continue @@ -131,7 +137,7 @@ func getHorodate(fields *[][]byte) (*time.Time, error) { return nil, nil } -func treatLine(line []byte) (key string, horodate *time.Time, data []byte, err error) { +func treatLine(line []byte, legacyMode bool) (key string, horodate *time.Time, data []byte, err error) { line = bytes.TrimSpace(line) if len(line) <= 1 { @@ -139,8 +145,11 @@ func treatLine(line []byte) (key string, horodate *time.Time, data []byte, err e } if computeChecksum(line[:len(line)-1]) != line[len(line)-1] { - log.Printf("BAD checksum on %s: calculated: %c\n", line, computeChecksum(line[:len(line)-1])) - return + // Try checksum mode 1 + if !legacyMode || computeChecksum(line[:len(line)-2]) != line[len(line)-1] { + log.Printf("BAD checksum on %s: calculated: %c\n", line, computeChecksum(line[:len(line)-1])) + return + } } fields := bytes.Fields(line) @@ -203,7 +212,7 @@ func main() { frames := make(chan []byte) go readSerial(s, frames) - go treatFrames(frames, writer) + go treatFrames(frames, writer, *legacyMode) interrupt := make(chan os.Signal, 1) signal.Notify(interrupt, os.Interrupt, syscall.SIGTERM) diff --git a/measurements.go b/measurements.go index d675034..e72f2bb 100644 --- a/measurements.go +++ b/measurements.go @@ -1,6 +1,25 @@ package main var ( + Legacy2Std = map[string]string{ + "BASE": "EAST", + "HCHC": "EASF01", + "HCHP": "EASF02", + "EJPHN": "EASF01", + "EJPHPM": "EASF02", + "BBRHCJB": "EASF01", + "BBRHPJB": "EASF02", + "BBRHCJW": "EASF03", + "BBRHPJW": "EASF04", + "BBRHCJR": "EASF05", + "BBRHPJR": "EASF06", + "IINST": "IRMS1", + "IINST1": "IRMS1", + "IINST2": "IRMS2", + "IINST3": "IRMS3", + "PAPP": "SINSTS", + } + MeasurementGroups = map[string][]string{ "EAS": []string{"EAST", "EASF01", "EASF02", "EASF03", "EASF04", "EASF05", "EASF06", "EASF07", "EASF08", "EASF09", "EASF10", "EASD01", "EASD02", "EASD03", "EASD04"}, "ERQ": []string{"ERQ1", "ERQ2", "ERQ3", "ERQ4"},