-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
101 lines (70 loc) · 1.97 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
/* eslint no-console : 0 */
/* eslint no-global-assign : 0 */
process.title = 'iris2mqtt';
terminating = false;
if (typeof process.env.NODE_ENV !== 'undefined' && process.env.NODE_ENV !== null && process.env.NODE_ENV !== '') {
appEnv = process.env.NODE_ENV;
}
else {
appEnv = 'development';
}
// hgbg libraries
api = require('./api');
bitmask = require('./bitmask');
hass = require('./hass');
hex = require('./hex');
json = require('./json');
log = require('./log-output');
mqtt = require('./mqtt');
num = require('./num');
xbee = require('./xbee');
update = new (require('./update'))();
// Global init
async function init() {
log.msg('Initializing');
// console.dir(process.argv, { depth : null, showHidden : true });
// Configure term event listeners
process.on('SIGTERM', async () => {
console.log('');
log.msg('Caught SIGTERM :: terminating = ' + terminating);
if (terminating === true) return;
await term();
});
process.on('SIGINT', async () => {
console.log('');
log.msg('Caught SIGINT :: terminating = ' + terminating);
if (terminating === true) return;
await term();
});
// Read JSON config and status files
await json.read();
// Start Home Assistant interface
await hass.init();
// Start MQTT client
await mqtt.init();
// Start XBee interface
await xbee.init();
// Start Express API server
await api.init();
// Update Home Assistant device registry
// hass.updateDeviceRegistry();
log.msg('Initialized');
} // async init()
// Global term
async function term() {
terminating = true;
log.msg('Terminating');
// Terminate Express API server
await api.term();
// Terminate XBee interface
await xbee.term();
// Terminate MQTT client
await mqtt.term();
// Terminate Home Assistant interface
await hass.term();
// Write JSON config and status files, and clear save interval
await json.write('term');
log.msg('Terminated');
} // async term()
// FASTEN SEATBELTS
(async () => { await init(); })();