Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PyOhNet examples
21-10-2016, 12:06 PM
Post: #1
PyOhNet examples
Hi,

I am having a go at using PyOhNet. I want to build a basic control point for the Raspberry Pi and eventually a few other things.

In the Test folder, the TestDeviceList.py just works. I cannot try TestControlEventing.py since there is no TestDvTestBasic.exe present, but I guess that doesnt matter.

However, I'm having a little trouble setting up an example control point device and eventually eventing / subscriptions and invocing actions.

From the TestDeviceList.py I can see how to get a devicelist, and I can do [device instance].Start(). Then I am a bit stuck.

A few pointers how to get started would be much appreciated! And apologies if this should be obvious.

Thanks in advance,
Latka
Find all posts by this user
22-10-2016, 11:41 AM
Post: #2
RE: PyOhNet examples
Here is a very simple test app.

It gets a Device List, and creates a Device with a given friendlyname.
Then it creates a Proxy for the volume service.
Then it invokes a VolumeDec action on the Device.

All this works, but I don't seem to manage to make the event callback to work.
The below two lines doesn't work.
Code:
volumeProxy.SetPropertyInitialEvent( callback )
volumeProxy.SetPropertyChanged( callback )
Would it be possible to get an example of a working event callback?
Cheers,
L


Code:
import PyOhNet as ohNet
import inspect
import time

aDevName = 'Sitting room:Klimax DS'
aDevDomain = 'linn.co.uk'
aDevType = 'Source'
aDevVersion = 1

class ListDevices:

    def __init__( self, aAdapter=None ):
        ohNet.Initialise()
        ohNet.Start( 'CP', aAdapter )
        self.devList = ohNet.Cp.DeviceListUpnpDeviceType( aDevDomain, aDevType, aDevVersion, self._Added1, self._Removed1 )
        self.devList.AddAddedCb( self._Added2 )
        self.devList.AddRemovedCb( self._Removed2 )
        self.devList.RemoveAddedCb( self._Added1 )
        self.devList.RemoveRemovedCb( self._Removed1 )
        
    @staticmethod
    def Shutdown( ):
        ohNet.Shutdown()

    @staticmethod
    def _Added1( aDev ):
       pass
       print "    (1) ADDED ->", aDev.friendlyName, '  ', aDev.handle
    
    @staticmethod
    def _Removed1( aDev ):
       pass
       print "    (1) REMOVED ->", aDev.friendlyName
                
    @staticmethod
    def _Added2( aDev ):
       pass
       print "    (2) ADDED ->", aDev.friendlyName
      
    @staticmethod
    def _Removed2( aDev ):
       pass
       print "    (2) REMOVED ->", aDev.friendlyName


def callback(aValue):
    print aValue


if __name__ == '__main__':
    deviceList = ListDevices()
    time.sleep(3)
    for device in deviceList.devList.devs:
        if device.friendlyName == aDevName:
            device.Start()
            volumeProxy = device.AvOpenhomeOrgVolume# sets up proxy for volume service
            device.AvOpenhomeOrgVolume.SyncVolumeDec()# Invokes action
            volumeProxy.SetPropertyInitialEvent( callback )
            volumeProxy.SetPropertyChanged( callback )
    while True:
        try:
            pass
            time.sleep(.3)
        except KeyboardInterrupt:
            deviceList.Shutdown()
            print 'Exiting test app'
            break
Find all posts by this user
24-10-2016, 02:00 PM
Post: #3
RE: PyOhNet examples
You need to subscribe to the service to receive events
...also callback does not receive the evented data - you have to request it

The following should work
Code:
def callback():
    print "Volume evented -", volumeProxy.PropertyVolume()
    


if __name__ == '__main__':
    deviceList = ListDevices()
    time.sleep(3)
    for device in deviceList.devList.devs:
        if device.friendlyName == aDevName:
            device.Start()
            volumeProxy = device.AvOpenhomeOrgVolume# sets up proxy for volume service
            device.AvOpenhomeOrgVolume.SyncVolumeDec()# Invokes action
            volumeProxy.SetPropertyInitialEvent( callback )
            volumeProxy.SetPropertyChanged( callback )
            volumeProxy.Subscribe()
    while True:
Find all posts by this user
24-10-2016, 03:40 PM
Post: #4
RE: PyOhNet examples
Thank you good Sir!

I cannot believe I missed the Subscribe () bit. ;-)

Also a huge thanks for pointing out the part about the callback.
Find all posts by this user


Forum Jump: