This repository has been archived by the owner on Oct 31, 2024. It is now read-only.
forked from logantgt/harmony-span
-
Notifications
You must be signed in to change notification settings - Fork 0
/
harmony-span.js
257 lines (226 loc) · 8.19 KB
/
harmony-span.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
/// Import Modules ///
const WebHooks = require("node-webhooks");
const fs = require("fs");
const Express = require("express");
const ssdp = require("./ssdp");
const colorout = require("./coreoutput");
const request = require("request");
const mqtt = require("mqtt");
const yaml = require("node-yaml");
const ip = require("ip");
const CONFIG_FILE = "config.yaml";
const DEFAULT_HOST = "127.0.0.1";
const DEFAULT_PORT = 8060;
let conf;
let port;
let host;
let app;
let server;
let webHooks;
let mqttClient;
// Array of client's IP-addresses. Clients are Logitech Harmony Hubs
let clients = [];
function init() {
conf = yaml.readSync(CONFIG_FILE);
if (conf.webserverConfig.hasOwnProperty("bindHost") && conf.webserverConfig.bindHost != "") {
host = conf.webserverConfig.bindHost;
if (host == "0.0.0.0") {
colorout.log("debug", "[Webserver] Binding to all available local IPs; one is " + ip.address());
} else {
colorout.log("debug", "[Webserver] Found webserver-host in config-file. Using " + host);
}
} else {
host = DEFAULT_HOST;
colorout.log("debug", "[Webserver]: Found no webserver-host in config-file. Falling back to " + host);
}
if (conf.webserverConfig.hasOwnProperty("port") && conf.webserverConfig.port != "") {
port = conf.webserverConfig.port;
colorout.log("debug", "[Webserver] Found port in config-file. Using " + port);
} else {
port = DEFAULT_PORT;
colorout.log("debug", "[Webserver] Found no port in config-file. Falling back to " + port);
}
app = Express();
webHooks = new WebHooks({ db: {} });
request.shouldKeepAlive = false;
attachWebhooks();
configureWebserverRoutes();
if (host == "0.0.0.0") {
ssdp.run("http://" + ip.address() + ":" + port + "/");
} else {
ssdp.run("http://" + host + ":" + port + "/");
}
process.on("SIGINT", () => {
colorout.log("info", "[Webserver] Shutting down");
server.close();
process.exit();
});
if (host == "0.0.0.0") {
// bind to all IPs.
server = app.listen(port);
} else {
server = app.listen(port, host);
}
let mqttConfig = conf.mqttConfig;
if (mqttConfig.hasOwnProperty("serverUrl") && mqttConfig.hasOwnProperty("serverUsername") && mqttConfig.hasOwnProperty("serverPassword") && mqttConfig.enabled == true) {
connectMqttServer();
}
if (host == "0.0.0.0") {
colorout.log("info", "[Webserver] Configuration Menu available at http://" + ip.address() + ":" + port + "/config/");
} else {
colorout.log("info", "[Webserver] Configuration Menu available at http://" + host + ":" + port + "/config/");
}
}
function attachWebhooks() {
conf.buttons.forEach(function(button) {
webHooks.remove(button.name);
if (button.action == "POST") webHooks.add(button.name, button.url);
});
}
function connectMqttServer() {
// TODO check if client is connected
mqttClient = mqtt.connect(conf.mqttConfig.serverUrl, {
username: conf.mqttConfig.serverUsername,
password: conf.mqttConfig.serverPassword,
reconnectPeriod: 0,
connectTimeout: 5 * 1000
});
mqttClient.on("connect", function() {
colorout.log("debug", "[MQTT-Connection] connected to MQTT-server");
})
mqttClient.on("error", function() {
colorout.log("error", "[MQTT-Connection] could not connect to MQTT-server");
});
}
function configureWebserverRoutes() {
// server static content from public directory (http://.../config/*)
app.use(Express.static('public'));
// automatically interpret incoming post messages as JSON if Content-Type=application/json
app.use(Express.json());
// log all requests to debug
app.use((req, res, next) => {
colorout.log("debug", "[Webserver] " + req.method + " " + req.url + " from " + req.ip);
next();
});
/// Send RootResponse.xml ///
app.get('/', (req, res) => {
if (!clients.includes(req.ip)) {
clients.push(req.ip);
colorout.log("info", "[Webserver] Logitech Hub at " + req.ip + " found me! Sending RootResponse.xml...");
}
res.type('application/xml');
res.send(fs.readFileSync('RootResponse.xml', 'utf8'));
res.end();
});
/// Button Event Handler ///
app.post('/keypress/:action', function(req, res) {
triggerAction(req.params['action']);
res.end();
});
///
/// HarmonySpan Configuration API
///
// get all buttons config
app.get('/buttons/', function(req, res) {
if (!req.accepts('json')) {
res.sendStatus(415);
} else {
res.set('Content-Type', 'application/json');
res.send(JSON.stringify(conf.buttons));
}
});
// get specific button config
app.get('/buttons/:id', function(req, res) {
if (!req.accepts('json')) {
res.sendStatus(415);
} else {
let id = req.params['id'];
if (!id.match(/[0-9]{1,2}/) || parseInt(id) < 0 || parseInt(id) > conf.buttons.length - 1) {
res.send(404);
} else {
res.set('Content-Type', 'application/json');
res.send(conf.buttons[id]);
}
}
});
// set specific button's config
app.put('/buttons/:id', function(req, res) {
// TODO validate
let data = req.body;
conf.buttons[data.id] = data;
res.sendStatus(204);
yaml.writeSync(CONFIG_FILE, conf);
attachWebhooks();
colorout.log("debug", "[Core] Updated config file.");
});
// get MQTT server config
app.get('/mqttconfig', function(req, res) {
res.contentType('application/json');
res.send(JSON.stringify(conf.mqttConfig));
});
// set MQTT server config
app.post('/mqttconfig', function(req, res) {
let data = req.body;
let enabledBefore = conf.mqttConfig.enabled;
let enabledAfter = data.enabled;
if (enabledBefore != enabledAfter) {
}
conf.mqttConfig = data;
yaml.writeSync(CONFIG_FILE, conf);
colorout.log("debug", "[Core] Updated config file.");
if (enabledBefore != enabledAfter) {
if (enabledAfter) {
connectMqttServer();
} else {
mqttClient.end();
}
}
res.sendStatus(204);
});
app.get('/mqttconnected', function(req, res) {
res.set("Content-Type", "application/json");
res.send("{\"connected\": " + mqttClient.connected + " }");
});
}
function triggerAction(buttonFunction) {
let buttonIndex = getButtonIndex(buttonFunction);
let button = conf.buttons[buttonIndex];
if (button.enabled) {
switch (button.action) {
case "GET":
request(button.url, function(error, response, body) {
if (error != null) {
colorout.log("error", "[Webserver] HTTP GET: error: " + error);
} else {
colorout.log("debug", "[Webserver] HTTP GET: " + response.statusCode);
}
});
break;
case "POST":
webHooks.trigger(button.name, JSON.parse(button.postPayload), button.httpHeaders);
// console.log(JSON.parse(button.postPayload));
break;
case "MQTT":
if (mqttClient && mqttClient.connected) {
mqttClient.publish(button.mqttTopic, button.mqttMessage);
colorout.log("debug", "[MQTT-Connection] Sent mqtt message");
} else {
// TODO try reconnect
colorout.log("error", "[MQTT-Connection] MQTT not connected");
}
break;
default:
colorout.log("error", "[Core] Unknown action: " + button.action);
}
} else {
colorout.log("debug", "[Core] Button disabled. Won't fire action");
}
}
function getButtonIndex(btnFunction) {
let i;
for (i = 0; i < conf.buttons.length; i++) {
if (conf.buttons[i].name == btnFunction) return i;
}
return -1;
}
init();