Click here to Skip to main content
15,895,799 members
Articles / Programming Languages / Javascript

Node.js - Spokes JavaScript On Your Desktop

7 Jan 2013CPOL5 min read 25.1K   91   5  
Node.js - Spokes JavaScript On Your Desktop.

This article is in the Product Showcase section for our sponsors at CodeProject. These articles are intended to provide you with information on products and services that we consider useful and of value to developers.

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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
Today’s smart devices and applications have untapped potential in the realm of context-aware computing. Plantronics is making it possible for its audio devices to deliver contextual information to a range of applications through Plantronics headsets. The Plantronics Spokes SDK allows developers to create a range of business applications that will have the power to change the way we communicate and collaborate.

Please check out our DevZone for more info on the Spokes SDK:
http://developer.plantronics.com/community/devzone

Comments and Discussions