|
| 1 | +/** |
| 2 | + * @submodule plugins_command_13 |
| 3 | + */ |
| 4 | +var action = require('../action'); |
| 5 | + |
| 6 | +/** |
| 7 | + * The module identifier used by the logger. |
| 8 | + * |
| 9 | + * @property IDLOG |
| 10 | + * @type string |
| 11 | + * @private |
| 12 | + * @final |
| 13 | + * @readOnly |
| 14 | + * @default [setState] |
| 15 | + */ |
| 16 | +var IDLOG = '[setState]'; |
| 17 | + |
| 18 | +(function() { |
| 19 | + |
| 20 | + /** |
| 21 | + * The logger. It must have at least three methods: _info, warn and error._ |
| 22 | + * |
| 23 | + * @property logger |
| 24 | + * @type object |
| 25 | + * @private |
| 26 | + * @default console |
| 27 | + */ |
| 28 | + var logger = console; |
| 29 | + |
| 30 | + try { |
| 31 | + /** |
| 32 | + * Map associations between ActionID and callback to execute at the end |
| 33 | + * of the command. |
| 34 | + * |
| 35 | + * @property map |
| 36 | + * @type {object} |
| 37 | + * @private |
| 38 | + */ |
| 39 | + var map = {}; |
| 40 | + |
| 41 | + /** |
| 42 | + * Command plugin to set the presence state of an extension. |
| 43 | + * |
| 44 | + * Use it with _astproxy_ module as follow: |
| 45 | + * |
| 46 | + * ast_proxy.doCmd({ command: 'setState', exten: '214', state: 'BUSY' }, function (res) { |
| 47 | + * // some code |
| 48 | + * }); |
| 49 | + * |
| 50 | + * |
| 51 | + * @class setState |
| 52 | + * @static |
| 53 | + */ |
| 54 | + var setState = { |
| 55 | + |
| 56 | + /** |
| 57 | + * Execute asterisk action to set the presence state. |
| 58 | + * |
| 59 | + * @method execute |
| 60 | + * @param {object} am Asterisk manager used to send the action |
| 61 | + * @param {object} args The object containing optional parameters |
| 62 | + * @param {function} cb The callback function |
| 63 | + * @static |
| 64 | + */ |
| 65 | + execute: function(am, args, cb) { |
| 66 | + try { |
| 67 | + var act; |
| 68 | + // action for asterisk |
| 69 | + act = { |
| 70 | + Action: 'Command', |
| 71 | + Command: 'devstate change Custom:DND' + args.exten + ' ' + args.state |
| 72 | + }; |
| 73 | + |
| 74 | + // set the action identifier |
| 75 | + act.ActionID = action.getActionId('setState'); |
| 76 | + |
| 77 | + // add association ActionID-callback |
| 78 | + map[act.ActionID] = cb; |
| 79 | + |
| 80 | + // send action to asterisk |
| 81 | + am.send(act); |
| 82 | + |
| 83 | + } catch (err) { |
| 84 | + logger.error(IDLOG, err.stack); |
| 85 | + } |
| 86 | + }, |
| 87 | + |
| 88 | + /** |
| 89 | + * It's called from _astproxy_ component for each data received |
| 90 | + * from asterisk and relative to this command. |
| 91 | + * |
| 92 | + * @method data |
| 93 | + * @param {object} data The asterisk data for the current command |
| 94 | + * @static |
| 95 | + */ |
| 96 | + data: function(data) { |
| 97 | + try { |
| 98 | + // check callback |
| 99 | + if (map[data.actionid]) { |
| 100 | + map[data.actionid](null); |
| 101 | + delete map[data.actionid]; // remove association ActionID-callback |
| 102 | + } else { |
| 103 | + map[data.actionid](new Error('error')); |
| 104 | + delete map[data.actionid]; // remove association ActionID-callback |
| 105 | + } |
| 106 | + |
| 107 | + } catch (err) { |
| 108 | + logger.error(IDLOG, err.stack); |
| 109 | + if (map[data.actionid]) { |
| 110 | + map[data.actionid](err); |
| 111 | + delete map[data.actionid]; |
| 112 | + } |
| 113 | + } |
| 114 | + }, |
| 115 | + |
| 116 | + /** |
| 117 | + * Set the logger to be used. |
| 118 | + * |
| 119 | + * @method setLogger |
| 120 | + * @param {object} log The logger object. It must have at least |
| 121 | + * three methods: _info, warn and error_ |
| 122 | + * @static |
| 123 | + */ |
| 124 | + setLogger: function(log) { |
| 125 | + try { |
| 126 | + if (typeof log === 'object' && |
| 127 | + typeof log.info === 'function' && |
| 128 | + typeof log.warn === 'function' && |
| 129 | + typeof log.error === 'function') { |
| 130 | + |
| 131 | + logger = log; |
| 132 | + } else { |
| 133 | + throw new Error('wrong logger object'); |
| 134 | + } |
| 135 | + } catch (err) { |
| 136 | + logger.error(IDLOG, err.stack); |
| 137 | + } |
| 138 | + } |
| 139 | + }; |
| 140 | + |
| 141 | + // public interface |
| 142 | + exports.data = setState.data; |
| 143 | + exports.execute = setState.execute; |
| 144 | + exports.setLogger = setState.setLogger; |
| 145 | + |
| 146 | + } catch (err) { |
| 147 | + logger.error(IDLOG, err.stack); |
| 148 | + } |
| 149 | +})(); |
0 commit comments