Ejemplo n.º 1
0
    def test_package_unpackage(self):
        now = datetime.datetime.now()
        now_sec = datetime.datetime(now.year, now.month, now.day, now.hour,
                                    now.minute, now.second, 0)
        pos = CvtdBusPosition(now_sec, 12.467, -136.51, 0)
        b = pos.package()
        pos2 = CvtdBusPosition(b)

        self.assertEqual(pos.timestamp, pos2.timestamp)
        self.assertAlmostEqual(pos.lat, pos2.lat, 1)
        self.assertAlmostEqual(pos.lon, pos2.lon, 1)
        self.assertEqual(pos.direction, pos2.direction)
Ejemplo n.º 2
0
def pull_data(key, locator):
    NUM_ELEMENTS = 5
    newElements = set()

    os.system(
        "curl -o cvtddata.txt http://cvtd.info:8080/CVTDfeed/V200/XML/_System.php?key={} -s"
        .format(key))
    tree = ET.parse('cvtddata.txt')
    root = tree.getroot()
    for bus in root:
        for i in range(NUM_ELEMENTS - 1, 0, -1):
            try:
                rid = bus[2].text
                bnum = bus[3].text
                rnum = bus[4].text
                route = bus[5].text
                t = datetime.strptime(bus[8][i].text.strip(),
                                      "%Y-%m-%d %H:%M:%S.%f")
                lat = float(bus[8][i][0].text)
                lon = float(bus[8][i][1].text)
                direction = int(bus[8][i][2].text)
                if lat == 0.0 and lon == 0.0:
                    continue

                entry = locator.find(rid, t)
                if entry is None:
                    newPos = CvtdBusPosition(t, lat, lon, direction)
                    newElements.add(rid)
                    locator.insert_append(rid, newPos)
            except (IndexError, ValueError):
                continue
    return newElements
Ejemplo n.º 3
0
 def read_locator(self):
     if self.dirPath:
         for filename in os.listdir(self.dirPath):
             # File will be some/path/route_XYZ.dat. We want the XYZ
             rnum = filename.split('/')[-1][6:-4]
             with open("/".join((self.dirPath, filename)), 'rb') as f:
                 data = f.read()
             offset = 0
             packsize = struct.calcsize(pack_string)
             while offset + packsize <= len(data):
                 b = data[offset:offset + packsize]
                 self.insert(rnum, CvtdBusPosition(b))
                 offset = offset + packsize
Ejemplo n.º 4
0
    def test_find(self):
        locator = CvtdBusLocator()
        locator.insert(
            "BLUE",
            CvtdBusPosition(datetime(2020, 6, 27, 11, 13, 14), 41.123,
                            -111.123, 180))
        locator.insert(
            "BLUE",
            CvtdBusPosition(datetime(2020, 6, 27, 12, 13, 14), 41.223,
                            -111.223, 0))
        locator.insert(
            "BLUE",
            CvtdBusPosition(datetime(2020, 6, 27, 13, 13, 14), 41.323,
                            -111.323, 90))
        locator.insert(
            "GREEN",
            CvtdBusPosition(datetime(2020, 6, 27, 14, 13, 14), 41.423,
                            -111.423, 270))
        locator.insert(
            "GREEN",
            CvtdBusPosition(datetime(2020, 6, 27, 15, 13, 14), 41.523,
                            -111.523, 45))
        locator.insert(
            "GREEN",
            CvtdBusPosition(datetime(2020, 6, 27, 16, 13, 14), 41.623,
                            -111.623, 0))

        self.assertEqual(
            41.223,
            locator.find_next_after("BLUE", datetime(2020, 6, 27, 11, 14,
                                                     14)).lat)
        self.assertEqual(
            41.123,
            locator.find_next_after("BLUE", datetime(2020, 6, 27, 10, 14,
                                                     14)).lat)
        self.assertEqual(
            None,
            locator.find_next_after("BLUE", datetime(2020, 6, 27, 13, 14, 14)))
        self.assertEqual(
            41.423,
            locator.find_next_before("GREEN", datetime(2020, 6, 27, 14, 14,
                                                       14)).lat)
        self.assertEqual(
            41.523,
            locator.find_next_before("GREEN", datetime(2020, 6, 27, 15, 14,
                                                       14)).lat)
        self.assertEqual(
            41.623,
            locator.find_next_before("GREEN", datetime(2021, 6, 27, 1, 2,
                                                       3)).lat)
        self.assertEqual(
            None,
            locator.find_next_before("GREEN", datetime(2019, 6, 27, 1, 2, 3)))
