|
var SpokesJS = require('./spokes');
var spokes = null; //spokes session manager (class defined in spokes.js)
var plugin_registered = false;
var plugin_name = "NodeSpokes";
//------------------------------------------------
// connectToSpokes - attempt to create a new Spokes session manager, register this app,
// and get a valid device to work with. Begin polling for events on success.
//------------------------------------------------
var connectToSpokes = function()
{
//attempt to make a new Spokes object with localhost:32001/Spokes as the base url path for REST calls
spokes = new SpokesJS.Spokes("http://localhost:32001/Spokes");
spokes.Device.deviceList( function(result)
{
//on success...
if(!result.isError)
{
//on valid device found...
if(result.Result[0] !== null)
{
//attempt to connect to the valid device and start polling for events from it.
spokes.Device.attach(result.Result[0].Uid, controlInterface);
pollDeviceEvents();
}
else
{
console.log("Error: Device was null on connecting to Spokes. Is there a Plantronics device connected?");
}
}
else
{
console.log("Error connecting to Spokes.");
}
});
};
//------------------------------------------------
// controllerInterface - callback function to that registers this app on successful contact with a valid device.
//------------------------------------------------
var controlInterface = function(session)
{
//if an error popped up in our session or we failed to connect with a device...
if(session.isError || !spokes.Device.isAttached)
{
console.log("Session Registration Error");
}
else
{
registerPlugin();
}
};
//------------------------------------------------
// registerPlugin - where the actual dirty work is done for app registration.
//------------------------------------------------
var registerPlugin = function()
{
//only register if we haven't done so already...
if(!plugin_registered)
{
spokes.Plugin.register(plugin_name, function(result)
{
if(!result.isError)
{
//successfully registered the plugin. Set status of plugin to active.
spokes.Plugin.isActive(plugin_name, true, function(result)
{
if(!result.isError)
{
plugin_registered = true;
}
else
{
console.log("Error checking if plugin is active: " + result.Err.Description);
}
});
}
else
{
console.log("Error registering plugin: " + result.Err.Description);
}
});
}
};
//------------------------------------------------
// pollDeviceEvents - regularly ask the Spokes service for new, if any, events to work with.
// This is the main program loop where all the program-specific work will be done.
//------------------------------------------------
var pollDeviceEvents = function()
{
setInterval(function()
{
if(spokes === null || !spokes.Device.isAttached)
{
return;
}
//ask for events, if any
spokes.Device.events(function(result)
{
var i;
if(result.isError)
{
console.log("Error polling for events: " + result.Err.Description);
}
else
{
//we have one or more events!
if(result.Result.length > 0)
{
i = 0;
//iterate over the events while we haven't hit the end of the array of events returned
while(i < result.Result.length)
{
//the meat of our logic. Do unique actions based on event received.
//See SessionCallState() in Spokes.js for a detailed list of events
console.log(result.Result[i].Event_Name);
i++; //increment counter so we're not stuck in an infinite loop
}
}
}
});
}, 2000); //repeat this loop every 2 seconds.
};
connectToSpokes();
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.