Beispiel #1
0
class Zone(Renderer):
    """Raumfeld Zone"""
    def __init__(self, name, udn, location):
        self._rooms = []
        self._udn = udn
        self._name = name
        self._location = location
        scheme, netloc, _, _, _, _ = urllib2.urlparse.urlparse(location)
        self._address = '{0}://{1}'.format(scheme, netloc)
        # ToDo: get correct ControlLocation from the XML file
        self._renderingControl = SoapClient(
            location='{0}/RenderingService/Control'.format(self._address),
            action='urn:upnp-org:serviceId:RenderingControl#',
            namespace='http://schemas.xmlsoap.org/soap/envelope/',
            soap_ns='soap',
            ns='s',
            exceptions=False)
        self._avTransport = SoapClient(
            location='{0}/TransportService/Control'.format(self._address),
            action='urn:schemas-upnp-org:service:AVTransport:1#',
            namespace='http://schemas.xmlsoap.org/soap/envelope/',
            soap_ns='soap',
            ns='s',
            exceptions=False)

    def reinit(self, name, udn, location):
        self._udn = udn
        self._name = name
        self._location = location
        scheme, netloc, _, _, _, _ = urllib2.urlparse.urlparse(location)
        self._address = '{0}://{1}'.format(scheme, netloc)
        # ToDo: get correct ControlLocation from the XML file
        self._renderingControl = SoapClient(
            location='{0}/RenderingService/Control'.format(self._address),
            action='urn:upnp-org:serviceId:RenderingControl#',
            namespace='http://schemas.xmlsoap.org/soap/envelope/',
            soap_ns='soap',
            ns='s',
            exceptions=False)
        self._avTransport = SoapClient(
            location='{0}/TransportService/Control'.format(self._address),
            action='urn:schemas-upnp-org:service:AVTransport:1#',
            namespace='http://schemas.xmlsoap.org/soap/envelope/',
            soap_ns='soap',
            ns='s',
            exceptions=False)

    def _removeRoomByUDN(self, udn):
        """Remove the room with the UDN from the list of rooms"""
        for room_element in self._rooms:
            if room_element.UDN == udn:
                self._rooms.remove(room_element)

    def getRoomByUDN(self, udn):
        """Try to get the room of the zone by its UDN"""
        for room_element in self._rooms:
            if room_element.UDN == udn:
                return room_element
        return None

    def getRooms(self):
        """Returns the list of rooms in this zone"""
        return self._rooms

    def getRoomsByName(self, name):
        """Searches for rooms with a special name"""
        rooms = []
        for room in self._rooms:
            if room.Name.find(name):
                rooms.append(room)
        return rooms

    def bend(self, uri=None, meta=None):
        """BendAVTransportURI"""
        self._avTransport.BendAVTransportURI(InstanceID=0,
                                             CurrentURI=uri,
                                             CurrentURIMetaData=meta)

    """Generic function for getting all media info"""

    @property
    def media_info(self):
        """Get the media information"""
        info = self._avTransport.GetMediaInfo(InstanceID=1)
        info_dict = {
            'NrTracks': info.NrTracks,
            'MediaDuration': info.MediaDuration,
            'CurrentURI': info.CurrentURI,
            'CurrentURIMetaData': info.CurrentURIMetaData,
            'NextUri': info.NextUri,
            'NextUriMetaData': info.NextUriMetaData,
            'PlayMedium': info.PlayMedium,
            'RecordMedium': info.RecordMedium,
            'WriteStatus': info.WriteStatus
        }
        return info_dict

    """For each info there are extra functions"""

    @property
    def media_info_NrTracks(self):
        return self.media_info()['NrTracks']

    @property
    def media_info_MediaDuration(self):
        return self.media_info()['MediaDuration']

    @property
    def media_info_CurrentURI(self):
        return self.media_info()['CurrentURI']

    @property
    def media_info_CurrentURIMetaData(self):
        return self.media_info()['CurrentURIMetaData']

    @property
    def media_info_NextUri(self):
        return self.media_info()['NextUri']

    @property
    def media_info_NextUriMetaData(self):
        return self.media_info()['NextUriMetaData']

    @property
    def media_info_PlayMedium(self):
        return self.media_info()['PlayMedium']

    @property
    def media_info_RecordMedium(self):
        return self.media_info()['RecordMedium']

    @property
    def media_info_WriteStatus(self):
        return self.media_info()['WriteStatus']

    """Generic function for getting all position info"""

    @property
    def position_info(self):
        """Get the position information"""
        info = self._avTransport.GetPositionInfo(InstanceID=1)
        info_dict = {
            'Track': info.Track,
            'TrackDuration': info.TrackDuration,
            'TrackMetaData': info.TrackMetaData,
            'TrackURI': info.TrackURI,
            'RelTime': info.RelTime,
            'AbsTime': info.AbsTime,
            'RelCount': info.RelCount,
            'AbsCount': info.AbsCount
        }
        return info_dict

    """For each info there are extra functions"""

    @property
    def position_info_Track(self):
        return self.position_info()['Track']

    @property
    def position_info_TrackDuration(self):
        return self.position_info()['TrackDuration']

    @property
    def position_info_TrackMetaData(self):
        return self.position_info()['TrackMetaData']

    @property
    def position_info_TrackURI(self):
        return self.position_info()['TrackURI']

    @property
    def position_info_RelTime(self):
        return self.position_info()['RelTime']

    @property
    def position_info_AbsTime(self):
        return self.position_info()['AbsTime']

    @property
    def position_info_RelCount(self):
        return self.position_info()['RelCount']

    @property
    def position_info_AbsCount(self):
        return self.position_info()['AbsCount']

    """Generic function for getting all transport info"""

    @property
    def transport_info(self):
        """Get the transport information"""
        info = self._avTransport.GetTransportInfo(InstanceID=1)
        info_dict = {
            'CurrentTransportState': info.CurrentTransportState,
            'CurrentTransportStatus': info.CurrentTransportStatus,
            'CurrentSpeed': info.CurrentSpeed
        }
        return info_dict

    """For each info there are extra functions"""

    @property
    def transport_info_CurrentTransportState(self):
        return self.transport_info()['CurrentTransportState']

    @property
    def transport_info_CurrentTransportStatus(self):
        return self.transport_info()['CurrentTransportStatus']

    @property
    def transport_info_CurrentSpeed(self):
        return self.transport_info()['CurrentSpeed']
