Display key and hide checksum

This commit is contained in:
nemunaire 2021-02-04 00:58:03 +01:00
parent de686ef53d
commit 7dc2d509c8
1 changed files with 59 additions and 35 deletions

94
main.go
View File

@ -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)