-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
458 lines (421 loc) · 11.1 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
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
package main
import (
"bytes"
"encoding/binary"
"flag"
"fmt"
"net"
"os"
"strings"
"sync"
"time"
"github.com/gravwell/ingest"
"github.com/gravwell/ingest/entry"
ds18b20 "github.com/traetox/goDS18B20"
gpio "github.com/traetox/goGPIO"
)
var (
configLocOverride = flag.String("c", "", "Config file location override")
verbose = flag.Bool("v", false, "Verbose stdout logging")
compressorControlAliases []string
lg simplelog
tempTagId uint16 = 0x1200
compTagId uint16 = 0x1320
tempFunc tempPacker = binaryTempPack
compFunc compPacker = binaryCompPack
)
type logger interface {
Info(string, ...interface{}) error
Warn(string, ...interface{}) error
Error(string, ...interface{}) error
}
type tempPacker func(uint16, entry.Timestamp, float32, string) ([]byte, error)
type compPacker func(uint16, entry.Timestamp, entry.Timestamp) ([]byte, error)
func init() {
flag.Parse()
}
func main() {
c, err := OpenConfig(*configLocOverride) //open the default location
if err != nil {
lg.Error("Failed to open config file: %v", err)
return
}
if err = setupPackers(c.dataFormat); err != nil {
lg.Error("failed to setup packers: %v", err)
return
}
//open probes and initialize them
if err := ds18b20.Setup(); err != nil {
lg.Error("Setup failed: %v", err)
return
}
probes, err := ds18b20.New()
if err != nil {
lg.Error("Failed to create new probe group: %v", err)
return
}
//open compressor and initializae it
compressor, err := gpio.New(int(c.CompressorGPIO()))
if err != nil {
lg.Error("Failed to open compressorGPIO: %v", err)
return
}
if err := compressor.SetOutput(); err != nil {
lg.Error("Failed to set compressor as output: %v", err)
return
}
if err := compressor.Off(); err != nil {
lg.Error("Failed to initialize the compressor: %v", err)
return
}
aliases := c.Aliases()
vlog("Starting with probe aliases: %v", aliases)
for k, v := range aliases {
vlog("Assigning %s to %s", k, v)
if err := probes.AssignAlias(k, v); err != nil {
lg.Error("Probe alias error %s: %v", v, err)
return
}
cc, err := c.AliasCompressorControl(k)
if err != nil {
lg.Error("Failed to find alias when testing for compressor control %v", err)
return
}
if cc {
compressorControlAliases = append(compressorControlAliases, v)
}
}
xx, err := probes.Read()
vlog("testing: %v: %v", xx, err)
xx, err = probes.ReadAlias()
vlog("testing alias: %v: %v", xx, err)
defer probes.Close()
if len(compressorControlAliases) == 0 {
lg.Error("No probes with compressor control have been defined")
return
}
mxConfig, err := c.MuxerConfig()
if err != nil {
lg.Error("Failed to acquire ingester config: %v", err)
os.Exit(-1)
}
//open an ingester
igst, err := ingest.NewMuxer(mxConfig)
if err != nil {
lg.Error("Failed to get muxer config: %v", err)
os.Exit(-1)
}
if err := igst.Start(); err != nil {
lg.Error("Failed to start ingest: %v", err)
os.Exit(-1)
}
if err := igst.WaitForHot(3 * time.Second); err != nil {
lg.Error("Wait for hot timeout: %v", err) //not fatal
os.Exit(-1)
}
kl := &keglog{
mx: igst,
}
if kl.logtag, err = igst.GetTag(printTag); err != nil {
lg.Error("Failed to get print tag: %v", err)
os.Exit(-1)
}
if kl.kegtag, err = igst.GetTag(kegTag); err != nil {
lg.Error("Failed to get temp tag: %v", err)
os.Exit(-1)
}
if kl.comptag, err = igst.GetTag(compTag); err != nil {
lg.Error("Failed to get compressor tag: %v", err)
os.Exit(-1)
}
//fire off the management routine
wg := sync.WaitGroup{}
wg.Add(2)
go manageCompressor(probes, compressor, c, kl, &wg)
go recordTemps(c.TemperatureRecordInterval(), probes, kl, &wg)
wg.Wait()
}
func recordTemps(interval time.Duration, probes *ds18b20.ProbeGroup, kt *keglog, wg *sync.WaitGroup) {
defer wg.Done()
vlog("Starting temperature recording routine with: %v", probes)
for {
temps := make(map[string]float32, 1)
t, err := probes.ReadAlias()
if err != nil {
lg.Error("Failed to read probes: %v", err)
vlog("Failed to read probes: %v", err)
time.Sleep(interval)
continue
}
vlog("Read aliases: %v, %v", t, err)
for k, v := range t {
temps[k] = v.Celsius()
}
if err := kt.AddTemps(temps); err != nil {
lg.Error("Failed to add temps: %v", err)
vlog("Failed to Update temperatures: %v", err)
time.Sleep(interval)
continue
}
vlog("Updated temperatures: %v", temps)
vlog("sleeping %v", interval)
time.Sleep(interval)
}
}
func compressorPanic(compressor *gpio.GPIO) {
if err := compressor.Off(); err != nil {
lg.Error("Failed to force compressor off: %v", err)
} else {
lg.Error("Forced compressor off")
}
}
func manageCompressor(probes *ds18b20.ProbeGroup, compressor *gpio.GPIO, c *conf, kt *keglog, wg *sync.WaitGroup) {
defer compressorPanic(compressor) //ensure that compressor always goes off on our way out
defer wg.Done()
min, max, target := c.TemperatureRange()
probeValues := make([]float32, len(compressorControlAliases))
nilTime := time.Time{}
var started time.Time
started = nilTime //i know this should initialize to the zero time... but i am anal
for {
//update temps
if err := probes.Update(); err != nil {
lg.Info("Update failure: %v", err)
time.Sleep(c.ProbeInterval())
continue
}
temps, err := probes.Read()
if err != nil {
lg.Error("Read failed: %v", err)
time.Sleep(c.ProbeInterval())
continue
}
//check temperatures and set compressor
forceOff := false
for i := range compressorControlAliases {
t := temps[compressorControlAliases[i]]
if t.Celsius() < min {
forceOff = true //going off is always priority
if err := compressor.Off(); err != nil {
lg.Error("Failed to turn off compressor: %v", err)
}
break
}
probeValues[i] = t.Celsius()
}
//check if we are being forced off
if forceOff {
if err := compressor.Off(); err != nil {
lg.Error("Compressor failed to disengage: %v", err)
}
if started != nilTime {
if err := kt.AddCompressor(started, time.Now()); err != nil {
lg.Error("Failed to track compressor interval: %v", err)
}
}
started = time.Time{}
continue
}
if compressor.State() {
allInsideRange := true
//we want to make sure the compressor is on long enough to actually do something
if time.Since(started).Seconds() < c.CompressorMinOnTime().Seconds() {
allInsideRange = false
}
//loop through and see what we should do
for i := range probeValues {
//we keep the compressor on if one of the probes is showing a warm value
if probeValues[i] > target {
allInsideRange = false
}
}
if allInsideRange {
//everybody is good
if err := compressor.Off(); err != nil {
lg.Error("Failed to disengage compressor: %v", err)
}
if err := kt.AddCompressor(started, time.Now()); err != nil {
lg.Error("Failed to track compressor interval: %v", err)
}
started = nilTime
}
//at least one probe outside the range, so leave the compressor on
} else {
allInsideRange := true
//compressor is off, determine if we should turn on
for i := range probeValues {
if probeValues[i] > max {
allInsideRange = false
}
}
if !allInsideRange {
started = time.Now()
if err := compressor.On(); err != nil {
lg.Error("Compressor failed to start: %v", err)
}
}
}
vlog("Updated compressor data")
time.Sleep(c.ProbeInterval())
}
}
type keglog struct {
mx *ingest.IngestMuxer
logtag entry.EntryTag
kegtag entry.EntryTag
comptag entry.EntryTag
}
func (kl keglog) Printf(arg string, args ...interface{}) error {
src, err := kl.mx.SourceIP()
if err != nil {
return err
}
s := fmt.Sprintf(arg, args...)
e := entry.Entry{
TS: entry.Now(),
Tag: kl.logtag,
SRC: src,
Data: []byte(s),
}
return kl.mx.WriteEntry(&e)
}
func (kl keglog) AddTemps(temps map[string]float32) error {
src, err := kl.mx.SourceIP()
if err != nil {
vlog("Failed to get source IP: %v", err)
return err
}
for k, v := range temps {
ts := entry.Now()
bts, err := tempFunc(tempTagId, ts, v, k)
if err != nil {
return err
}
e := entry.Entry{
TS: ts,
Tag: kl.kegtag,
SRC: src,
Data: bts,
}
err = kl.mx.WriteEntry(&e)
vlog("Sending temperaturer %d %v %v %s: %v", tempTagId, ts, v, k, err)
if err != nil {
return err
}
}
return nil
}
func (kl keglog) AddCompressor(start, stop time.Time) (err error) {
s := entry.FromStandard(start)
e := entry.FromStandard(stop)
var src net.IP
if src, err = kl.mx.SourceIP(); err != nil {
vlog("Failed to get source IP: %v", err)
return
}
diff := e.Sec - s.Sec
if diff < 0 {
diff = 0
}
var bts []byte
if bts, err = compFunc(compTagId, s, e); err != nil {
return
}
ent := entry.Entry{
TS: entry.Now(),
SRC: src,
Tag: kl.comptag,
Data: bts,
}
err = kl.mx.WriteEntry(&ent)
vlog("Sending compressor %d %v %v %v: %v", compTagId, s, e, diff, err)
return
}
func vlog(f string, args ...interface{}) {
if !*verbose {
return
}
ln := strings.Trim(fmt.Sprintf(f, args...), "\n\t ")
fmt.Println(time.Now().Format(time.StampMicro), ln)
}
type simplelog struct{}
func (nl simplelog) Info(f string, args ...interface{}) (err error) {
_, err = fmt.Fprintf(os.Stdout, f+"\n", args...)
return
}
func (nl simplelog) Warn(f string, args ...interface{}) (err error) {
_, err = fmt.Fprintf(os.Stdout, f+"\n", args...)
return
}
func (nl simplelog) Error(f string, args ...interface{}) (err error) {
_, err = fmt.Fprintf(os.Stderr, f+"\n", args...)
return
}
func setupPackers(tp string) (err error) {
tp = strings.TrimSpace(tp)
switch tp {
case `binary`:
tempFunc = binaryTempPack
compFunc = binaryCompPack
case `text`:
tempFunc = textTempPack
compFunc = textCompPack
default:
err = fmt.Errorf("unknown packing format: %v", tp)
}
return
}
func binaryTempPack(tg uint16, ts entry.Timestamp, v float32, name string) (bts []byte, err error) {
bb := bytes.NewBuffer(nil)
if err = binary.Write(bb, binary.BigEndian, tempTagId); err != nil {
return
}
if err = binary.Write(bb, binary.BigEndian, ts.Sec); err != nil {
return
}
if err = binary.Write(bb, binary.BigEndian, ts.Nsec); err != nil {
return
}
if err = binary.Write(bb, binary.BigEndian, v); err != nil {
return
}
if _, err = bb.WriteString(name); err != nil {
return
}
bts = bb.Bytes()
return
}
func textTempPack(tg uint16, ts entry.Timestamp, v float32, name string) (bts []byte, err error) {
bts = []byte(fmt.Sprintf("%s\t%x\t%f\t%s", ts.String(), tg, v, name))
return
}
func binaryCompPack(tg uint16, s, e entry.Timestamp) (bts []byte, err error) {
diff := e.Sec - s.Sec
if diff < 0 {
diff = 0
}
bb := bytes.NewBuffer(nil)
if err = binary.Write(bb, binary.BigEndian, tg); err != nil {
return
}
//encode start, stop, then timeone
if err = binary.Write(bb, binary.BigEndian, s); err != nil {
return
}
if err = binary.Write(bb, binary.BigEndian, e); err != nil {
return
}
if err = binary.Write(bb, binary.BigEndian, diff); err != nil {
return
}
bts = bb.Bytes()
return
}
func textCompPack(tg uint16, s, e entry.Timestamp) (bts []byte, err error) {
diff := e.Sec - s.Sec
if diff < 0 {
diff = 0
}
bts = []byte(fmt.Sprintf("%s\t%s\t%x\t%ds", s.String(), e.String(), tg, diff))
return
}