forked from saxn-paule/pimatic-pio-remote
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpio-remote.js
463 lines (385 loc) · 13.3 KB
/
pio-remote.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
var __bind = function(fn, me){
return function(){
return fn.apply(me, arguments);
};
},
__hasProp = {}.hasOwnProperty,
__extends = function(child, parent) {
for (var key in parent) {
if (__hasProp.call(parent, key)) child[key] = parent[key];
}
function ctor() {
this.constructor = child;
}
ctor.prototype = parent.prototype;
child.prototype = new ctor();
child.__super__ = parent.prototype;
return child;
};
module.exports = function(env) {
var M, PioRemotePlugin, PioRemoteActionProvider, PioRemoteActionHandler, pioRemotePlugin, pioRemoteActionHandler, client, pluginConfig, connected;
M = env.matcher;
var Promise = env.require('bluebird');
var net = env.require('net');
var util = env.require('util');
var assert = env.require('cassert');
/**
* Default connection parameters
**/
var defaultHost = '192.168.0.15';
var defaultPort= 23;
var defaultMaxVolume = 98;
var defaultBrand = 'pioneer';
var currentVolume = 0;
var currentDisplay = '';
var hmgDisplay = '';
var currentInput = '';
/**
* Additional codes (control iPod etc.) for pioneer or denon receivers could be found here:
* http://www.pioneerelectronics.com/StaticFiles/PUSA/Files/Home%20Custom%20Install/VSX-1120-K-RS232.PDF
* and here
* http://openrb.com/wp-content/uploads/2012/02/AVR3312CI_AVR3312_PROTOCOL_V7.6.0.pdf
**/
var controlCodes = require("./commands.json");
/**
* THE PLUGIN ITSELF
**/
PioRemotePlugin = (function(_super) {
__extends(PioRemotePlugin, _super);
function PioRemotePlugin() {
this.init = __bind(this.init, this);
return PioRemotePlugin.__super__.constructor.apply(this, arguments);
}
PioRemotePlugin.prototype.init = function(app, framework, config) {
var deviceConfigDef;
this.framework = framework;
this.config = config;
pluginConfig = config;
this.framework.ruleManager.addActionProvider(new PioRemoteActionProvider(this.framework));
deviceConfigDef = require("./device-config-schema");
this.framework.deviceManager.registerDeviceClass("AVRSensor", {
configDef: deviceConfigDef.AVRSensor,
createCallback: (function(_this) {
return function(config) {
return new AVRSensor(config, framework);
};
})(this)
});
};
return PioRemotePlugin;
})(env.plugins.Plugin);
pioRemotePlugin = new PioRemotePlugin;
/**
* THE ACTION PROVIDER
**/
PioRemoteActionProvider = (function(_super) {
__extends(PioRemoteActionProvider, _super);
function PioRemoteActionProvider(framework, config) {
this.framework = framework;
this.config = config;
this.connectAction = __bind(this.connectAction, this);
}
/**
*This function handles action in the form of `sendAvr "some string"`
**/
PioRemoteActionProvider.prototype.parseAction = function(input, context) {
var commandTokens, fullMatch, m, match, onEnd, retVal, setCommand;
retVal = null;
commandTokens = null;
fullMatch = false;
setCommand = (function(_this) {
return function(m, tokens) {
return commandTokens = tokens;
};
})(this);
onEnd = (function(_this) {
return function() {
return fullMatch = true;
};
})(this);
m = M(input, context).match("sendAvr ").matchStringWithVars(setCommand);
if (m.hadMatch()) {
match = m.getFullMatch();
return {
token: match,
nextInput: input.substring(match.length),
actionHandler: new PioRemoteActionHandler(this.framework, commandTokens)
};
} else {
return null;
}
};
return PioRemoteActionProvider;
})(env.actions.ActionProvider);
/**
* THE ACTION HANDLER
**/
PioRemoteActionHandler = (function(_super) {
__extends(PioRemoteActionHandler, _super);
function PioRemoteActionHandler(framework, commandTokens) {
this.framework = framework;
this.commandTokens = commandTokens;
this.executeAction = __bind(this.executeAction, this);
this.connect = __bind(this.connect, this);
this.disconnect = __bind(this.disconnect, this);
this.sendCommand = __bind(this.sendCommand, this);
}
/**
* Method for establishing connection to the receiver
**/
PioRemoteActionHandler.prototype.connect = function(command) {
if(!client || !connected) {
env.logger.info('Client isn\'t connected yet, establish new connection...');
client = new net.Socket();
client.on('data', function(data) {
pioRemoteActionHandler.handleData(data.toString());
});
/**
* this event is triggered, if the connection is successfully established
**/
client.on('connect', function() {
env.logger.info('Connection successfully established');
connected = true;
});
client.on('error', function(ex) {
env.logger.info('An error occured during connecting to avr: ' + ex);
connected = false;
});
client.on('close', function() {
if(client && connected) {
env.logger.info('Connection closed');
client.disconnect();
}
});
client.connect(pluginConfig.port ? pluginConfig.port : defaultPort,
pluginConfig.host ? pluginConfig.host : defaultHost, function() {
if(command) {
/**
* assuming everything went okay, because there is no callback
**/
return pioRemoteActionHandler.sendCommand(command);
} else {
return 'done';
}
});
} else {
env.logger.info('Client already connected, nothing to do.');
}
return 'done';
};
/**
* Method to call when connection isn't needed any longer
**/
PioRemoteActionHandler.prototype.disconnect = function() {
if(client && connected) {
env.logger.info('Closing telnet session...');
client.destroy();
client = undefined;
connected = false;
return 'disconnected';
} else {
env.logger.info('Nothing to disconnect from.');
}
return 'disconnected';
};
/**
* Method for sending a command
**/
PioRemoteActionHandler.prototype.sendCommand = function(command) {
var brand = pluginConfig.brand || defaultBrand;
/**
* parse the command
**/
if(command && command.indexOf('\.') > 0) {
var splittedCommand = command.split('\.');
var category = splittedCommand[0];
var func = splittedCommand[1];
var maxVolume = pluginConfig.maxVolume || defaultMaxVolume;
if(maxVolume > controlCodes[brand].maxVolume) {
maxVolume = controlCodes[brand].maxVolume;
}
var value = '';
/**
* Handle max volume to avoid damage on user and equipment
**/
if(category === 'volume' && func === 'up') {
var volLevel = (currentVolume + 80.5) * 2;
if(volLevel >= maxVolume) {
currentDisplay = 'Vol max reached!';
return 'Vol max reached!';
}
}
if(category === 'volume' && func === 'down') {
var volLevel = (currentVolume + 80.5) * 2;
if(volLevel <= 1) {
currentDisplay = 'Vol min reached!';
return 'Vol min reached!';
}
}
if(splittedCommand.length === 3) {
value = splittedCommand[2];
env.logger.info('value: ' + value);
/**
* Calculate correct value depending on maxValue
**/
if(category === 'volume' && func === 'set') {
if(category === 'volume' && value < 0) {
value = '001';
}
value = Math.round((value * maxVolume / 100),0);
var volumeDigits = 3;
if (brand === 'denon') {
volumeDigits = 2;
}
while(value.toString().length < volumeDigits) {
value = '0' + value;
}
}
/**
* Catch negative values
**/
}
if(controlCodes[brand][category][func]) {
var dataToWrite = value + controlCodes[brand][category][func] + '\r';
var dataSent = client.write(dataToWrite);
if(dataSent) {
return 'Sent command: ' + dataToWrite;
} else {
return 'An error occured while sending data: ' + dataToWrite;
}
}
} else {
return 'Unsupported command: ' + command;
}
};
/**
* Handle the incoming data
**/
PioRemoteActionHandler.prototype.handleData = function(stringyfiedData) {
if(stringyfiedData.indexOf('VOL') === 0) {
currentVolume = (0.5 * stringyfiedData.substring(3,6) - 80.5);
} else if(stringyfiedData.indexOf('FL0') === 0) { // handle default display
var str = '';
for (var i = 4; i < stringyfiedData.length; i += 2) {
str += String.fromCharCode(parseInt(stringyfiedData.substr(i, 2), 16));
}
// Cut text to 15 characters
if(str.length > 15) {
str = str.substring(0, 15);
}
/**
if(hmgDisplay.length !== 0 && stringyfiedData.indexOf('FL00') !== 0) {
currentDisplay = str + '\n\r' + hmgDisplay;
} else {
currentDisplay = str;
}
**/
currentDisplay = str;
} else if(stringyfiedData.indexOf('FN') === 0) { // handle input display
currentInput = stringyfiedData;
} else if(stringyfiedData.indexOf('GEH01020') >= 0) { // handle H.M.G. display
var start = stringyfiedData.indexOf('GEH01020') + 9;
var stop = stringyfiedData.indexOf('GEH02023') -3;
hmgDisplay = stringyfiedData.substring(start, stop);
env.logger.info('H.M.G. Display: ' + hmgDisplay);
} else {
env.logger.info('Received: ' + stringyfiedData);
}
};
/**
* This function handles action in the form of `sendAVR "command"`
**/
PioRemoteActionHandler.prototype.executeAction = function() {
return this.framework.variableManager.evaluateStringExpression(this.commandTokens).then((function(_this) {
return function(command) {
switch(command) {
case 'connect':
return pioRemoteActionHandler.connect();
break;
case 'disconnect':
return pioRemoteActionHandler.disconnect();
break;
default:
/**
* If no connection to the AVR exists, connect first and send the command afterwards
**/
if(!client || !connected) {
return pioRemoteActionHandler.connect(command);
} else {
return pioRemoteActionHandler.sendCommand(command);
}
}
};
})(this));
};
return PioRemoteActionHandler;
})(env.actions.ActionHandler);
pioRemoteActionHandler = new PioRemoteActionHandler;
/**
* THE VOLUME SENSOR
**/
AVRSensor = (function(_super) {
__extends(AVRSensor, _super);
function AVRSensor(config, framework) {
var attr;
this.config = config;
this.id = config.id;
this.name = config.name;
this.attributes = {};
var func = (function(_this) {
return function(attr) {
var name = attr.name;
assert(name === 'vol' || name === 'display');
switch (name) {
case 'vol':
_this.attributes[name] = {
description: name,
type: "number"
};
var getter = (function() {
if(!client) {
pioRemoteActionHandler.connect();
} else {
pioRemoteActionHandler.sendCommand('volume.status');
}
return Promise.resolve(currentVolume);
});
_this.attributes[name].unit = 'dB';
_this.attributes[name].acronym = 'VOL';
break;
case 'display':
_this.attributes[name] = {
description: name,
type: "string"
};
var getter = (function() {
if(!client) {
pioRemoteActionHandler.connect();
}
return Promise.resolve(currentDisplay);
});
break;
default:
throw new Error("Illegal attribute name: " + name + " in AVRSensor.");
}
_this._createGetter(name, getter);
return setInterval((function() {
return getter().then(function(value) {
return _this.emit(name, value);
})["catch"](function(error) {
return env.logger.debug(error.stack);
});
}), 1000);
};
})(this);
for (var i = 0; i < this.config.attributes.length; i++) {
attr = this.config.attributes[i];
func(attr);
}
AVRSensor.__super__.constructor.call(this);
}
return AVRSensor;
})(env.devices.Sensor);
module.exports.PioRemoteActionProvider = PioRemoteActionProvider;
return pioRemotePlugin;
};