-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
134 lines (118 loc) · 4.66 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
'use strict';
/*
* NEEO driver for myStrom WiFi Switch device
* https://github.com/zehnm/neeo-mystrom-switch
*
* Control local myStrom WiFi switches with NEEO.
* WiFi Switch v2 devices are auto-discovered on local subnet.
* Manual configuration is possible in config/mystrom.json.
*
* Still needs some improvements:
* - code clean up (module exports, naming conventions etc.)
* - error & auto retry handling
* - option to use myStrom cloud (either for initial discovery only or for full device access)
* - setting device reachability flag with connectivity test
* ... and I really don't like Java Script, so certain things are probably a bit messy :/
*
* Tested with:
* - Node.js v8.9.1
* - NEEO SDK 0.48.13 https://github.com/NEEOInc/neeo-sdk
* - myStrom WiFi Switch v2 (firmware 3.60) and v1 (firmware 2.31) https://www.mystrom.ch/
*/
const neeoapi = require('neeo-sdk');
const logger = require('winston');
const Constants = require('./lib/constants');
const NeeoDevice = require('./lib/neeoDevice');
logger.level = process.env.LOG_LEVEL ? process.env.LOG_LEVEL : 'info';
// default configuration with required parameters. Customize in driver.json
// Optional: neeo.brainIp, neeo.callbackIp, mystrom.polling.interval, mystrom.polling.duration
var config = {
"neeo": {
"callbackPort": 6336
},
"mystrom": {
"discoveryModes": {
"configFile": false, // read devices from configration file
"local": true // local discovery mode (listen for UDP broadcast)
},
"localDiscovery": {
"listenAddress": "0.0.0.0", // listen address for UDP broadcast. 0.0.0.0 = all interfaces
"reachableTimeout": 30, // timeout in seconds to consider a device offline if no discovery message received
"deviceTypeFilter": ["WS2"] // only consider the specified myStrom device types. See ./lib/mystrom/myStrom.js
}
}
};
console.log('NEEO device "myStrom WiFi Switch" v0.1.0');
console.log('------------------------------------------');
// Config file is optional
try {
config = require(__dirname + '/config/driver.json');
} catch (e) {
logger.info('Cannot find or load config.json! Using default values.');
}
const neeoDevices = [];
const neeoSwitchDevice = new NeeoDevice(config);
neeoDevices.push(neeoSwitchDevice.buildDevice(Constants.DEVICE_NAME, Constants.MANUFACTURER));
var brainIp = process.env.BRAINIP;
var baseurl = undefined;
if (brainIp) {
logger.info('[NEEO] Using NEEO Brain IP from env variable:', brainIp);
} else if (config.neeo.brainIp) {
brainIp = config.neeo.brainIp;
logger.info('[NEEO] Using NEEO Brain IP from configuration:', brainIp);
}
// baseurl must be set for certain network setup (i.e. Windows with Hyper-V) until SDK is fixed.
// See forum and related issue with auto-discovery: https://github.com/NEEOInc/neeo-sdk/issues/36
if (config.neeo.callbackIp) {
baseurl = 'http://' + config.neeo.callbackIp + ':' + config.neeo.callbackPort;
}
if (brainIp) {
startDeviceServer(brainIp, config.neeo.callbackPort, baseurl, neeoDevices);
} else {
logger.info('[NEEO] discover one NEEO Brain...');
neeoapi.discoverOneBrain()
.then((brain) => {
logger.info('[NEEO] Brain discovered:', brain.name);
brainIp = brain; // save discovered IP for shutdown hook
startDeviceServer(brain, config.neeo.callbackPort, baseurl, neeoDevices);
});
}
function startDeviceServer(brain, port, callbackBaseurl, neeoDevices) {
logger.info('[NEEO] Starting server on port %d ...', port);
neeoapi.startServer({
brain,
port,
baseurl: callbackBaseurl,
name: Constants.ADAPTER_NAME,
devices: neeoDevices
})
.then(() => {
logger.info('[NEEO] API server ready! Use the NEEO app to search for "%s" or "%s".', Constants.MANUFACTURER, Constants.DEVICE_NAME);
})
.catch((error) => {
logger.error('[NEEO] Error starting device server!', error.message);
process.exit(9);
});
}
// shutdown hook for graceful shutdown
var gracefulShutdown = function() {
logger.verbose("Received kill signal, shutting down gracefully.");
neeoapi.stopServer({brain: brainIp, name: Constants.ADAPTER_NAME})
.then(() => {
logger.info('[NEEO] Stopped and unregistered device server');
process.exit();
})
.catch((error) => {
logger.warn('[NEEO] Error while stopping and unregistering device server:', error);
process.exit();
});
// force exit after 10s
setTimeout(function() {
logger.error("Could not stopping NEEO device driver in time, forcefully shutting down");
process.exit();
}, 10000);
}
// listen for TERM signal .e.g. kill
process.on ('SIGTERM', gracefulShutdown);
// listen for INT signal e.g. Ctrl-C
process.on ('SIGINT', gracefulShutdown);