-
Notifications
You must be signed in to change notification settings - Fork 0
/
call-fav-kiosk.js
190 lines (171 loc) · 5.65 KB
/
call-fav-kiosk.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import xapi from "xapi";
const config = {
target: "<target SIP address>",
};
/*********************************************************
* Subscribe to status and events
**********************************************************/
let listeners = [];
let timeout = null;
let fromContact = null;
let sip = null;
let startTime = null;
const serverUrl = "https://adjd-server.glitch.me/data/";
// Check if kiosk mode is available first before applying config and starting listeners
xapi.Config.UserInterface.Kiosk.Mode.get()
.then((mode) => {
console.log(`Kiosk Mode currently set to [${mode}]`);
if (mode == "On") {
startListeners();
}
})
.catch((error) =>
console.log(
"Kiosk Mode not available on this device - this macro will not work - Error: ",
error.message
)
);
// Start or Stop listeners if Kiosk mode changes
xapi.Config.UserInterface.Kiosk.Mode.on((mode) => {
if (mode == "On") {
startListeners();
} else {
stopListeners();
}
});
function startListeners() {
console.log("Setting up listners");
listeners.push(
xapi.Status.SystemUnit.State.NumberOfActiveCalls.on(processCallCount)
);
listeners.push(xapi.Status.Call.RemoteNumber.on(processRemoteNumber));
listeners.push(xapi.Status.UserInterface.WebView.on(processWebViews));
xapi.Status.SystemUnit.State.NumberOfActiveCalls.get().then((r) =>
processCallCount(r)
);
}
function stopListeners() {
console.log("Stopping listners");
const lenght = listeners.length;
for (let i = 0; i < lenght; i++) {
const listener = listeners.shift();
listener();
}
}
/*********************************************************
* Main functions and event subscriptions
**********************************************************/
let currentState = "Standby";
async function processWebViews(event) {
if (!event.hasOwnProperty("URL")) return;
const url = event.URL;
const id = event.id;
console.log(url);
const hash = url.split("?")[Number("1")];
console.log("hash", hash);
const paramPairs = hash.split("&");
// Create an object to store the parameters
const params = {};
// Iterate through parameter pairs and store them in the object
paramPairs.forEach((pair) => {
const [key, value] = pair.split("=");
params[key] = decodeURIComponent(value); // Use decodeURIComponent to handle special characters
});
console.log("params", params);
console.log("sip:", params["sip"]);
console.log("from:", params["from"]);
console.log("time:", params["time"]);
sip = params["sip"];
sip = sip.split("#")[0];
timeout = params["time"];
fromContact = params["from"];
fromContact = fromContact.split("#")[0];
// const sip=hash.split('=')[Number('0')]
// const uri=hash.split('=')[Number('1')]
// console.log(sip)
// console.log(uri)
if ("sip" in params) {
xapi.Command.Dial({ Number: params["sip"] });
return;
}
}
async function processCallCount(event) {
console.log("Number of calls: " + event);
if (event == "1") {
if (timeout) {
startTime = Date.now(); // Record the start time when 'Start' button is clicked
// timerInterval = setInterval(updateTimer, 1000);
setTimeout(function () {
xapi.Command.Call.Disconnect({ CallId: 0 });
console.log("Timer is up!");
}, timeout * 1000);
}
} else {
// clearInterval(timerInterval); // Stop updating the timer when 'Stop' button is clicked
if (startTime) {
const endTime = Date.now(); // Get the current time when 'Stop' button is clicked
const elapsedTime = (endTime - startTime) / 1000; // Calculate elapsed time in seconds
console.log(parseInt(elapsedTime)); // Display elapsed time
let updatedTime = timeout - parseInt(elapsedTime);
if (updatedTime >= 0) {
const payload = {
time: updatedTime,
};
const Header = ["Content-Type: application/json"];
return xapi.Command.HttpClient.Post(
{
AllowInsecureHTTPS: "True",
Header,
ResultBody: "PlainText",
Url: serverUrl + fromContact,
},
JSON.stringify(payload)
)
.then((result) => {
xapi.Config.UserInterface.Kiosk.URL.get().then((url) => {
console.log(url);
if (fromContact) {
if (currentState === "Standby") {
currentState = "New";
const prehash = url.split("/form")[0];
const newUrl =
prehash +
"/form?email=" +
fromContact +
"#state=" +
currentState;
xapi.Config.UserInterface.Kiosk.URL.set(newUrl);
} else {
currentState = "Standby";
const prehash = url.split("/form")[0];
const newUrl =
prehash +
"/form?email=" +
fromContact +
"#state=" +
currentState;
xapi.Config.UserInterface.Kiosk.URL.set(newUrl);
}
}
});
})
.catch((e) => console.log("error in post", e));
}
}
}
}
// function updateTimer() {
// const currentTime = Date.now();
// const elapsedTime = (currentTime - startTime) / 1000;
// console.log(elapsedTime.toFixed(2));
// }
function processRemoteNumber(event) {
console.log(`Remote Number[${event}]`);
// Check if a monitoring number is present
const match = config.queues.find((queue) => event.startsWith(queue.number));
// Ignore calls with not matched number
if (!match) {
console.log("No number matched");
return;
}
}