From 503a2a9667e4303e95448faf6c1f199773ab50d6 Mon Sep 17 00:00:00 2001 From: Klayton Smith II Date: Wed, 21 Aug 2024 11:40:47 -0400 Subject: [PATCH] working fm radio and internet radio --- amplipi/streams/base_streams.py | 41 +++++++++++++++++---------------- streams/fmradio.py | 15 ++++++++---- 2 files changed, 31 insertions(+), 25 deletions(-) diff --git a/amplipi/streams/base_streams.py b/amplipi/streams/base_streams.py index 0990f5fb3..113905456 100644 --- a/amplipi/streams/base_streams.py +++ b/amplipi/streams/base_streams.py @@ -221,38 +221,39 @@ def _is_running(self): def _read_info(self) -> models.SourceInfo: """ Read the current stream info and metadata, caching it """ try: - with open(f'{self._get_config_folder()}/metadata.json', 'r') as file: - info = json.loads(file.read()) + if not os.path.exists(f'{self._get_config_folder()}/metadata.json'): + with open(f'{self._get_config_folder()}/metadata.json', 'r') as file: + info = json.loads(file.read()) - # populate fields that are type-consistent - info['name'] = self.full_name() - info['type'] = self.stype - info['supported_cmds'] = self.supported_cmds + # populate fields that are type-consistent + info['name'] = self.full_name() + info['type'] = self.stype + info['supported_cmds'] = self.supported_cmds - # set state to stopped if it is not present in the metadata (e.g. on startup) - if 'state' not in info: - info['state'] = 'stopped' + # set state to stopped if it is not present in the metadata (e.g. on startup) + if 'state' not in info: + info['state'] = 'stopped' - self._cached_info = models.SourceInfo(**info) + self._cached_info = models.SourceInfo(**info) - # set stopped message if stream is stopped - if self.stopped_message and self._cached_info.state == 'stopped': - self._cached_info.artist = self.stopped_message - self._cached_info.track = '' - self._cached_info.album = '' + # set stopped message if stream is stopped + if self.stopped_message and self._cached_info.state == 'stopped': + self._cached_info.artist = self.stopped_message + self._cached_info.track = '' + self._cached_info.album = '' - # set default image if none is provided - if not self._cached_info.img_url: - self._cached_info.img_url = self.default_image_url + # set default image if none is provided + if not self._cached_info.img_url: + self._cached_info.img_url = self.default_image_url - return self._cached_info + return self._cached_info except Exception as e: logger.exception(f'Error reading metadata for {self.name}: {e}') return models.SourceInfo(name=self.full_name(), state='stopped') def info(self) -> models.SourceInfo: """ Get cached stream info and source metadata """ - + if self._watch_metadata: return self._cached_info else: diff --git a/streams/fmradio.py b/streams/fmradio.py index 8a0982997..a0958f481 100644 --- a/streams/fmradio.py +++ b/streams/fmradio.py @@ -12,6 +12,15 @@ import os import sys import traceback +import signal + + +def signal_handler(sig, _): + """Handle sigterm signal.""" + log(f"Caught signal {sig}, exiting.") + os.system("killall -9 rtl_fm") + traceback.print_exc(file=sys.stdout) + sys.exit(0) parser = argparse.ArgumentParser(prog='runfm', description='play a radio station using an RTL-SDR dongle') parser.add_argument('freq', type=str, help='radio station frequency (ex: 96.1)') @@ -22,6 +31,7 @@ parser.add_argument('--verbose', action='store_true', help='show more verbose output') args = parser.parse_args() +signal.signal(signal.SIGTERM, signal_handler) def log(info): if args.log: @@ -148,11 +158,6 @@ def main(): update = False - except KeyboardInterrupt: - print("Shutdown requested...exiting") - os.system("killall -9 rtl_fm") - traceback.print_exc(file=sys.stdout) - sys.exit(0) except Exception: os.system("killall -9 rtl_fm") traceback.print_exc(file=sys.stdout)