-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
145 lines (124 loc) · 4.75 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
const _ = require('lodash')
const logging = require('homeautomation-js-lib/logging.js')
const health = require('homeautomation-js-lib/health.js')
const venstar = require('./lib/venstar.js')
const mqtt_helpers = require('homeautomation-js-lib/mqtt_helpers.js')
var topic_prefix = process.env.TOPIC_PREFIX
if (_.isNil(topic_prefix)) {
logging.warn('TOPIC_PREFIX not set, not starting')
process.abort()
}
var mqttOptions = { qos: 1 }
var shouldRetain = process.env.MQTT_RETAIN
if (_.isNil(shouldRetain)) {
shouldRetain = true
}
mqttOptions['retain'] = shouldRetain
var connectedEvent = function() {
health.healthyEvent()
const topics = [topic_prefix + '/fan/set',
topic_prefix + '/query',
topic_prefix + '/mode/set',
topic_prefix + '/setting/+/set',
topic_prefix + '/temperature/target/set',
topic_prefix + '/temperature/cool/set',
topic_prefix + '/temperature/heat/set'
]
logging.info('Connected, subscribing ')
topics.forEach(function(topic) {
logging.info(' => Subscribing to: ' + topic)
client.subscribe(topic, { qos: 1 })
}, this)
}
var disconnectedEvent = function() {
health.unhealthyEvent()
}
// Setup MQTT
const client = mqtt_helpers.setupClient(connectedEvent, disconnectedEvent)
client.on('message', (topic, message) => {
logging.info(' ' + topic + ':' + message, {
topic: topic,
value: message
})
var target = '' + message
if (topic.indexOf('/mode/set') >= 0) {
logging.info('MQTT Set Mode: ' + target, {
action: 'setmode',
value: target
})
venstar.updateThermostat(target, 'none', 0, 0, 0)
} else if (topic.indexOf('/query') >= 0) {
logging.info('Querying: ' + target, {
action: 'query',
result: target
})
venstar.query(target)
} else if (topic.indexOf('/fan/set') >= 0) {
logging.info('MQTT Set Fan Mode: ' + target, {
action: 'setfanmode',
result: target
})
venstar.updateThermostat('none', target, 0, 0, 0)
} else if (topic.indexOf('/temperature/heat/set') >= 0) {
logging.info('MQTT Set Heat Temperature: ' + target, {
action: 'setheat',
result: target
})
venstar.updateThermostat('none', 'none', 0, target, 0)
} else if (topic.indexOf('/temperature/cool/set') >= 0) {
logging.info('MQTT Set Cool Temperature: ' + target, {
action: 'setcool',
result: target
})
venstar.updateThermostat('none', 'none', target, 0, 0)
} else if (topic.indexOf('/temperature/target/set') >= 0) {
logging.info('MQTT Set Target Temperature: ' + target, {
action: 'settarget',
result: target
})
venstar.updateThermostat('none', 'none', 0, 0, target)
} else if (topic.indexOf('/set') >= 0) {
logging.info('MQTT General Setting: ' + target, {
action: 'settarget',
result: target
})
const components = topic.split('/')
const settingName = components[components.length - 2]
venstar.updateThermostatSetting(settingName, message)
}
})
venstar.on('alert-updated', (alert) => {
const value = alert.active == 'true' ? 1 : 0
client.smartPublish(mqtt_helpers.generateTopic(topic_prefix, 'alert', alert.name.toString()), value.toString(), mqttOptions)
})
venstar.on('runtime-updated', (runtime) => {
Object.keys(runtime).forEach(key => {
const value = runtime[key]
if (!_.isNil(value)) {
client.smartPublish(mqtt_helpers.generateTopic(topic_prefix, 'runtime', key.toString()), value.toString(), mqttOptions)
}
})
})
venstar.on('query-response', (type, result) => {
Object.keys(result).forEach(key => {
const value = result[key]
if (!_.isNil(value)) {
client.publish(mqtt_helpers.generateTopic(topic_prefix, 'result', type, key.toString()), value.toString())
}
})
})
venstar.on('sensor-updated', (sensor) => {
logging.debug('sensor: ' + JSON.stringify(sensor))
if (!_.isNil(sensor.temp)) {
client.smartPublish(mqtt_helpers.generateTopic(topic_prefix, 'sensor', sensor.name.toString(), 'temp'), sensor.temp.toString(), mqttOptions)
}
if (!_.isNil(sensor.hum)) {
client.smartPublish(mqtt_helpers.generateTopic(topic_prefix, 'sensor', sensor.name.toString(), 'humidity'), sensor.hum.toString(), mqttOptions)
}
})
venstar.on('statistic-updated', (statistic, value) => {
client.smartPublish(topic_prefix + '/' + statistic.toString(), value.toString(), mqttOptions)
})
venstar.on('target-temperature-updated', (target) => {
client.smartPublish(topic_prefix + '/temperature/target', target.toString(), mqttOptions)
})