Last active
February 25, 2019 22:12
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Constructor for creating a endpoint object with video xapi. | |
const util = require('util'); | |
const EventEmitter = require('events').EventEmitter; | |
const jsxapi = require('jsxapi'); | |
//pass in object | |
function TPXapi(endpoint){ | |
this.endpoint = endpoint; | |
this.xapi; | |
this.connectedStatus = 'false'; | |
this.domain = '.webex.com'; | |
this.mysite = 'mysite'; | |
this.init(); | |
} | |
util.inherits(TPXapi,EventEmitter); | |
TPXapi.prototype.init = function(){ | |
const self = this; | |
return self.connect() | |
.then((status) =>{ | |
console.log(status); | |
self.onReady(); | |
return; | |
}) | |
.catch((err) => { | |
console.error(err); | |
}) | |
} | |
//connect to ssh service on endpoints | |
TPXapi.prototype.connect = function() { | |
var self = this; | |
return new Promise((resolve, reject) => { | |
self.xapi = jsxapi.connect('ssh://' + self.endpoint.ipAddress||self.endpoint.url, { | |
username: self.endpoint.username, | |
password: self.endpoint.password, | |
keepaliveInterval: 4000 | |
}); | |
self.onError(); | |
resolve ("Connection opening..........") | |
.catch ((err) => { | |
reject (console.error(err)); | |
}); | |
}); | |
} | |
//close ssh connection | |
TPXapi.prototype.closeConnect = function(){ | |
const self = this; | |
return new Promise((resolve, reject) => { | |
console.log("xapi session closed."); | |
self.connectedStatus = "false"; | |
resolve (self.xapi.close()); | |
return self; | |
}) | |
}; | |
//Load event monitoring after connecting via ssh to endpoint | |
TPXapi.prototype.onReady = function(){ | |
const self = this; | |
self.xapi.on('ready', () => { | |
console.log("connexion successful!"); | |
self.connectedStatus = "true"; | |
self.webexMeeting(); | |
return self; | |
}) | |
}; | |
//event monitors for webex meetings | |
TPXapi.prototype.webexMeeting = function(){ | |
const self =this; | |
self.xapi.event.on('UserInterface Extensions Event PageOpened', (event) => { | |
console.log('event', event.PageId); | |
if (event.PageId == 'webex_dial') { | |
//Intial press of webex button | |
self.xapi.command('UserInterface Message TextInput Display', { | |
FeedbackId: 'MeetingID_webex', | |
InputType: 'Numeric', | |
KeyboardState: 'Open', | |
Title: 'Join a Webex Meeting', | |
Text: 'Enter the Meeting ID', | |
Placeholder: 'Meeting ID', | |
SubmitText: 'Join' | |
}); | |
} | |
}); | |
//Making calling to webex meeeting once ID is entered. | |
self.xapi.event.on('UserInterface Message TextInput Response', (event) => { | |
if (event.FeedbackId === 'MeetingID_webex') { | |
var ID = event.Text; | |
var URI = ID + '@' +self.mysite+ self.domain; | |
self.xapi.command('Dial', { Number: URI }); | |
} | |
}); | |
}; | |
//track ssh error events and reconnect | |
TPXapi.prototype.onError = function(){ | |
const self = this; | |
self.xapi.on('error', (err) => { | |
console.error(`connexion failed: ${err}`); | |
if(err === 'client-timeout'){ | |
setTimeout(function(){ | |
self.init(); | |
}, 4000) | |
}else{ | |
self.closeConnect(); | |
setTimeout(function(){ | |
self.init(); | |
}, 4000) | |
} | |
}); | |
}; | |
module.exports = TPXapi; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment