forked from gonium/gosdm630
-
Notifications
You must be signed in to change notification settings - Fork 0
/
datagram.go
286 lines (263 loc) · 7.13 KB
/
datagram.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package sdm630
import (
"fmt"
"log"
"math"
"time"
)
// UniqueIdFormat is a format string for unique ID generation.
// It expects one %d conversion specifier,
// which will be replaced with the device ID.
// The UniqueIdFormat can be changed on program startup,
// before any additional goroutines are started.
var UniqueIdFormat string = "Instrument%d"
// Readings combines readings of all measurements into one data structure
type Readings struct {
UniqueId string
Timestamp time.Time
Unix int64
DeviceId uint8 `json:"ModbusDeviceId"`
Power ThreePhaseReadings
Voltage ThreePhaseReadings
Current ThreePhaseReadings
Cosphi ThreePhaseReadings
Import ThreePhaseReadings
TotalImport *float64
Export ThreePhaseReadings
TotalExport *float64
THD THDInfo
Frequency *float64
}
type THDInfo struct {
// Current ThreePhaseReadings
// AvgCurrent float64
VoltageNeutral ThreePhaseReadings
AvgVoltageNeutral *float64
}
type ThreePhaseReadings struct {
L1 *float64
L2 *float64
L3 *float64
}
// F2fp helper converts float64 to *float64
func F2fp(x float64) *float64 {
if math.IsNaN(x) {
return nil
}
return &x
}
// Fp2f helper converts *float64 to float64, correctly handles uninitialized
// variables
func Fp2f(x *float64) float64 {
if x == nil {
// this is not initialized yet - return NaN
return math.NaN()
}
return *x
}
func (r *Readings) String() string {
fmtString := "%s " +
"L1: %.1fV %.2fA %.0fW %.2fcos | " +
"L2: %.1fV %.2fA %.0fW %.2fcos | " +
"L3: %.1fV %.2fA %.0fW %.2fcos | " +
"%.1fHz"
return fmt.Sprintf(fmtString,
r.UniqueId,
Fp2f(r.Voltage.L1),
Fp2f(r.Current.L1),
Fp2f(r.Power.L1),
Fp2f(r.Cosphi.L1),
Fp2f(r.Voltage.L2),
Fp2f(r.Current.L2),
Fp2f(r.Power.L2),
Fp2f(r.Cosphi.L2),
Fp2f(r.Voltage.L3),
Fp2f(r.Current.L3),
Fp2f(r.Power.L3),
Fp2f(r.Cosphi.L3),
Fp2f(r.Frequency),
)
}
// IsOlderThan returns true if the reading is older than the given timestamp.
func (r *Readings) IsOlderThan(ts time.Time) (retval bool) {
return r.Timestamp.Before(ts)
}
func tpAdd(lhs ThreePhaseReadings, rhs ThreePhaseReadings) ThreePhaseReadings {
res := ThreePhaseReadings{
L1: F2fp(Fp2f(lhs.L1) + Fp2f(rhs.L1)),
L2: F2fp(Fp2f(lhs.L2) + Fp2f(rhs.L2)),
L3: F2fp(Fp2f(lhs.L3) + Fp2f(rhs.L3)),
}
return res
}
/*
* Adds two readings. The individual values are added except for
* the time: the latter of the two times is copied over to the result
*/
func (lhs *Readings) add(rhs *Readings) (*Readings, error) {
if lhs.DeviceId != rhs.DeviceId {
return &Readings{}, fmt.Errorf(
"Cannot add readings of different devices - got IDs %d and %d",
lhs.DeviceId, rhs.DeviceId)
}
res := &Readings{
UniqueId: lhs.UniqueId,
DeviceId: lhs.DeviceId,
Voltage: tpAdd(lhs.Voltage, rhs.Voltage),
Current: tpAdd(lhs.Current, rhs.Current),
Power: tpAdd(lhs.Power, rhs.Power),
Cosphi: tpAdd(lhs.Cosphi, rhs.Cosphi),
Import: tpAdd(lhs.Import, rhs.Import),
TotalImport: F2fp(Fp2f(lhs.TotalImport) +
Fp2f(rhs.TotalImport)),
Export: tpAdd(lhs.Export, rhs.Export),
TotalExport: F2fp(Fp2f(lhs.TotalExport) +
Fp2f(rhs.TotalExport)),
THD: THDInfo{
VoltageNeutral: tpAdd(lhs.THD.VoltageNeutral, rhs.THD.VoltageNeutral),
AvgVoltageNeutral: F2fp(Fp2f(lhs.THD.AvgVoltageNeutral) +
Fp2f(rhs.THD.AvgVoltageNeutral)),
},
Frequency: F2fp(Fp2f(lhs.Frequency) +
Fp2f(rhs.Frequency)),
}
if lhs.Timestamp.After(rhs.Timestamp) {
res.Timestamp = lhs.Timestamp
res.Unix = lhs.Unix
} else {
res.Timestamp = rhs.Timestamp
res.Unix = rhs.Unix
}
return res, nil
}
func tpDivide(lhs ThreePhaseReadings, scaler float64) ThreePhaseReadings {
res := ThreePhaseReadings{
L1: F2fp(Fp2f(lhs.L1) / scaler),
L2: F2fp(Fp2f(lhs.L2) / scaler),
L3: F2fp(Fp2f(lhs.L3) / scaler),
}
return res
}
/*
* Divide a reading by an integer. The individual values are divided except
* for the time: it is simply copied over to the result
*/
func (lhs *Readings) divide(scaler float64) *Readings {
res := &Readings{
Timestamp: lhs.Timestamp,
Unix: lhs.Unix,
DeviceId: lhs.DeviceId,
UniqueId: lhs.UniqueId,
Voltage: tpDivide(lhs.Voltage, scaler),
Current: tpDivide(lhs.Current, scaler),
Power: tpDivide(lhs.Power, scaler),
Cosphi: tpDivide(lhs.Cosphi, scaler),
Import: tpDivide(lhs.Import, scaler),
TotalImport: F2fp(Fp2f(lhs.TotalImport) / scaler),
Export: tpDivide(lhs.Export, scaler),
TotalExport: F2fp(Fp2f(lhs.TotalExport) / scaler),
THD: THDInfo{
VoltageNeutral: tpDivide(lhs.THD.VoltageNeutral, scaler),
AvgVoltageNeutral: F2fp(Fp2f(lhs.THD.AvgVoltageNeutral) / scaler),
},
Frequency: F2fp(Fp2f(lhs.Frequency) / scaler),
}
return res
}
// MergeSnip adds the values represented by the QuerySnip to the
// Readings and updates the current time stamp
func (r *Readings) MergeSnip(q QuerySnip) {
r.Timestamp = q.ReadTimestamp
r.Unix = r.Timestamp.Unix()
switch q.IEC61850 {
case "VolLocPhsA":
r.Voltage.L1 = &q.Value
case "VolLocPhsB":
r.Voltage.L2 = &q.Value
case "VolLocPhsC":
r.Voltage.L3 = &q.Value
case "AmpLocPhsA":
r.Current.L1 = &q.Value
case "AmpLocPhsB":
r.Current.L2 = &q.Value
case "AmpLocPhsC":
r.Current.L3 = &q.Value
case "WLocPhsA":
r.Power.L1 = &q.Value
case "WLocPhsB":
r.Power.L2 = &q.Value
case "WLocPhsC":
r.Power.L3 = &q.Value
case "AngLocPhsA":
r.Cosphi.L1 = &q.Value
case "AngLocPhsB":
r.Cosphi.L2 = &q.Value
case "AngLocPhsC":
r.Cosphi.L3 = &q.Value
case "TotkWhImportPhsA":
r.Import.L1 = &q.Value
case "TotkWhImportPhsB":
r.Import.L2 = &q.Value
case "TotkWhImportPhsC":
r.Import.L3 = &q.Value
case "TotkWhImport":
r.TotalImport = &q.Value
case "TotkWhExportPhsA":
r.Export.L1 = &q.Value
case "TotkWhExportPhsB":
r.Export.L2 = &q.Value
case "TotkWhExportPhsC":
r.Export.L3 = &q.Value
case "TotkWhExport":
r.TotalExport = &q.Value
// case OpCodeL1THDCurrent:
// r.THD.Current.L1 = &q.Value
// case OpCodeL2THDCurrent:
// r.THD.Current.L2 = &q.Value
// case OpCodeL3THDCurrent:
// r.THD.Current.L3 = &q.Value
// case OpCodeAvgTHDCurrent:
// r.THD.AvgCurrent = &q.Value
case "ThdVolPhsA":
r.THD.VoltageNeutral.L1 = &q.Value
case "ThdVolPhsB":
r.THD.VoltageNeutral.L2 = &q.Value
case "ThdVolPhsC":
r.THD.VoltageNeutral.L3 = &q.Value
case "ThdVol":
r.THD.AvgVoltageNeutral = &q.Value
case "Freq":
r.Frequency = &q.Value
default:
log.Fatalf("Cannot merge unknown IEC: %+v", q)
}
}
// ReadingSlice is a type alias for a slice of readings.
type ReadingSlice []Readings
// NotOlderThan creates a new ReadingSlice of latest data
func (r ReadingSlice) NotOlderThan(ts time.Time) (res ReadingSlice) {
res = ReadingSlice{}
for _, reading := range r {
if !reading.IsOlderThan(ts) {
res = append(res, reading)
}
}
return res
}
// Average calculates average across a ReadingSlice
func (r *ReadingSlice) Average() (*Readings, error) {
var avg *Readings
var err error
for idx, r := range *r {
if idx == 0 {
// This is the first element - initialize our accumulator
avg = &r
} else {
avg, err = r.add(avg)
if err != nil {
return nil, err
}
}
}
return avg.divide(float64(len(*r))), nil
}