commit 4f034e132e3164e1b02f9368f9ab972dcbe7f002 Author: Christopher Ramey Date: Tue Oct 1 15:26:43 2019 -0800 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1377554 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.swp diff --git a/cmd/sutron/main.go b/cmd/sutron/main.go new file mode 100644 index 0000000..823ef0b --- /dev/null +++ b/cmd/sutron/main.go @@ -0,0 +1,54 @@ +package main + +import ( + "git.binarythought.com/cdramey/sdb" + "git.binarythought.com/cdramey/sutron" + "fmt" + "bytes" + "io/ioutil" + "os" +) + +func main() { + if len(os.Args) < 2 { + fmt.Fprintf(os.Stderr, + "required argument missing\n") + os.Exit(1) + } + + raw, err := ioutil.ReadFile(os.Args[1]) + if err != nil { + fmt.Fprintf(os.Stderr, + "error reading \"%s\": %s\n", + os.Args[1], err.Error()) + } + + sdbdata, err := sdb.ParseSDB(raw) + if err != nil { + fmt.Fprintf(os.Stderr, + "%s\n", err.Error()) + os.Exit(1) + } + + for _, i := range sdbdata.Elements { + switch v := i.(type) { + case sdb.MOPayload: + // Seek forward to the First "B" - Sadly necessary + // in certain scenarios + l := bytes.Index(v.Payload, []byte("B")) + if l != -1 { + sutrondata, err := sutron.ParseSutronB(v.Payload[l:]) + if err != nil { + fmt.Fprintf(os.Stderr, + "%s\n", err.Error()) + os.Exit(1) + } + + fmt.Printf("Sutron Block: %s\n", string(sutrondata.Block)) + fmt.Printf("Sutron Group: %s\n", string(sutrondata.Group)) + fmt.Printf("Sutron Delta: %s\n", string(sutrondata.Delta)) + fmt.Printf("Sutron Voltage: %s\n", string(sutrondata.Voltage)) + } + } + } +} diff --git a/sutron.go b/sutron.go new file mode 100644 index 0000000..3bd7573 --- /dev/null +++ b/sutron.go @@ -0,0 +1,47 @@ +package sutron + +import ( +// "encoding/binary" + "fmt" +) + +type SutronB struct { + Block byte + Group byte + Delta byte + Data []byte + Voltage byte +} + +func (s SutronB) GroupString() string { + switch s.Group { + case '1': + return "Scheduled Transmission" + case '2': + return "Alarm Transmission" + case '3': + return "Forced Transmission" + default: + return "" + } +} + +func ParseSutronB(raw []byte) (*SutronB, error) { + raw_len := len(raw) + if raw_len < 5 { + return nil, fmt.Errorf("message structure too short: %d bytes", + len(raw)) + } + + if raw[0] != 'B' { + return nil, fmt.Errorf("wrong sutron format: %s", raw[0]) + } + + data := SutronB{} + data.Block = raw[0] + data.Group = raw[1] + data.Delta = raw[2] + data.Data = raw[3:raw_len - 1] + data.Voltage = raw[raw_len - 1] + return &data, nil +}