def main():
    # Parse CLI args
    args = setup_cli_args(cli.grpc_host, cli.mqtt_host, cli.camera_name,
                          cli.log_level)

    # Setup logging
    setup_logging(level=args[LOG_LEVEL])

    # Start location reader
    with LocationClient(args[GRPC_HOST]) as loc_client:

        # Define MQTT callbacks
        def on_connect(mqtt_client, userdata, flags, rc):
            logger.info("Connected with result code: {0}".format(rc))
            Thread(target=publish_locations,
                   args=(mqtt_client, userdata)).start()

        def publish_locations(mqtt_client, userdata):
            while True:
                x_loc, y_loc = loc_client.get_xy()
                if x_loc is not None and y_loc is not None:
                    result, mid = mqtt_client.publish("{0}/x".format(
                        userdata[CAMERA_NAME]),
                                                      payload=x_loc[0])
                    result, mid = mqtt_client.publish("{0}/y".format(
                        userdata[CAMERA_NAME]),
                                                      payload=y_loc[0])

        # Setup MQTT client
        with MqttConnection(args[MQTT_HOST],
                            userdata={CAMERA_NAME: args[CAMERA_NAME]},
                            on_connect=on_connect):
            waitForKeyboardInterrupt()

    logger.info("Exiting...")
예제 #2
0
def main():
    # Parse CLI args
    args = setup_cli_args(cli.grpc_host, cli.mqtt_host, cli.led_name,
                          cli.led_brightness, cli.log_level)

    # Setup logging
    setup_logging(level=args[LOG_LEVEL])

    blinkt = BlinktSubscriber(args[LED_BRIGHTNESS])

    # Define MQTT callbacks
    def on_connect(mqtt_client, userdata, flags, rc):
        logger.info("Connected with result code: {0}".format(rc))
        mqtt_client.subscribe("leds/{0}".format(userdata[TOPIC]))

    def on_message(mqtt_client, userdata, msg):
        logger.info("{0} {1}".format(msg.topic, msg.payload))

    # Setup MQTT client
    with MqttConnection(args[MQTT_HOST],
                        userdata={TOPIC: args[LED_NAME]},
                        on_connect=on_connect,
                        on_message=on_message):
        waitForKeyboardInterrupt()

    logger.info("Exiting...")
예제 #3
0
def average_publisher(mqtt_client, userdata):
    while True:
        try:
            time.sleep(1)
            payload = str(userdata[MESSAGES]).encode("utf-8")
            mqtt_client.publish("metrics/msg_rate", payload=payload, qos=0)
            mqtt_client.publish("logging/metrics/msg_rate", payload=payload, qos=0)
            with userdata[LOCK]:
                userdata[MESSAGES] = 0
        except BaseException as e:
            logger.error("Failure in publish averages() [%s]", e, exc_info=True)
            time.sleep(1)


if __name__ == "__main__":
    # Parse CLI args
    parser = argparse.ArgumentParser()
    cli.mqtt_host(parser)
    args = vars(parser.parse_args())

    # Setup MQTT client
    with MqttConnection(args[MQTT_HOST],
                        userdata={LOCK: Lock(),
                                  MESSAGES: 0},
                        on_connect=on_connect,
                        on_message=on_message):
        waitForKeyboardInterrupt()

    logger.info("Exiting...")
예제 #4
0
                        default=10,
                        type=int,
                        help="Moving average size [10]")
    cli.log_level(parser)
    args = vars(parser.parse_args())

    # Setup logging
    setup_logging(level=args[LOG_LEVEL])

    userdata = {
        TOPIC: "lidar/{0}/cm".format(args[DEVICE]),
        COMMAND: "lidar/{0}/command".format(args[DEVICE]),
        ENABLED: True,
        MOVING_AVERAGE: MovingAverage(args[AVG_SIZE]),
        OOR_VALUES: OutOfRangeValues(size=args[OOR_SIZE]),
        OOR_TIME: args[OOR_TIME]
    }

    with SerialReader(func=fetch_data,
                      userdata=userdata,
                      port=SerialReader.lookup_port(args[DEVICE_ID])
                      if args.get(DEVICE_ID) else args[SERIAL_PORT],
                      baudrate=args[BAUD_RATE]):
        with MqttConnection(hostname=args[MQTT_HOST],
                            userdata=userdata,
                            on_connect=on_connect,
                            on_message=frc_utils.on_message):
            waitForKeyboardInterrupt()

    logger.info("Exiting...")
