61 lines
1.8 KiB
Python
Executable File
61 lines
1.8 KiB
Python
Executable File
#! /usr/bin/env python3
|
|
|
|
import socket
|
|
import signal
|
|
import asyncio
|
|
from dataclasses import dataclass
|
|
|
|
from mqtthandler.command import command
|
|
from mqtthandler.handler import MQTTConfig, MQTTHandler, task
|
|
|
|
from streamer.fileradio import FileRadio
|
|
|
|
class RadioHandler(MQTTHandler):
|
|
def __init__(
|
|
self,
|
|
mqtt_config: MQTTConfig,
|
|
handler_id: str,
|
|
):
|
|
super().__init__(mqtt_config, handler_id)
|
|
|
|
self.radio = FileRadio("./data/StarWars60.mp3", handler_id)
|
|
|
|
@task
|
|
async def publish_stream_path(self):
|
|
await self.set_property("path", self.radio.stream_path(), 1, True)
|
|
await self.set_property("file", self.radio.path, 1, True)
|
|
|
|
@command({"type": "object"}, "Start the radio stream.")
|
|
async def start(self, args):
|
|
print("Start requested")
|
|
self.radio.start_stream()
|
|
|
|
# Manually renamed this one bc it conflicts with the base class version
|
|
@command({"type": "object"}, "Stop the radio stream.", "stop")
|
|
async def stop_stream(self, args):
|
|
print("Stop requested")
|
|
self.radio.end_stream()
|
|
|
|
@command({"type": "string"}, "Change the stream path.")
|
|
async def change_path(self, args):
|
|
self.radio = FileRadio(self.radio.path, args)
|
|
await self.publish_stream_path()
|
|
|
|
@command({"type": "string"}, "Tune the radio for different playback.")
|
|
async def tune(self, args):
|
|
print("Tune request: ", args)
|
|
self.radio = FileRadio(args, self.radio.name)
|
|
await self.publish_stream_path()
|
|
|
|
async def main():
|
|
handler_id = f"radio-{socket.gethostname()}"
|
|
mqtt_config = MQTTConfig(host="127.0.0.1")
|
|
handler = RadioHandler(mqtt_config, handler_id)
|
|
|
|
signal.signal(signal.SIGINT, lambda signum, frame: handler.stop())
|
|
await handler.run()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
asyncio.run(main())
|