-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
67 lines (53 loc) · 1.46 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
package main
import (
"os"
"time"
"github.com/anderskvist/GoHelpers/log"
"github.com/anderskvist/GoHelpers/version"
"github.com/anderskvist/DVIEnergiSmartControl/dvi"
"github.com/anderskvist/DVIEnergiSmartControl/influx"
"github.com/anderskvist/DVIEnergiSmartControl/mqtt"
ini "gopkg.in/ini.v1"
)
func main() {
cfg, err := ini.Load(os.Args[1])
if err != nil {
log.Criticalf("Fail to read file: %v", err)
os.Exit(1)
}
log.Infof("DVIEnergiSmartControl version: %s.\n", version.Version)
influxconfig := false
mqttconfig := false
if cfg.Section("influxdb").Key("url").String() != "" {
log.Info("Activating InfluxDB plugin")
influxconfig = true
}
if cfg.Section("mqtt").Key("url").String() != "" {
log.Info("Activating MQTT plugin")
mqttconfig = true
go mqtt.MonitorMQTT(cfg)
}
poll := cfg.Section("main").Key("poll").MustInt(60)
log.Infof("Polltime is %d seconds.\n", poll)
ticker := time.NewTicker(time.Duration(poll) * time.Second)
for ; true; <-ticker.C {
log.Notice("Tick")
log.Info("Getting data from DVI")
dviData, err := dvi.GetDviData(cfg)
if err != nil {
log.Error("Error getting data from DVI")
continue
}
log.Info("Done getting data from DVI")
if influxconfig {
log.Info("Saving data to InfluxDB")
influx.SaveToInflux(cfg, dviData)
log.Info("Done saving to InfluxDB")
}
if mqttconfig {
log.Info("Sending data to MQTT")
mqtt.SendToMQTT(cfg, dviData)
log.Info("Done sending to MQTT")
}
}
}