예제 #5
0
    # Subscribe to all broker messages
    topic = userdata[TOPIC]
    mqtt_client.subscribe(topic)
    setup_logging(filename=args[LOG_FILE],
                  format="%(asctime)s %(levelname)-6s %(message)s",
                  level=logging.DEBUG)
    logger.info("Connected, subscribing to topic %s", topic)
    # print("Connected, subscribing to topic {0}".format(topic))


def on_message(mqtt_client, userdata, msg):
    logger.info("%s : %s", msg.topic, msg.payload)
    # print("{0} : {1}".format(msg.topic, msg.payload))


if __name__ == "__main__":
    # Parse CLI args
    parser = argparse.ArgumentParser()
    cli.mqtt_host(parser)
    cli.log_file(parser)
    cli.mqtt_topic(parser)
    args = vars(parser.parse_args())

    with MqttConnection(args[MQTT_HOST],
                        userdata={TOPIC: args[MQTT_TOPIC]},
                        on_connect=on_connect,
                        on_message=on_message):
        waitForKeyboardInterrupt()

    logger.info("Exiting...")
    "lidar/rear/command", "heading/command", "camera/gear/command"
]

if __name__ == "__main__":

    def on_connect(mqtt_client, userdata, flags, rc):
        logger.info("Connected with result code: %s", rc)
        Thread(target=enable_disable, args=(mqtt_client, )).start()

    def enable_disable(client):
        while True:
            for i in cmds:
                client.publish(i, payload="OFF", qos=0)
            time.sleep(3)
            for i in cmds:
                client.publish(i, payload="ON", qos=0)
            time.sleep(10)

    # Parse CLI args
    parser = argparse.ArgumentParser()
    cli.mqtt_host(parser)
    cli.log_level(parser)
    args = vars(parser.parse_args())

    with MqttConnection(hostname=args[MQTT_HOST],
                        userdata={},
                        on_connect=on_connect):
        waitForKeyboardInterrupt()

    logger.info("Exiting...")
예제 #7
0
@nav.on(nav.UP)
def handle_up_button(ch, evt):
    item_deque.rotate(1)
    assign_selected_sensor()


@nav.on(nav.DOWN)
def handle_down_button(ch, evt):
    item_deque.rotate(-1)
    assign_selected_sensor()


if __name__ == "__main__":
    # Parse CLI args
    args = cli.setup_cli_args(cli.mqtt_host, cli.log_level())

    # Setup logging
    setup_logging(level=args[LOG_LEVEL])

    Thread(target=lcd_display, args=(item_dict, 0.1)).start()

    # Setup MQTT client
    with MqttConnection(args[MQTT_HOST],
                        userdata={ITEM_DICT: item_dict},
                        on_connect=on_connect,
                        on_message=on_message):
        waitForKeyboardInterrupt()

    logger.info("Exiting...")
                    x_loc = loc_client.get_x()

                    if not userdata[ENABLED]:
                        continue

                    if x_loc is not None and abs(x_loc[0] - prev_value) > 1:
                        result, mid = mqtt_client.publish(
                            "{0}/x".format(userdata[TOPIC]),
                            payload="{0}:{1}".format(x_loc[0],
                                                     x_loc[1]).encode('utf-8'))
                        prev_value = x_loc[0]

                except BaseException as e:
                    logger.error("Failure in publish_locations() [%s]",
                                 e,
                                 exc_info=True)
                    time.sleep(1)

        # Setup MQTT client
        with MqttConnection(args[MQTT_HOST],
                            userdata={
                                TOPIC: args[MQTT_TOPIC],
                                COMMAND: args[MQTT_TOPIC] + "/command",
                                ENABLED: True
                            },
                            on_connect=on_connect,
                            on_message=frc_utils.on_message):
            waitForKeyboardInterrupt()

    logger.info("Exiting...")