Callback Server¶
An asynchronous client that listens to messages broadcast by the server.
The client also accepts callback functions.
The client subclasses python threading so methods are built-in to the class object.
Callbacks can be configured in two different ways:
- Using decorators
- Using the ‘add_callback’ method
Decorators
squeeze = LMSCallbackServer()
@squeeze.event(squeeze.VOLUME_CHANGE)
def volume_event(event=None):
print "Volume event received: {}".format(event)
squeeze.set_server("192.168.0.1")
squeeze.start()
If you are using decorators inside a class then this will happen before your class has been initialised so you need to provide the callback server with a reference to the class instance.
squeeze = LMSCallbackServer()
class MyClass(object):
def __init__(self):
self.squeeze = squeeze
self.squeeze.set_server("192.168.0.1", parent_class=self)
self.squeeze.start()
@squeeze.event(squeeze.VOLUME_CHANGE)
def volume_event(self, event=None):
print "Volume event received: {}".format(event)
Multiple events can be added with multiple decorators
@squeeze.event(squeeze.VOLUME_CHANGE)
@squeeze.event(squeeze.PLAY_PAUSE)
def generic_event(event=None):
print "Event received: {}".format(event)
Or by passing events as a list
@squeeze.event([squeeze.VOLUME_CHANGE, squeeze.PLAY_PAUSE])
def generic_event(event=None):
print "Event received: {}".format(event)
Using ‘add_callback’ method
def volume_event(event=None):
print "Volume event received: {}".format(event)
squeeze = LMSCallbackServer("192.168.0.1")
squeeze.add_callback(squeeze.VOLUME_CHANGE, volume_event)
squeeze.start()
-
exception
LMSTools.callbackserver.
CallbackServerError
¶
-
class
LMSTools.callbackserver.
LMSCallbackServer
(hostname=None, port=9090, username='', password='')¶ Parameters: - hostname (str) – (optional) ip address/name of the server (excluding “http://” prefix)
- port (int) – (optional) port on which the telent interface is running (default 9090)
- username (str) – (optional) username for access on telnet port
- password (str) – (optional) password for access on telnet port
If the class is initialised without the hostname parameter then the “set_server” method must be called before starting the server otherwise a CallbackServerError will be raised.
Events
The following events are currently define in the class.
Const MIXER_ALL: Captures all mixer events Const VOLUME_CHANGE: Captures volume events Const PLAYLIST_ALL: Captures all playlist events Const PLAY_PAUSE: Captures play/pause events Const PLAY: Captures play event Const PAUSE: Captures pause event Const PLAYLIST_OPEN: Captures playlist open event Const PLAYLIST_CHANGE_TRACK: Captures track changes Const PLAYLIST_LOAD_TRACKS: Captures loadtracks event Const PLAYLIST_ADD_TRACKS: Captures addtracks event Const PLAYLIST_LOADED: Captures “playlist load_done” event Const PLAYLIST_REMOVE: Captures “playlist delete” event Const PLAYLIST_CLEAR: Captures playlist clear event Const PLAYLIST_CHANGED: Captures PLAYLIST_LOAD_TRACKS, PLAYLIST_LOADED, PLAYLIST_ADD_TRACKS, PLAYLIST_REMOVE, PLAYLIST_CLEAR Const CLIENT_ALL: Captures all client events Const CLIENT_NEW: Captures new client events Const CLIENT_DISCONNECT: Captures client disconnect events Const CLIENT_RECONNECT: Captures client reconnect events Const CLIENT_FORGET: Captures client forget events Const SYNC: Captures sync events Const SERVER_ERROR: Custom event for server errors Const SERVER_CONNECT: Custom event for server connection -
add_callback
(event, callback)¶ Define a callback.
Parameters: - event (event) – Event type
- callback (function/method) – Reference to the function/method to be called if matching event is received. The function/method must accept one parmeter which is the event string.
-
remove_callback
(event)¶ Remove a callback.
Parameters: event (event) – Event type
-
run
()¶ Method representing the thread’s activity.
You may override this method in a subclass. The standard run() method invokes the callable object passed to the object’s constructor as the target argument, if any, with sequential and keyword arguments taken from the args and kwargs arguments, respectively.
-
set_server
(hostname, port=9090, username='', password='', parent_class=None)¶ Parameters: - hostname (str) – (required) ip address/name of the server (excluding “http://” prefix)
- port (int) – (optional) port on which the telent interface is running (default 9090)
- username (str) – (optional) username for access on telnet port
- password (str) – (optional) password for access on telnet port
- parent_class (object) – (optional) reference to a class instance. Required where decorators have been used on class methods prior to initialising the class.
Provide details of the server if not provided when the class is initialised (e.g. if you are using decorators to define callbacks).
-
stop
()¶ Stop the callack server thread.