This repository has been archived by the owner on Feb 24, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 96
/
app.js
268 lines (224 loc) · 6.68 KB
/
app.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
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
var express = require('express')
var reqlib = require('app-root-path').require
var logger = require('morgan')
var cookieParser = require('cookie-parser')
var bodyParser = require('body-parser')
var app = express()
var SerialPort = require('serialport')
var jsonStore = reqlib('/lib/jsonStore.js')
var cors = require('cors')
var ZWaveClient = reqlib('/lib/ZwaveClient')
var MqttClient = reqlib('/lib/MqttClient')
var Gateway = reqlib('/lib/Gateway')
var store = reqlib('config/store.js')
var debug = reqlib('/lib/debug')('App')
var history = require('connect-history-api-fallback')
var utils = reqlib('/lib/utils.js')
const renderIndex = reqlib('/lib/renderIndex')
var gw //the gateway instance
let io
debug('Zwave2Mqtt version: ' + require('./package.json').version)
debug('Application path:' + utils.getPath(true))
// view engine setup
app.set('views', utils.joinPath(false, 'views'))
app.set('view engine', 'ejs')
app.use(logger('dev'))
app.use(bodyParser.json({ limit: '50mb' }))
app.use(
bodyParser.urlencoded({
limit: '50mb',
extended: true,
parameterLimit: 50000
})
)
app.use(cookieParser())
app.get('/', renderIndex)
app.use('/', express.static(utils.joinPath(false, 'dist')))
app.use(cors())
app.use(history())
function startGateway () {
var settings = jsonStore.get(store.settings)
var mqtt, zwave
if (settings.mqtt) {
mqtt = new MqttClient(settings.mqtt)
}
if (settings.zwave) {
zwave = new ZWaveClient(settings.zwave, io)
}
gw = new Gateway(settings.gateway, zwave, mqtt)
}
app.startSocket = function (server) {
io = require('socket.io')(server)
if (gw.zwave) gw.zwave.socket = io
io.on('connection', function (socket) {
debug('New connection', socket.id)
socket.on('INITED', function () {
if (gw.zwave)
socket.emit('INIT', {
nodes: gw.zwave.nodes,
info: gw.zwave.ozwConfig,
error: gw.zwave.error,
cntStatus: gw.zwave.cntStatus
})
})
socket.on('ZWAVE_API', async function (data) {
debug('Zwave api call:', data.api, data.args)
if (gw.zwave) {
var result = await gw.zwave.callApi(data.api, ...data.args)
result.api = data.api
socket.emit('API_RETURN', result)
}
})
socket.on('HASS_API', async function (data) {
switch (data.apiName) {
case 'delete':
gw.publishDiscovery(data.device, data.node_id, true, true)
break
case 'discover':
gw.publishDiscovery(data.device, data.node_id, false, true)
break
case 'rediscoverNode':
gw.rediscoverNode(data.node_id)
break
case 'disableDiscovery':
gw.disableDiscovery(data.node_id)
break
case 'update':
gw.zwave.updateDevice(data.device, data.node_id)
break
case 'add':
gw.zwave.addDevice(data.device, data.node_id)
break
case 'store':
await gw.zwave.storeDevices(data.devices, data.node_id, data.remove)
break
}
})
socket.on('disconnect', function () {
debug('User disconnected', socket.id)
})
})
const interceptor = function (write) {
return function (...args) {
io.emit('DEBUG', args[0].toString())
write.apply(process.stdout, args)
}
}
process.stdout.write = interceptor(process.stdout.write)
process.stderr.write = interceptor(process.stderr.write)
}
// ----- APIs ------
app.get('/health', async function (req, res) {
var mqtt = false
var zwave = false
if (gw) {
mqtt = gw.mqtt ? gw.mqtt.getStatus().status : false
zwave = gw.zwave ? gw.zwave.getStatus().status : false
}
var status = mqtt && zwave
res.status(status ? 200 : 500).send(status ? 'Ok' : 'Error')
})
app.get('/health/:client', async function (req, res) {
var client = req.params.client
if (client !== 'zwave' && client !== 'mqtt')
res.status(500).send("Requested client doesn 't exist")
else {
status = gw && gw[client] ? gw[client].getStatus().status : false
}
res.status(status ? 200 : 500).send(status ? 'Ok' : 'Error')
})
//get settings
app.get('/api/settings', async function (req, res) {
var data = {
success: true,
settings: jsonStore.get(store.settings),
devices: gw.zwave ? gw.zwave.devices : {},
serial_ports: []
}
if (process.platform !== 'sunos') {
try {
var ports = await SerialPort.list()
} catch (error) {
debug(error)
}
data.serial_ports = ports ? ports.map(p => p.path) : []
res.json(data)
} else res.json(data)
})
//get config
app.get('/api/exportConfig', function (req, res) {
return res.json({
success: true,
data: jsonStore.get(store.nodes),
message: 'Successfully exported nodes JSON configuration'
})
})
//import config
app.post('/api/importConfig', async function (req, res) {
var config = req.body.data
try {
if (!gw.zwave) throw Error('Zwave client not inited')
if (!Array.isArray(config)) throw Error('Configuration not valid')
else {
for (let i = 0; i < config.length; i++) {
const e = config[i]
if (e && (!e.hasOwnProperty('name') || !e.hasOwnProperty('loc'))) {
throw Error('Configuration not valid')
} else if (e) {
await gw.zwave.callApi('_setNodeName', i, e.name || '')
await gw.zwave.callApi('_setNodeLocation', i, e.loc || '')
if (e.hassDevices)
await gw.zwave.storeDevices(e.hassDevices, i, false)
}
}
}
res.json({ success: true, message: 'Configuration imported successfully' })
} catch (error) {
debug(error.message)
return res.json({ success: false, message: error.message })
}
})
//update settings
app.post('/api/settings', function (req, res) {
jsonStore
.put(store.settings, req.body)
.then(data => {
res.json({ success: true, message: 'Configuration updated successfully' })
return gw.close()
})
.then(() => startGateway())
.catch(err => {
debug(err)
res.json({ success: false, message: err.message })
})
})
// catch 404 and forward to error handler
app.use(function (req, res, next) {
var err = new Error('Not Found')
err.status = 404
next(err)
})
// error handler
app.use(function (err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message
res.locals.error = req.app.get('env') === 'development' ? err : {}
console.log(
'%s %s %d - Error: %s',
req.method,
req.url,
err.status,
err.message
)
// render the error page
res.status(err.status || 500)
res.redirect('/')
})
startGateway()
process.removeAllListeners('SIGINT')
process.on('SIGINT', function () {
debug('Closing...')
gw.close()
process.exit()
})
module.exports = app