Ejemplo n.º 5
0
    def test_append_locator_file(self):
        if os.name == "nt":
            locatorPath = "c:/Users/kenyo/Desktop/locatorTest"
        elif os.name == "posix":
            locatorPath = "/tmp/locatorTest"
        else:
            raise AttributeError("Error: Unknown operating system")
        self.assertFalse(os.path.isdir(locatorPath))
        os.mkdir(locatorPath)
        locator = CvtdBusLocator(locatorPath)
        locator.insert(
            "BLUE",
            CvtdBusPosition(datetime(2020, 6, 27, 10, 5, 2), 41.123, -111.123,
                            180))
        locator.write_locator()

        locator.insert_append(
            "BLUE",
            CvtdBusPosition(datetime(2020, 6, 27, 11, 5, 2), 41.223, -111.223,
                            0))
        locator.insert_append(
            "BLUE",
            CvtdBusPosition(datetime(2020, 6, 27, 12, 5, 2), 41.323, -111.323,
                            0))
        locator.insert_append(
            "GREEN",
            CvtdBusPosition(datetime(2020, 6, 27, 11, 5, 2), 41.987, -111.987,
                            0))

        locator2 = CvtdBusLocator(locatorPath)
        locator2.read_locator()
        self.assertTrue("BLUE" in locator2.pos)
        self.assertTrue("GREEN" in locator2.pos)
        self.assertFalse("5" in locator2.pos)
        self.assertFalse("9" in locator2.pos)
        self.assertEqual(2, len(locator2.pos.keys()))

        self.assertEqual(3, len(locator2.pos['BLUE']))
        self.assertEqual(1, len(locator2.pos['GREEN']))

        self.assertEqual(locator2.pos["BLUE"][0].timestamp,
                         locator.pos["BLUE"][0].timestamp)
        self.assertAlmostEqual(locator2.pos["BLUE"][0].lat,
                               locator.pos["BLUE"][0].lat, 1)
        self.assertAlmostEqual(locator2.pos["BLUE"][0].lon,
                               locator.pos["BLUE"][0].lon, 1)
        self.assertEqual(locator2.pos["BLUE"][0].direction,
                         locator.pos["BLUE"][0].direction)
        self.assertEqual(locator2.pos["BLUE"][1].timestamp,
                         locator.pos["BLUE"][1].timestamp)
        self.assertAlmostEqual(locator2.pos["BLUE"][1].lat,
                               locator.pos["BLUE"][1].lat, 1)
        self.assertAlmostEqual(locator2.pos["BLUE"][1].lon,
                               locator.pos["BLUE"][1].lon, 1)
        self.assertEqual(locator2.pos["BLUE"][1].direction,
                         locator.pos["BLUE"][1].direction)
        self.assertEqual(locator2.pos["BLUE"][2].timestamp,
                         locator.pos["BLUE"][2].timestamp)
        self.assertAlmostEqual(locator2.pos["BLUE"][2].lat,
                               locator.pos["BLUE"][2].lat, 1)
        self.assertAlmostEqual(locator2.pos["BLUE"][2].lon,
                               locator.pos["BLUE"][2].lon, 1)
        self.assertEqual(locator2.pos["BLUE"][2].direction,
                         locator.pos["BLUE"][2].direction)

        self.assertEqual(locator2.pos["GREEN"][0].timestamp,
                         locator.pos["GREEN"][0].timestamp)
        self.assertAlmostEqual(locator2.pos["GREEN"][0].lat,
                               locator.pos["GREEN"][0].lat, 1)
        self.assertAlmostEqual(locator2.pos["GREEN"][0].lon,
                               locator.pos["GREEN"][0].lon, 1)
        self.assertEqual(locator2.pos["GREEN"][0].direction,
                         locator.pos["GREEN"][0].direction)

        os.remove('/'.join((locatorPath, "route_BLUE.dat")))
        os.remove('/'.join((locatorPath, "route_GREEN.dat")))
        os.rmdir(locatorPath)
