-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.js
52 lines (43 loc) · 1.42 KB
/
lib.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
'use strict';
var util = require('util'),
config = require('@fc24/config'),
request = require('request-promise');
var snomConfig = config('snom'),
urls = {
key: '/command.htm?key=%s',
dial: '/command.htm?number=%s&outgoing_uri=%s',
reboot: '/advanced_update.htm?reboot=Reboot',
reset: '/advanced_update.htm?reset=Reset',
upgrade: '/dummy.htm?swload=load&firmware=%s',
ring: '/line_login.htm?PLAY_RINGER:%s=+Ringer'
},
acceptedKeys = /CANCEL|ENTER|OFFHOOK|ONHOOK|RIGHT|LEFT|UP|DOWN|VOLUME_UP|VOLUME_DOWN|MENU|REDIAL|DND|REC|F[1-4]|SPEAKER|HEADSET|TRANSFER|F_HOLD|[0-9]|P^([1-9]|1[0-5])/;
function validateArgs(command, args) {
if (command === 'key') {
return acceptedKeys.test(args[0]);
}
return true;
}
function executeCommand(type, ip, args) {
var url,
timeout = 5000,
command = urls[type];
if (!command) {
console.error('Command ' + type + ' not supported');
process.exit(1);
}
if (!validateArgs(type, args)) {
console.error('Arguments not permitted');
process.exit(1)
}
command = util.format.bind(null, urls[type]).apply(null, args);
url = util.format('http://%s:%s@%s%s', snomConfig.user, snomConfig.password, ip, command);
return request({url, timeout})
.then(function(body) {
return body;
});
}
module.exports = {
executeCommand,
urls
};