-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathOS2LClient.js
232 lines (199 loc) · 5.53 KB
/
OS2LClient.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
const { EventEmitter } = require("events");
const bonjour = require('bonjour')();
const net = require("net");
const DataHandler = require("./DataHandler.js");
/**
* Provides a TCP client that is able to talk in OS2L language
*/
class OS2LClient extends EventEmitter {
/**
* Creates an OS2LClient instance
* @param {Object} [options] Options object
* @param {Number} [options.port] Port to connect to
* @param {String} [options.host] Host to connect to
* @param {Boolean} [options.useDNS_SD] Use DNS-SD to detect port and host automatically
* @param {Boolean} [options.autoReconnect] Do reconnect automatically after connection lost?
* @param {Boolean} [options.autoReconnectInterval] Interval to try reconnects in milliseconds
*/
constructor(options = {}) {
super();
this.port = 1504;
this.host = "local";
this.useDNS_SD = true;
this.autoReconnect = true;
this.autoReconnectInterval = 1000;
this.dataHandler = new DataHandler();
this.dataHandler.on("data", object => {
if (object.evt == "feedback") {
this.emit("feedback", object.name || "", object.state || "off", object.page);
}
});
this.dataHandler.on("error", err => {
this.emit("error", err)
});
if (typeof options != "object") throw new Error("Expected an object for options!");
if ("port" in options) {
this.port = Number(options.port);
}
if ("host" in options) {
this.host = String(options.host);
}
if ("useDNS_SD" in options) {
this.useDNS_SD = Boolean(options.useDNS_SD);
}
if ("autoReconnect" in options) {
this.autoReconnect = Boolean(options.autoReconnect);
}
if ("autoReconnectInterval" in options) {
this.autoReconnectInterval = Number(options.autoReconnectInterval);
}
// The TCP client
this.client = null;
if (this.autoReconnect) {
this.on("closed", async () => {
this.emit("warning", "OS2L Client had error. Trying to reconnect...");
await new Promise(resolve => setTimeout(resolve, this.autoReconnectInterval))
this.connect();
});
}
}
/**
* Connects the client
* @param {Function} callback Is called when a connection was made
* @return {Promise<void>}
*/
connect(callback) {
return new Promise(async (resolve, reject) => {
const cb = () => {
if (callback) callback();
this.emit("connected");
resolve();
};
if (this.client) {
this.emit("warning", new Error("OS2L Client is already connected!"));
return;
}
this.client = new net.Socket();
this.client.on("error", async err => {
this.close();
if (!this.autoReconnect) {
this.emit("error", err);
reject()
}
});
this.client.on("data", data => {
this.dataHandler.handle(data.toString('utf8'));
});
this.client.on("close", () => {
if (this.client) {
this.close();
}
});
if (this.useDNS_SD) {
this._dnsSdFind((host, port) => {
this.client.connect(port, host, cb);
});
} else {
this.client.connect(this.port, this.host, cb);
}
});
}
_dnsSdFind(cb) {
bonjour.findOne({type: "os2l"}, function (service) {
cb(service.host, service.port);
});
}
_sendObject(object) {
if (!this.client) return;
let json;
if (typeof object == "string") {
json = object;
} else {
json = JSON.stringify(object);
}
this.client.write(json);
}
/**
* Closes the connection
*/
close() {
if (!this.client) {
this.emit("error", new Error("Can't close OS2LClient because it is not open!"));
return;
}
this.client.destroy();
this.client.unref();
this.client = null;
this.emit("closed");
}
/**
* Sends a "btn" even with state "on"
* @param {String} name Name of the button
*/
buttonOn(name) {
if (!this.client) this.emit("warning", new Error("OS2L Client not connected"));
this._sendObject({
evt: "btn",
name: name,
state: "on"
});
}
/**
* Sends a "btn" event with status "off"
* @param {} name Name of the button
*/
buttonOff(name) {
if (!this.client) this.emit("warning", new Error("OS2L Client not connected"));
this._sendObject({
evt: "btn",
name: name,
state: "off"
});
}
/**
* Sends a "cmd" event
* @param {Number} id Id of the event
* @param {Number} param Parameter between 0 and 1
*/
command(id, param) {
if (!this.client) this.emit("warning", new Error("OS2L Client not connected"));
this._sendObject({
evt: "cmd",
id: id,
param: param
});
}
/**
* Sends a "beat" event
* @param {Boolean} change
* @param {Number} pos Position of the beat
* @param {Number} bpm Beats per minute
*/
beat(change, pos, bpm) {
if (!this.client) this.emit("warning", new Error("OS2L Client not connected"));
this._sendObject({
evt: "beat",
change: change,
pos: pos,
bpm: bpm
});
}
/**
* Sends a custom object
* @param {Object} object Object to be stringified to json
*/
custom(object) {
if (!this.client) this.emit("warning", new Error("OS2L Client not connected"));
this._sendObject(object);
}
/**
* Is true when the client is currently connected to a server
*/
get isConnected() {
return (this.client != null);
}
}
module.exports = OS2LClient;
/**
* @typedef {["error" | "connected" | "closed" | "feedback"]} OS2LClientEvents
*/