Spokes offers different ways to use its API and there are some interesting thing that can be done to get to know your device.
Python is my language of choice when 'poking around' to see see what an API can generate based on different inputs. This helps me to come up to speed and start leveraging its full potential.
With Spokes it is the same. You can also use a language that offers an interactive approach to play with its API.
In this example below, I connect to Spokes via its REST API by simply using the httplib and json modules from Python 2.7.3:
import httplib
import json
class PLTDevice:
def __init__(self, spokes, uid):
self.DeviceInfoURL = '/Spokes/DeviceServices/'+uid+'/Info'
self.AttachURL = '/Spokes/DeviceServices/'+uid+'/Attach'
self.ReleaseURL = '/Spokes/DeviceServices/'+uid+'/Release'
self.attached = False
self.session = None
self.uid = uid
self.spokes = spokes
self.spokes.conn.request('GET', self.DeviceInfoURL);
r = self.spokes.conn.getresponse()
if r.status == 200:
response = r.read()
response = json.loads(response)
self.device = response['Result']
def __getattr__(self, name):
if name in self.device.keys():
return self.device[name]
def attach(self):
self.spokes.conn.request('GET', self.AttachURL);
r = self.spokes.conn.getresponse()
if r.status == 200:
response = r.read()
response = json.loads(response)
if response['Err']==None:
self.attached = True
self.session = response['Result']
def release(self):
self.spokes.conn.request('GET', self.ReleaseURL);
r = self.spokes.conn.getresponse()
if r.status == 200:
response = r.read()
response = json.loads(response)
if response['Err']==None:
self.attached = False
self.session = None
def get_events(self, queue=127):
eventsURL = '/Spokes/DeviceServices/'+self.session+'/Events?queue='+str(queue)
self.spokes.conn.request('GET', eventsURL);
r = self.spokes.conn.getresponse()
if r.status == 200:
response = r.read()
response = json.loads(response)
print response
class Spokes:
def __init__(self):
self.host = '127.0.0.1'
self.port = '32001'
self.conn = httplib.HTTPConnection(self.host+":"+self.port)
self.DevicesListURL = '/Spokes/DeviceServices/DeviceList'
self.devices=[]
def get_device_list(self):
self.conn.request('GET', self.DevicesListURL);
r = self.conn.getresponse()
if r.status == 200:
response = r.read()
response = json.loads(response)
if response['Err']==None:
for d in response['Result']:
self.devices.append(PLTDevice(self, d['Uid']))
return self.devices
This is by no means a full solution, it is simply a quick and dirty way to invoke the most basic APIs from the REST interface (I've added close to no error checks). It does show how one could interact with Spokes in an interactive way to learn the device events and how to use them.
Now, after loading the code above and switching to the interactive mode:
>>> s=Spokes()
>>> print s
<__main__.Spokes instance at 0x029FDFD0>
>>> print s.devices
[]
>>> dl = s.get_device_list()
>>> print len(dl)
1
>>> d = dl[0]
>>> print d.attached
False
>>> d.attach()
>>> print d.attached
True
>>> d.get_events()
{u'Description': u'', u'Err': None, u'Type_Name': u'DeviceEventArray', u'Result': [{u'Event_Log_Type_Name': u'HeadsetStateChange', u'Event_Id': 14, u'Timestamp': 634939487913969843L, u'Age': 10853, u'Event_Name': u'Doff', u'Event_Log_Type_Id': 2}, {u'Event_Log_Type_Name': u'HeadsetStateChange', u'Event_Id': 13, u'Timestamp': 634939487969032861L, u'Age': 5347, u'Event_Name': u'Don', u'Event_Log_Type_Id': 2}], u'Type': 6, u'isError': False}
Note that since I didn't continue implementing the code for the events and other APIs, the event list is displayed as it was returned. I simply parse the JSON result and print it in get_events()
:
I specially like Python for its flexibility on accessing the values on its data structures as demonstrated above in PLTDevice.__getattr__
. ProductName
is not defined as a member in the class, but still gets accessed as one. Its content comes from the response we received from Spokes.
>>> print d.ProductName
Plantronics BT300
From here you could start building your app to actually monitor the events queue and act on them, or keep on looking around in interactive mode to see what else you can do with the device and APIs.
This approach usually works well for me on troubleshooting REST APIs and I hope it helps you as well.
Just another way of doing it.
This article was written by Ricardo de Andrade. Ricardo is a Systems Architect and Evangelist at Plantronics helping the developer community, clients and partners with the Spokes SDK and building solutions around current and future offerings. Ricardo has an extensive background in software and cloud distributed architectures, and in specific telecommunications. Ricardo previously worked for Microsoft where he helped clients and partners to develop cloud based speech recognition applications and integrate their web services into the Microsoft Tellme services.