forked from gaussmeter/teslamater
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
369 lines (351 loc) · 11.9 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
// https://www.eclipse.org/paho/clients/golang/
/*
Todo
make configurable....
-- log level
*/
package main
import (
"encoding/json"
"io/ioutil"
"net/http"
"os"
"strconv"
"strings"
"time"
MQTT "github.com/eclipse/paho.mqtt.golang"
ag "github.com/gaussmeter/mqttagent"
pkger "github.com/markbates/pkger"
log "github.com/sirupsen/logrus"
randstr "github.com/thanhpk/randstr"
)
// Config - defines all of the states.
type Config struct {
HomePluggedInAsleep HomePluggedInAsleep `json:"homepluggedinasleep"`
HomePluggedInAwake HomePluggedInAwake `json:"homepluggedinawake"`
HomeUnpluggedAsleep HomeUnpluggedAsleep `json:"homeunpluggedasleep"`
HomeUnpluggedAwake HomeUnpluggedAwake `json:"homeunpluggedawake"`
NotHomeAlseep NotHomeAlseep `json:"nothomeasleep"`
NotHomeAwake NotHomeAwake `json:"nothomeawake"`
Default Default `json:"default"`
UnHealthy UnHealthy `json:"unhealthy"`
Offline Offline `json:"offline"`
HomeUpdateAvailable HomeUpdateAvailable `json:"homeupdateavailable"`
Updating Updating `json:"updating"`
Charging Charging `json:"charging"`
DoorOpen DoorOpen `json:"dooropen"`
TrunkOrFrunkOpen TrunkOrFrunkOpen `json:"trunkorfrunkopen"`
WindowOpenAwake WindowOpenAwake `json:"windowopenawake"`
WindowOpenAsleep WindowOpenAsleep `json:"windowopenasleep"`
}
// Lumen - body of the POST to lumenHost
type Lumen struct {
Bright int `json:"bright"`
Animation string `json:"animation"`
Percent int `json:"percent"`
Velocity int `json:"velocity"`
R int `json:"r"`
G int `json:"g"`
B int `json:"b"`
W int `json:"w"`
R2 int `json:"r2"`
G2 int `json:"g2"`
B2 int `json:"b2"`
W2 int `json:"w2"`
}
// Default -- default to use when insufficient state is known
type Default struct {
Lumen Lumen `json:"lumen"`
}
// NotHomeAlseep --
type NotHomeAlseep struct {
Lumen Lumen `json:"lumen"`
}
// HomePluggedInAsleep --
type HomePluggedInAsleep struct {
Lumen Lumen `json:"lumen"`
}
// HomeUnpluggedAsleep --
type HomeUnpluggedAsleep struct {
Lumen Lumen `json:"lumen"`
}
// NotHomeAwake --
type NotHomeAwake struct {
Lumen Lumen `json:"lumen"`
}
// HomePluggedInAwake --
type HomePluggedInAwake struct {
Lumen Lumen `json:"lumen"`
}
// HomeUnpluggedAwake --
type HomeUnpluggedAwake struct {
Lumen Lumen `json:"lumen"`
}
// UnHealthy --
type UnHealthy struct {
Lumen Lumen `json:"lumen"`
}
// Offline --
type Offline struct {
Lumen Lumen `json:"lumen"`
}
// HomeUpdateAvailable --
type HomeUpdateAvailable struct {
Lumen Lumen `json:"lumen"`
}
// Updating --
type Updating struct {
Lumen Lumen `json:"lumen"`
}
// Charging --
type Charging struct {
Lumen Lumen `json:"lumen"`
}
// DoorOpen --
type DoorOpen struct {
Lumen Lumen `json:"lumen"`
}
// TrunkOrFrunkOpen --
type TrunkOrFrunkOpen struct {
Lumen Lumen `json:"lumen"`
}
// WindowOpenAwake --
type WindowOpenAwake struct {
Lumen Lumen `json:"lumen"`
}
// WindowOpenAsleep --
type WindowOpenAsleep struct {
Lumen Lumen `json:"lumen"`
}
var config Config
var debug bool = true
var geoFence string = "unset"
var speed int = -1
var state string = "unset"
var pluggedIn bool = false
var chargeLimitSoc int = -1
var batteryLevel int = -1
var healthy bool = true
var host string = "ws://192.168.1.51:9001"
var car string = "1"
var home string = "Home"
var topicPrefix string = "teslamate/cars/"
var lumenHost string = "http://192.168.1.127:9000/lumen"
var user string = ""
var pass string = ""
var loopSleep time.Duration = 250
var doorOpen bool = false
var trunkOpen bool = false
var frunkOpen bool = false
var windowOpen bool = false
var updateAvailable bool = false
var shiftState string = "P"
var out []byte
var httpClient = &http.Client{Timeout: time.Second * 5}
var geoFenceMq MQTT.MessageHandler = func(client MQTT.Client, msg MQTT.Message) {
geoFence = string(msg.Payload())
}
var speedMq MQTT.MessageHandler = func(client MQTT.Client, msg MQTT.Message) {
speed, _ = strconv.Atoi(string(msg.Payload()))
}
var stateMq MQTT.MessageHandler = func(client MQTT.Client, msg MQTT.Message) {
state = string(msg.Payload())
}
var pluggedInMq MQTT.MessageHandler = func(client MQTT.Client, msg MQTT.Message) {
pluggedIn, _ = strconv.ParseBool(string(msg.Payload()))
}
var chargeLimitSocMq MQTT.MessageHandler = func(client MQTT.Client, msg MQTT.Message) {
chargeLimitSoc, _ = strconv.Atoi(string(msg.Payload()))
}
var batteryLevelMq MQTT.MessageHandler = func(client MQTT.Client, msg MQTT.Message) {
batteryLevel, _ = strconv.Atoi(string(msg.Payload()))
}
var healthyMq MQTT.MessageHandler = func(client MQTT.Client, msg MQTT.Message) {
healthy, _ = strconv.ParseBool(string(msg.Payload()))
}
var doorOpenMq MQTT.MessageHandler = func(client MQTT.Client, msg MQTT.Message) {
doorOpen, _ = strconv.ParseBool(string(msg.Payload()))
}
var trunkOpenMq MQTT.MessageHandler = func(client MQTT.Client, msg MQTT.Message) {
trunkOpen, _ = strconv.ParseBool(string(msg.Payload()))
}
var frunkOpenMq MQTT.MessageHandler = func(client MQTT.Client, msg MQTT.Message) {
frunkOpen, _ = strconv.ParseBool(string(msg.Payload()))
}
var windowOpenMq MQTT.MessageHandler = func(client MQTT.Client, msg MQTT.Message) {
windowOpen, _ = strconv.ParseBool(string(msg.Payload()))
}
var updateAvailableMq MQTT.MessageHandler = func(client MQTT.Client, msg MQTT.Message) {
updateAvailable, _ = strconv.ParseBool(string(msg.Payload()))
}
var shiftStateMq MQTT.MessageHandler = func(client MQTT.Client, msg MQTT.Message) {
shiftState = string(msg.Payload())
}
func getSetting(setting string, defaultValue string) (value string) {
if os.Getenv(setting) != "" {
log.WithFields(log.Fields{"configFrom": "env", setting: os.Getenv(setting)}).Info()
return os.Getenv(setting)
}
log.WithFields(log.Fields{"configFrom": "default", setting: defaultValue}).Info()
return defaultValue
}
func init() {
log.SetFormatter(&log.TextFormatter{
DisableColors: false,
FullTimestamp: true,
})
// get config from environment
log.SetReportCaller(false)
host = getSetting("MQTT_HOST", host)
user = getSetting("MQTT_USER", user)
pass = getSetting("MQTT_PASS", pass)
lumenHost = getSetting("LUMEN_HOST", lumenHost)
car = getSetting("CAR_NUMBER", car)
home = getSetting("GEOFENCE_HOME", home)
}
func main() {
configJSON, err := os.Open("config.json")
if err != nil {
log.WithFields(log.Fields{"Error": err.Error()}).Warn("config.json not found, default will be used.")
} else {
log.Info("loaded config.json")
}
defaultJSON, err := pkger.Open("/default.json")
if err != nil {
log.WithFields(log.Fields{"Error": err.Error()}).Error()
os.Exit(1)
} else {
log.Info("loaded default.json")
}
byteValue, _ := ioutil.ReadAll(defaultJSON)
json.Unmarshal(byteValue, &config)
byteValue, _ = ioutil.ReadAll(configJSON)
json.Unmarshal(byteValue, &config)
configJSON.Close()
defaultJSON.Close()
agent := ag.NewAgent(host, "teslamater-"+randstr.String(4), user, pass)
err = agent.Connect()
if err != nil {
log.WithField("error", err).Error("Can't connect to mqtt server")
os.Exit(1)
}
agent.Subscribe(topicPrefix+car+"/geofence", geoFenceMq)
agent.Subscribe(topicPrefix+car+"/speed", speedMq)
agent.Subscribe(topicPrefix+car+"/state", stateMq)
agent.Subscribe(topicPrefix+car+"/plugged_in", pluggedInMq)
agent.Subscribe(topicPrefix+car+"/charge_limit_soc", chargeLimitSocMq)
agent.Subscribe(topicPrefix+car+"/battery_level", batteryLevelMq)
agent.Subscribe(topicPrefix+car+"/healthy", healthyMq)
agent.Subscribe(topicPrefix+car+"/doors_open", doorOpenMq)
agent.Subscribe(topicPrefix+car+"/trunk_open", trunkOpenMq)
agent.Subscribe(topicPrefix+car+"/frunk_open", frunkOpenMq)
agent.Subscribe(topicPrefix+car+"/windows_open", windowOpenMq)
agent.Subscribe(topicPrefix+car+"/update_available", updateAvailableMq)
agent.Subscribe(topicPrefix+car+"/shift_state", shiftStateMq)
var body string = ""
var lastBody string = ""
var velocity int = 1
var percent int = 0
var lastSendTime int64 = 0
for !agent.IsTerminated() {
if speed == 0 {
velocity = 1
} else {
velocity = speed
}
if batteryLevel != 0 && chargeLimitSoc != 0 {
percent = int((float32(batteryLevel) / float32(chargeLimitSoc)) * 100)
} else {
percent = 10
}
loopSleep = 250
switch true {
case healthy == false:
out, _ = json.Marshal(config.UnHealthy.Lumen)
break
case state == "offline":
out, _ = json.Marshal(config.Offline.Lumen)
break
case updateAvailable && geoFence == "Home":
out, _ = json.Marshal(config.HomeUpdateAvailable.Lumen)
break
case state == "updating":
out, _ = json.Marshal(config.Updating.Lumen)
break
case state == "charging":
out, _ = json.Marshal(config.Charging.Lumen)
break
case doorOpen == true:
out, _ = json.Marshal(config.DoorOpen.Lumen)
break
case trunkOpen == true || frunkOpen == true:
out, _ = json.Marshal(config.TrunkOrFrunkOpen.Lumen)
break
case windowOpen == true && state != "asleep" && state != "driving":
out, _ = json.Marshal(config.WindowOpenAwake.Lumen)
break
case windowOpen == true && state == "asleep":
out, _ = json.Marshal(config.WindowOpenAsleep.Lumen)
break
case state == "unset" || speed == -1 || batteryLevel == -1 || chargeLimitSoc == -1:
out, _ = json.Marshal(config.Default.Lumen)
log.Info("too many unset values")
loopSleep = 3000
break
case geoFence == home && pluggedIn == true && state != "asleep":
config.HomePluggedInAwake.Lumen.Percent = percent
config.HomePluggedInAwake.Lumen.Velocity = velocity
out, _ = json.Marshal(config.HomePluggedInAwake.Lumen)
break
case geoFence == home && pluggedIn == true && state == "asleep":
config.HomePluggedInAsleep.Lumen.Percent = percent
config.HomePluggedInAsleep.Lumen.Velocity = velocity
out, _ = json.Marshal(config.HomePluggedInAsleep.Lumen)
break
case geoFence == home && pluggedIn == false && state != "asleep":
config.HomeUnpluggedAwake.Lumen.Percent = percent
config.HomeUnpluggedAwake.Lumen.Velocity = velocity
out, _ = json.Marshal(config.HomeUnpluggedAwake.Lumen)
break
case geoFence == home && pluggedIn == false && state == "asleep":
config.HomeUnpluggedAsleep.Lumen.Percent = percent
config.HomeUnpluggedAsleep.Lumen.Velocity = velocity
out, _ = json.Marshal(config.HomeUnpluggedAsleep.Lumen)
break
case geoFence != home && state != "asleep":
config.NotHomeAwake.Lumen.Percent = percent
config.NotHomeAwake.Lumen.Velocity = velocity
out, _ = json.Marshal(config.NotHomeAwake.Lumen)
break
case geoFence != home && state == "asleep":
config.NotHomeAlseep.Lumen.Percent = percent
config.NotHomeAlseep.Lumen.Velocity = velocity
out, _ = json.Marshal(config.NotHomeAlseep.Lumen)
break
}
body = string(out)
if body != lastBody || time.Now().Unix()-lastSendTime > 90 {
//todo: ?escape json body in log?
if body != lastBody {
log.WithFields(log.Fields{"GeoFence": geoFence , "Speed": speed, "State": state, "PluggedIn": pluggedIn, "Healthy": healthy, "ChargeLimit": chargeLimitSoc, "ChargeLevel": batteryLevel, "Percent": percent, "DoorOpen": doorOpen, "TrunkOpen": trunkOpen, "FrunkOpen": frunkOpen, "WindowOpen": windowOpen, "UpdateAvailable": updateAvailable, "ShiftState": shiftState}).Info()
log.WithFields(log.Fields{"body": body}).Info()
}
req, err := http.NewRequest(http.MethodPut, lumenHost, strings.NewReader(body))
if debug == true && err != nil {
log.WithFields(log.Fields{"error": err.Error()}).Info()
}
resp, err := httpClient.Do(req)
if debug == true && err == nil && body != lastBody {
log.WithFields(log.Fields{"statusCode": resp.StatusCode}).Info("response")
}
if debug == true && err != nil {
log.WithFields(log.Fields{"error": err.Error()}).Info()
}
resp.Body.Close()
lastBody = body
lastSendTime = time.Now().Unix()
}
time.Sleep(loopSleep * time.Millisecond)
httpClient.CloseIdleConnections()
}
}