-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
178 lines (155 loc) · 4.67 KB
/
main.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package main
import (
"context"
"flag"
"fmt"
"github.com/tarm/serial"
"hemtjan.st/kraft/kaifa"
"io"
"lib.hemtjan.st/client"
"lib.hemtjan.st/device"
"lib.hemtjan.st/feature"
"lib.hemtjan.st/transport/mqtt"
"log"
"os"
"time"
)
const (
// Re-use currentPower from hemtjanst for positive power (i.e. power flowing into the system from the grid)
currentPower = string(feature.CurrentPower)
// Define a custom feature for produced power (e.g. if exporting Solar power to the grid)
currentPowerProduced = "currentPowerProduced"
energyUsed = string(feature.EnergyUsed)
energyProduced = "energyProduced"
phaseCurrent = "phase%dCurrent"
phaseVoltage = "phase%dVoltage"
)
func main() {
serialDevice := flag.String("device", "/dev/ttyUSB0", "Serial device")
baudFlag := flag.Int("speed", 2400, "Baud rate of serial port")
topicName := flag.String("topic", "powerMeter/house", "Topic of hemtjanst device")
name := flag.String("name", "House Power Meter", "Name of hemtjanst device")
mqFlags := mqtt.MustFlags(flag.String, flag.Bool)
flag.Parse()
ctx := context.Background()
mq, err := mqtt.New(ctx, mqFlags())
if err != nil {
log.Fatalf("connecting to mqtt: %v", err)
}
// Spawn a goroutine to detect MQTT errors and handle reconnect
go func() {
for {
ok, err := mq.Start()
if err != nil {
log.Printf("MQTT Error: %s", err)
}
if !ok {
os.Exit(1)
}
time.Sleep(3 * time.Second)
log.Printf("MQTT: Reconnecting")
}
}()
var d client.Device
// pushData gets called on each message
pushData := func(msg *kaifa.Message) {
if d == nil {
// Device is created once the first message is received
// since we need to know which features are supported
// and the model/serial number
var err error
info := &device.Info{
Topic: *topicName,
Name: *name,
Manufacturer: "Kaifa",
Features: map[string]*feature.Info{},
Type: "energyMeter",
}
if msg.MeterID != nil {
info.SerialNumber = *msg.MeterID
}
if msg.MeterType != nil {
info.Model = *msg.MeterType
}
if msg.ActivePowerPositive != nil {
info.Features[currentPower] = &feature.Info{}
}
if msg.ActivePowerNegative != nil {
info.Features[currentPowerProduced] = &feature.Info{}
}
for _, ph := range msg.Phases {
info.Features[fmt.Sprintf(phaseCurrent, ph.Index)] = &feature.Info{}
info.Features[fmt.Sprintf(phaseVoltage, ph.Index)] = &feature.Info{}
}
if msg.ActiveEnergyPositive != nil {
info.Features[energyUsed] = &feature.Info{}
}
if msg.ActiveEnergyNegative != nil {
info.Features[energyProduced] = &feature.Info{}
}
d, err = client.NewDevice(info, mq)
if err != nil {
log.Fatalf("error creating device: %v", err)
}
}
if msg.ActivePowerPositive != nil {
// Power imported from the grid in Watts
_ = d.Feature(currentPower).Update(fmt.Sprintf("%d", *msg.ActivePowerPositive))
}
if msg.ActivePowerNegative != nil {
// Power exported to the grid in Watts
_ = d.Feature(currentPowerProduced).Update(fmt.Sprintf("%d", *msg.ActivePowerNegative))
}
for _, ph := range msg.Phases {
// Current in Amperes
_ = d.Feature(fmt.Sprintf(phaseCurrent, ph.Index)).Update(fmt.Sprintf("%.3f", ph.Current))
// Voltage in Volts
_ = d.Feature(fmt.Sprintf(phaseVoltage, ph.Index)).Update(fmt.Sprintf("%.1f", ph.Voltage))
}
if msg.ActiveEnergyPositive != nil {
// Convert to float and divide by 1000 to get kWh
_ = d.Feature(energyUsed).Update(fmt.Sprintf("%.3f", float64(*msg.ActiveEnergyPositive)/1000))
}
if msg.ActiveEnergyNegative != nil {
// Convert to float and divide by 1000 to get kWh
_ = d.Feature(energyProduced).Update(fmt.Sprintf("%.3f", float64(*msg.ActiveEnergyNegative)/1000))
}
}
cfg := &serial.Config{
Name: *serialDevice,
Baud: *baudFlag,
Parity: serial.ParityEven,
Size: 8,
}
// Open serial to read & discard everything for 200ms to drain incoming buffer
s, err := serial.OpenPort(cfg)
if err != nil {
log.Fatalf("error opening %s: %v", *serialDevice, err)
}
go func() {
_, _ = io.ReadAll(s)
}()
time.Sleep(200 * time.Millisecond)
_ = s.Close()
s, err = serial.OpenPort(cfg)
if err != nil {
log.Fatalf("error opening %s: %v", *serialDevice, err)
}
r := kaifa.NewReader(s)
for {
// Main loop, keep reading frames until serial closes or program is terminated
fr, err := r.ReadFrame()
if err != nil {
if err == io.EOF {
log.Printf("EOF from serial device, exiting")
return
}
log.Fatalf("error while reading frame: %v", err)
}
msg, err := kaifa.Unmarshal(fr)
if err != nil {
log.Fatalf("Error unmarshalling frame: %v\nData: %X", err, fr)
}
pushData(msg)
}
}