Skip to content

Commit 9edc0e0

Browse files
authored
fix: when set DND, state must be BUSY (#17)
1 parent 6117477 commit 9edc0e0

File tree

2 files changed

+184
-1
lines changed

2 files changed

+184
-1
lines changed

lib/plugins_command_13/setState.js

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
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+
})();

lib/proxy_logic_13/proxy_logic_13.js

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9937,7 +9937,41 @@ function setAsteriskPresence(extension, presenceState, cb) {
99379937
exten: extension,
99389938
state: presenceState
99399939
}, function (error) {
9940-
cb(error);
9940+
if (error) {
9941+
logger.error(IDLOG, 'setPresenceState to "'+ presenceState +'" for "' + extension + ' failed');
9942+
cb(error);
9943+
}
9944+
9945+
// if presence is DND, set also state to BUSY
9946+
if(presenceState === 'DND') {
9947+
astProxy.doCmd({
9948+
command: 'setState',
9949+
exten: extension,
9950+
state: 'BUSY'
9951+
}, function (error) {
9952+
if (error) {
9953+
logger.error(IDLOG, 'setState to "BUSY" for "' + extension + ' failed');
9954+
cb(error);
9955+
}
9956+
});
9957+
}
9958+
9959+
// if presence is AVAILABLE, set also state to NOT_INUSE
9960+
if(presenceState === 'AVAILABLE') {
9961+
astProxy.doCmd({
9962+
command: 'setState',
9963+
exten: extension,
9964+
state: 'NOT_INUSE'
9965+
}, function (error) {
9966+
if (error) {
9967+
logger.error(IDLOG, 'setState to "NOT_INUSE" for "' + extension + ' failed');
9968+
cb(error);
9969+
}
9970+
});
9971+
}
9972+
9973+
// return callback
9974+
cb();
99419975
});
99429976
} catch (e) {
99439977
logger.error(IDLOG, e.stack);

0 commit comments

Comments
 (0)