Browse Source

Initial commit

master
Christopher Ramey 5 years ago
committed by Christopher Ramey
commit
4f034e132e
  1. 1
      .gitignore
  2. 54
      cmd/sutron/main.go
  3. 47
      sutron.go

1
.gitignore

@ -0,0 +1 @@
*.swp

54
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))
}
}
}
}

47
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
}
Loading…
Cancel
Save