-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathOS2LServer.js
186 lines (156 loc) · 4.11 KB
/
OS2LServer.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
const { EventEmitter } = require("events");
const bonjour = require("bonjour")();
const net = require("net");
const DataHandler = require("./DataHandler.js");
/**
* The OS2LServer handles incoming commands. Usally this is part of a DMX Software.
*/
class OS2LServer extends EventEmitter {
/**
* Creates an instance of an OS2LServer
* @param {Object} options Options object
* @param {Number} options.port Port to listen on
* @param {Boolean} [options.doPublish] Should the server publish itself to DNS-SD
*/
constructor(options = {port: 1503}) {
super();
this.dataHandler = new DataHandler();
this.dataHandler.on("data", object => {
this.emit("data", object);
this.emit(object.evt, object);
if (object.evt == "btn") {
if (object.state == "on") {
this.emit("btnOn", object.name);
} else {
this.emit("btnOff", object.name);
}
}
});
this.dataHandler.on("error", err => {
this.emit("error", err)
});
// Option parameters
this.port = 1503;
this.doPublish = true;
if (typeof options != "object") throw new Error("Expected an object for options!");
if ("port" in options) {
this.port = Number(options.port);
}
if ("doPublish" in options) {
this.doPublish = Boolean(options.doPublish);
}
// Other attributes
this.net = null;
this.clients = [];
// Publish
if (this.doPublish) {
this.service = bonjour.publish({
name: "os2l",
type: "os2l",
port: this.port
});
}
this.on("error", () => {
this.stop();
});
}
/**
* Starts listening
* @param {Function} [callback] Is called when the server started listening.
*/
start(callback = null) {
return new Promise((resolve, reject) => {
if (this.net) {
let err = new Error("OS2LServer is already running!");
this.emit("warning", err);
reject(err);
return;
}
this.net = net.createServer((client) => {
this.clients.push(client);
this.emit("connection");
client.on("error", err => {
client.destroy();
let index = this.clients.indexOf(client);
if (index >= 0) {
this.clients.splice(index, 1);
}
this.emit("closed");
});
client.on("data", data => {
this.dataHandler.handle(data.toString('utf8'));
});
client.on("end", () => {
let index = this.clients.indexOf(client);
if (index >= 0) {
this.clients.splice(index, 1);
}
this.emit("closed");
});
});
this.net.on("error", err => {
this.emit("error", err);
reject();
});
this.net.listen(this.port, () => {
if (callback) {
callback();
}
resolve();
});
});
}
/**
* Stops the server
*/
stop() {
if (!this.net) {
this.emit("warning", new Error("OS2LSever can't close because it is not running."));
return;
}
this.net.close();
this.net.unref();
this.net = null;
for (const client of this.clients){
client.destroy()
}
if (this.service) {
this.service.stop();
this.service = null;
}
this.emit("closed");
}
/**
* Adds an event listener
* @param {OS2LServerEvents} args Type of the event
*/
addListener(...args) {
super.addListener(...args);
}
/**
* Adds an event listener
* @param {OS2LServerEvents} args Type of the event
*/
on(...args) {
super.on(...args);
}
/**
* Sends feedback to all clients
* @param {String} name Name of the button
* @param {Boolean} state State of the button
* @param {String} [page] Name of the page
*/
feedback(name, state, page = undefined) {
let json = JSON.stringify({
evt: "feedback",
name, state, page
});
for (let client of this.clients) {
client.write(json);
}
}
}
module.exports = OS2LServer;
/**
* @typedef {["error" | "warning" | "data" | "btn" | "cmd" | "btnOn" | "btnOf" | "beat" | "connection" | "closed"]} OS2LServerEvents
*/