Skip to content

Commit 503a2a9

Browse files
committed
working fm radio and internet radio
1 parent 5a7e6bf commit 503a2a9

File tree

2 files changed

+31
-25
lines changed

2 files changed

+31
-25
lines changed

amplipi/streams/base_streams.py

+21-20
Original file line numberDiff line numberDiff line change
@@ -221,38 +221,39 @@ def _is_running(self):
221221
def _read_info(self) -> models.SourceInfo:
222222
""" Read the current stream info and metadata, caching it """
223223
try:
224-
with open(f'{self._get_config_folder()}/metadata.json', 'r') as file:
225-
info = json.loads(file.read())
224+
if not os.path.exists(f'{self._get_config_folder()}/metadata.json'):
225+
with open(f'{self._get_config_folder()}/metadata.json', 'r') as file:
226+
info = json.loads(file.read())
226227

227-
# populate fields that are type-consistent
228-
info['name'] = self.full_name()
229-
info['type'] = self.stype
230-
info['supported_cmds'] = self.supported_cmds
228+
# populate fields that are type-consistent
229+
info['name'] = self.full_name()
230+
info['type'] = self.stype
231+
info['supported_cmds'] = self.supported_cmds
231232

232-
# set state to stopped if it is not present in the metadata (e.g. on startup)
233-
if 'state' not in info:
234-
info['state'] = 'stopped'
233+
# set state to stopped if it is not present in the metadata (e.g. on startup)
234+
if 'state' not in info:
235+
info['state'] = 'stopped'
235236

236-
self._cached_info = models.SourceInfo(**info)
237+
self._cached_info = models.SourceInfo(**info)
237238

238-
# set stopped message if stream is stopped
239-
if self.stopped_message and self._cached_info.state == 'stopped':
240-
self._cached_info.artist = self.stopped_message
241-
self._cached_info.track = ''
242-
self._cached_info.album = ''
239+
# set stopped message if stream is stopped
240+
if self.stopped_message and self._cached_info.state == 'stopped':
241+
self._cached_info.artist = self.stopped_message
242+
self._cached_info.track = ''
243+
self._cached_info.album = ''
243244

244-
# set default image if none is provided
245-
if not self._cached_info.img_url:
246-
self._cached_info.img_url = self.default_image_url
245+
# set default image if none is provided
246+
if not self._cached_info.img_url:
247+
self._cached_info.img_url = self.default_image_url
247248

248-
return self._cached_info
249+
return self._cached_info
249250
except Exception as e:
250251
logger.exception(f'Error reading metadata for {self.name}: {e}')
251252
return models.SourceInfo(name=self.full_name(), state='stopped')
252253

253254
def info(self) -> models.SourceInfo:
254255
""" Get cached stream info and source metadata """
255-
256+
256257
if self._watch_metadata:
257258
return self._cached_info
258259
else:

streams/fmradio.py

+10-5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@
1212
import os
1313
import sys
1414
import traceback
15+
import signal
16+
17+
18+
def signal_handler(sig, _):
19+
"""Handle sigterm signal."""
20+
log(f"Caught signal {sig}, exiting.")
21+
os.system("killall -9 rtl_fm")
22+
traceback.print_exc(file=sys.stdout)
23+
sys.exit(0)
1524

1625
parser = argparse.ArgumentParser(prog='runfm', description='play a radio station using an RTL-SDR dongle')
1726
parser.add_argument('freq', type=str, help='radio station frequency (ex: 96.1)')
@@ -22,6 +31,7 @@
2231
parser.add_argument('--verbose', action='store_true', help='show more verbose output')
2332
args = parser.parse_args()
2433

34+
signal.signal(signal.SIGTERM, signal_handler)
2535

2636
def log(info):
2737
if args.log:
@@ -148,11 +158,6 @@ def main():
148158

149159
update = False
150160

151-
except KeyboardInterrupt:
152-
print("Shutdown requested...exiting")
153-
os.system("killall -9 rtl_fm")
154-
traceback.print_exc(file=sys.stdout)
155-
sys.exit(0)
156161
except Exception:
157162
os.system("killall -9 rtl_fm")
158163
traceback.print_exc(file=sys.stdout)

0 commit comments

Comments
 (0)