Ejemplo n.º 6
0
    def test_insert(self):
        locator = CvtdBusLocator()
        locator.insert(
            "BLUE",
            CvtdBusPosition(datetime(2020, 6, 27, 11, 13, 14), 41.123,
                            -111.123, 0))
        self.assertEqual(1, len(locator.pos.keys()))
        self.assertEqual(1, len(locator.pos["BLUE"]))
        self.assertEqual(datetime(2020, 6, 27, 11, 13, 14),
                         locator.pos["BLUE"][0].timestamp)
        self.assertEqual(41.123, locator.pos["BLUE"][0].lat)
        self.assertEqual(-111.123, locator.pos["BLUE"][0].lon)

        locator.insert(
            "BLUE",
            CvtdBusPosition(datetime(2020, 6, 27, 14, 13, 14), 41.223,
                            -111.223, 0))
        self.assertEqual(1, len(locator.pos.keys()))
        self.assertEqual(2, len(locator.pos["BLUE"]))
        self.assertEqual(datetime(2020, 6, 27, 11, 13, 14),
                         locator.pos["BLUE"][0].timestamp)
        self.assertEqual(41.123, locator.pos["BLUE"][0].lat)
        self.assertEqual(-111.123, locator.pos["BLUE"][0].lon)
        self.assertEqual(datetime(2020, 6, 27, 14, 13, 14),
                         locator.pos["BLUE"][1].timestamp)
        self.assertEqual(41.223, locator.pos["BLUE"][1].lat)
        self.assertEqual(-111.223, locator.pos["BLUE"][1].lon)

        locator.insert(
            "BLUE",
            CvtdBusPosition(datetime(2020, 6, 27, 17, 13, 14), 41.323,
                            -111.323, 0))
        self.assertEqual(1, len(locator.pos.keys()))
        self.assertEqual(3, len(locator.pos["BLUE"]))
        self.assertEqual(datetime(2020, 6, 27, 11, 13, 14),
                         locator.pos["BLUE"][0].timestamp)
        self.assertEqual(41.123, locator.pos["BLUE"][0].lat)
        self.assertEqual(-111.123, locator.pos["BLUE"][0].lon)
        self.assertEqual(datetime(2020, 6, 27, 14, 13, 14),
                         locator.pos["BLUE"][1].timestamp)
        self.assertEqual(41.223, locator.pos["BLUE"][1].lat)
        self.assertEqual(-111.223, locator.pos["BLUE"][1].lon)
        self.assertEqual(datetime(2020, 6, 27, 17, 13, 14),
                         locator.pos["BLUE"][2].timestamp)
        self.assertEqual(41.323, locator.pos["BLUE"][2].lat)
        self.assertEqual(-111.323, locator.pos["BLUE"][2].lon)

        locator.insert(
            "BLUE",
            CvtdBusPosition(datetime(2020, 6, 27, 13, 13, 14), 41.423,
                            -111.423, 0))
        self.assertEqual(1, len(locator.pos.keys()))
        self.assertEqual(4, len(locator.pos["BLUE"]))
        self.assertEqual(datetime(2020, 6, 27, 11, 13, 14),
                         locator.pos["BLUE"][0].timestamp)
        self.assertEqual(41.123, locator.pos["BLUE"][0].lat)
        self.assertEqual(-111.123, locator.pos["BLUE"][0].lon)
        self.assertEqual(datetime(2020, 6, 27, 13, 13, 14),
                         locator.pos["BLUE"][1].timestamp)
        self.assertEqual(41.423, locator.pos["BLUE"][1].lat)
        self.assertEqual(-111.423, locator.pos["BLUE"][1].lon)
        self.assertEqual(datetime(2020, 6, 27, 14, 13, 14),
                         locator.pos["BLUE"][2].timestamp)
        self.assertEqual(41.223, locator.pos["BLUE"][2].lat)
        self.assertEqual(-111.223, locator.pos["BLUE"][2].lon)
        self.assertEqual(datetime(2020, 6, 27, 17, 13, 14),
                         locator.pos["BLUE"][3].timestamp)
        self.assertEqual(41.323, locator.pos["BLUE"][3].lat)
        self.assertEqual(-111.323, locator.pos["BLUE"][3].lon)

        locator.insert(
            "BLUE",
            CvtdBusPosition(datetime(2020, 6, 27, 9, 13, 14), 41.523, -111.523,
                            0))
        self.assertEqual(1, len(locator.pos.keys()))
        self.assertEqual(5, len(locator.pos["BLUE"]))
        self.assertEqual(datetime(2020, 6, 27, 9, 13, 14),
                         locator.pos["BLUE"][0].timestamp)
        self.assertEqual(41.523, locator.pos["BLUE"][0].lat)
        self.assertEqual(-111.523, locator.pos["BLUE"][0].lon)
        self.assertEqual(datetime(2020, 6, 27, 11, 13, 14),
                         locator.pos["BLUE"][1].timestamp)
        self.assertEqual(41.123, locator.pos["BLUE"][1].lat)
        self.assertEqual(-111.123, locator.pos["BLUE"][1].lon)
        self.assertEqual(datetime(2020, 6, 27, 13, 13, 14),
                         locator.pos["BLUE"][2].timestamp)
        self.assertEqual(41.423, locator.pos["BLUE"][2].lat)
        self.assertEqual(-111.423, locator.pos["BLUE"][2].lon)
        self.assertEqual(datetime(2020, 6, 27, 14, 13, 14),
                         locator.pos["BLUE"][3].timestamp)
        self.assertEqual(41.223, locator.pos["BLUE"][3].lat)
        self.assertEqual(-111.223, locator.pos["BLUE"][3].lon)
        self.assertEqual(datetime(2020, 6, 27, 17, 13, 14),
                         locator.pos["BLUE"][4].timestamp)
        self.assertEqual(41.323, locator.pos["BLUE"][4].lat)
        self.assertEqual(-111.323, locator.pos["BLUE"][4].lon)

        locator.insert(
            "GREEN",
            CvtdBusPosition(datetime(2020, 6, 27, 10, 13, 14), 41.623,
                            -111.623, 0))
        self.assertEqual(2, len(locator.pos.keys()))
        self.assertEqual(5, len(locator.pos["BLUE"]))
        self.assertEqual(datetime(2020, 6, 27, 9, 13, 14),
                         locator.pos["BLUE"][0].timestamp)
        self.assertEqual(41.523, locator.pos["BLUE"][0].lat)
        self.assertEqual(-111.523, locator.pos["BLUE"][0].lon)
        self.assertEqual(datetime(2020, 6, 27, 11, 13, 14),
                         locator.pos["BLUE"][1].timestamp)
        self.assertEqual(41.123, locator.pos["BLUE"][1].lat)
        self.assertEqual(-111.123, locator.pos["BLUE"][1].lon)
        self.assertEqual(datetime(2020, 6, 27, 13, 13, 14),
                         locator.pos["BLUE"][2].timestamp)
        self.assertEqual(41.423, locator.pos["BLUE"][2].lat)
        self.assertEqual(-111.423, locator.pos["BLUE"][2].lon)
        self.assertEqual(datetime(2020, 6, 27, 14, 13, 14),
                         locator.pos["BLUE"][3].timestamp)
        self.assertEqual(41.223, locator.pos["BLUE"][3].lat)
        self.assertEqual(-111.223, locator.pos["BLUE"][3].lon)
        self.assertEqual(datetime(2020, 6, 27, 17, 13, 14),
                         locator.pos["BLUE"][4].timestamp)
        self.assertEqual(41.323, locator.pos["BLUE"][4].lat)
        self.assertEqual(-111.323, locator.pos["BLUE"][4].lon)
        self.assertEqual(1, len(locator.pos["GREEN"]))
        self.assertEqual(datetime(2020, 6, 27, 10, 13, 14),
                         locator.pos["GREEN"][0].timestamp)
        self.assertEqual(41.623, locator.pos["GREEN"][0].lat)
        self.assertEqual(-111.623, locator.pos["GREEN"][0].lon)

        locator.insert(
            "GREEN",
            CvtdBusPosition(datetime(2020, 6, 27, 3, 13, 14), 41.723, -111.723,
                            45))
        self.assertEqual(2, len(locator.pos.keys()))
        self.assertEqual(5, len(locator.pos["BLUE"]))
        self.assertEqual(datetime(2020, 6, 27, 9, 13, 14),
                         locator.pos["BLUE"][0].timestamp)
        self.assertEqual(41.523, locator.pos["BLUE"][0].lat)
        self.assertEqual(-111.523, locator.pos["BLUE"][0].lon)
        self.assertEqual(datetime(2020, 6, 27, 11, 13, 14),
                         locator.pos["BLUE"][1].timestamp)
        self.assertEqual(41.123, locator.pos["BLUE"][1].lat)
        self.assertEqual(-111.123, locator.pos["BLUE"][1].lon)
        self.assertEqual(datetime(2020, 6, 27, 13, 13, 14),
                         locator.pos["BLUE"][2].timestamp)
        self.assertEqual(41.423, locator.pos["BLUE"][2].lat)
        self.assertEqual(-111.423, locator.pos["BLUE"][2].lon)
        self.assertEqual(datetime(2020, 6, 27, 14, 13, 14),
                         locator.pos["BLUE"][3].timestamp)
        self.assertEqual(41.223, locator.pos["BLUE"][3].lat)
        self.assertEqual(-111.223, locator.pos["BLUE"][3].lon)
        self.assertEqual(datetime(2020, 6, 27, 17, 13, 14),
                         locator.pos["BLUE"][4].timestamp)
        self.assertEqual(41.323, locator.pos["BLUE"][4].lat)
        self.assertEqual(-111.323, locator.pos["BLUE"][4].lon)
        self.assertEqual(2, len(locator.pos["GREEN"]))
        self.assertEqual(datetime(2020, 6, 27, 3, 13, 14),
                         locator.pos["GREEN"][0].timestamp)
        self.assertEqual(41.723, locator.pos["GREEN"][0].lat)
        self.assertEqual(-111.723, locator.pos["GREEN"][0].lon)
        self.assertEqual(datetime(2020, 6, 27, 10, 13, 14),
                         locator.pos["GREEN"][1].timestamp)
        self.assertEqual(41.623, locator.pos["GREEN"][1].lat)
        self.assertEqual(-111.623, locator.pos["GREEN"][1].lon)

        locator.insert(
            "GREEN",
            CvtdBusPosition(datetime(2020, 6, 27, 2, 13, 14), 41.823, -111.823,
                            0))
        self.assertEqual(2, len(locator.pos.keys()))
        self.assertEqual(5, len(locator.pos["BLUE"]))
        self.assertEqual(datetime(2020, 6, 27, 9, 13, 14),
                         locator.pos["BLUE"][0].timestamp)
        self.assertEqual(41.523, locator.pos["BLUE"][0].lat)
        self.assertEqual(-111.523, locator.pos["BLUE"][0].lon)
        self.assertEqual(datetime(2020, 6, 27, 11, 13, 14),
                         locator.pos["BLUE"][1].timestamp)
        self.assertEqual(41.123, locator.pos["BLUE"][1].lat)
        self.assertEqual(-111.123, locator.pos["BLUE"][1].lon)
        self.assertEqual(datetime(2020, 6, 27, 13, 13, 14),
                         locator.pos["BLUE"][2].timestamp)
        self.assertEqual(41.423, locator.pos["BLUE"][2].lat)
        self.assertEqual(-111.423, locator.pos["BLUE"][2].lon)
        self.assertEqual(datetime(2020, 6, 27, 14, 13, 14),
                         locator.pos["BLUE"][3].timestamp)
        self.assertEqual(41.223, locator.pos["BLUE"][3].lat)
        self.assertEqual(-111.223, locator.pos["BLUE"][3].lon)
        self.assertEqual(datetime(2020, 6, 27, 17, 13, 14),
                         locator.pos["BLUE"][4].timestamp)
        self.assertEqual(41.323, locator.pos["BLUE"][4].lat)
        self.assertEqual(-111.323, locator.pos["BLUE"][4].lon)
        self.assertEqual(3, len(locator.pos["GREEN"]))
        self.assertEqual(datetime(2020, 6, 27, 2, 13, 14),
                         locator.pos["GREEN"][0].timestamp)
        self.assertEqual(41.823, locator.pos["GREEN"][0].lat)
        self.assertEqual(-111.823, locator.pos["GREEN"][0].lon)
        self.assertEqual(datetime(2020, 6, 27, 3, 13, 14),
                         locator.pos["GREEN"][1].timestamp)
        self.assertEqual(41.723, locator.pos["GREEN"][1].lat)
        self.assertEqual(-111.723, locator.pos["GREEN"][1].lon)
        self.assertEqual(datetime(2020, 6, 27, 10, 13, 14),
                         locator.pos["GREEN"][2].timestamp)
        self.assertEqual(41.623, locator.pos["GREEN"][2].lat)
        self.assertEqual(-111.623, locator.pos["GREEN"][2].lon)