1

New chunking logic

This commit is contained in:
Evan Pratten 2024-11-21 15:24:31 -05:00
parent 57b4f2e52c
commit a4b2ea29ee

View File

@ -36,6 +36,7 @@ def main() -> int:
rbn_socket.sendall(b'va3ujf\r\n') rbn_socket.sendall(b'va3ujf\r\n')
# Handle incoming RBN spots # Handle incoming RBN spots
buffer = b''
while True: while True:
# Read incoming packet (may contain multiple spots) # Read incoming packet (may contain multiple spots)
@ -43,39 +44,45 @@ def main() -> int:
if not data: if not data:
break break
# Append to buffer
buffer += data
# Split the packet into spots # Split the packet into spots
for spot in data.splitlines(): if b'\r\n' in buffer:
# Some lines aren't spots chunks = buffer.splitlines()
if not spot.startswith(b'DX'): buffer = chunks.pop() # Store the un-finished chunk for later
continue for spot in chunks:
# Some lines aren't spots
# Parse the spot if not spot.startswith(b'DX'):
match = RBN_SPOT_RE.match(spot.decode('utf-8')) continue
if not match:
logger.warning("Failed to parse spot: %s", spot) # Parse the spot
continue match = RBN_SPOT_RE.match(spot.decode('utf-8'))
if not match:
values = match.groupdict() logger.warning("Failed to parse spot: %s", spot)
continue
# Parse the timestamp into something more useful
utc_now = datetime.now(UTC) values = match.groupdict()
timestamp = datetime(utc_now.year, utc_now.month, utc_now.day, int(values["time"][:2]), int(values["time"][2:4]), 0)
# Parse the timestamp into something more useful
# Sanitize into a new dict utc_now = datetime.now(UTC)
sanitized = { timestamp = datetime(utc_now.year, utc_now.month, utc_now.day, int(values["time"][:2]), int(values["time"][2:4]), 0)
"spotter": values["spotter"].upper(),
"frequency_khz": float(values["frequency"]), # Sanitize into a new dict
"spotted": values["spotted"].upper(), sanitized = {
"mode": values["mode"].upper(), "spotter": values["spotter"].upper(),
"db": int(values["db"]), "frequency_khz": float(values["frequency"]),
"speed": values["notes"] if values["notes"] and values["notes"].endswith("WPM") else None, "spotted": values["spotted"].upper(),
"grid": values["notes"] if values["notes"] and (not values.get("notes", "WPM").endswith("WPM")) else None, "mode": values["mode"].upper(),
"timestamp": timestamp.timestamp(), "db": int(values["db"]),
} "speed": values["notes"] if values["notes"] and values["notes"].endswith("WPM") else None,
"grid": values["notes"] if values["notes"] and (not values.get("notes", "WPM").endswith("WPM")) else None,
# Publish the spot to the MQTT broker "timestamp": timestamp.timestamp(),
logger.debug("Writing spot: %s", sanitized) }
mqtt_client.publish(f"radio/spots/rbn/{args.rbn_mode}/{sanitized['spotted']}", payload=json.dumps(sanitized))
# Publish the spot to the MQTT broker
logger.debug("Writing spot: %s", sanitized)
mqtt_client.publish(f"radio/spots/rbn/{args.rbn_mode}/{sanitized['spotted']}", payload=json.dumps(sanitized))
return 0 return 0