-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrouters.js
85 lines (76 loc) · 3.7 KB
/
routers.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
/**
* The central routers for data flow of events notifying & data push.
* For the dataflow of events notifying, handlers for incoming notification are required.
* For the dataflow of data push, data unmarshalling is required to push to the Polycom phones.
*
* Created by Xiaolei Y. ([email protected]) on 12/11/2014.
*/
var xml2js = require('xml2js'),
telnetclient = require('./telnetclient'),
mappings = require('./utils/mappings'),
httpclient = require('./httpclient'),
callHandler = require('./handlers/call-events'),
hooksHandler = require('./handlers/hook-events'),
lineRegHandler = require('./handlers/line-reg-events'),
phoneLockHandler = require('./handlers/phone-lock-events'),
userLoginHandler = require('./handlers/userlogin-events'),
msgFormatter = require('./handlers/message-formatter');
/**
* All required event handlers which defined at respective javascript files.
*
* @type {{IncomingCallEvent: Function, OutgoingCallEvent: Function, OffHookEvent: Function, OnHookEvent: Function, PhoneLockedEvent: Function, PhoneUnlockedEvent: Function, CallStateChangeEvent: Function, LineRegistrationEvent: Function, LineUnregistrationEvent: Function, UserLoginOutEvent: Function}}
*/
var _handlers = {
'IncomingCallEvent': callHandler.handleIncomingCall,
'OutgoingCallEvent': callHandler.handleOutgoingCall,
'CallStateChangeEvent': callHandler.hanldeCallstateChange,
'OffHookEvent': hooksHandler.hanldeOffhook,
'OnHookEvent': hooksHandler.hanldeOnhook,
'PhoneLockedEvent': phoneLockHandler.hanldePhonelock,
'PhoneUnlockedEvent': phoneLockHandler.hanldePhoneunlock,
'LineRegistrationEvent': lineRegHandler.hanldeLineReg,
'LineUnregistrationEvent': lineRegHandler.hanldeLineUnreg,
'UserLoginOutEvent': userLoginHandler.hanldeUserLoginout
};
// The telnet port on which server is listening
var _telnetport = '23';
module.exports = {
/**
* The router definition for notification data flow, which comes from Polycom phone and send to the hardware.
*
* @param inbound The notification data in JSON format, which has the same structure with the original XML packet.
*/
phone2device: function(inbound) {
var eventType = Object.getOwnPropertyNames(inbound)[0];
var handler = _handlers[eventType];
if (!handler) {
console.error('Received invalid event which could be not processed: ' + eventType);
return;
}
var outbound = handler(inbound);
var outboundXml = new xml2js.Builder({
rootName: 'root',
headless: true
}).buildObject(outbound);
var receivers = mappings.phone2device(inbound[eventType].PhoneIP);
if (!receivers) {
console.error('The receivers for incoming notifications from phone ' + inbound[eventType].PhoneIP
+ ' are not defined, please check the config/mappings.xml for correction.');
return;
}
// send outbound message the devices configured in mappings.xml
for (var idx = 0; idx < receivers.length; idx++) {
var receiver = receivers[idx];
telnetclient.send(receiver, outboundXml);
}
},
/**
* The router definition for datapush data flow, which comes from the hardware and send to Polycom phones.
*
* @param inbound The notification data in JSON format, which has the same structure with the original XML packet.
*/
device2phone: function(inbound) {
var outbound = msgFormatter.format(inbound);
httpclient.pushaction(outbound.phoneIP, outbound.username, outbound.password, outbound.data);
}
}