Skip to content

Commit

Permalink
first completely functional version
Browse files Browse the repository at this point in the history
  • Loading branch information
klay2000 committed Aug 23, 2024
1 parent 05f6391 commit 2b1e355
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 69 deletions.
9 changes: 4 additions & 5 deletions amplipi/streams/base_streams.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,8 @@ def on_modified(_, event):
# logger.debug(f'mute timer cancelled for {self.name}')
self.unmute()

self._observer = Observer()
# self._fs_event_handler = FileSystemEventHandler()
self._fs_event_handler = handler()
# self._fs_event_handler.on_modified = lambda _: self._read_info
self._observer = Observer()
self._observer.schedule(self._fs_event_handler, metadata_path)
self._observer.start()

Expand All @@ -181,10 +179,11 @@ def _stop_info_watcher(self):

if self._watch_metadata:
if self._observer:
self._observer.stop()
self._observer.join()
logger.debug(f' observer stopped for {self.name}')
del self._observer
self._observer = None
self._fs_event_handler = None
logger.debug(" metadata watcher stopped")

def restart(self):
"""Reset this stream by disconnecting and reconnecting"""
Expand Down
10 changes: 0 additions & 10 deletions amplipi/streams/file_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,3 @@ def disconnect(self):
self.bkg_thread.join()
self._disconnect()
self.proc = None

# def info(self) -> models.SourceInfo:
# source = models.SourceInfo(
# name=self.full_name(),
# state=self.state,
# img_url='static/imgs/plexamp.png',
# type=self.stream_type
# )
# source.supported_cmds = self.supported_cmds
# return source
64 changes: 11 additions & 53 deletions amplipi/streams/pandora.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,17 +148,24 @@ def _read_info(self, write_state=False) -> models.SourceInfo:
# HACK skip this read if it doesn't have state and trigger a new one by adding the state
i: dict = {}
with open(f"{self._get_config_folder()}/metadata.json", 'r', encoding='utf-8') as file:
i = json.load(file)
try: # try/catch because the file may be empty due to the way the eventtcmd script is written
i = json.load(file)
except json.JSONDecodeError:
return self._cached_info

if write_state or ("state" in i.keys() and i["state"] == ""):
i["state"] = self.state
with open(f"{self._get_config_folder()}/metadata.json", 'w', encoding='utf-8') as file:
json.dump(i, file)
return self._cached_info

self._cached_info.img_url = self._cached_info.img_url.replace('http:', 'https:') # HACK: hack to just replace with https

return super()._read_info()
super()._read_info()

if self._cached_info.img_url is not None:
self._cached_info.img_url = self._cached_info.img_url.replace('http:', 'https:') # HACK: hack to just replace with https

return self._cached_info

def info(self) -> models.SourceInfo:
i = super().info()
Expand All @@ -175,56 +182,7 @@ def info(self) -> models.SourceInfo:
i.rating = models.PandoraRating.DEFAULT

return i

# def info(self) -> models.SourceInfo:
# src_config_folder = f'{utils.get_folder("config")}/srcs/v{self.vsrc}'
# loc = f'{src_config_folder}/.config/pianobar/currentSong'
# source = models.SourceInfo(
# name=self.full_name(),
# state=self.state,
# supported_cmds=list(self.supported_cmds.keys()),
# img_url='static/imgs/pandora.png',
# type=self.stream_type
# )

# if len(self.stations) == 0:
# self.load_stations()

# try:
# with open(loc, 'r', encoding='utf-8') as file:
# for line in file.readlines():
# line = line.strip()
# if line:
# data = line.split(',,,')
# if self.track != data[1]: # When song changes, stop inverting state
# self.invert_liked_state = False
# source.state = self.state
# source.artist = data[0]
# source.track = data[1]
# self.track = data[1]
# source.album = data[2]
# source.img_url = data[3].replace('http:', 'https:') # HACK: kind of a hack to just replace with https
# initial_rating = models.PandoraRating(int(data[4]))

# source.rating = initial_rating

# # Pianobar doesn't update metadata after a song starts playing
# # so when you like a song you have to change the state manually until next song
# if self.invert_liked_state:
# if int(data[4]) == models.PandoraRating.DEFAULT.value:
# source.rating = models.PandoraRating.LIKED
# elif int(data[4]) == models.PandoraRating.LIKED.value:
# source.rating = models.PandoraRating.DEFAULT

# source.station = data[5]
# return source
# except Exception:
# pass
# # logger.error('Failed to get currentSong - it may not exist: {}'.format(e))
# # TODO: report the status of pianobar with station name, playing/paused, song info
# # ie. Playing: "Cameras by Matt and Kim" on "Matt and Kim Radio"
# return source


def send_cmd(self, cmd):
""" Pianobar's commands
cmd: Command string sent to pianobar's control fifo
Expand Down
2 changes: 1 addition & 1 deletion streams/eventcmd.sh
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ case "$1" in

echo -n "${artist},,,${title},,,${album},,,${coverArt},,,${rating},,,${stationName}" > "$currentSong"
truncate -s 0 $metadata
printf "{\n\"artist\":\"${artist}\",\n\"track\":\"${title}\",\n\"album\":\"${album}\",\n\"img-url\":\"${coverArt}\",\n\"rating\":${rating},\n\"station\":\"${stationName}\",\n\"state\":\"\"\n}" >> "$metadata"
printf "{\n\"artist\":\"${artist}\",\n\"track\":\"${title}\",\n\"album\":\"${album}\",\n\"img_url\":\"${coverArt}\",\n\"rating\":${rating},\n\"station\":\"${stationName}\",\n\"state\":\"\"\n}" >> "$metadata"

stationList
;;
Expand Down

0 comments on commit 2b1e355

Please sign in to comment.