<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">
<html><body style='font-family: Verdana,Geneva,sans-serif'>
<p>I am trying to create simple publisher that will send media player updates to another process.  I understand there are event loop conflicts between zmq and twisted.  Can anyone recommend a better way to code this?</p>
<p> </p>
<p>#! /usr/bin/python2.5<br /><br />import sys<br />sys.path.insert(1, './lib')<br /><br />import cPickle as pickle<br /><br />from twisted.internet import reactor<br />from coherence.base import Coherence<br />from coherence.upnp.devices.control_point import ControlPoint<br />from coherence.upnp.core import DIDLLite<br /><br />import zmq<br /><br />ZonePlayers = []<br />Son = [0,0,0,0,0,0,0,0,0,0,0,0]<br />SonArray = [{}]<br />sck = None<br /><br />class SonosPlayer:<br />        SonosCount = 0<br /><br />        def __init__(self, uuid):<br />                global SonArray<br />                self.uuid = uuid<br /><br />                self.index = SonosPlayer.SonosCount<br />                SonosPlayer.SonosCount += 1<br />                SonArray[self.index] = {'uuid':self.uuid}<br /><br />        def returnSonosCount(self):<br />                return SonosPlayer.SonosCount<br /><br />        def state_variable_change(self, variable):<br />                global sck<br />                global SonArray<br />                print "variable: %s" % variable.name<br />                #print "variable value: %s" % variable.value<br />                print "update from: %s" % self.uuid<br />                #print SonArray<br /><br />                if variable.name == 'CurrentTrackMetaData':<br />                        if variable.value != None and len(variable.value)>0:<br />                                try:<br />                                        elt = DIDLLite.DIDLElement.fromString(variable.value)<br />                                        for item in elt.getItems():<br />                                                SonArray[self.index]['creator'] = item.creator<br />                                                SonArray[self.index]['albumArtist'] = item.albumArtist<br />                                                SonArray[self.index]['artist'] = item.artist<br />                                                SonArray[self.index]['albumArtURI'] = item.albumArtURI<br />                                                SonArray[self.index]['title'] = item.title<br />                                                SonArray[self.index]['album'] = item.album<br />                                except SyntaxError:<br />                                        print "current error"<br />                                        return<br />                elif variable.name == 'NextTrackMetaData':<br />                        if variable.value != None and len(variable.value)>0:<br />                                try:<br />                                        elt = DIDLLite.DIDLElement.fromString(variable.value)<br />                                        for item in elt.getItems():<br />                                                SonArray[self.index]['next_creator'] = item.creator<br />                                                SonArray[self.index]['next_albumArtist'] = item.albumArtist<br />                                                SonArray[self.index]['next_artist'] = item.artist<br />                                                SonArray[self.index]['next_albumArtURI'] = item.albumArtURI<br />                                                SonArray[self.index]['next_title'] = item.title<br />                                                SonArray[self.index]['next_album'] = item.album<br />                                except SyntaxError:<br />                                        print "next error"<br />                                        return<br />                elif variable.name == 'TransportState':<br />                        SonArray[self.index]['TransportState'] = variable.value<br /><br />                f = open("SonosEvents.pkl", "wb")<br />                pickle.dump(SonArray, f, protocol=2)<br />                f.close()<br /><br />                msg = self.uuid + " " + SonArray[self.index]['title']<br />                print msg<br />                sck.send(msg)<br /><br /></p>
<p>def media_renderer_found(client,udn):<br />        global ZonePlayers<br />        global Son<br />        Xudn = udn[5:-3]<br />        print ("%s:%s:%s" % (udn, Xudn, ZonePlayers))<br />        if Xudn in ZonePlayers:<br />                print "media_renderer_found", udn<br />                Son[SonosPlayer.SonosCount] = SonosPlayer(Xudn)<br />                client.av_transport.subscribe_for_variable('CurrentTrackMetaData', Son[SonosPlayer.SonosCount].state_variable_change)<br />                client.av_transport.subscribe_for_variable('NextTrackMetaData', Son[SonosPlayer.SonosCount].state_variable_change)<br />                client.av_transport.subscribe_for_variable('LastChange', Son[SonosPlayer.SonosCount].state_variable_change)<br />                client.av_transport.subscribe_for_variable('TransportState', Son[SonosPlayer.SonosCount].state_variable_change)<br /><br />def media_renderer_removed(udn):<br />    print "media_renderer_removed", udn<br /><br />def start():<br />        control_point = ControlPoint(Coherence({'logmode':'warn'}), auto_client=['MediaRenderer'])<br />        control_point.connect(media_renderer_found, 'Coherence.UPnP.ControlPoint.MediaRenderer.detected')<br />        control_point.connect(media_renderer_removed, 'Coherence.UPnP.ControlPoint.MediaRenderer.removed')<br /><br /><br />f = open("ZonePlayers.pkl", "rb")<br />ZonePlayers = pickle.load(f)<br />f.close()<br /><br />context = zmq.Context()<br />sck = context.socket(zmq.PUB)<br />sck.bind('tcp://127.0.0.1:9999')<br /><br />reactor.callWhenRunning(start)<br />reactor.run()<br /><br /></p>
<div> </div>
</body></html>