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 mqtthandler.handler import MQTTHandler, task
|
||||||
from prometheus_client.parser import text_string_to_metric_families
|
from prometheus_client.parser import text_string_to_metric_families
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class MediaMTXConfig:
|
class MediaMTXConfig:
|
||||||
host: str = "http://localhost"
|
host: str = "http://localhost"
|
||||||
@ -32,10 +33,14 @@ class MediaMTXHandler(MQTTHandler):
|
|||||||
cache = {}
|
cache = {}
|
||||||
|
|
||||||
while True:
|
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:
|
async with aiohttp.ClientSession(auth=auth) as session:
|
||||||
while True:
|
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()
|
metrics = await r.text()
|
||||||
|
|
||||||
for family in text_string_to_metric_families(metrics):
|
for family in text_string_to_metric_families(metrics):
|
||||||
@ -46,7 +51,9 @@ class MediaMTXHandler(MQTTHandler):
|
|||||||
cache[topic] = sample.value
|
cache[topic] = sample.value
|
||||||
print(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)
|
await asyncio.sleep(1)
|
||||||
|
|
||||||
|
|||||||
@ -20,6 +20,7 @@ from .command import (
|
|||||||
|
|
||||||
from .property import Property
|
from .property import Property
|
||||||
|
|
||||||
|
|
||||||
def get_identifier(cache_path: Path) -> str:
|
def get_identifier(cache_path: Path) -> str:
|
||||||
"""
|
"""
|
||||||
Determine an MQTT client ID using the following order:
|
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)
|
cache_path.write_text(client_id)
|
||||||
return client_id
|
return client_id
|
||||||
|
|
||||||
|
|
||||||
def generate_identifier() -> str:
|
def generate_identifier() -> str:
|
||||||
return secrets.token_urlsafe(6)
|
return secrets.token_urlsafe(6)
|
||||||
|
|
||||||
|
|
||||||
class Status(Enum):
|
class Status(Enum):
|
||||||
ONLINE = auto()
|
ONLINE = auto()
|
||||||
OFFLINE = auto()
|
OFFLINE = auto()
|
||||||
|
|
||||||
|
|
||||||
class MQTTHandler:
|
class MQTTHandler:
|
||||||
DEVICE = "device"
|
DEVICE = "device"
|
||||||
META = "meta"
|
META = "meta"
|
||||||
@ -52,12 +56,9 @@ class MQTTHandler:
|
|||||||
COMMAND = "command"
|
COMMAND = "command"
|
||||||
STATUS = "status"
|
STATUS = "status"
|
||||||
|
|
||||||
def __init__(
|
def __init__(self, name: str):
|
||||||
self,
|
|
||||||
name: str
|
|
||||||
):
|
|
||||||
self.name = name
|
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.topic_base = lambda: f"{MQTTHandler.DEVICE}/{self.identifier}"
|
||||||
self.meta_topic = lambda: f"{self.topic_base()}/{MQTTHandler.META}"
|
self.meta_topic = lambda: f"{self.topic_base()}/{MQTTHandler.META}"
|
||||||
@ -76,7 +77,7 @@ class MQTTHandler:
|
|||||||
if name in self._properties:
|
if name in self._properties:
|
||||||
await self._properties[name](value, **kwargs)
|
await self._properties[name](value, **kwargs)
|
||||||
else:
|
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)
|
await self._publish(f"{self.property_topic()}/{name}", value, **kwargs)
|
||||||
|
|
||||||
async def register_property(
|
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()}/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(
|
||||||
await self._publish(f"{self.meta_topic()}/host", socket.gethostname(), qos=1, retain=True)
|
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(
|
async def _execute_command(
|
||||||
self,
|
self,
|
||||||
@ -234,9 +239,7 @@ class MQTTHandler:
|
|||||||
await asyncio.gather(*tasks)
|
await asyncio.gather(*tasks)
|
||||||
|
|
||||||
except aiomqtt.MqttError as e:
|
except aiomqtt.MqttError as e:
|
||||||
print(
|
print(f"MQTT connection error: {e}. Reconnecting in {INTERVAL}s...")
|
||||||
f"MQTT connection error: {e}. Reconnecting in {INTERVAL}s..."
|
|
||||||
)
|
|
||||||
await asyncio.sleep(INTERVAL)
|
await asyncio.sleep(INTERVAL)
|
||||||
|
|
||||||
finally:
|
finally:
|
||||||
|
|||||||
2
radio.py
2
radio.py
@ -10,6 +10,7 @@ from mqtthandler.handler import MQTTHandler, task
|
|||||||
|
|
||||||
from streamer.fileradio import FileRadio
|
from streamer.fileradio import FileRadio
|
||||||
|
|
||||||
|
|
||||||
class RadioHandler(MQTTHandler):
|
class RadioHandler(MQTTHandler):
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
@ -45,6 +46,7 @@ class RadioHandler(MQTTHandler):
|
|||||||
self.radio = FileRadio(args, self.radio.name)
|
self.radio = FileRadio(args, self.radio.name)
|
||||||
await self.publish_stream_path()
|
await self.publish_stream_path()
|
||||||
|
|
||||||
|
|
||||||
async def main():
|
async def main():
|
||||||
handler = RadioHandler("radio")
|
handler = RadioHandler("radio")
|
||||||
signal.signal(signal.SIGINT, lambda signum, frame: handler.stop())
|
signal.signal(signal.SIGINT, lambda signum, frame: handler.stop())
|
||||||
|
|||||||
@ -21,9 +21,9 @@ class FileRadio(Streamer):
|
|||||||
self.playback = subprocess.Popen(
|
self.playback = subprocess.Popen(
|
||||||
[
|
[
|
||||||
"/usr/bin/ffmpeg",
|
"/usr/bin/ffmpeg",
|
||||||
"-re", # http://trac.ffmpeg.org/wiki/StreamingGuide#The-reflag
|
"-re", # http://trac.ffmpeg.org/wiki/StreamingGuide#The-reflag
|
||||||
"-stream_loop", # Loop the stream -
|
"-stream_loop", # Loop the stream -
|
||||||
"-1", # ...indefinitely
|
"-1", # ...indefinitely
|
||||||
"-i",
|
"-i",
|
||||||
self.path,
|
self.path,
|
||||||
"-c:a",
|
"-c:a",
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
from threading import Thread
|
from threading import Thread
|
||||||
|
|
||||||
|
|
||||||
def is_alive(subprocess):
|
def is_alive(subprocess):
|
||||||
return True if (subprocess and subprocess.poll() is None) else False
|
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())
|
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__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user