0
import unittest

service = ('u', 'urn:schemas-upnp-org:service:SwitchPower:1')
binary_light_type = 'urn:schemas-upnp-org:device:BinaryLight:1'

def on_new_device(dev):
    """ Callback triggered when a new device is found.
    """
    print 'Got new device:', dev.udn
    print "Type 'list' to see the whole list"

    if not dev:
        return

def get_switch_service(device):
    return device.services[service[1]]

def create_control_point():
    """ Creates the control point and binds callbacks to device events.
    """
    c = ControlPoint()
    c.subscribe('new_device_event', on_new_device)
    c.subscribe('removed_device_event', on_removed_device)
    return c

def main():
    """ Main loop iteration receiving input commands.
    """
    c = create_control_point()
    c.start()
    run_async_function(_handle_cmds, (c, ))
    reactor.add_after_stop_func(c.stop)
    reactor.main()


def _exit(c):
    """ Stops the _handle_cmds loop
    """
    global running_handle_cmds
    running_handle_cmds = False

def _search(c):
    """ Start searching for devices of type upnp:rootdevice and repeat
    search every 600 seconds (UPnP default)
    """
    c.start_search(600, 'upnp:rootdevice')

def _get_status(c):
    """ Gets the binary light status and print if it's on or off.
    """
    try:
        service = get_switch_service(c.current_server)
        status_response = service.GetStatus()
        if status_response['ResultStatus'] == '1':
            print 'Binary light status is on'
        else:
            print 'Binary light status is off'
    except Exception, e:
        if not hasattr(c, 'current_server') or not c.current_server:
            print 'BinaryLight device not set.Please use set_light <n>'
        else:
            print 'Error in get_status():', e

def _get_target(c):
    """ Gets the binary light target and print if it's on or off.
    """
    try:
        service = get_switch_service(c.current_server)
        status_response = service.GetTarget()
        if status_response['RetTargetValue'] == '1':
            print 'Binary light target is on'
        else:
            print 'Binary light target is off'
    except Exception, e:
        if not hasattr(c, 'current_server') or not c.current_server:
            print 'BinaryLight device not set.Please use set_light <n>'
        else:
            print 'Error in get_target():', e
def _stop(c):
    """ Stop searching
    """
    c.stop_search()

def _list_devices(c):
    """ Lists the devices that are in network.
    """
    k = 0
    for d in c.get_devices().values():
        print 'Device no.:', k
        print 'UDN:', d.udn
        print 'Name:', d.friendly_name
        print 'Device type:', d.device_type
        print 'Services:', d.services.keys() # Only print services name
        print 'Embedded devices:', [dev.friendly_name for dev in \
             d.devices.values()] # Only print embedded devices names
        print
        k += 1

running_handle_cmds = True
commands = {'exit': _exit, 

            'search': _search,
            'stop': _stop,
            'list': _list_devices,


            'get_status': _get_status,
            'get_target': _get_target}


def _handle_cmds(c):
    while running_handle_cmds:
        try:
            input = raw_input('>>> ').strip()
            if len(input.split(" ")) > 0:
                try:
                    if len(input.split(" ")) > 1:
                        commands[input.split(" ")[0]](c, input.split(" ")[1])
                    else:
                        commands[input.split(" ")[0]](c)
                except KeyError, IndexError:
                    print 'Invalid command, try help'
                except TypeError:
                    print 'Wrong usage, try help to see'
        except KeyboardInterrupt, EOFError:
            c.stop()
            break

    # Stops the main loop
    reactor.main_quit()

# Here's our "unit tests".
class IsOddTests(unittest.TestCase):

    def testOne(self):
        self.failUnless(_search(c))

    def testTwo(self):
        self.failIf(_search(upnp:rootdevice))

def main():
    unittest.main()

When I try to run this file , it throws me 2 errors:The above code is wriiten for test cases.

======================================================================
ERROR: testOne (__main__.IsOddTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "controlpt.py", line 145, in testOne
    self.failUnless(_get_status(c))
NameError: global name 'c' is not defined

======================================================================
ERROR: testTwo (__main__.IsOddTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "controlpt.py", line 148, in testTwo
    self.failIf(_search(0))
  File "controlpt.py", line 55, in _search
    c.start_search(600, 'upnp:rootdevice')
AttributeError: 'int' object has no attribute 'start_search'

----------------------------------------------------------------------
Ran 2 tests in 0.000s

1 Answer 1

1

The first one is simple: call it with something that is defined.

The second one is also simple: call it with an object that has a start_search attribute.

Beyond that we don't know. You haven't explained what any of this code is and what it's supposed to do.

Sign up to request clarification or add additional context in comments.

3 Comments

I only have used this code. Where should I define for the first test ?case
so, what it should be?? I mean?? I am using only this file and executing it with python controlpt.py...where should I make change??
Sorry, but it sounds like you should hand this project over to someone who knows elementary Python programming.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.