<s> '''<STR_LIT>''' <EOL> from RESTfulResource import RESTfulResource <EOL> from urlparse import urlparse <EOL> import json <EOL> import httplib <EOL> import mosquitto <EOL> class Observer ( RESTfulResource ) : <EOL> def __init__ ( self , parentObject = None , resourceDescriptor = { } ) : <EOL> RESTfulResource . __init__ ( self , parentObject , resourceDescriptor ) <EOL> self . _settings = self . _resourceDescriptor <EOL> self . _baseObject = self . resources [ '<STR_LIT>' ] <EOL> self . _linkBaseDict = self . resources [ '<STR_LIT>' ] . resources <EOL> self . _thisURI = self . resources [ '<STR_LIT>' ] . Properties . get ( '<STR_LIT>' ) + self . resources [ '<STR_LIT>' ] . resources [ '<STR_LIT>' ] . Properties . get ( '<STR_LIT>' ) <EOL> self . _settings . update ( { '<STR_LIT>' : self . _thisURI } ) <EOL> self . _init ( ) <EOL> def _init ( self ) : <EOL> pass <EOL> def _updateSettings ( self ) : <EOL> pass <EOL> def get ( self , Key = None ) : <EOL> if Key != None : <EOL> return self . _settings [ Key ] <EOL> else : <EOL> return self . _settings <EOL> def set ( self , newSettings ) : <EOL> self . _settings . update ( newSettings ) <EOL> self . _updateSettings ( ) <EOL> def notify ( self , resource ) : <EOL> self . _notify ( resource ) <EOL> def _notify ( self , resource ) : <EOL> pass <EOL> def linkToRef ( self , linkPath ) : <EOL> '''<STR_LIT>''' <EOL> self . _currentDict = self . _linkBaseDict <EOL> self . _pathElements = linkPath . split ( '<STR_LIT:/>' ) <EOL> for pathElement in self . _pathElements [ : - <NUM_LIT:1> ] : <EOL> if len ( pathElement ) > <NUM_LIT:0> : <EOL> self . _currentDict = self . _currentDict [ pathElement ] . resources <EOL> self . _resource = self . _currentDict [ self . _pathElements [ - <NUM_LIT:1> ] ] <EOL> return self . _resource <EOL> class httpPublisher ( Observer ) : <EOL> def _notify ( self , resource ) : <EOL> self . _jsonObject = json . dumps ( resource . get ( ) ) <EOL> self . _uriObject = urlparse ( self . _settings [ '<STR_LIT>' ] ) <EOL> self . _httpConnection = httplib . HTTPConnection ( self . _uriObject . netloc ) <EOL> self . _httpConnection . request ( '<STR_LIT>' , self . _uriObject . path , self . _jsonObject , { "<STR_LIT:Content-Type>" : "<STR_LIT:application/json>" } ) <EOL> self . _httpConnection . getresponse ( ) <EOL> return <EOL> class callbackNotifier ( Observer ) : <EOL> def _notify ( self , resource = None ) : <EOL> self . linkToRef ( urlparse ( self . _settings [ '<STR_LIT>' ] ) . path ) . handleNotify ( resource ) <EOL> return <EOL> class httpSubscriber ( Observer ) : <EOL> def _init ( self ) : <EOL> self . _observerSettings = { } <EOL> if '<STR_LIT>' in self . _settings : <EOL> self . _createRemoteObserver ( ) <EOL> def _updateSettings ( self ) : <EOL> if self . _settings . has_key ( '<STR_LIT>' ) : <EOL> if not '<STR_LIT>' in self . _observerSettings : <EOL> self . _createRemoteObserver ( ) <EOL> def _createRemoteObserver ( self ) : <EOL> self . _thisURI = self . _settings [ '<STR_LIT>' ] <EOL> self . _observerURI = self . _settings [ '<STR_LIT>' ] <EOL> self . _observerName = self . _settings [ '<STR_LIT>' ] <EOL> self . _observerDescriptor = { '<STR_LIT>' : self . _observerName , '<STR_LIT>' : '<STR_LIT>' } <EOL> self . _observerSettings = { '<STR_LIT>' : self . _thisURI } <EOL> self . _jsonHeader = { "<STR_LIT:Content-Type>" : "<STR_LIT:application/json>" } <EOL> self . _uriObject = urlparse ( self . _observerURI ) <EOL> self . _httpConnection = httplib . HTTPConnection ( self . _uriObject . netloc ) <EOL> self . _httpConnection . request ( '<STR_LIT:POST>' , self . _uriObject . path + '<STR_LIT>' , json . dumps ( self . _observerDescriptor ) , self . _jsonHeader ) <EOL> self . _httpConnection . getresponse ( ) <EOL> self . _httpConnection . request ( '<STR_LIT>' , self . _uriObject . path + '<STR_LIT>' + '<STR_LIT:/>' + self . _observerName , json . dumps ( self . _observerSettings ) , self . _jsonHeader ) <EOL> self . _httpConnection . getresponse ( ) <EOL> return <EOL> class xivelyPublisher ( Observer ) : <EOL> def _init ( self ) : <EOL> self . _apiPath = self . _settings [ '<STR_LIT>' ] + '<STR_LIT:/>' + self . _settings [ '<STR_LIT>' ] + '<STR_LIT>' <EOL> self . _uriObject = urlparse ( self . _apiPath ) <EOL> self . _requestHeader = { '<STR_LIT:Content-Type>' : '<STR_LIT:application/json>' , '<STR_LIT>' : self . _settings [ '<STR_LIT>' ] } <EOL> self . _streamBody = { '<STR_LIT:id>' : self . _settings [ '<STR_LIT>' ] , '<STR_LIT>' : <NUM_LIT:0> } <EOL> self . _requestBody = { '<STR_LIT:version>' : '<STR_LIT>' , '<STR_LIT>' : [ self . _streamBody ] } <EOL> if '<STR_LIT>' in self . _settings : <EOL> self . _updateInterval = self . _settings [ '<STR_LIT>' ] <EOL> else : <EOL> self . _updateInterval = <NUM_LIT:1> <EOL> self . _nextUpdateDelay = <NUM_LIT:1> <EOL> def _notify ( self , resource = None ) : <EOL> self . _nextUpdateDelay -= <NUM_LIT:1> <EOL> if not self . _nextUpdateDelay : <EOL> self . _nextUpdateDelay = self . _updateInterval <EOL> self . _streamBody . update ( { '<STR_LIT>' : resource . get ( ) } ) <EOL> self . _httpConnection = httplib . HTTPConnection ( self . _uriObject . netloc ) <EOL> self . _httpConnection . request ( '<STR_LIT>' , self . _uriObject . path , json . dumps ( self . _requestBody ) , self . _requestHeader ) <EOL> self . _httpConnection . getresponse ( ) <EOL> class mqttObserver ( Observer ) : <EOL> def _init ( self ) : <EOL> if not '<STR_LIT>' in self . _settings : <EOL> self . _settings . update ( { '<STR_LIT>' : '<STR_LIT:localhost>' } ) <EOL> self . _connection = self . _settings [ '<STR_LIT>' ] <EOL> if '<STR_LIT::>' in self . _connection : <EOL> self . _host , self . _port = self . _connection . split ( '<STR_LIT::>' ) <EOL> self . _port = int ( self . _port ) <EOL> else : <EOL> self . _host = self . _connection <EOL> self . _port = <NUM_LIT> <EOL> self . _objectPath = self . resources [ '<STR_LIT>' ] . resources [ '<STR_LIT>' ] . Properties . get ( '<STR_LIT>' ) <EOL> self . _observableProperty = self . resources [ '<STR_LIT>' ] . resources [ '<STR_LIT>' ] <EOL> if not '<STR_LIT>' in self . _settings : <EOL> self . _settings . update ( { '<STR_LIT>' : self . _objectPath } ) <EOL> self . _subTopic = self . _settings [ '<STR_LIT>' ] <EOL> if not '<STR_LIT>' in self . _settings : <EOL> self . _settings . update ( { '<STR_LIT>' : self . _objectPath } ) <EOL> self . _pubTopic = self . _settings [ '<STR_LIT>' ] <EOL> if not '<STR_LIT>' in self . _settings : <EOL> self . _settings . update ( { '<STR_LIT>' : <NUM_LIT> } ) <EOL> self . _keepAlive = self . _settings [ '<STR_LIT>' ] <EOL> if not '<STR_LIT>' in self . _settings : <EOL> self . _settings . update ( { '<STR_LIT>' : <NUM_LIT:0> } ) <EOL> self . _QoS = self . _settings [ '<STR_LIT>' ] <EOL> self . _pubs = { } <EOL> self . _connected = False <EOL> self . _subscribed = False <EOL> self . _waitConnack = False <EOL> self . _waitSuback = False <EOL> self . _waitPuback = False <EOL> self . _updating = False <EOL> def on_connect ( mosq , obj , rc ) : <EOL> print ( "<STR_LIT>" + str ( rc ) ) <EOL> self . _waitConnack = False <EOL> self . _connected = True <EOL> def on_message ( mosq , obj , msg ) : <EOL> print ( "<STR_LIT>" + msg . topic + "<STR_LIT:U+0020>" + str ( msg . qos ) + "<STR_LIT:U+0020>" + str ( msg . payload ) ) <EOL> if self . _subTopic not in self . _pubs : <EOL> self . _updating = True <EOL> self . _observableProperty . set ( json . loads ( msg . payload ) ) <EOL> self . _updating = False <EOL> else : <EOL> self . _pubs . pop ( self . _subTopic ) <EOL> def on_publish ( mosq , obj , mid ) : <EOL> print ( "<STR_LIT>" + str ( mid ) ) <EOL> self . _waitPuback = False <EOL> def on_subscribe ( mosq , obj , mid , granted_qos ) : <EOL> print ( "<STR_LIT>" + str ( mid ) + "<STR_LIT:U+0020>" + str ( granted_qos ) ) <EOL> self . _waitSuback = False <EOL> self . _subscribed = True <EOL> def on_log ( mosq , obj , level , string ) : <EOL> print ( string ) <EOL> self . _mqttc = mosquitto . Mosquitto ( ) <EOL> self . _mqttc . on_message = on_message <EOL> self . _mqttc . on_connect = on_connect <EOL> self . _mqttc . on_publish = on_publish <EOL> self . _mqttc . on_subscribe = on_subscribe <EOL> self . _mqttc . loop_start ( ) <EOL> self . _waitConnack = True <EOL> self . _mqttc . connect ( self . _host , self . _port , self . _keepAlive ) <EOL> while self . _waitConnack : pass <EOL> if not self . _subTopic == '<STR_LIT>' : <EOL> self . _waitSuback = True <EOL> self . _mqttc . subscribe ( self . _subTopic , self . _QoS ) <EOL> while self . _waitSuback : pass <EOL> def _notify ( self , resource ) : <EOL> if not self . _pubTopic == '<STR_LIT>' : <EOL> if not ( self . _updating and ( self . _pubTopic == self . _subTopic ) ) : <EOL> if self . _pubTopic == self . _subTopic : <EOL> self . _pubs . update ( { self . _pubTopic : None } ) <EOL> self . _waitPuback = True <EOL> self . _mqttc . publish ( self . _pubTopic , json . dumps ( resource . get ( ) ) , self . _QoS ) <EOL> while self . _waitPuback : pass <EOL> class coapNotifier ( Observer ) : <EOL> def _init ( self ) : <EOL> self . _client = None <EOL> self . _token = None <EOL> self . _seqNo = None <EOL> self . _maxAge = None <EOL> pass <EOL> def _notify ( self , resource ) : <EOL> pass <EOL> def delete ( self ) : <EOL> pass <EOL> class Observers ( RESTfulResource ) : <EOL> def __init__ ( self , parentObject = None , resourceDescriptor = { } ) : <EOL> RESTfulResource . __init__ ( self , parentObject , resourceDescriptor ) <EOL> self . _observers = { } <EOL> def onUpdate ( self , resource ) : <EOL> self . _onUpdate ( resource ) <EOL> def _onUpdate ( self , resource ) : <EOL> for observer in self . _observers : <EOL> self . _observers [ observer ] . notify ( resource ) <EOL> def get ( self , Key = None ) : <EOL> if Key == None : <EOL> return self . _observers . keys ( ) <EOL> return self . _observer [ Key ] <EOL> def set ( self ) : <EOL> pass <EOL> def create ( self , resourceDescriptor ) : <EOL> resourceName = resourceDescriptor [ '<STR_LIT>' ] <EOL> resourceClass = resourceDescriptor [ '<STR_LIT>' ] <EOL> if resourceName not in self . resources : <EOL> self . resources . update ( { resourceName : globals ( ) [ resourceClass ] ( self , resourceDescriptor ) } ) <EOL> self . _observers . update ( { resourceName : self . resources [ resourceName ] } ) <EOL> return self . resources [ resourceName ] <EOL> def delete ( self , observerName ) : <EOL> if observerName in self . _observers . keys ( ) : <EOL> self . _observers . remove ( observerName ) <EOL> self . resources . remove ( observerName ) <EOL> return observerName <EOL> return None </s>
