def on_client_connect(client, userdata, msg): """ Function called when a client connects. """ global clients data = json.loads(msg.payload.decode('utf-8')) new_client = {} new_client['id'] = data['id'] new_client['icon'] = data['icon'] core.log.info("Client " + new_client['id'] + " connected.") mqtt.subscribe('client/' + new_client['id'] + '/#') clients = [c for c in clients if c['id'] != new_client['id'] ] # delete already existing one with the same id clients.append(new_client) relays_list = [] # add its relays to the list ... for relay in Relay.query.filter_by(raspi_id=new_client['id']): pin = relay.pin relays_list.append(pin) # then ask the state of these relays to the raspberry mqtt.publish('client/' + new_client['id'] + '/relay/update_state', json.dumps({'gpios': relays_list})) socketio.emit('client_connect', new_client, namespace='/client', broadcast=True)
def execute(text: str): valid, message = SpeechAction.check_parameters(text) if not valid: core.log.warning(message) return core.log.info("Pronouncing '" + text + "'") mqtt.publish('client/speech/speak', json.dumps({'text': text}))
def execute(direction: str, speed: int): valid, message = MotionAction.check_parameters(direction, speed) if not valid: core.log.warning(message) return core.log.info("Moving with values " + direction + ", " + str(speed)) mqtt.publish('client/' + core_config.get_motion_raspi_id() + '/motion', json.dumps({ 'direction': direction, 'speed': speed }))
def execute(index: int): valid, message = ServoSequenceAction.check_parameters(index) if not valid: core.log.warning(message) return with db.app.app_context(): db_servo = Servo.query.distinct(Servo.raspi_id).first() if db_servo is None: core.log.warning("No default servo raspi set.") return raspi_id = db_servo.raspi_id core.log.info("Executing servo sequence '" + str(index) + "'") mqtt.publish('client/' + raspi_id + '/servo/sequence', json.dumps({'index': index}))
def execute(label: str, state: int = None): if state != 1 and state != 0: state = '' valid, message = RelayAction.check_parameters(label) if not valid: core.log.warning(message) return core.log.info("Activating relay '" + label + "'") with db.app.app_context(): db_rel = Relay.query.filter_by(label=label, enabled=True).first() pin = db_rel.pin parity = db_rel.parity raspi_id = db_rel.raspi_id # if the relay is paired if parity != '': # recover all the pair relays pairs_rel = Relay.query.filter(Relay.parity == parity, Relay.label != label) # check if the pairs relays aren't activated if all([ pair.label not in RelayAction.relay_states or RelayAction.relay_states[pair.label] == 0 for pair in pairs_rel ]): # activate the relay on the corresponding raspberry mqtt.publish('client/' + raspi_id + '/relay/activate', json.dumps({ 'gpio': pin, 'state': state })) else: core.log.warning( "Cannot activate relay '%s', pair already activated", label) else: mqtt.publish('client/' + raspi_id + '/relay/activate', json.dumps({ 'gpio': pin, 'state': state }))
def execute(label: str, position: int, speed: int = 0): valid, message = ServoAction.check_parameters(label, position, speed) if not valid: core.log.warning(message) return with db.app.app_context(): db_servo = Servo.query.filter_by(label=label).first() pin = db_servo.pin raspi_id = db_servo.raspi_id core.log.info("Moving servo '" + label + "' to " + str(position) + " at speed " + str(speed)) mqtt.publish( 'client/' + raspi_id + '/servo/set_position', json.dumps({ 'gpio': pin, 'position': position, 'speed': speed }))
def execute(): mqtt.publish('client/vision/detect_objects')
def start_camera_stream(): mqtt.publish('client/vision/start') vision.log.info("Started camera streaming.")
def stop_camera_stream(): mqtt.publish('client/vision/stop') vision.log.info("Stopped camera streaming.")
def reboot(): core.log.info("Reboot rasperries") mqtt.publish('raspi/reboot', 'reboot')
def shutdown(): core.log.info("Shutdown rasperries") mqtt.publish('raspi/shutdown', 'shutdown')
def stop_speech_recognition(): hearing.log.info("Stopped speech recognition") mqtt.publish('client/hearing/stop')
def start_speech_recognition(): hearing.log.info("Started speech recognition") mqtt.publish('client/hearing/start')