Ejemplo n.º 1
0
    def testAddSkoobot(self):
        """
        Test addition of skoobots using the addSkoobot() method

        The method adds a skoobot to the registry using an address
        and an optional name.
        """
        registry = SkoobotRegistry(self.tempPath)
        namedAddr = "ff:ff:ff:ff:ff:ff"
        namedName = "newSkoobot"
        unnamedAddr = "ff:ff:ff:ff:ff:fe"

        with self.subTest("Add named Skoobot"):
            registry.addSkoobot(namedAddr, namedName)
            self.assertEqual(4, len(registry.registry))
            self.assertEqual(1, len(registry.getSkoobotsByAddress(namedAddr)))
            self.assertEqual(1, len(registry.getSkoobotsByName(namedName)))

        with self.subTest("Add unnamed Skoobot"):
            registry.addSkoobot(unnamedAddr)
            self.assertEqual(5, len(registry.registry))
            skoobots = registry.getSkoobotsByAddress(unnamedAddr)
            self.assertEqual(1, len(skoobots))
            self.assertIn(skoobots[0][1], registry.skoobotNames)

        with self.subTest("Add duplicate Skoobot"):
            # Bug #7: By default this replaces the existing
            # skoobot. If the replace=False parameter is set,
            # it raises a RuntimeError unless the parameters
            # are compatible with the existing entry.
            #
            # It is always true that it does not result in a
            # duplicate address.
            registry.addSkoobot(namedAddr, namedName)
            self.assertEqual(5, len(registry.registry))

            registry.addSkoobot(namedAddr, replace=False)
            self.assertEqual(5, len(registry.registry))

            with self.assertRaises(RuntimeError):
                registry.addSkoobot(unnamedAddr, namedName, replace=False)

        with self.subTest("Test invalid parameters"):
            with self.assertRaises(TypeError):
                registry.addSkoobot((namedAddr, namedName))

            with self.assertRaises(TypeError):
                registry.addSkoobot(namedAddr, (namedAddr, namedName))
Ejemplo n.º 2
0
def scan():
    transport = TransportBluepy()
    registry = SkoobotRegistry()

    rawDevices = transport.findRawDevices()
    skoobots = []
    for device in rawDevices:
        scanList = device.getScanData()
        for scanItem in scanList:
            if scanItem[0] == 9 and scanItem[2] == "Skoobot":
                skoobots.append(device)

    for skoobot in skoobots:
        # print(transport.rawDeviceInfoStr(skoobot))
        addr = skoobot.addr
        registry.addSkoobot(addr)
        name = registry.getSkoobotsByAddress(addr)[0][1]
        if registry.getDefaultName() == None:
            registry.setDefault(name)
        defaultText = " (default)" if registry.getDefaultName() == name else ""
        msg = "Added Skoobot {0:s} to registry with name {1:s}{2:s}"
        print(msg.format(addr, name, defaultText))
    print("Saving to list of Skoobots to registry {0:s}".format(
        registry.registryPath))
    registry.save()
    shutil.chown(registry.registryPath, os.getlogin())
Ejemplo n.º 3
0
    def testGetSkoobotByAddress(self):
        """
        Test the getSkoobotsByAddress() method

        The method should return a list of (addr, name) tupes
        for the skoobot matching addr, if any. Addresses are unique
        so there cannot be more than one. We verify uniqueness in
        the adding tests.
        """
        registry = SkoobotRegistry(self.tempPath)
        addrs = (self.skooAddr, self.skooDupAddr1, self.skooDupAddr2,
                 "nomatch", None)
        matchExpected = (self.skooAddr, self.skooDupAddr1, self.skooDupAddr2)
        for addr in addrs:
            expectedLen = 1 if addr in matchExpected else 0
            with self.subTest(addr=addr, expectedLen=expectedLen):
                skoobots = registry.getSkoobotsByAddress(addr)
                self.assertEqual(expectedLen, len(skoobots))
                if expectedLen == 1:
                    # There is exactly 1 skoobot in the list, so use it.
                    skoobot = skoobots[0]
                    if addr == self.skooAddr:
                        self.assertEqual(addr, skoobot[0])
                        self.assertEqual(self.skooName, skoobot[1])
                    else:
                        self.assertEqual(addr, skoobot[0])
                        self.assertEqual(self.skooDupName, skoobot[1])