Exemplo n.º 1
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...")
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...")
Exemplo n.º 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...")