-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
276 lines (252 loc) · 11.3 KB
/
app.js
File metadata and controls
276 lines (252 loc) · 11.3 KB
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
269
270
271
272
273
274
275
276
/*
Church Clocks by Kardinia Church 2020
This project aims to provide a npm interface to get information from church specific applications like ProPresenter, ProVideoPlayer, and Elvanto
app.js: Main entry point
*/
/* Constructor
- enableWebSocket: Should the websocket be enabled?
- webSocketPassword: Websocket password
- webSocketPort: The port of the websocket
*/
const { EventEmitter } = require("events");
const WebSocket = require('ws');
const http = require('http');
const fs = require("fs");
const url = require('url');
const os = require('os');
module.exports = class ChurchClocks extends EventEmitter {
constructor(enableWebSocket = false, webSocketPassword = "", webSocketPort = 9955, enableWebServer = false, webServerPort = 80, filePath = os.homedir() + "/.church-clocks/") {
super();
var object = this;
this.filePath = filePath;
this.enableWebSocket = enableWebSocket;
this.enableWebServer = enableWebServer;
this.webServerPort = webServerPort;
this.webSocketPort = webSocketPort;
if (this.enableWebSocket) {
object.emit("information", object.generateInformationEvent("application", "websocket", "Websocket enabled"));
this.connections = [];
this.wsPassword = webSocketPassword;
this.wss = new WebSocket.Server({ port: webSocketPort });
object.on("connectionStatus", function (message) {
if (object.wss.clients.size !== 0) {
object.wss.clients.forEach(function each(client) {
if (client.readyState === WebSocket.OPEN) {
client.send(JSON.stringify({
"event": "connectionStatus",
"value": message
}));
}
});
}
});
object.on("functionEvent", function (message) {
if (object.wss.clients.size !== 0) {
object.wss.clients.forEach(function each(client) {
if (client.readyState === WebSocket.OPEN) {
client.send(JSON.stringify({
"event": "functionEvent",
"value": message
}));
}
});
}
});
object.on("information", function (message) {
if (object.wss.clients.size !== 0) {
object.wss.clients.forEach(function each(client) {
if (client.readyState === WebSocket.OPEN) {
client.send(JSON.stringify({
"event": "information",
"value": message
}));
}
});
}
});
object.on("error", function (message) {
if (object.wss.clients.size !== 0) {
object.wss.clients.forEach(function each(client) {
if (client.readyState === WebSocket.OPEN) {
client.send(JSON.stringify({
"event": "error",
"value": message
}));
}
});
}
});
object.on("control", function (message) {
if (object.wss.clients.size !== 0) {
object.wss.clients.forEach(function each(client) {
if (client.readyState === WebSocket.OPEN) {
client.send(JSON.stringify({
"event": "control",
"value": message
}));
}
});
}
//Handle control messages
if(message.type == "exit") {
object.emit("information", object.generateInformationEvent("application", "Exit", "Exiting process - " + message.reason));
process.exit();
}
});
//When we get a connection
this.wss.on("connection", function connection(ws) {
object.emit("information", object.generateInformationEvent("application", "websocket", "Client " + object.wss.clients.size + " connected"));
//Send generic information
var fns = {};
for(var i in object.functions) {
fns[object.functions[i].function] = {
"enabled": object.functions[i].enabled,
"hasIncomingHandler": object.functions[i].incomingHandler !== undefined,
"hasConfigurableItems": object.functions[i].hasConfigurableItems == true
}
}
//If we get a message from this client handle it
ws.on("message", function (message) {
//If there is a password set then make sure it's correct
try {
var msg = JSON.parse(message);
var passCorrect = object.wsPassword == msg.password || object.wsPassword == "";
//If its a applications specific call handle it
if(msg.function == "application") {
switch(msg.command) {
case "login": {
ws.send(JSON.stringify({
"event": "response",
"function": msg.function,
"command": msg.command,
"value": {
correct: passCorrect,
functions: fns
}
}));
return;
}
}
}
//Send the message to the function
var response = object.sendToFunctions({
"passwordCorrect": passCorrect,
"function": msg.function,
"command": msg.command,
"value": msg.value
}, (result) => {
ws.send(JSON.stringify({
"event": "response",
"function": msg.function,
"command": msg.command,
"value": result
}));
});
}
catch (e) { object.emit("error", object.generateErrorState("application", "internal", e.toString())); }
});
});
}
//If webserver is enabled set it up
if (this.enableWebServer == true) {
object.emit("information", object.generateInformationEvent("application", "webserver", "Webserver enabled"));
http.createServer(function (request, response) {
var path = url.parse(request.url).pathname;
if (path == "/") { path = "index.html"; }
fs.readFile("./web/" + path, function (error, data) {
if (error) {
response.writeHead(404);
response.write("404: Not found");
response.end();
}
else {
if (path.includes(".css")) { response.writeHead(200, { "Content-Type": "text/css" }); }
else if (path.includes(".js")) { response.writeHead(200, { "Content-Type": "text/javascript" }); }
else { response.writeHead(200, { "Content-Type": "text/html" }); }
response.write(data);
response.end();
}
});
}).listen(webServerPort);
}
this.functions = require("./functions/functions.js").getFunctions(this.filePath + "functionSettings/");
//Setup functions
for (var i in this.functions) {
this.functions[i].setup(this);
}
}
//Generate the connection state emitter
generateConnectionState(func, state) {
return {
"function": func,
"state": state
}
}
//Generate the error state emitter
generateErrorState(func, type, error) {
return {
"function": func,
"type": type,
"error": error
}
}
//Generate a function event
generateInformationEvent(func, type, value) {
return {
"function": func,
"type": type,
"value": value
}
}
//Generate a control event
generateControlEvent(type, reason) {
return {
type: type,
reason: reason
}
}
//Send a message to the internal functions to process something
sendToFunctions(message, callback) {
var funcFound = false;
if(message.function == "application") {
switch(message.command) {
case "restart": {
if(message.passwordCorrect == true) {
this.emit("control", this.generateControlEvent("exit", "Restarting process on settings change"));
}
else {
this.emit("error", this.generateErrorState("application", "authenticationError", "Incorrect password"));
}
break;
}
}
}
else {
for (var i in this.functions) {
if(this.functions[i].function == message.function) {
try {
var result = this.functions[i].handleIncoming(message, callback);
if (result !== false) { funcFound = true; break; }
}
catch (e) { }
}
}
if (funcFound == false) {
this.emit("error", this.generateErrorState("application", "functionRequest", "Failed to find requested function"));
}
}
}
//Attempt connection to all the modules
connect() {
var success = true;
this.emit("connectionStatus", this.generateConnectionState("application", "Attempting Connection"));
this.emit("information", this.generateInformationEvent("application", "configuration", "Web Socket Enabled: " + this.enableWebSocket));
this.emit("information", this.generateInformationEvent("application", "configuration", "Web Server Port: " + this.webServerPort));
this.emit("information", this.generateInformationEvent("application", "configuration", "Web Server Enabled: " + this.enableWebServer));
this.emit("information", this.generateInformationEvent("application", "configuration", "Web Socket Port: " + this.webSocketPort));
//Connect functions
for (var i in this.functions) {
this.functions[i].connect();
}
}
}