Formatting
This commit is contained in:
parent
d04a52be5d
commit
c0900a2d78
@ -1,3 +0,0 @@
|
||||
device1,secret1
|
||||
device2,secret2
|
||||
dashboard,adminpass
|
||||
|
13
mediamtx.py
13
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)
|
||||
|
||||
|
||||
@ -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"))
|
||||
|
||||
@ -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:
|
||||
|
||||
2
radio.py
2
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())
|
||||
|
||||
@ -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
4
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__":
|
||||
|
||||
Loading…
Reference in New Issue
Block a user