-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
136 lines (119 loc) · 3.2 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
package main
import (
"context"
"fmt"
"log"
"os"
"os/signal"
"syscall"
"time"
"github.com/aeytom/silence-data/hass"
"github.com/aeytom/silence-data/silence"
influxdb2 "github.com/influxdata/influxdb-client-go/v2"
ilog "github.com/influxdata/influxdb-client-go/v2/log"
)
func main() {
ParseArgs()
ha := hass.Connect(Conf.HomeAssistant)
defer ha.Disconnect()
ixclient := influxdb2.NewClient(Conf.Influx.Url, Conf.Influx.Token)
ixclient.Options().SetLogLevel(ilog.DebugLevel)
si := silence.Silence{}
if err := si.Login(Conf.Silence.Email, Conf.Silence.Password); err != nil {
log.Fatalln(err)
}
var err error
var profile silence.ProfileResponse
if profile, err = si.Me(); err != nil {
log.Fatalln(err)
} else {
log.Printf("%#v", profile)
}
// silence.TripsList(profile.Id, 100)
// panic("Schluss")
scooters, err := si.Details()
if err != nil {
log.Fatalln(err)
} else {
for _, sc := range scooters {
hass.RegisterScooter(ha, sc)
}
}
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
ticker := time.NewTicker(30 * time.Second)
done := make(chan bool)
hastatus := ha.Subscribe(hass.DiscoveryPrefix+"/status", 0)
go func() {
for {
select {
case <-done:
return
case s := <-sigs:
log.Print("Got signal: ", s)
done <- true
return
case t := <-ticker.C:
log.Println("Tick at", t)
scooters, err := si.Details()
if err != nil {
log.Fatalln(err)
}
log.Printf("%#v", scooters)
for _, scooter := range scooters {
hass.SendStatus(ha, scooter)
hass.SendLocation(ha, scooter)
sendToInflux(ixclient, scooter)
}
case t := <-hastatus:
log.Printf("Got homeasistent msg '%v' via topic '%s'\n", t.Payload(), t.Topic())
for _, s := range ha.Scooters {
ha.SendDiscovery(*s)
}
}
}
}()
<-done
ticker.Stop()
}
func sendToInflux(ixclient influxdb2.Client, scooter silence.ScooterResp) {
writeAPI := ixclient.WriteAPIBlocking(Conf.Influx.Org, Conf.Influx.Bucket)
tags := map[string]string{
"id": scooter.Id,
"model": scooter.Model,
"revision": scooter.Revision,
"color": scooter.Color,
"name": scooter.Name,
"imei": scooter.Imei,
"frameno": scooter.FrameNo,
"firmware": scooter.TrackingDevice.FirmwareVersion,
"battery": fmt.Sprint(scooter.BatteryId),
}
fields := map[string]interface{}{
"speed": scooter.LastLocation.CurrentSpeed,
"bsoc": scooter.BatterySoc,
"btemp": scooter.BatteryTemperature,
"odo": scooter.Odometer,
"mtemp": scooter.MotorTemperature,
"itemp": scooter.InverterTemperature,
"range": scooter.Range,
"velocity": scooter.Velocity,
"lat": scooter.LastLocation.Latitude,
"lon": scooter.LastLocation.Longitude,
}
lt := scooter.LastConnection
if scooter.LastLocation.Time != "" {
lt = scooter.LastLocation.Time
} else if scooter.LastReportTime != "" {
lt = scooter.LastReportTime
}
log.Printf("last location/report time %#v", lt)
if lrt, err := time.Parse(time.RFC3339, lt); err != nil {
log.Fatalln(err)
} else {
point := influxdb2.NewPoint("scooter", tags, fields, lrt)
if err := writeAPI.WritePoint(context.Background(), point); err != nil {
log.Fatal(err)
}
}
}