Beispiel #2
0
class Zone(object):
    """Raumfeld Zone"""

    def __init__(self, name, udn, location):
        self._rooms = []
        self._udn = udn
        self._name = name
        self._location = location
        scheme, netloc, _, _, _, _ = urllib2.urlparse.urlparse(location)
        self._address = '{0}://{1}'.format(scheme, netloc)
        # ToDo: get correct ControlLocation from the XML file
        self._renderingControl = SoapClient(
            location='{0}/RenderingService/Control'.format(self._address),
            action='urn:upnp-org:serviceId:RenderingControl#',
            namespace='http://schemas.xmlsoap.org/soap/envelope/',
            soap_ns='soap', ns='s', exceptions=False)
        self._avTransport = SoapClient(
            location='{0}/TransportService/Control'.format(self._address),
            action='urn:schemas-upnp-org:service:AVTransport:1#',
            namespace='http://schemas.xmlsoap.org/soap/envelope/',
            soap_ns='soap', ns='s', exceptions=False)

    def _removeRoomByUDN(self, udn):
        """Remove the room with the UDN from the list of rooms"""
        for room_element in self._rooms:
            if room_element.UDN == udn:
                self._rooms.remove(room_element)

    def getRoomByUDN(self, udn):
        """Try to get the room of the zone by its UDN"""
        for room_element in self._rooms:
            if room_element.UDN == udn:
                return room_element
        return None

    @property
    def Name(self):
        """Get the name of the zone"""
        return self._name

    @property
    def UDN(self):
        """Get the UDN of the zone"""
        return self._udn

    @property
    def Location(self):
        """Get the location URI"""
        return self._location

    @property
    def Address(self):
        """Get the network address"""
        return self._address

    def play(self, uri=None, meta=None):
        """Start playing
        :param uri: (optional) play a specific uri
        """
        if meta:
            self._avTransport.SetAVTransportURI(
                InstanceID=0, CurrentURI=uri, CurrentURIMetaData=meta)
        elif uri:
            self._avTransport.SetAVTransportURI(
                InstanceID=0, CurrentURI=uri, CurrentURIMetaData="")
        else:
            self._avTransport.Play(InstanceID=1, Speed=2)

    def bend(self, uri=None, meta=None):
        """BendAVTransportURI"""
        self._avTransport.BendAVTransportURI(
            InstanceID=0, CurrentURI=uri, CurrentURIMetaData=meta)

    def next(self):
        """Next"""
        self._avTransport.Next(InstanceID=1)

    def previous(self):
        """Previous"""
        self._avTransport.Previous(InstanceID=1)

    def pause(self):
        """Pause"""
        self._avTransport.Pause(InstanceID=1)

    def seek(self, target, unit = 'ABS_TIME'):
        """Seek; unit = _ABS_TIME_/REL_TIME/TRACK_NR"""
        return self._avTransport.Seek(InstanceID=1, Unit = unit, Target = target)

    @property
    def transport_state(self):
        """Get Current Transport State"""
        return (self._avTransport.GetTransportInfo(InstanceID=1)
                .CurrentTransportState)

    @property
    def volume(self):
        """get/set the current volume"""
        try:
            return int(self._renderingControl.GetVolume(InstanceID=1).CurrentVolume)
        except:
            return 0

    @volume.setter
    def volume(self, value):
        self._renderingControl.SetVolume(InstanceID=1, DesiredVolume=value)

    def changeVolume(self, value):
        self._renderingControl.ChangeVolume(InstanceID=1, Amount=value)

    @property
    def mute(self):
        """get/set the current mute state"""
        response = self._renderingControl.GetMute(InstanceID=1, Channel=1)
        return int(response.CurrentMute) == 1

    @mute.setter
    def mute(self, value):
        self._renderingControl.SetMute(InstanceID=1, DesiredMute=1 if value else 0, Channel=1)

    @property
    def uri(self):
        """Get the uri of the currently played medium"""
        return self._avTransport.GetMediaInfo(InstanceID=1).CurrentURI

    @property
    def uri_metadata(self):
        """Get CurrentURIMetaData"""
        return self._avTransport.GetMediaInfo(InstanceID=1).CurrentURIMetaData

    @property
    def media_info(self):
        """Get TrackURI"""
        info = self._avTransport.GetMediaInfo(InstanceID=1)
        media_dic = {'tracks' : info.NrTracks,
            'media_duration' : info.MediaDuration,
            'uri_metadata' : info.CurrentURIMetaData,
            'uri' : info.CurrentURI}
        return media_dic

    @property
    def position_info(self):
        """Get TrackURI"""
        info = self._avTransport.GetPositionInfo(InstanceID=1)
        pos_dic = {'track' : info.Track,
            'track_duration' : info.TrackDuration,
            'track_metadata' : info.TrackMetaData,
            'track_uri' : info.TrackURI,
            'rel_time' : info.RelTime,
            'abs_time' : info.AbsTime}
        return pos_dic

    @property
    def track_uri(self):
        """Get TrackURI"""
        return self._avTransport.GetPositionInfo(InstanceID=1).TrackURI

    @property
    def track_metadata(self):
        """Get TrackURIMetaData"""
        return self._avTransport.GetPositionInfo(InstanceID=1).TrackMetaData

    @property
    def track_duration(self):
        """Get TrackDuration"""
        return self._avTransport.GetPositionInfo(InstanceID=1).TrackDuration

    @property
    def track_rel_time(self):
        """Get RelTime"""
        return self._avTransport.GetPositionInfo(InstanceID=1).RelTime

    @property
    def track_abs_time(self):
        """Get AbsTime"""
        return self._avTransport.GetPositionInfo(InstanceID=1).AbsTime

    def getRooms(self):
        """Returns the list of rooms in this zone"""
        return self._rooms;

    def getRoomsByName(self, name):
        """Searches for rooms with a special name"""
        rooms= []
        for room in self._rooms:
            if room.Name.find(name):
                rooms.append(room)
        return rooms