def __init__(self, wsPort): self._irTrans = RpiIRTrans(self, 18, 25, 17, 38000) self._irTrans.register_Encoder("DAIKIN", DaikinCode()) self._log = None self._wsServer = BroadcastServer(wsPort, self.cb_ServerWS, self._log) # demarre le websocket server self._run()
class RpiTransceiver(): def __init__(self, wsPort): self._irTrans = RpiIRTrans(self, 18, 25, 17, 38000) self._irTrans.register_Encoder("DAIKIN", DaikinCode()) self._log = None self._wsServer = BroadcastServer(wsPort, self.cb_ServerWS, self._log) # demarre le websocket server self._run() def cb_ServerWS(self, message): """Callback en provenance d'un client via server Websocket (resquest avec ou sans ack)""" blockAck = False report = {'error': 'Message not handle.'} ackMsg = {} erAck = '' self._irTrans.getState() print "WS - Client Request", message if message.has_key('header') : if message['header']['type'] in ('req', 'req-ack'): if message['request'] == 'server-hbeat' : report['error'] ='' elif message['request'] == 'sendIRCode' : erAck = "IR emitter don't confirm final reception." report = self._irTrans.sendIRCode(message['encoder'], message['datatype'], message['code']) elif message['request'] == 'getMemIRCode' : erAck = 'Fail to get IR Code in memory.' report = self._irTrans.getMemIRcode() elif message['request'] == 'setTolerances' : erAck = 'Fail to set tolerances.' report = self._irTrans.setTolerances(message['encoder'], message['tolerances']) elif message['request'] == 'getTolerances' : erAck = 'Fail to get tolerances.' report = self._irTrans.getTolerances(message['encoder']) elif message['request'] == 'getState' : erAck = 'Fail to get state.' report = self._irTrans.getState() else : erAck = 'Client request Fail.' report['error'] ='Unknown request.' print "commande inconnue" if message['header']['type'] == 'req-ack' and not blockAck : ackMsg['header'] = {'type': 'ack', 'idws' : message['header']['idws'], 'idmsg' : message['header']['idmsg'], 'ip' : message['header']['ip'] , 'timestamp' : long(time.time()*100)} ackMsg['request'] = message['request'] if report : if report['error'] != '': ackMsg['error'] = erAck else : ackMsg['error'] = '' ackMsg['data'] = report else : ackMsg['error'] = 'No data report.' self._wsServer.sendAck(ackMsg) else : raise RpiTransceiverException("WS request bad format : {0}".format(message)) def _run(self): print "Manager Started" try: while 1 : time.sleep(1) finally: # when you CTRL+C exit, we clean up self._irTrans.close() self._wsServer.close() print" *** Clean up exit :)" def sendToWSClients(self, type, message): msg = {"host" : os.uname()[1], 'type': type, 'data': message} print "message to clients : {0}".format(msg) if self._wsServer : self._wsServer.broadcastMessage(msg)