def set_difficulty(self, difficulty): if difficulty not in self.available_difficulties: logger.error( "Unknown difficulty '{}' (must be one of {}): skipping".format( difficulty, ", ".join(self.available_difficulties))) return for letter in self.letters: letter.difficulty = difficulty
def process_event(self, event): try: self.service.process_event(event) except Exception as e: formatted_exception = "{}: {}".format(type(e).__name__, e) try: self.protocol.send_log_error("Error while trying to process event={}: {}".format( event, formatted_exception)) except Exception: pass logger.error("Error while trying to process event={} ({})".format(event, formatted_exception))
def process_parse_exception(self, line, exception): formatted_exception = "{}: {}".format( type(exception).__name__, exception) try: self.service.factory.protocol.send_log_error( "Error while trying to forward arduino line={} ({})".format( line, formatted_exception)) except Exception: pass logger.error( "Error while trying to forward arduino line={} ({})".format( line, formatted_exception))
def connection_lost(self, reason): try: self.service.factory.protocol.send_log_error( "Connection lost with port {} (reason={})".format( self.port, reason)) except Exception: pass logger.error("Connection lost with port {} (reason={})".format( self.port, reason)) self.protocol = None reactor.callLater(self.reconnection_delay, self.connect)
def send_event(self, event): if self.protocol: self.protocol.send_event(event) else: try: self.service.factory.protocol.send_log_error( "Cannot send event {} to {} because the connection is not established: skipping" .format(self.port, event)) except Exception: pass logger.error( "Cannot send event {} to {} because the connection is not established: skipping" .format(self.port, event))
def process_event(self, event): logger.debug("Processing event '{}'".format(event)) if type(event) is not dict: logger.error("Unknown event: skipping") return category = self.get_category(event) method = self.get_method_from_category(category) self.check_arguments(event, method) self.pop_superfluous_fields(event, method) method(**event)
def connect(self): try: self.protocol = JSONSerialProtocol(self.process_event, self.process_parse_exception, self.connection_lost) SerialPort(self.protocol, self.port, reactor, baudrate=self.baud_rate) except Exception as e: reactor.callLater(self.reconnection_delay, self.connect) formatted_exception = "{}: {}".format(type(e).__name__, e) try: self.service.factory.protocol.send_log_error( "Error while trying to connect to serial on port {} ({})". format(self.port, formatted_exception)) except Exception: pass logger.error( "Error while trying to connect to serial on port {} ({})". format(self.port, formatted_exception))
def generate_success_sequence(self): logger.debug("Generating new success sequence") new_sequence = [] available_floppies = self.difficulties[ self.difficulty]["available_floppies"] if 6 > len(available_floppies): # The algorithm below might end up in a deadlock logger.error( "Please submit at least 6 available floppies: skipping sequence generation" ) return for i in range(5): valid_choice = False while not valid_choice: floppy = random.choice(available_floppies) if self.success_sequence[ i] != floppy and floppy not in new_sequence: new_sequence.append(floppy) valid_choice = True self.success_sequence = new_sequence logger.info("The new success sequence is {}".format( self.success_sequence))
def process_serial_event(self, event): logger.debug("Processing event '{}'".format(event)) if self.ARDUINO_PROTOCOL.EVENT_TYPE not in event: logger.error("Event has no event_type: skipping") return if event[self.ARDUINO_PROTOCOL. EVENT_TYPE] == self.ARDUINO_PROTOCOL.FLOPPY_READ: if self.ARDUINO_PROTOCOL.READER not in event: logger.error("Event has no reader: skipping") return if self.ARDUINO_PROTOCOL.TAG not in event: logger.error("Event has no value: skipping") return reader = event[self.ARDUINO_PROTOCOL.READER] tag = event[self.ARDUINO_PROTOCOL.TAG] self.on_rfid_read(reader, tag) elif event[self.ARDUINO_PROTOCOL. EVENT_TYPE] == self.ARDUINO_PROTOCOL.MODE_MANUAL: self.notify_manual_mode()
def clientConnectionFailed(self, connector, reason): logger.error('Connection failed. Reason: {}'.format(reason)) ReconnectingClientFactory.clientConnectionFailed(self, connector, reason)
def clientConnectionLost(self, connector, reason): logger.error('Connection lost. Reason: {}'.format(reason)) ReconnectingClientFactory.clientConnectionLost(self, connector, reason)
def process_serial_event(self, event): # Error because events should not be received from the arduino logger.error(event) self.send_event(event)