var innovaphone = innovaphone || {};
innovaphone.RccTutorial= innovaphone.RccTutorial|| function (start, args) {
this.createNode("body");
var that = this;
var rcc = null;
//Consume the Client API to receive the PBX URL
var clientApi = start.consumeApi("com.innovaphone.client");
window.addEventListener("message", onpostmessage);
this.clientAPI = clientApi;
window.addEventListener("message", onpostmessage);
function onpostmessage(e) {
if (e.data) {
var obj = JSON.parse(e.data);
if (obj.mt == "ApiUpdate" && !rcc) {
var url = obj.apis["com.innovaphone.client"]["@client"]["url"];
if (url) {
url = "ws" + url.slice(4, url.search("/APPCLIENT/")) + "/APPS/websocket";
//Set an AppWebsocket connection to the PBX
rcc = new innovaphone.appwebsocket.Connection(url, start.name, null, null, rcc_connected, rcc_message);
}
}
}
}
//This function is called when the AppWebsocket connection is set:
function rcc_connected(d, user, dn, appdomain) {
//The first message that you need to send is UserInitialize with the CN of the user you want to monitorize (in this case the login user)
rcc.send({ mt: "UserInitialize", api: "RCC", cn: rcc.logindata.info.cn });
}
//This function will be called every time a RCC JSON is received
//With every JSON you send, a Result message should be received
function rcc_message(msg) {
if (msg.mt == "UserInitializeResult") {
that.userID = msg.user; //Save the ID
}
else if (msg.mt == "CallInfo") {
if (msg.msg == "del") {
//If a call is disconnected
}
else if (msg.msg.indexOf("-setup") > -1) {
//If a call is set
}
}
else if (msg.mt == "UserConnectResult") {
//Call connected
}
else if (msg.mt == "UserRcResult") {
//Answer to mute/unmute a call
}
else if (msg.mt == "UserParkResult") {
//Answer to park a call
}
else if (msg.mt == "UserPickupResult") {
//Answer to pick up a call
}
else if (msg.mt == "UserHoldResult") {
//Answer to put a call on hold
}
else if (msg.mt == "UserRetrieveResult") {
//Answer to retrieve a call
}
else if (msg.mt == "UserClearResult") {
//Answer to clear a call
}
}
function Call(msg, parent, queue) {
var call = this;
this.id = msg.call;
this.createNode("div", null, null, "callsList").addEvent("click", onSelectCall);
parent.add(call);
this.delete = function () {
call.parent.rem(call);
}
this.connect = function () {
//Send to connect the call
rcc.send({ mt: "UserConnect", api: "RCC", call: call.id });
}
this.connected = function (id) {
//Save the ID of the call
if (id > 0) call.id = id;
}
this.disconnect = function () {
//Send to clear the call
rcc.send({ mt: "UserClear", api: "RCC", call: call.id, cause: 26 });
}
this.mute = function () {
//Send to mute the call
rcc.send({ mt: "UserRc", api: "RCC", call: call.id, rc: 15 });
}
this.unmute = function () {
//Send to unmute the call
rcc.send({ mt: "UserRc", api: "RCC", call: call.id, rc: 14 });
}
this.hold = function () {
//Send to put the call on hold
rcc.send({ mt: "UserHold", api: "RCC", call: call.id });
}
this.retrieve = function () {
//Send to retrieve the call
rcc.send({ mt: "UserRetrieve", api: "RCC", call: call.id });
}
this.park = function () {
//Send to park the call
rcc.send({ mt: "UserPark", api: "RCC", call: call.id, position: -1 });
}
this.unpark = function () {
//Send to pick up the call
rcc.send({ mt: "UserPickup", api: "RCC", call: call.id, user: that.initID });
}
}
Call.prototype = innovaphone.ui1.nodePrototype;
}
innovaphone.RccTutorial.prototype = innovaphone.ui1.nodePrototype;