Sonos soco活动handeling

时间:2017-05-21 19:18:21

标签: python sonos

我正在为ubuntu开发一个简单的sonos客户端。

过去3天试图破解如何使用事件模块。它会侦听网络上的更改,并通过通知进行报告。我能听到我所拥有的四个发言者中的一个,但不是全部。 (链接到模块文档http://docs.python-soco.com/en/latest/advanced/events.html

我猜测“while True”循环正在停止迭代。任何人都可以指出我正确的方向如何正确地构建这个?下面是我目前的代码(是的,非常新的编程)

import soco
from soco import discovery
from soco.events import event_listener
from soco.music_services import MusicService
import logging
from pprint import pprint



class SpeakerEvents():
    def __init__(self, speaker):
        logging.basicConfig(level=logging.DEBUG)
        self.device = speaker
        print(self.device)

        self.sub2 = self.device.avTransport.subscribe()  # track_information
        self.sub3 = self.device.renderingControl.subscribe()  # volume_information
        while True:
            try:
                event = sub2.events.get(timeout=0.5)
                pprint(event.variables.get("current_track_meta_data"))
                pprint(event.variables.get("transport_state"))
                # pprint(event.variables)

            except:
                pass


class SonosSpeakerInstance():
    def __init__(self, speaker):
        self.speaker = speaker
        print(self.speaker)
        self.EventListener(self.speaker)

    def EventListener(self, speaker):
        self.speaker = speaker
        print("instance speaker {}".format(self.speaker))
        if self.speaker.is_coordinator == True:
            print("yes")
            SpeakerEvents(self.speaker)
        else:
            print("no")

    def AllSpeakerInformation(self, speaker):
        self.speaker = speaker
        currently_playing = self.speaker.get_current_track_info()
        self.speaker = speaker
        speaker_info = {"speaker_name": self.speaker.player_name,
                        "ip_address": self.speaker.ip_address,
                        "album": currently_playing["album"],
                        "title": currently_playing["title"],
                        "artist": currently_playing["artist"],
                        "album_art": currently_playing["album_art"]
                        }

        return speaker_info

    def ArtImage(self, speaker_info):
        pass

    def SpeakerSubscriptionAvTransport(self, speaker):
        sub2 = speaker.avTransport.subscribe()
        while True:
            try:
                event = sub2.event.get(timeout=0.5)
                self.TransportState(event.variables.get("transport_state"))

            except:
                pass

    def TransportState(self, speaker_avTransport_info):
        self.speaker_info = speaker_avTransport_info
        print(self.speaker_info)


def SpeakerVolume(speaker):
    pass


if __name__ == "__main__":
    speaker_instances = []
    device = soco.discover()

    for speaker in device:
        speaker_instances.append(SonosSpeakerInstance(speaker))

0 个答案:

没有答案