From 7dc2d509c827411a313e99f22f08d275bbc534fa Mon Sep 17 00:00:00 2001 From: Pierre-Olivier Mercier Date: Thu, 4 Feb 2021 00:58:03 +0100 Subject: [PATCH] Display key and hide checksum --- main.go | 94 ++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 59 insertions(+), 35 deletions(-) diff --git a/main.go b/main.go index 781023b..70307a1 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,7 @@ package main import ( "bytes" "flag" + "fmt" "io" "log" "os" @@ -27,7 +28,7 @@ func readSerial(s *serial.Port, c chan []byte) { unread.Write(buf[:n]) for { - line, err := unread.ReadBytes('\n') + line, err := unread.ReadBytes(2) if err == io.EOF { if _, err = unread.Write(line); err != nil { log.Println(err) @@ -38,9 +39,31 @@ func readSerial(s *serial.Port, c chan []byte) { break } - c <- bytes.TrimRightFunc(line, func(r rune) bool { - return unicode.IsSpace(r) || unicode.IsControl(r) - }) + c <- bytes.TrimRightFunc(line, unicode.IsControl) + } + } +} + +func treatFrames(frames chan []byte) { + first := true + for { + frame := <-frames + + // Skip the first frame because it's not complete + if first { + first = false + continue + } + + for _, line := range bytes.Split(frame, []byte("\r\n")) { + key, horodate, data, err := treatLine(line) + + if err != nil { + log.Println(err) + continue + } + + fmt.Println(key, horodate, data) } } } @@ -55,11 +78,11 @@ func computeChecksum(area []byte) (checksum byte) { return } -func getHorodate(fields *[][]byte) (horodate time.Time, err error) { - if len(*fields) == 4 && len((*fields)[1]) == 13 { - horodate, err = time.Parse("060102150405", string((*fields)[1][1:])) +func getHorodate(fields *[][]byte) (*time.Time, error) { + if (len(*fields) == 4 || string((*fields)[0]) == "DATE") && len((*fields)[1]) == 13 { + horodate, err := time.Parse("060102150405", string((*fields)[1][1:])) if err != nil { - horodate = time.Now().Truncate(time.Second).UTC() + return nil, err } // Handle "saison" @@ -71,36 +94,37 @@ func getHorodate(fields *[][]byte) (horodate time.Time, err error) { // Mark field as treated *fields = append((*fields)[:1], (*fields)[2:]...) - } else { - horodate = time.Now().Truncate(time.Second).UTC() + + return &horodate, nil } - return + return nil, nil } -func treatLines(c chan []byte) { - for { - line := <-c +func treatLine(line []byte) (key string, horodate *time.Time, data [][]byte, err error) { + line = bytes.TrimSpace(line) - if len(line) <= 1 { - continue - } - - 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])) - continue - } - - fields := bytes.Fields(line) - - horodate, err := getHorodate(&fields) - if err != nil { - log.Println(err) - continue - } - - log.Println(horodate, fields) + if len(line) <= 1 { + return } + + 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 + } + + fields := bytes.Fields(line) + + key = string(fields[0]) + + horodate, err = getHorodate(&fields) + if err != nil { + return + } + + data = fields[1 : len(fields)-1] + + return } func main() { @@ -127,9 +151,9 @@ func main() { log.Fatal(err) } - c := make(chan []byte) - go readSerial(s, c) - go treatLines(c) + frames := make(chan []byte) + go readSerial(s, frames) + go treatFrames(frames) interrupt := make(chan os.Signal, 1) signal.Notify(interrupt, os.Interrupt, syscall.SIGTERM)