-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
123 lines (97 loc) · 3.26 KB
/
index.js
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
const mqtt = require('mqtt')
const _ = require('lodash')
const logging = require('homeautomation-js-lib/logging.js')
const health = require('homeautomation-js-lib/health.js')
const tesla = require('./lib/tesla.js')
const mqtt_helpers = require('homeautomation-js-lib/mqtt_helpers.js')
// Config
var topic_prefix = process.env.TOPIC_PREFIX
if (_.isNil(topic_prefix)) {
logging.warn('TOPIC_PREFIX not set, not starting')
process.abort()
}
var mqttOptions = {}
var shouldRetain = process.env.MQTT_RETAIN
if (_.isNil(shouldRetain)) {
shouldRetain = false
}
if (!_.isNil(shouldRetain)) {
mqttOptions['retain'] = shouldRetain
}
var client = null
const reserveTopicSuffix = '/reserve/percent/set'
const modeTopicSuffix = '/reserve/mode/set'
var connectedEvent = function() {
logging.info('connected')
health.healthyEvent()
client.subscribe(topic_prefix + reserveTopicSuffix)
client.subscribe(topic_prefix + modeTopicSuffix)
}
var disconnectedEvent = function() {
logging.info('disconnected')
health.unhealthyEvent()
}
// Setup MQTT
logging.info('connecting to MQTT host')
client = mqtt_helpers.setupClient(connectedEvent, disconnectedEvent)
client.on('message', (topic, message) => {
logging.info(' ' + topic + ':' + message)
if (topic.toString().includes(reserveTopicSuffix)) {
tesla.setReservePercent(message)
} else if (topic.toString().includes(modeTopicSuffix)) {
tesla.setMode(message.toString())
}
})
tesla.on('soe-updated', (result) => {
if (_.isNil(result)) {
logging.error(' soe-updated failed')
health.unhealthyEvent()
return
}
client.smartPublish(topic_prefix + '/reserve/battery/percent', '' + result.toFixed(2), mqttOptions)
health.healthyEvent()
})
tesla.on('solar-updated', (result) => {
if (_.isNil(result)) {
logging.error(' soe-updated failed')
health.unhealthyEvent()
return
}
client.smartPublish(topic_prefix + '/stats/solar_generation', result.toFixed(2).toString(), mqttOptions)
health.healthyEvent()
})
tesla.on('grid-updated', (result) => {
if (_.isNil(result)) {
logging.error(' grid-updated failed')
health.unhealthyEvent()
return
}
client.smartPublish(topic_prefix + '/stats/grid_usage', result.toFixed(2).toString(), mqttOptions)
client.smartPublish(topic_prefix + '/stats/grid_active', '' + (result > 50 ? '1' : '0'), mqttOptions)
health.healthyEvent()
})
tesla.on('battery-updated', (result) => {
if (_.isNil(result)) {
logging.error(' battery-updated failed')
health.unhealthyEvent()
return
}
client.smartPublish(topic_prefix + '/stats/battery_usage', result.toFixed(2).toString(), mqttOptions)
health.healthyEvent()
})
tesla.on('load-updated', (result) => {
if (_.isNil(result)) {
logging.error(' load-updated failed')
health.unhealthyEvent()
return
}
client.smartPublish(topic_prefix + '/stats/home_load', result.toFixed(2).toString(), mqttOptions)
health.healthyEvent()
})
tesla.startPolling()
// setTimeout(function() {
// tesla.setReservePercent(50)
// // tesla.setMode('self_consumption')
// // // tesla.setMode('backup')
// // // tesla.setMode('reserve')
// }, 10 * 1000)