astproxy
Version:
Node.js proxy for Asterisk PBX
123 lines (111 loc) • 2.84 kB
JavaScript
/**
* @module astproxy
* @submodule plugins_event_13
*/
/**
* The module identifier used by the logger.
*
* @property IDLOG
* @type string
* @private
* @final
* @readOnly
* @default [deviceStateChanged]
*/
var IDLOG = '[deviceStateChanged]';
/**
* The asterisk proxy.
*
* @property astProxy
* @type object
* @private
*/
var astProxy;
(function() {
/**
* The logger. It must have at least three methods: _info, warn and error._
*
* @property logger
* @type object
* @private
* @default console
*/
var logger = console;
try {
/**
* The plugin that handles the deviceStateChanged event.
*
* @class deviceStateChanged
* @static
*/
var deviceStateChanged = {
/**
* It's called from _astproxy_ component for each
* deviceStateChanged event received from the asterisk.
*
* @method data
* @param {object} data The asterisk event data
* @static
*/
data: function(data) {
try {
if (data.device &&
data.device.indexOf('/') !== -1 &&
data.device.split('/').length === 2 &&
data.device.split('/')[0].toLowerCase() === 'pjsip' &&
data.event === 'DeviceStateChange') {
logger.info(IDLOG, 'received event ' + data.event);
astProxy.proxyLogic.evtDeviceStatusChanged(data.device.split('/')[1]);
}
} catch (err) {
logger.error(IDLOG, err.stack);
}
},
/**
* Set the logger to be used.
*
* @method setLogger
* @param {object} log The logger object. It must have at least
* three methods: _info, warn and error_
* @static
*/
setLogger: function(log) {
try {
if (typeof log === 'object' &&
typeof log.info === 'function' &&
typeof log.warn === 'function' &&
typeof log.error === 'function') {
logger = log;
} else {
throw new Error('wrong logger object');
}
} catch (err) {
logger.error(IDLOG, err.stack);
}
},
/**
* Store the asterisk proxy to visit.
*
* @method visit
* @param {object} ap The asterisk proxy module.
*/
visit: function(ap) {
try {
// check parameter
if (!ap || typeof ap !== 'object') {
throw new Error('wrong parameter');
}
astProxy = ap;
} catch (err) {
logger.error(IDLOG, err.stack);
}
}
};
// public interface
exports.data = deviceStateChanged.data;
exports.visit = deviceStateChanged.visit;
exports.setLogger = deviceStateChanged.setLogger;
} catch (err) {
logger.error(IDLOG, err.stack);
}
})();