示例#1
0
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)
示例#2
0
文件: actions.py 项目: valorun/CIPHER
    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}))
示例#3
0
文件: actions.py 项目: valorun/CIPHER
    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
                     }))
示例#4
0
文件: actions.py 项目: valorun/CIPHER
 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}))
示例#5
0
文件: actions.py 项目: valorun/CIPHER
    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
                             }))
示例#6
0
文件: actions.py 项目: valorun/CIPHER
    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
                }))
示例#7
0
文件: actions.py 项目: valorun/CIPHER
 def execute():
     mqtt.publish('client/vision/detect_objects')
示例#8
0
def start_camera_stream():
    mqtt.publish('client/vision/start')
    vision.log.info("Started camera streaming.")
示例#9
0
def stop_camera_stream():
    mqtt.publish('client/vision/stop')
    vision.log.info("Stopped camera streaming.")
示例#10
0
def reboot():
    core.log.info("Reboot rasperries")
    mqtt.publish('raspi/reboot', 'reboot')
示例#11
0
def shutdown():
    core.log.info("Shutdown rasperries")
    mqtt.publish('raspi/shutdown', 'shutdown')
示例#12
0
文件: events.py 项目: valorun/CIPHER
def stop_speech_recognition():
    hearing.log.info("Stopped speech recognition")
    mqtt.publish('client/hearing/stop')
示例#13
0
文件: events.py 项目: valorun/CIPHER
def start_speech_recognition():
    hearing.log.info("Started speech recognition")
    mqtt.publish('client/hearing/start')