-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlcdprocjs.js
496 lines (460 loc) · 12.2 KB
/
lcdprocjs.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
"use strict";
var Socket = require("net").Socket;
var events = require("events");
var util = require("util");
var _ = require("lodash");
var debug = false; // pfft log...
//##############################################################################
//##############################################################################
/**
* LCDproc Client object.
*
* Default config:
* ```
* {
* host: "localhost",
* port: 13666,
* name: "lcdprocjs"
* }
* ```
* @param {Object} config client configuration
* @event ready signal a ready client
*/
function Client(config) {
this.config = {
host: "localhost",
port: 13666,
name: "lcdprocjs"
};
_.assign(this.config, config);
this.lcdprocConfig = {
version: 0,
protocol: 0,
size: {
width: 0,
height: 0
},
cellsize: {
width: 0,
height: 0
}
};
this.screens = {};
this.screenCnt = 0;
this.socket = new Socket();
events.EventEmitter.call(this);
}
util.inherits(Client, events.EventEmitter);
module.exports.Client = Client;
/**
* Connect, handshaking, messages processing
* @fires ready client ready
* @fires shown Screen shown by server
* @fires hidden Screen hidden by server
*/
Client.prototype.connect = function() {
var self = this;
// Socket listeners
this.socket.on("connect", function() {
self._write("hello");
});
this.socket.on("data", function(buf) {
var data = buf.toString().trim();
// On CONNECT only
if(data.indexOf("connect") == 0) {
var params = data.split(" ");
params.forEach(function(value, i) {
switch(value) {
case "LCDproc": self.lcdprocConfig.version = params[i+1]; break;
case "protocol": self.lcdprocConfig.protocol = params[i+1]; break;
case "wid": self.lcdprocConfig.size.width = +params[i+1]; break;
case "hgt": self.lcdprocConfig.size.height = +params[i+1]; break;
case "cellwid": self.lcdprocConfig.cellsize.width = +params[i+1]; break;
case "cellhgt": self.lcdprocConfig.cellsize.height = +params[i+1]; break;
}
});
self._write("client_set", "-name", quoteString(self.config.name));
self.emit("ready");
} else {
// All other messages
// (data can be a string with multiple message from LCDproc)
data.split("\n").forEach(function(msg) {
if(msg == "success") return;
// debug
log("RECV", msg);
var m = msg.substring(0, 6);
if(m == "listen") {
// screen visible
if(!_.isUndefined(self.screens[msg.substring(7)])) {
self.screens[msg.substring(7)].emit("shown");
}
} else if(m == "ignore") {
// screen hidden
if(!_.isUndefined(self.screens[msg.substring(7)])) {
self.screens[msg.substring(7)].emit("hidden");
}
}
});
}
});
this.socket.on("error", function(error) {
log("ERROR", error);
self.socket.destroy();
});
// this.socket.on("end", function() {
// log("END");
// });
// this.socket.on("close", function(had_error) {
// log("CLOSE", had_error);
// });
// Socket connect
this.socket.setEncoding("utf8");
this.socket.connect(this.config.port, this.config.host);
}
/**
* Disconnect
*/
Client.prototype.close = function() {
this.socket.end();
}
/**
* A factory method which adds a Screen to this Client.
*
* See [LCDproc Developer's Guide](http://lcdproc.sourceforge.net/docs/lcdproc-0-5-7-dev.html#language-screens)
*
* @param {Object} config screen configuration
* @return {Screen}
*/
Client.prototype.addScreen = function(config) {
var id = this._newScreenId();
this.screens[id] = new Screen(this, id, config);
return this.screens[id];
}
/*
* [Private method]
* Write arguments to socket (arg can be an array or a string).
*/
Client.prototype._write = function() {
var out = [];
for (var i = 0; i < arguments.length; i++) {
if(_.isArray(arguments[i])) {
out = out.concat(arguments[i]);
} else {
out.push(arguments[i]);
}
}
var o = out.join(" ");
log("SEND", o);
this.socket.write(o + "\n");
}
/*
* [Private method]
* Delete a screen from the internal map
*/
Client.prototype._unrefScreen = function(id) {
delete this.screens[id];
}
/*
* [Private method]
* Return an id for a new screen
*/
Client.prototype._newScreenId = function() {
return this.config.name + "_s" + this.screenCnt++;
}
//##############################################################################
/**
* A Screen.
*
* Example config:
* ```
* {
* priority: "info", // hidden|background|info|foreground|alert|input|$int
* heartbeat: "open", // { on | off | open }
* backlight: "open", // { on | off | toggle | open | blink | flash }
* [...]
* }
* ```
*
* @param {Client} client Client instance
* @param {Screen} screenId id
* @param {Object} config initial configuration
* @event
*/
function Screen(client, screenId, config) {
this.client = client;
this.screenId = screenId;
this.config = {};
this.widgets = {};
this.widgetCnt = 0;
this.client._write("screen_add", this.screenId);
this.setConfig(config);
events.EventEmitter.call(this);
}
util.inherits(Screen, events.EventEmitter);
/**
* Set a new config for the screen
* @param {Object} config new configuration (adds or overwrite)
*/
Screen.prototype.setConfig = function(config) {
if(_.isEmpty(config)) return;
_.assign(this.config, config);
this.client._write("screen_set", this.screenId, flattenObj(config));
}
/**
* Delete this Screen
*/
Screen.prototype.delete = function() {
this.client._write("screen_del", this.screenId);
this.client._unrefScreen(this.screenId);
}
/**
* Adds a generic Widget to this screen.
*
* @param {String} type widget type
* @return {Widget}
*/
Screen.prototype.addWidget = function(type) {
var id = this._newWidgetId();
this.widgets[id] = new Widget(this, id, type);
return this.widgets[id];
}
/**
* Adds a TitleWidget
* @return {TitleWidget}
*/
Screen.prototype.addTitle = function() {
var id = this.screenId + "_wTITLE";
if(_.isUndefined(this.widgets[id])) {
this.widgets[id] = new TitleWidget(this, id);
return this.widgets[id];
}
return this.widgets[id];
}
/**
* Adds a StringWidget
* @return {StringWidget}
*/
Screen.prototype.addString = function() {
var id = this._newWidgetId();
this.widgets[id] = new StringWidget(this, id);
return this.widgets[id];
}
/**
* Adds a HorizontalBarWidget
* @return {HorizontalBarWidget}
*/
Screen.prototype.addHorizontalBar = function() {
var id = this._newWidgetId();
this.widgets[id] = new HorizontalBarWidget(this, id);
return this.widgets[id];
}
/**
* Adds a VerticalBarWidget
* @return {VerticalBarWidget}
*/
Screen.prototype.addVerticalBar = function() {
var id = this._newWidgetId();
this.widgets[id] = new VerticalBarWidget(this, id);
return this.widgets[id];
}
/**
* Adds a BigNumberWidget
* @return {BigNumberWidget}
*/
Screen.prototype.addBigNumber = function() {
var id = this._newWidgetId();
this.widgets[id] = new BigNumberWidget(this, id);
return this.widgets[id];
}
/*
* [Private method]
* Returns an id for a new widget
*/
Screen.prototype._newWidgetId = function() {
return this.screenId + "_w" + this.widgetCnt++;
}
/*
* [Private method]
* Delete a widget from the internal map
*/
Screen.prototype._unrefWidget = function(id) {
delete this.widgets[id];
}
//##############################################################################
/**
* A generic Widget
* @param {Screen} screen Screen instance
* @param {string} widgetId id
* @param {string} widgetType widget type
*/
function Widget(screen, widgetId, widgetType) {
this.screen = screen;
this.widgetId = widgetId;
this.widgetType = widgetType;
this.screen.client._write("widget_add", this.screen.screenId, widgetId, widgetType);
}
/**
* Set widget params
*/
Widget.prototype.setParams = function() {
this.params = Array.apply(null, arguments);
this.screen.client._write("widget_set", this.screen.screenId, this.widgetId, this.params);
}
/**
* Delete this widget from screen
*/
Widget.prototype.delete = function() {
this.screen.client._write("widget_del", this.screen.screenId, this.widgetId);
this.screen._unrefWidget(this.widgetId);
}
//##############################################################################
/*
* Specialized Widgets
*/
/**
* Title widget
*/
function TitleWidget(screen, widgetId) {
Widget.call(this, screen, widgetId, "title");
}
util.inherits(TitleWidget, Widget);
/**
* Set title text
* @param {String} text text
*/
TitleWidget.prototype.setTitle = function(text) {
this.setParams(quoteString(text));
}
/**
* String widget
*/
function StringWidget(screen, widgetId) {
Widget.call(this, screen, widgetId, "string");
}
util.inherits(StringWidget, Widget);
/**
* Set widget position and text
* @param {Integer} x x position
* @param {Integer} y y position
* @param {String} text text
*/
StringWidget.prototype.set = function(x, y, text) {
this.setParams(x, y, quoteString(text));
}
/**
* Set widget position
* @param {Integer} x x position
* @param {Integer} y y position
*/
StringWidget.prototype.setPos = function(x, y) {
if(_.isUndefined(this.params)) {
this.setParams(x, y, 0);
} else {
this.setParams(x, y, this.params[2]);
}
}
/**
* Set widget text
* @param {String} text text
*/
StringWidget.prototype.setText = function(text) {
if(_.isUndefined(this.params)) {
this.set(1, 1, text); // fallback
} else {
this.set(this.params[0], this.params[1], text);
}
}
/**
* Horizontal bar widget
*/
function HorizontalBarWidget(screen, widgetId) {
Widget.call(this, screen, widgetId, "hbar");
}
util.inherits(HorizontalBarWidget, Widget);
/**
* Set widget position and value
* @param {Integer} x x position
* @param {Integer} y y position
* @param {Float} percent number between 0 and 1
*/
HorizontalBarWidget.prototype.set = function(x, y, percent) {
this.setParams(x, y,
Math.round((this.screen.client.lcdprocConfig.size.width - x + 1) *
this.screen.client.lcdprocConfig.cellsize.width * percent));
}
/**
* Set widget position
* @param {Integer} x x position
* @param {Integer} y y position
*/
HorizontalBarWidget.prototype.setPos = StringWidget.prototype.setPos;
/**
* Set widget value
* @param {Float} percent number between 0 and 1
*/
HorizontalBarWidget.prototype.setValue = function(percent) {
if(_.isUndefined(this.params)) {
this.set(1, 1, percent); // fallback
} else {
this.set(this.params[0], this.params[1], percent);
}
}
/**
* Vertical bar widget
*/
function VerticalBarWidget(screen, widgetId) {
Widget.call(this, screen, widgetId, "vbar");
}
util.inherits(VerticalBarWidget, Widget);
/**
* Set widget position and value
* @param {Integer} x x position
* @param {Integer} y y position
* @param {Float} percent number between 0 and 1
*/
VerticalBarWidget.prototype.set = function(x, y, percent) {
this.setParams(x, y,
Math.round(y * this.screen.client.lcdprocConfig.cellsize.height * percent));
}
/**
* Set widget position
* @param {Integer} x x position
* @param {Integer} y y position
*/
VerticalBarWidget.prototype.setPos = StringWidget.prototype.setPos;
/**
* Set widget value
* @param {Float} percent number between 0 and 1
*/
VerticalBarWidget.prototype.setValue = HorizontalBarWidget.prototype.setValue;
/**
* BigNumber widget
*/
function BigNumberWidget(screen, widgetId) {
Widget.call(this, screen, widgetId, "num");
}
util.inherits(BigNumberWidget, Widget);
/**
* Set position and value
* @param {Integer} x x position
* @param {Integer} number number between 1 an 10 (the special number 10 is a colon)
*/
BigNumberWidget.prototype.set = function(x, number) {
this.setParams(x, number);
}
//##############################################################################
//##############################################################################
function quoteString(str) {
return "{" + str + "}";
}
function flattenObj(obj, donotdashkeys) {
var out = [];
_.forOwn(obj, function(value, key) {
out.push((donotdashkeys?"":"-") + key);
out.push(value);
});
return out;
}
function log() {
if(debug) console.log.apply(null, arguments);
}