-
Notifications
You must be signed in to change notification settings - Fork 1
/
logger.js
214 lines (170 loc) · 6.46 KB
/
logger.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
const WebSocket = require('ws');
const fs = require('fs');
const path_util = require('path');
const sprintf = require('sprintf-js').sprintf;
const assert = require('assert');
const debug = require('./writers/debug.js');
var config = require('./config.json');
if(config_checks()) {
global.config = config;
global.debug = config.app.debug;
var subscriptions = config.hub.events.subscriptions;
}
else {
process.exit(1);
}
var hub = config.hub;
var events = hub.events;
var logs = hub.logs;
global.hub = config.hub;
global.events = hub.events;
global.logs = hub.logs;
var webserver = config.app.webserver;
if(webserver.enable) {
const express = require('express');
const server = express();
const bodyParser = require('body-parser')
server.use(bodyParser.urlencoded({ extended: false }));
server.post('/select_devices', function(req, res) {
var html = '<html><title>Selected Devices</title><body><div>You selected the following devices:</div>';
fs.writeFile("config.json.bak", JSON.stringify(config, null, 2), function (err) {
if (err)
throw err;
});
subscriptions = [];
for(var device in req.body) {
subscriptions.push(parseInt(req.body[device]));
html += '<div>' + device + '</div>';
}
delete require.cache[require.resolve('./config.json')];
fs.writeFile("config.json", JSON.stringify(config, null, 2), function (err) { });
html += '<div><br/><br/>Your configuration has been saved and your previous configuration has been saved to config.json.bak.</body></html>';
res.send(html);
});
server.get('/get_devices', function (req, res) {
const request = require('request');
request(webserver.maker_api_url, { json: true }, (err, resp, body) => {
if (err) { return console.log(err); }
var html = '<html><title>Select Devices</title><style type="text/css">font-family:verdana; font-size: 10pt;</style><body><h4>Please select your devices below:</h4><br/><form method="POST" action="select_devices">';
for(var d in body) {
var device = body[d];
var index = subscriptions.indexOf(parseInt(device.id));
html += "<input type=\"checkbox\" id=\"" + device.label + "\" name=\"" + device.label + "\" value=\"" + device.id + "\"" + (index !== -1 ? " checked " : "") + ">" + device.label + "<br/>";
}
html += '<input type="submit" /></form></body></html>';
res.send(html);
});
});
server.listen(webserver.port);
console.log('Listening on port ' + webserver.port);
}
//Setup the listener for the events websocket.
if(events.enabled) {
var con = sprintf('ws://%s/%s', hub.hub_host, events.socket_name);
var ws = getSocket(con);
ws.on('open', function(err) {
if (err) throw err;
if(global.debug) console.log("Waiting on events");
});
ws.on('message', function incoming(data) {
var out_data = getEventData(JSON.parse(data));
var writers = events.writers;
if(subscriptions !== null && Array.isArray(subscriptions) && subscriptions.length > 0) {
inSubs = subscriptions.indexOf(out_data.deviceId) !== -1;
}
else { inSubs = true; }
if(inSubs) {
for(var w in writers) {
var writer = writers[w];
if(writer.enable) {
var o = require(writer.module);
o.write(out_data, writer);
}
}
}
});
}
if(logs.enabled) {
var con = sprintf('ws://%s/%s', hub.hub_host, logs.socket_name);
var ws = getSocket(con);
ws.on('message', function incoming(data) {
var out_data = getLogData(JSON.parse(data));
if(global.debug) console.log("Waiting on events");
var writers = logs.writers;
for(var w in writers) {
var writer = writers[w];
if(writer.enable) {
var o = require(writer.module);
o.write(out_data, writer);
}
}
});
}
function getSocket(connection) {
return new WebSocket(connection);
}
function getEventData(data) {
var ts = Date.now();
var hsts = new Date().toString();
var return_data = {
source: data.source,
name: data.name,
displayName: data.displayName,
value: data.value,
unit: data.unit,
deviceId: data.deviceId,
hubId: data.hubId,
locationId: data.locationId,
installedAppId: data.installedAppId,
descriptionText: data.descriptionText,
timestamp: ts,
date: hsts
};
return return_data;
}
function getLogData(data) {
var return_data = {
name: data.name,
msg: data.msg,
id: data.id,
time: data.time,
type: data.type,
level: data.level
};
return return_data;
}
// process.on('uncaughtException', (err) => {
// console.error('There was an uncaught error', err);
// process.exit(1);
// });
function config_checks() {
var host = config.hub;
var msg = [];
console.log("Checking configuration");
if(!host.events.enabled && !host.logs.enabled) {
msg.push("Events or Logs must be enabled (host: { events: { enabled: true|false } } { logs: { enabled: true|false } })");
}
if(host.events.enabled) {
if(host.events.socket_name === undefined || host.events.socket_name == '') {
msg.push("Socket name is required for events. Please set the socket name to 'eventsocket' (or whatever the current name is).");
}
if(host.events.writers === undefined || host.events.writers.length == 0) {
msg.push("No event writers have been defined.");
}
}
if(host.logs.enabled) {
if(host.logs.socket_name === undefined || host.logs.socket_name == '') {
msg.push("Socket name is required for logs. Please set the socket name to 'logsocket' (or whatever the current name is).");
}
if(host.logs.writers === undefined || host.logs.writers == 0) {
msg.push("No log writers have been defined.");
}
}
if(msg.length > 0) {
msg.forEach(function(m) {
console.log(m);
});
return false;
}
else { console.log("Configuration check passed"); return true; }
}