// Array for Devices of User - we fill it later
var hwdevices = [];
// Function for getting the right URL to PBX - For RccApi we need a AppSocket to PBX.
function convertToWebSocket(url) {
// Check if connections was established over http or https
if (url.startsWith("http://")) {
//Build Websocketurl of PBX with ws://
return "ws://" + url.slice(7, url.indexOf("/", 7)) + "/PBX0/APPS/websocket";
} else if (url.startsWith("https://")) {
//Build Websocketurl of PBX with wss://
return "wss://" + url.slice(8, url.indexOf("/", 8)) + "/PBX0/APPS/websocket";
} else {
// Return Websocket URL for AppWebsocket
return url;
}
}
// Genereate WebsocketURL from PBX by start-Variable
var pbxurl = convertToWebSocket(start.clientUrl);
//console.log(pbxurl);
// Create innovaphone AppWebSocket to PBX
var pbxconn = new innovaphone.appwebsocket.Connection(pbxurl, start.name, null, null);
pbxconn.checkBuild = false;
pbxconn.onconnected = pbxconn_connected;
pbxconn.onmessage = pbxconn_message;
var pbxUserCn = "";
// If we are connected to PBX, we can send UserInitialize. (RccApi has to be activated on AppObjekt.)
function pbxconn_connected() {
console.log("PBX Connected - " + pbxconn.logindata.info.cn);
// Just to show from what Object you could get the CN of af User......
pbxUserCn = pbxconn.logindata.info.cn;
//Connection is up, we create a Dropdown for Devices of the User.
createDropdown();
}
function pbxconn_message(obj) {
// If everything is okay, we will get UserInitializeResult. Now you can use RccApi
if (obj.mt == "UserInitializeResult"){
console.log ("RccApi Initialized - Have fun !!!");
}
}
// We need the phoneApi to get information about what devices are availabe
var phoneApi = start.consumeApi("com.innovaphone.phone");
// Add an Eventlistener for incoming APP Messages
window.addEventListener("message", onwindowmessage);
function onwindowmessage(msg) {
const obj = JSON.parse(msg.data);
if (obj.mt === "ApiUpdate") {
//console.log(obj);
// Get Devices of User from phoneApi.model
// console.log(phoneApi.model);
// Loop through each device
for (const deviceId in phoneApi.model) {
// Check if the device is its own property and not inherited
if (phoneApi.model.hasOwnProperty(deviceId)) {
// Access the current device
const device = phoneApi.model[deviceId];
// Log the title of the device
console.log("Devicename:", device.title);
// Log the model of the device
console.log("HardwareId:", deviceId.substring(4));
console.log("------------------------------");
hwdevices.push([device.title, deviceId.substring(4)]);
}
}
}
}
function createDropdown() {
var select = document.createElement('select');
select.id = 'deviceDropdown';
// Loop through the hwdevices array
for (var i = 0; i < hwdevices.length; i++) {
var option = document.createElement('option');
option.value = hwdevices[i][1]; // HardwareId as the value of the option
option.textContent = hwdevices[i][0]; // Devicename as the text of the option
select.appendChild(option);
}
// Add event listener for when an option is selected
select.addEventListener('change', function() {
var selectedValue = this.value; // Get the selected value
// Call your function here with the selected value
GetRccApi(selectedValue);
});
// Add the dropdown menu to the body
document.body.appendChild(select);
}
// Function to be called when an option is selected
function GetRccApi(selectedValue) {
console.log('Selected value:', selectedValue);
pbxconn.send({"mt":"UserInitialize","api":"RCC","xfer":true,"cn":pbxUserCn,"hw":selectedValue,"disc":true})
}