Formatting

This commit is contained in:
Jono Targett 2026-03-20 00:05:05 +10:30
parent d04a52be5d
commit c0900a2d78
7 changed files with 33 additions and 21 deletions

View File

@ -1,3 +0,0 @@
device1,secret1
device2,secret2
dashboard,adminpass
1 device1 secret1
2 device2 secret2
3 dashboard adminpass

View File

@ -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)

View File

@ -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,10 +56,7 @@ 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"))
@ -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:

View File

@ -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())

View File

@ -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",

View File

@ -1,5 +1,6 @@
from threading import Thread
def is_alive(subprocess):
return True if (subprocess and subprocess.poll() is None) else False

4
ubx.py
View File

@ -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__":