From c0900a2d785faaaf90864b5a14c31bf5ef85ffdc Mon Sep 17 00:00:00 2001 From: Jono Targett Date: Fri, 20 Mar 2026 00:05:05 +1030 Subject: [PATCH] Formatting --- broker/users.csv | 3 --- mediamtx.py | 13 ++++++++++--- mqtthandler/handler.py | 25 ++++++++++++++----------- radio.py | 2 ++ streamer/fileradio.py | 6 +++--- streamer/streamer.py | 1 + ubx.py | 4 +++- 7 files changed, 33 insertions(+), 21 deletions(-) delete mode 100644 broker/users.csv diff --git a/broker/users.csv b/broker/users.csv deleted file mode 100644 index c365ad6..0000000 --- a/broker/users.csv +++ /dev/null @@ -1,3 +0,0 @@ -device1,secret1 -device2,secret2 -dashboard,adminpass diff --git a/mediamtx.py b/mediamtx.py index 3b733d4..1383bdb 100755 --- a/mediamtx.py +++ b/mediamtx.py @@ -10,6 +10,7 @@ from mqtthandler.command import command from mqtthandler.handler import MQTTHandler, task from prometheus_client.parser import text_string_to_metric_families + @dataclass class MediaMTXConfig: host: str = "http://localhost" @@ -32,10 +33,14 @@ class MediaMTXHandler(MQTTHandler): cache = {} while True: - auth = aiohttp.BasicAuth(login=self.config.username, password=self.config.password) + auth = aiohttp.BasicAuth( + login=self.config.username, password=self.config.password + ) async with aiohttp.ClientSession(auth=auth) as session: while True: - async with session.get(f"{self.config.host}{self.config.metrics_path}") as r: + async with session.get( + f"{self.config.host}{self.config.metrics_path}" + ) as r: metrics = await r.text() for family in text_string_to_metric_families(metrics): @@ -46,7 +51,9 @@ class MediaMTXHandler(MQTTHandler): cache[topic] = sample.value print(topic, sample.value) - await self.set_property(topic, sample.value, qos=0, retain=True) + await self.set_property( + topic, sample.value, qos=0, retain=True + ) await asyncio.sleep(1) diff --git a/mqtthandler/handler.py b/mqtthandler/handler.py index 25c5443..f7ab09d 100644 --- a/mqtthandler/handler.py +++ b/mqtthandler/handler.py @@ -20,6 +20,7 @@ from .command import ( from .property import Property + def get_identifier(cache_path: Path) -> str: """ Determine an MQTT client ID using the following order: @@ -38,13 +39,16 @@ def get_identifier(cache_path: Path) -> str: cache_path.write_text(client_id) return client_id + def generate_identifier() -> str: return secrets.token_urlsafe(6) + class Status(Enum): ONLINE = auto() OFFLINE = auto() + class MQTTHandler: DEVICE = "device" META = "meta" @@ -52,12 +56,9 @@ class MQTTHandler: COMMAND = "command" STATUS = "status" - def __init__( - self, - name: str - ): + def __init__(self, name: str): self.name = name - self.identifier = get_identifier(Path(f"/tmp/{self.name}.tmp")) + self.identifier = get_identifier(Path(f"/tmp/{self.name}.tmp")) self.topic_base = lambda: f"{MQTTHandler.DEVICE}/{self.identifier}" self.meta_topic = lambda: f"{self.topic_base()}/{MQTTHandler.META}" @@ -76,7 +77,7 @@ class MQTTHandler: if name in self._properties: await self._properties[name](value, **kwargs) else: - #print(f"Warning: proeprty {name} is unregistered") + # print(f"Warning: proeprty {name} is unregistered") await self._publish(f"{self.property_topic()}/{name}", value, **kwargs) async def register_property( @@ -136,8 +137,12 @@ class MQTTHandler: ) await self._publish(f"{self.meta_topic()}/name", self.name, qos=1, retain=True) - await self._publish(f"{self.meta_topic()}/type", type(self).__name__, qos=1, retain=True) - await self._publish(f"{self.meta_topic()}/host", socket.gethostname(), qos=1, retain=True) + await self._publish( + f"{self.meta_topic()}/type", type(self).__name__, qos=1, retain=True + ) + await self._publish( + f"{self.meta_topic()}/host", socket.gethostname(), qos=1, retain=True + ) async def _execute_command( self, @@ -234,9 +239,7 @@ class MQTTHandler: await asyncio.gather(*tasks) except aiomqtt.MqttError as e: - print( - f"MQTT connection error: {e}. Reconnecting in {INTERVAL}s..." - ) + print(f"MQTT connection error: {e}. Reconnecting in {INTERVAL}s...") await asyncio.sleep(INTERVAL) finally: diff --git a/radio.py b/radio.py index 45af3ca..4dfd746 100755 --- a/radio.py +++ b/radio.py @@ -10,6 +10,7 @@ from mqtthandler.handler import MQTTHandler, task from streamer.fileradio import FileRadio + class RadioHandler(MQTTHandler): def __init__( self, @@ -45,6 +46,7 @@ class RadioHandler(MQTTHandler): self.radio = FileRadio(args, self.radio.name) await self.publish_stream_path() + async def main(): handler = RadioHandler("radio") signal.signal(signal.SIGINT, lambda signum, frame: handler.stop()) diff --git a/streamer/fileradio.py b/streamer/fileradio.py index 8fb243c..b084b66 100644 --- a/streamer/fileradio.py +++ b/streamer/fileradio.py @@ -21,9 +21,9 @@ class FileRadio(Streamer): self.playback = subprocess.Popen( [ "/usr/bin/ffmpeg", - "-re", # http://trac.ffmpeg.org/wiki/StreamingGuide#The-reflag - "-stream_loop", # Loop the stream - - "-1", # ...indefinitely + "-re", # http://trac.ffmpeg.org/wiki/StreamingGuide#The-reflag + "-stream_loop", # Loop the stream - + "-1", # ...indefinitely "-i", self.path, "-c:a", diff --git a/streamer/streamer.py b/streamer/streamer.py index de92ba0..b5e779a 100644 --- a/streamer/streamer.py +++ b/streamer/streamer.py @@ -1,5 +1,6 @@ from threading import Thread + def is_alive(subprocess): return True if (subprocess and subprocess.poll() is None) else False diff --git a/ubx.py b/ubx.py index 3050982..5922dd6 100755 --- a/ubx.py +++ b/ubx.py @@ -181,7 +181,9 @@ async def main(): ) signal.signal(signal.SIGINT, lambda signum, frame: handler.stop()) - await handler.run("127.0.0.1", port=1883, username="device", password="devicesecret") + await handler.run( + "127.0.0.1", port=1883, username="device", password="devicesecret" + ) if __name__ == "__main__":