Display key and hide checksum

This commit is contained in:
nemunaire 2021-02-04 00:58:03 +01:00
parent de686ef53d
commit 7dc2d509c8

94
main.go
View File

@ -3,6 +3,7 @@ package main
import ( import (
"bytes" "bytes"
"flag" "flag"
"fmt"
"io" "io"
"log" "log"
"os" "os"
@ -27,7 +28,7 @@ func readSerial(s *serial.Port, c chan []byte) {
unread.Write(buf[:n]) unread.Write(buf[:n])
for { for {
line, err := unread.ReadBytes('\n') line, err := unread.ReadBytes(2)
if err == io.EOF { if err == io.EOF {
if _, err = unread.Write(line); err != nil { if _, err = unread.Write(line); err != nil {
log.Println(err) log.Println(err)
@ -38,9 +39,31 @@ func readSerial(s *serial.Port, c chan []byte) {
break break
} }
c <- bytes.TrimRightFunc(line, func(r rune) bool { c <- bytes.TrimRightFunc(line, unicode.IsControl)
return unicode.IsSpace(r) || unicode.IsControl(r) }
}) }
}
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 return
} }
func getHorodate(fields *[][]byte) (horodate time.Time, err error) { func getHorodate(fields *[][]byte) (*time.Time, error) {
if len(*fields) == 4 && len((*fields)[1]) == 13 { if (len(*fields) == 4 || string((*fields)[0]) == "DATE") && len((*fields)[1]) == 13 {
horodate, err = time.Parse("060102150405", string((*fields)[1][1:])) horodate, err := time.Parse("060102150405", string((*fields)[1][1:]))
if err != nil { if err != nil {
horodate = time.Now().Truncate(time.Second).UTC() return nil, err
} }
// Handle "saison" // Handle "saison"
@ -71,36 +94,37 @@ func getHorodate(fields *[][]byte) (horodate time.Time, err error) {
// Mark field as treated // Mark field as treated
*fields = append((*fields)[:1], (*fields)[2:]...) *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) { func treatLine(line []byte) (key string, horodate *time.Time, data [][]byte, err error) {
for { line = bytes.TrimSpace(line)
line := <-c
if len(line) <= 1 { if len(line) <= 1 {
continue 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]))
continue
}
fields := bytes.Fields(line)
horodate, err := getHorodate(&fields)
if err != nil {
log.Println(err)
continue
}
log.Println(horodate, fields)
} }
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() { func main() {
@ -127,9 +151,9 @@ func main() {
log.Fatal(err) log.Fatal(err)
} }
c := make(chan []byte) frames := make(chan []byte)
go readSerial(s, c) go readSerial(s, frames)
go treatLines(c) go treatFrames(frames)
interrupt := make(chan os.Signal, 1) interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt, syscall.SIGTERM) signal.Notify(interrupt, os.Interrupt, syscall.SIGTERM)