from brisa.core.reactors import install_default_reactor
reactor = install_default_reactor()
import os
import unittest
from brisa.upnp.device import Device, Service
from brisa.upnp.device.service import StateVariable
class SwitchPower(Service):
def __init__(self):
Service.__init__(self,
'SwitchPower',
'urn:schemas-upnp-org:service:SwitchPower:1',
'',
os.getcwd() + '/SwitchPower-scpd.xml')
self.target = False
self.status = False
def SetTarget(self, *args, **kwargs):
self.target = kwargs['NewTargetValue']
self.status = self.target
self.set_state_variable('Status', self.target)
print 'Light switched ', {'1': 'on', '0': 'off'}.get(self.target, None)
return {}
def GetTarget(self, *args, **kwargs):
return开发者_高级运维 {'RetTargetValue': self.target}
def soap_GetStatus(self, *args, **kwargs):
return {'ResultStatus': self.status}
class BinaryLight(object):
def __init__(self):
self.server_name = 'Binary Light device'
self.device = None
def _create_device(self):
project_page = 'https://garage.maemo.org/projects/brisa'
self.device = Device('urn:schemas=upnp-org:device:BinaryLight:1',
self.server_name,
manufacturer = 'Ankit',
model_name = 'Binary Light Device',
model_description = 'Test Device',
model_number = '1.0',
model_url=project_page)
def _add_service(self):
switch = SwitchPower()
self.device.add_service(switch)
def start(self):
self._create_device()
self._add_services()
self.device.start()
reactor.add_after_stop_func(self.device.stop)
reactor.main()
# Here's our "unit tests".
class IsOddTests(unittest.TestCase):
def testOne(self):
self.failUnless(_create_device('urn:schemas=upnp-org:device:BinaryLight:1'))
print self.failUnless(_create_device('urn:schemas=upnp-org:device:BinaryLight:1'))
def main():
unittest.main()
if __name__ == '__main__':
device = BinaryLight()
device.start()
If you want tests to be run when you invoke the script, you need:
if __name__ == '__main__':
unittest.main()
However, the generally recommended practice is to put your tests in a sep. file (with the above "main"), so you can keep the main section in your existing program.
精彩评论