<s> from binascii import hexlify <EOL> from twisted . internet import reactor <EOL> from twisted . internet . defer import inlineCallbacks , maybeDeferred , returnValue <EOL> from twisted . internet . serialport import SerialPort <EOL> import smartanthill . network . protocol as sanp <EOL> from smartanthill . exception import NetworkRouterConnectFailure <EOL> from smartanthill . service import SAMultiService <EOL> from smartanthill . util import get_service_named <EOL> class ControlService ( SAMultiService ) : <EOL> def __init__ ( self , name ) : <EOL> SAMultiService . __init__ ( self , name ) <EOL> self . _protocol = sanp . ControlProtocolWrapping ( <EOL> self . climessage_protocallback ) <EOL> self . _litemq = None <EOL> def startService ( self ) : <EOL> self . _litemq = get_service_named ( "<STR_LIT>" ) <EOL> self . _protocol . makeConnection ( self ) <EOL> self . _litemq . consume ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> self . inmessage_mqcallback ) <EOL> self . _litemq . consume ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> self . outmessage_mqcallback ) <EOL> SAMultiService . startService ( self ) <EOL> def stopService ( self ) : <EOL> SAMultiService . stopService ( self ) <EOL> self . _litemq . unconsume ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . _litemq . unconsume ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> def write ( self , message ) : <EOL> self . _litemq . produce ( "<STR_LIT>" , "<STR_LIT>" , message , <EOL> dict ( binary = True ) ) <EOL> def inmessage_mqcallback ( self , message , properties ) : <EOL> self . log . debug ( "<STR_LIT>" % hexlify ( message ) ) <EOL> self . _protocol . dataReceived ( message ) <EOL> def outmessage_mqcallback ( self , message , properties ) : <EOL> self . log . debug ( "<STR_LIT>" % <EOL> ( message , properties ) ) <EOL> self . _protocol . send_message ( message ) <EOL> def climessage_protocallback ( self , message ) : <EOL> self . log . debug ( "<STR_LIT>" % message ) <EOL> self . _litemq . produce ( "<STR_LIT>" , "<STR_LIT>" , message ) <EOL> class TransportService ( SAMultiService ) : <EOL> def __init__ ( self , name ) : <EOL> SAMultiService . __init__ ( self , name ) <EOL> self . _protocol = sanp . TransportProtocolWrapping ( <EOL> self . rawmessage_protocallback ) <EOL> self . _litemq = None <EOL> def startService ( self ) : <EOL> self . _litemq = get_service_named ( "<STR_LIT>" ) <EOL> self . _protocol . makeConnection ( self ) <EOL> self . _litemq . consume ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> self . insegment_mqcallback ) <EOL> self . _litemq . consume ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> self . outmessage_mqcallback , ack = True ) <EOL> SAMultiService . startService ( self ) <EOL> def stopService ( self ) : <EOL> SAMultiService . stopService ( self ) <EOL> self . _litemq . unconsume ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . _litemq . unconsume ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> def rawmessage_protocallback ( self , message ) : <EOL> self . log . debug ( "<STR_LIT>" % hexlify ( message ) ) <EOL> self . _litemq . produce ( "<STR_LIT>" , "<STR_LIT>" , message , <EOL> dict ( binary = True ) ) <EOL> def write ( self , segment ) : <EOL> self . _litemq . produce ( "<STR_LIT>" , "<STR_LIT>" , segment , <EOL> dict ( binary = True ) ) <EOL> def insegment_mqcallback ( self , message , properties ) : <EOL> self . log . debug ( "<STR_LIT>" % hexlify ( message ) ) <EOL> self . _protocol . dataReceived ( message ) <EOL> @ inlineCallbacks <EOL> def outmessage_mqcallback ( self , message , properties ) : <EOL> self . log . debug ( "<STR_LIT>" % hexlify ( message ) ) <EOL> ctrlmsg = sanp . ControlProtocol . rawmessage_to_message ( message ) <EOL> def _on_err ( failure ) : <EOL> self . _litemq . produce ( "<STR_LIT>" , "<STR_LIT>" , ctrlmsg ) <EOL> failure . raiseException ( ) <EOL> d = maybeDeferred ( self . _protocol . send_message , message ) <EOL> d . addErrback ( _on_err ) <EOL> result = yield d <EOL> if result and ctrlmsg . ack : <EOL> self . _litemq . produce ( "<STR_LIT>" , "<STR_LIT>" , ctrlmsg ) <EOL> returnValue ( result ) <EOL> class RouterService ( SAMultiService ) : <EOL> RECONNECT_DELAY = <NUM_LIT:1> <EOL> def __init__ ( self , name , options ) : <EOL> SAMultiService . __init__ ( self , name , options ) <EOL> self . _protocol = sanp . RoutingProtocolWrapping ( <EOL> self . inpacket_protocallback ) <EOL> self . _router_device = None <EOL> self . _litemq = None <EOL> self . _reconnect_nums = <NUM_LIT:0> <EOL> self . _reconnect_callid = None <EOL> def startService ( self ) : <EOL> connection = self . options [ '<STR_LIT>' ] <EOL> try : <EOL> if connection . get_type ( ) == "<STR_LIT>" : <EOL> _kwargs = connection . params <EOL> _kwargs [ '<STR_LIT>' ] = self . _protocol <EOL> _kwargs [ '<STR_LIT>' ] = reactor <EOL> if "<STR_LIT:port>" in _kwargs : <EOL> _kwargs [ '<STR_LIT>' ] = _kwargs [ '<STR_LIT:port>' ] <EOL> del _kwargs [ '<STR_LIT:port>' ] <EOL> self . _router_device = SerialPort ( ** _kwargs ) <EOL> except : <EOL> self . log . error ( NetworkRouterConnectFailure ( self . options ) ) <EOL> self . _reconnect_nums += <NUM_LIT:1> <EOL> self . _reconnect_callid = reactor . callLater ( <EOL> self . _reconnect_nums * self . RECONNECT_DELAY , self . startService ) <EOL> return <EOL> self . _litemq = get_service_named ( "<STR_LIT>" ) <EOL> self . _litemq . consume ( <EOL> exchange = "<STR_LIT>" , <EOL> queue = "<STR_LIT>" + self . name , <EOL> routing_key = "<STR_LIT>" , <EOL> callback = self . outsegment_mqcallback <EOL> ) <EOL> SAMultiService . startService ( self ) <EOL> def stopService ( self ) : <EOL> SAMultiService . stopService ( self ) <EOL> if self . _reconnect_callid : <EOL> self . _reconnect_callid . cancel ( ) <EOL> if self . _router_device : <EOL> self . _router_device . loseConnection ( ) <EOL> if self . _litemq : <EOL> self . _litemq . unconsume ( "<STR_LIT>" , "<STR_LIT>" + self . name ) <EOL> def inpacket_protocallback ( self , packet ) : <EOL> self . log . debug ( "<STR_LIT>" % hexlify ( packet ) ) <EOL> self . _litemq . produce ( "<STR_LIT>" , "<STR_LIT>" , <EOL> sanp . RoutingProtocol . packet_to_segment ( packet ) , <EOL> dict ( binary = True ) ) <EOL> def outsegment_mqcallback ( self , message , properties ) : <EOL> if ord ( message [ <NUM_LIT:2> ] ) not in self . options [ '<STR_LIT>' ] : <EOL> return False <EOL> self . log . debug ( "<STR_LIT>" % hexlify ( message ) ) <EOL> self . _protocol . send_segment ( message ) <EOL> class ConnectionInfo ( object ) : <EOL> def __init__ ( self , uri ) : <EOL> assert "<STR_LIT::>" in uri <EOL> self . uri = uri <EOL> parts = uri . split ( "<STR_LIT::>" ) <EOL> self . type_ = parts [ <NUM_LIT:0> ] <EOL> self . params = dict ( ) <EOL> for part in parts [ <NUM_LIT:1> : ] : <EOL> key , value = part . split ( "<STR_LIT:=>" ) <EOL> self . params [ key ] = value <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % self . uri <EOL> def get_uri ( self ) : <EOL> return self . uri <EOL> def get_type ( self ) : <EOL> return self . type_ <EOL> class NetworkService ( SAMultiService ) : <EOL> def __init__ ( self , name , options ) : <EOL> SAMultiService . __init__ ( self , name , options ) <EOL> self . _litemq = None <EOL> def startService ( self ) : <EOL> self . _litemq = get_service_named ( "<STR_LIT>" ) <EOL> self . _litemq . declare_exchange ( "<STR_LIT>" ) <EOL> ControlService ( "<STR_LIT>" ) . setServiceParent ( self ) <EOL> TransportService ( "<STR_LIT>" ) . setServiceParent ( self ) <EOL> devices = get_service_named ( "<STR_LIT>" ) . get_devices ( ) <EOL> for devid , devobj in devices . iteritems ( ) : <EOL> netopts = devobj . options . get ( "<STR_LIT>" , { } ) <EOL> rconn = netopts . get ( "<STR_LIT>" , None ) <EOL> if not rconn : <EOL> continue <EOL> _options = { "<STR_LIT>" : ConnectionInfo ( rconn ) , <EOL> "<STR_LIT>" : [ devid ] } <EOL> RouterService ( "<STR_LIT>" % devid , <EOL> _options ) . setServiceParent ( self ) <EOL> SAMultiService . startService ( self ) <EOL> def stopService ( self ) : <EOL> SAMultiService . stopService ( self ) <EOL> self . _litemq . undeclare_exchange ( "<STR_LIT>" ) <EOL> def makeService ( name , options ) : <EOL> return NetworkService ( name , options ) </s>
