Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
ltindall committed Jan 23, 2024
1 parent 5a0735e commit be8ef38
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 143 deletions.
7 changes: 4 additions & 3 deletions birdseye/mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ def __init__(self, mqtt_host, mqtt_port, topics):
try:
self.client = paho.mqtt.client.Client()
self.client.on_connect = self.on_connect
#self.client.on_message = self.on_message
# self.client.on_message = self.on_message
self.client.on_publish = self.on_publish
#self.message_handler = message_handler
# self.message_handler = message_handler
self.client.connect(mqtt_host, mqtt_port, 60)
self.client.loop_start()

Expand All @@ -31,14 +31,15 @@ def on_message_func(self, message_handler):
def on_message(client, userdata, json_message):
json_data = json.loads(json_message.payload)
message_handler(json_data)

return on_message

def on_connect(self, client, userdata, flags, result_code):
logging.info(
f"Connected to MQTT broker {self.mqtt_host}:{self.mqtt_port} with result code {result_code}"
)
for topic, handler in self.topics:
self.client.subscribe(topic)
self.client.subscribe(topic)
self.client.message_callback_add(topic, self.on_message_func(handler))

def on_publish(self, client, userdata, mid):
Expand Down
103 changes: 55 additions & 48 deletions birdseye/mqtt_fake.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,86 +20,96 @@
def message_handler(data):
logging.info("Message handler received data: {}".format(data))

mqtt_client = BirdsEyeMQTT("localhost", 1883, [("gamutrf/inference",message_handler),("gamutrf/targets", message_handler)])
mqtt_client = BirdsEyeMQTT(
"localhost",
1883,
[("gamutrf/inference", message_handler), ("gamutrf/targets", message_handler)],
)

sensor_data = {
"metadata": {
"image_path": "/logs/inference/image_1700632876.885_640x640_2408895999Hz.png",
"orig_rows": "28897",
"rssi_max": "-46.058800",
"rssi_mean": "-69.604271",
"rssi_min": "-117.403526",
"rx_freq": "2408895999",
"ts": "1700632876.885"
},
"image_path": "/logs/inference/image_1700632876.885_640x640_2408895999Hz.png",
"orig_rows": "28897",
"rssi_max": "-46.058800",
"rssi_mean": "-69.604271",
"rssi_min": "-117.403526",
"rx_freq": "2408895999",
"ts": "1700632876.885",
},
"predictions": {
"mini2_telem": [
{
"rssi": "-40",
"conf": "0.33034399151802063",
"xywh": [609.8685302734375, 250.76278686523438, 20.482666015625, 7.45684814453125]
"rssi": "-40",
"conf": "0.33034399151802063",
"xywh": [
609.8685302734375,
250.76278686523438,
20.482666015625,
7.45684814453125,
],
}
],
"mini2_video": [
{
"rssi": "-80",
"conf": "0.33034399151802063",
"xywh": [609.8685302734375, 250.76278686523438, 20.482666015625, 7.45684814453125]
"rssi": "-80",
"conf": "0.33034399151802063",
"xywh": [
609.8685302734375,
250.76278686523438,
20.482666015625,
7.45684814453125,
],
}
]
},
],
},
"position": [32.922651, -117.120815],
"heading": 0,
"rssi": [-40, -60],
"gps": "fix"
"gps": "fix",
}
target_data = {

"altitude":4700,
"gps_fix_type":2,
"gps_stale":"false",
"heading":174.15,
"latitude":32.922651,
"longitude":-117.120815,
"relative_alt":4703,
"target_name":"drone1",
"time_boot_ms":904414,
"time_usec":None,
"vx":0.0,
"vy":0.0,
"vz":0.0

"altitude": 4700,
"gps_fix_type": 2,
"gps_stale": "false",
"heading": 174.15,
"latitude": 32.922651,
"longitude": -117.120815,
"relative_alt": 4703,
"target_name": "drone1",
"time_boot_ms": 904414,
"time_usec": None,
"vx": 0.0,
"vy": 0.0,
"vz": 0.0,
}

control_options = {
"sensor": sensor_data,
"target": target_data
}
control_map = {i:k for (i,k) in enumerate(control_options)}
control_options = {"sensor": sensor_data, "target": target_data}
control_map = {i: k for (i, k) in enumerate(control_options)}
control_key = None

def on_key_release(key):

global control_key
global control_key

if key == Key.esc:
exit()

try:
if key.char == '0':
if key.char == "0":
print(f"\nSelected sensor. Up/Down/Left/Right will control sensor.")
control_key = "sensor"
elif key.char == '1':
elif key.char == "1":
print(f"\nSelected target 1. Up/Down/Left/Right will control target 1.")
control_key = "target"
return
except:
pass

if control_key is None:
print(f"\nPlease select the device to control by pressing a number from {list(range(len(control_options)))}.")
print(
f"\nPlease select the device to control by pressing a number from {list(range(len(control_options)))}."
)
print(f"{control_map}")
return
return

if key == Key.right:
print("\nRight key pressed\n")
Expand All @@ -126,16 +136,13 @@ def on_key_release(key):
elif control_key == "target":
target_data["latitude"] -= 0.0001


if control_key == "sensor":
mqtt_client.client.publish(
"gamutrf/inference", json.dumps(sensor_data)
) # also tried qos = 1 and 2
logging.info("Started transmission to broker: {}".format(sensor_data))
elif control_key == "target":
mqtt_client.client.publish(
"gamutrf/targets", json.dumps(target_data)
)
mqtt_client.client.publish("gamutrf/targets", json.dumps(target_data))
logging.info("Started transmission to broker: {}".format(target_data))

with keyboard.Listener(on_release=on_key_release) as listener:
Expand Down
Loading

0 comments on commit be8ef38

Please sign in to comment.