-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathlocation_mqtt_publisher.py
executable file
·52 lines (36 loc) · 1.63 KB
/
location_mqtt_publisher.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import logging
from threading import Thread
import arc852.cli_args as cli
from arc852.cli_args import CAMERA_NAME, LOG_LEVEL, MQTT_HOST, GRPC_HOST
from arc852.cli_args import setup_cli_args
from arc852.mqtt_connection import MqttConnection
from arc852.utils import setup_logging, waitForKeyboardInterrupt
from location_client import LocationClient
logger = logging.getLogger(__name__)
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...")
if __name__ == "__main__":
main()