-
Notifications
You must be signed in to change notification settings - Fork 11
/
gotsmart.go
137 lines (124 loc) · 3.15 KB
/
gotsmart.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package main
import (
"bufio"
"flag"
"fmt"
"log"
"net/http"
"strings"
"sync"
"time"
"github.com/basvdlei/gotsmart/crc16"
"github.com/basvdlei/gotsmart/dsmr"
dsmrprometheus "github.com/basvdlei/gotsmart/dsmr/prometheus"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/tarm/serial"
)
const version = "0.0.3"
type frameupdate struct {
mutex sync.Mutex
Frame string
Time time.Time
}
func (f *frameupdate) ServeHTTP(w http.ResponseWriter, r *http.Request) {
f.mutex.Lock()
defer f.mutex.Unlock()
w.Header().Add("Content-Type", "text/plain; charset=utf-8")
w.Header().Add("Last-Modified", f.Time.Format(http.TimeFormat))
w.Write([]byte(f.Frame))
}
func (f *frameupdate) Update(frame string) {
f.mutex.Lock()
defer f.mutex.Unlock()
f.Frame = strings.Replace(frame, "\r", "", -1)
f.Time = time.Now()
}
func (f *frameupdate) Process(br *bufio.Reader, collector *dsmrprometheus.DSMRCollector) {
for {
if b, err := br.Peek(1); err == nil {
if string(b) != "/" {
fmt.Printf("Ignoring garbage character: %c\n", b)
br.ReadByte()
continue
}
} else {
continue
}
frame, err := br.ReadBytes('!')
if err != nil {
fmt.Printf("Error: %v\n", err)
continue
}
bcrc, err := br.ReadBytes('\n')
if err != nil {
fmt.Printf("Error: %v\n", err)
continue
}
// Check CRC
mcrc := strings.ToUpper(strings.TrimSpace(string(bcrc)))
crc := fmt.Sprintf("%04X", crc16.Checksum(frame))
if mcrc != crc {
fmt.Printf("CRC mismatch: %q != %q\n", mcrc, crc)
continue
}
f.Update(string(frame))
dsmrFrame, err := dsmr.ParseFrame(string(frame))
if err != nil {
log.Printf("could not parse frame: %v\n", err)
continue
}
collector.Update(dsmrFrame)
}
}
func main() {
var (
addrFlag = flag.String("listen-address", ":8080", "The address to listen on for HTTP requests.")
deviceFlag = flag.String("device", "/dev/ttyAMA0", "Serial device to read P1 data from.")
baudFlag = flag.Int("baud", 115200, "Baud rate (speed) to use.")
bitsFlag = flag.Int("bits", 8, "Number of databits.")
parityFlag = flag.String("parity", "none", "Parity the use (none/odd/even/mark/space).")
)
flag.Parse()
fmt.Printf("GotSmart (%s)\n", version)
var parity serial.Parity
switch *parityFlag {
case "none":
parity = serial.ParityNone
case "odd":
parity = serial.ParityOdd
case "even":
parity = serial.ParityEven
case "mark":
parity = serial.ParityMark
case "space":
parity = serial.ParitySpace
default:
log.Fatal("Invalid parity setting")
}
c := &serial.Config{
Name: *deviceFlag,
Baud: *baudFlag,
Size: byte(*bitsFlag),
Parity: parity,
}
p, err := serial.OpenPort(c)
if err != nil {
log.Fatal(err)
}
br := bufio.NewReader(p)
collector := &dsmrprometheus.DSMRCollector{}
prometheus.MustRegister(collector)
f := &frameupdate{mutex: sync.Mutex{}}
go f.Process(br, collector)
mux := http.NewServeMux()
mux.Handle("/metrics", promhttp.Handler())
mux.Handle("/", f)
srv := &http.Server{
Addr: *addrFlag,
Handler: mux,
ReadTimeout: 30 * time.Second,
WriteTimeout: 30 * time.Second,
}
log.Fatal(srv.ListenAndServe())
}