def send_message(self, message): if not self.is_running: raise Exception("Client cannot connect to IPC server. Not running.") socket = QLocalSocket(self) socket.connectToServer(self.socket_filename, QIODevice.WriteOnly) if not socket.waitForConnected(self.timeout): raise Exception(str(socket.errorString())) socket.write(pickle.dumps(message)) if not socket.waitForBytesWritten(self.timeout): raise Exception(str(socket.errorString())) socket.disconnectFromServer()
def send_message(self, message): if not self.is_running: raise Exception( "Client cannot connect to IPC server. Not running.") socket = QLocalSocket(self) socket.connectToServer(self.socket_filename, QIODevice.WriteOnly) if not socket.waitForConnected(self.timeout): raise Exception(str(socket.errorString())) socket.write(pickle.dumps(message)) if not socket.waitForBytesWritten(self.timeout): raise Exception(str(socket.errorString())) socket.disconnectFromServer()
class DoorbotGUI(QWidget, Ui_BotGUI): def __init__(self): QWidget.__init__(self) self.setupUi(self) self.accessDialog = AccessDialog(self) self.accessDialog.hide() self.dispatchTable = { 'time' : self.respTime, 'schedule' : self.respSchedule, 'bulletin' : self.respBulletin, 'readstatus' : self.respReadStatus, 'access' : self.respAccess } self.timer = QTimer() self.timer.timeout.connect(self.tick) self.timer.start(1000) self.reconnectTimer = QTimer() self.reconnectTimer.timeout.connect(self.onReconnect) self.reconnectTimer.stop() self.socket = QLocalSocket() self.socket.connected.connect(self.onConnect) self.socket.disconnected.connect(self.onDisconnect) self.socket.readyRead.connect(self.onReadyRead) self.socket.error.connect(self.onError) self.socket.connectToServer('doorbotgui') self.setCursor(Qt.BlankCursor) self.showFullScreen() self.show() def tick(self): pass def onReconnect(self): self.reconnectTimer.stop() self.labelHealth.setText('Reconnecting to backend.') self.socket.connectToServer('doorbotgui') def onConnect(self): self.labelHealth.setText('Connected to backend.') def onDisconnect(self): self.labelHealth.setText('Disconnected from backend.') self.reconnectTimer.start(2000) def onError(self, err): self.labelHealth.setText('Backend connect error ' + self.socket.errorString() + '.' ) self.reconnectTimer.start(2000) def onReadyRead(self): while self.socket.canReadLine(): rx = self.socket.readLine(2048).decode('utf-8') try: pkt = json.loads(rx) print(pkt) cmd = pkt['cmd'] if cmd in self.dispatchTable: self.dispatchTable[cmd](pkt) except ValueError: print('could not decode json') except: print('could not dispatch: %s', sys.exc_info()) def respTime(self, pkt): self.labelTime.setText(pkt['time']) def respSchedule(self, pkt): desc = pkt['description'] self.labelSchedule.setText(desc) def respBulletin(self, pkt): source = pkt['source'] self.textBrowser.setSource(QUrl(source)) self.textBrowser.reload() def respReadStatus(self, pkt): status = pkt['status'] if status == 'Status.INIT': statusText = 'Initializing System.' elif status == 'Status.READY': statusText = 'Please scan your tag below.' elif status == 'Status.READING': statusText = 'Reading tag...' elif status == 'Status.DENIED': statusText = 'Access denied.' elif status == 'Status.ALLOWED' or status == 'Status.LATCHED': statusText = 'Access granted.' elif status == 'Status.UNKNOWN': statusText = 'Tag not recognized.' elif status == 'Status.ERROR': statusText = 'An unexpected error has occurred. Contact [email protected].' else: statusText = 'Unexpected status: %s' % status self.labelReadStatus.setText(statusText) def respAccess(self, pkt): result = pkt['result'] member = pkt['member'] self.accessDialog.memberAccess(result, member)
class DoorbotGUI(QWidget, Ui_BotGUI): def __init__(self): QWidget.__init__(self) self.setupUi(self) self.accessDialog = AccessDialog(self) self.accessDialog.hide() self.dispatchTable = { 'time': self.respTime, 'schedule': self.respSchedule, 'bulletin': self.respBulletin, 'readstatus': self.respReadStatus, 'access': self.respAccess } self.timer = QTimer() self.timer.timeout.connect(self.tick) self.timer.start(1000) self.reconnectTimer = QTimer() self.reconnectTimer.timeout.connect(self.onReconnect) self.reconnectTimer.stop() self.socket = QLocalSocket() self.socket.connected.connect(self.onConnect) self.socket.disconnected.connect(self.onDisconnect) self.socket.readyRead.connect(self.onReadyRead) self.socket.error.connect(self.onError) self.socket.connectToServer('doorbotgui') self.setCursor(Qt.BlankCursor) self.showFullScreen() self.show() def tick(self): pass def onReconnect(self): self.reconnectTimer.stop() self.labelHealth.setText('Reconnecting to backend.') self.socket.connectToServer('doorbotgui') def onConnect(self): self.labelHealth.setText('Connected to backend.') def onDisconnect(self): self.labelHealth.setText('Disconnected from backend.') self.reconnectTimer.start(2000) def onError(self, err): self.labelHealth.setText('Backend connect error ' + self.socket.errorString() + '.') self.reconnectTimer.start(2000) def onReadyRead(self): while self.socket.canReadLine(): rx = self.socket.readLine(2048).decode('utf-8') try: pkt = json.loads(rx) print(pkt) cmd = pkt['cmd'] if cmd in self.dispatchTable: self.dispatchTable[cmd](pkt) except ValueError: print('could not decode json') except: print('could not dispatch: %s', sys.exc_info()) def respTime(self, pkt): self.labelTime.setText(pkt['time']) def respSchedule(self, pkt): desc = pkt['description'] self.labelSchedule.setText(desc) def respBulletin(self, pkt): source = pkt['source'] self.textBrowser.setSource(QUrl(source)) self.textBrowser.reload() def respReadStatus(self, pkt): status = pkt['status'] if status == 'Status.INIT': statusText = 'Initializing System.' elif status == 'Status.READY': statusText = 'Please scan your tag below.' elif status == 'Status.READING': statusText = 'Reading tag...' elif status == 'Status.DENIED': statusText = 'Access denied.' elif status == 'Status.ALLOWED' or status == 'Status.LATCHED': statusText = 'Access granted.' elif status == 'Status.UNKNOWN': statusText = 'Tag not recognized.' elif status == 'Status.ERROR': statusText = 'An unexpected error has occurred. Contact [email protected].' else: statusText = 'Unexpected status: %s' % status self.labelReadStatus.setText(statusText) def respAccess(self, pkt): result = pkt['result'] member = pkt['member'] self.accessDialog.memberAccess(result, member)