def change_status(self): """Manually change the status of the BITS system""" with session_scope() as session: curstatus = query.get_current_status(session) if curstatus is None: textstatus = Status.CLOSED else: textstatus = Status.OPEN if curstatus.value == Status.CLOSED else Status.CLOSED LOG.info('Change of BITS to status={}'.format(textstatus) + ' from web interface.') message = '' try: status = query.log_status(session, textstatus, 'web') broadcast(status.jsondict()) notifier.send_status(textstatus) message = "Ora la sede è {}.".format(textstatus) except IntegrityError: LOG.error("Status changed too quickly, not logged.") message = "Errore: modifica troppo veloce!" raise finally: self.render('templates/admin.html', page_message=message)
def change_status(self): """Manually change the status of the BITS system""" with session_scope() as session: curstatus = query.get_current_status(session) if curstatus is None: textstatus = Status.CLOSED else: textstatus = Status.OPEN if curstatus.value == Status.CLOSED else Status.CLOSED LOG.info('Change of BITS to status=%r from web interface.', textstatus) message = '' try: status = query.log_status(session, textstatus, 'web') broadcast(status.jsondict()) notifier.send_status(textstatus) message = "Ora la sede è {}.".format(textstatus) except IntegrityError: LOG.error("Status changed too quickly, not logged.") message = "Errore: modifica troppo veloce!" raise finally: self.render( 'templates/admin.html', page_message=message, roster=MACUpdateHandler.ROSTER )
def handle_command(self, command): """Reacts to received commands (callback). Will separate args and call appropriate handlers.""" # Meanwhile, go on with commands... RemoteListener.STREAM.read_until(b'\n', self.handle_command) command = command.strip('\n') if command: args = command.split(b' ') action = args[0] try: handler = RemoteListener.ACTIONS[action] except KeyError: LOG.warning('Remote received unknown command `%s`', args) else: # Execute handler (index 0) with args (index 1->end) try: handler(*args[1:]) except TypeError: LOG.error( 'Command `%s` called with wrong number of args', action ) else: LOG.warning('Remote received empty command.')
def send(string): if RemoteListener.STREAM is None: LOG.error("No Fonera connected! Not sending %r", string) return try: RemoteListener.STREAM.write(string) except StreamClosedError as error: LOG.error('Could not push message to Fonera! %s', error)
def handle_enter_command(userid): """Handles signal triggered when a new user enters.""" LOG.info('Received enter command: id={}'.format(userid)) try: userid = int(userid) except ValueError: LOG.error('Wrong type for parameters in temperature command!') return LOG.error('handle_enter_command not implemented.')
def handle_leave_command(userid): """Handles signal triggered when a known user leaves.""" LOG.info('Received leave command: id={}'.format(userid)) try: userid = int(userid) except ValueError: LOG.error('Wrong type for parameters in temperature command!') return LOG.error('handle_leave_command not implemented.')
def handle_leave_command(userid): """Handles signal triggered when a known user leaves.""" LOG.info('Received leave command: id=%r', userid) try: userid = int(userid) except ValueError: LOG.error('Wrong type for parameters in temperature command!') return LOG.error('handle_leave_command not implemented.')
def handle_sound_command(soundid): """Handles requests to play a sound.""" LOG.info('Received sound command: id={}'.format(soundid)) try: soundid = int(soundid) except ValueError: LOG.error('Wrong type for parameters in temperature command!') return else: notifier.send_sound(soundid)
def handle_sound_command(soundid): """Handles requests to play a sound.""" LOG.info('Received sound command: id=%r', soundid) try: soundid = int(soundid) except ValueError: LOG.error('Wrong type for parameters in temperature command!') return else: notifier.send_sound(soundid)
def handle_temperature_command(sensorid, value): """Receives and log data received from remote sensor.""" LOG.info('Received temperature: sensorid=%r, value=%r', sensorid, value) try: sensorid = int(sensorid) value = float(value) except ValueError: LOG.error('Wrong type for parameters in temperature command!') return with session_scope() as session: temp = query.log_temperature(session, value, sensorid, 'BITS') broadcast(temp.jsondict())
def handle_temperature_command(sensorid, value): """Receives and log data received from remote sensor.""" LOG.info('Received temperature: sensorid={}, value={}'.format( sensorid, value)) try: sensorid = int(sensorid) value = float(value) except ValueError: LOG.error('Wrong type for parameters in temperature command!') return with session_scope() as session: temp = query.log_temperature(session, value, sensorid, 'BITS') broadcast(temp.jsondict())
def session_scope(): """Provide a transactional scope around a series of operations.""" session = Session() try: yield session session.commit() except IntegrityError as e: LOG.error("Integrity error in DB, rolling back: {}".format(e)) session.rollback() except: LOG.error("Error in DB, rolling back.") session.rollback() raise finally: session.close()
def handle_stream(self, stream, address): """Handles inbound TCP connections asynchronously.""" LOG.info("New connection from Fonera.") if address[0] != options.control_remote_address: LOG.error( "Connection from `%s`, expected from `%s`. Ignoring.", address, options.control_remote_address ) return if RemoteListener.STREAM is not None: LOG.warning("Another connection was open, closing the previous one.") RemoteListener.STREAM.close() RemoteListener.STREAM = stream RemoteListener.STREAM.read_until(b'\n', self.handle_command)
def handle_message_command(message): """Handles message broadcast requests.""" LOG.info('Received message command: message=%r', message) try: decodedmex = base64.b64decode(message) except TypeError: LOG.error('Received message is not valid base64: %r', message) else: text = decodedmex.decode('utf8') #FIXME maybe get author ID from message? user = "******" with session_scope() as session: user = query.get_user(session, user) if not user: LOG.error("Non-existent user %r, not logging message.", user) return message = query.log_message(session, user, text) broadcast(message.jsondict()) notifier.send_message(text)
def handle_message_command(message): """Handles message broadcast requests.""" LOG.info('Received message command: message={!r}'.format(message)) try: decodedmex = base64.b64decode(message) except TypeError: LOG.error('Received message is not valid base64: {!r}'.format(message)) else: text = decodedmex.decode('utf8') #FIXME maybe get author ID from message? user = "******" with session_scope() as session: user = query.get_user(session, user) if not user: LOG.error( "Non-existent user {}, not logging message.".format(user)) return message = query.log_message(session, user, text) broadcast(message.jsondict()) notifier.send_message(text)
def handle_status_command(status): """Update status. Will reject two identical and consecutive updates (prevents opening when already open and vice-versa).""" LOG.info('Received status: %r', status) try: status = int(status) except ValueError: LOG.error('Wrong type for parameters in temperature command') return if status not in (0, 1): LOG.error('Non existent status %r, ignoring.', status) return textstatus = Status.OPEN if status == 1 else Status.CLOSED with session_scope() as session: curstatus = query.get_current_status(session) if curstatus is None or curstatus.value != textstatus: status = query.log_status(session, textstatus, 'BITS') broadcast(status.jsondict()) notifier.send_status(textstatus) else: LOG.error('BITS already open/closed! Ignoring.')
def handle_status_command(status): """Update status. Will reject two identical and consecutive updates (prevents opening when already open and vice-versa).""" LOG.info('Received status: {}'.format(status)) try: status = int(status) except ValueError: LOG.error('Wrong type for parameters in temperature command') return if status not in (0, 1): LOG.error('Non existent status {}, ignoring.'.format(status)) return textstatus = Status.OPEN if status == 1 else Status.CLOSED with session_scope() as session: curstatus = query.get_current_status(session) if curstatus is None or curstatus.value != textstatus: status = query.log_status(session, textstatus, 'BITS') broadcast(status.jsondict()) notifier.send_status(textstatus) else: LOG.error('BITS already open/closed! Ignoring.')