Skip to content

Commit

Permalink
working fm radio and internet radio
Browse files Browse the repository at this point in the history
  • Loading branch information
klay2000 committed Aug 21, 2024
1 parent 5a7e6bf commit 2d58fb5
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 25 deletions.
41 changes: 21 additions & 20 deletions amplipi/streams/base_streams.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 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:
Expand Down
15 changes: 10 additions & 5 deletions streams/fmradio.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)')
Expand All @@ -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:
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 2d58fb5

Please sign in to comment.