Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cps track status #35

Closed
wants to merge 7 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 44 additions & 15 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,25 @@
import re
from adapt.intent import IntentBuilder
from mycroft.skills.core import MycroftSkill, intent_handler
from mycroft.skills.common_play_skill import CPSTrackStatus
from mycroft.skills.audioservice import AudioService
from mycroft.util import wait_while_speaking
from os.path import join, exists
from threading import Lock


STATUS_KEYS = ['track', 'artist', 'album', 'image']


class PlaybackControlSkill(MycroftSkill):
def __init__(self):
super(PlaybackControlSkill, self).__init__('Playback Control Skill')
self.query_replies = {} # cache of received replies
self.query_replies = {} # cache of received replies
self.query_extensions = {} # maintains query timeout extensions
self.has_played = False
self.lock = Lock()
self.playback_data = {"playing": None,
"playlist": [],
"disambiguation": []}

# TODO: Make this an option for voc_match()? Only difference is the
# comparison using "==" instead of "in"
Expand Down Expand Up @@ -64,7 +67,7 @@ def voc_match_exact(self, utt, voc_filename, lang=None):

if not voc or not exists(voc):
raise FileNotFoundError(
'Could not find {}.voc file'.format(voc_filename))
'Could not find {}.voc file'.format(voc_filename))

with open(voc) as f:
self.voc_match_cache[cache_key] = f.read().splitlines()
Expand All @@ -80,11 +83,12 @@ def initialize(self):
self.add_event('play:query.response',
self.handle_play_query_response)
self.add_event('play:status',
self.handle_song_info)
self.handle_cps_status)
self.gui.register_handler('next', self.handle_next)
self.gui.register_handler('prev', self.handle_prev)

self.clear_gui_info()

# Handle common audio intents. 'Audio' skills should listen for the
# common messages:
# self.add_event('mycroft.audio.service.next', SKILL_HANDLER)
Expand All @@ -97,6 +101,10 @@ def clear_gui_info(self):
# Initialize track info variables
for k in STATUS_KEYS:
self.gui[k] = ''
self.playback_data = {"playing": None,
"playlist": [],
"disambiguation": []}
self.playback_status = CPSTrackStatus.END_OF_MEDIA

@intent_handler(IntentBuilder('').require('Next').require("Track"))
def handle_next(self, message):
Expand Down Expand Up @@ -153,7 +161,7 @@ def play(self, message):
# which will only return the first word of the target phrase
utt = message.data.get('utterance')
phrase = re.sub('^.*?' + message.data['Play'], '', utt).strip()
self.log.info("Resolving Player for: "+phrase)
self.log.info("Resolving Player for: " + phrase)
wait_while_speaking()
self.enclosure.mouth_think()

Expand Down Expand Up @@ -262,17 +270,38 @@ def _play_query_timeout(self, message):
if search_phrase in self.query_extensions:
del self.query_extensions[search_phrase]

def handle_song_info(self, message):
changed = False
def update_current_song(self, data):
self.playback_data["playing"] = data
for key in STATUS_KEYS:
val = message.data.get(key, '')
changed = changed or self.gui[key] != val
self.gui[key] = val

if changed:
self.log.info('\n-->Track: {}\n-->Artist: {}\n-->Image: {}'
''.format(self.gui['track'], self.gui['artist'],
self.gui['image']))
self.gui[key] = data.get(key, '')

def update_playlist(self, data):
self.playback_data["playlist"].append(data)
# sort playlist by requested order
self.playback_data["playlist"] = sorted(
self.playback_data["playlist"],
key=lambda i: int(i['playlist_position']) or 0)

# playback status
def handle_cps_status(self, message):
status = message.data["status"]

if status == CPSTrackStatus.PLAYING or \
status == CPSTrackStatus.PLAYING_AUDIOSERVICE or \
status == CPSTrackStatus.PLAYING_GUI or \
status == CPSTrackStatus.PLAYING_ENCLOSURE:
self.update_current_song(message.data)

elif status == CPSTrackStatus.DISAMBIGUATION:
# alternative results
self.playback_data["disambiguation"].append(message.data)

elif status == CPSTrackStatus.QUEUED or \
status == CPSTrackStatus.QUEUED_GUI or \
status == CPSTrackStatus.QUEUED_AUDIOSERVICE or \
status == CPSTrackStatus.QUEUED_ENCLOSURE:
# enclosure is handling playback and this is in playlist
self.update_playlist(message.data)


def create_skill():
Expand Down