def testSimulateMain( self ): # Regression test for main method # Get regions #regionServerResponse = probeForResponse( regionOptionsURL ) #writeToFile( regionServerResponse, "region.xml" ) # For debugging only regionServerResponse = readFromFile( "res/region.xml" ) # For debugging only regionDict = convertToDict( regionServerResponse ) regionsList = BusWorker.makeRegions( regionDict ); self.assertTrue( regionsList != None ) # Get server response #serverResponse = probeForResponse( nearbyStopURL ) #print( str(serverResponse) ) # For debugging only #writeToFile( serverResponse, "test.xml" ) # For debugging only serverResponse = readFromFile("res/test.xml") # For debugging only stopDict = convertToDict( serverResponse ) #print( "stopDict: " + str(stopDict) ) self.assertTrue( stopDict != None ) #timeServerResponse = probeForResponse( stopURL ) #print( str(timeServerResponse) ) # For debugging only #writeToFile(timeServerResponse, "time.xml") # For debugging only timeServerResponse = readFromFile("res/time.xml") # For debugging only timeDict = convertToDict( timeServerResponse ) self.assertTrue( timeDict != None ) #googleResponse = probeForResponse( distanceTimeURL ) #writeToFile( googleResponse, "google.xml") # For debugging only googleResponse = readFromFile("res/google.xml") # For debugging only googleDict = convertToDict( googleResponse ) self.assertTrue( googleDict != None )
def testConvertToDict(self): # Tests if xml can be converted to dictionary/map/hash-table inputFile = open('tests/res/test.xml').read() dicts = convertToDict(inputFile) #print(dicts) self.assertTrue( dicts != None ) self.assertTrue( isinstance(dicts, dict) ) self.assertTrue( dicts['version'] == "2" ) self.assertTrue( dicts['code'] == "200" ) self.assertTrue( dicts['text'] == "OK")
def test_get_all_regions( self ): # Regression test for main method # Get regions regionOptionsURL = "http://regions.onebusaway.org/regions-v3.xml" #regionServerResponse = probeForResponse( regionOptionsURL ) #writeToFile( regionServerResponse, "res/all_regions.xml" ) regionServerResponse = readFromFile("res/all_regions.xml") regionDict = convertToDict( regionServerResponse ) regionsList = BusWorker.makeRegions( regionDict ) assert( regionsList != None ) ex_lat = 38.8950905 ex_lon = -77.059198 print( "user coordinate is supported? " + str(BusWorker.getRegionFromCoordinates(regionsList, ex_lat, ex_lon)) ) self.assertTrue( BusWorker.getRegionFromCoordinates(regionsList, ex_lat, ex_lon) )
def main(): """ Connect to the OneBusAway server and perform certain behaviors. """ global currLat global currLong readCommandLineArgs() # Get all possible regions regionOptionsURL = "http://regions.onebusaway.org/regions-v3.xml" regionServerResponse = probeForResponse( regionOptionsURL ) regionDict = convertToDict( regionServerResponse ) regionsList = BusWorker.makeRegions( regionDict ) # Determines if user location is supported by the server user_region = BusWorker.getRegionFromCoordinates(regionsList, currLat, currLong) if( user_region == None ): print("Your location is not supported,", end=" ") print("or you live in a region without bus services!") sys.exit() serverURL = user_region.getRegionURL() # Get API key for OBA OBA_APIKey = getAPIKey( "keys/OBA_API.key" ) # Get all bus stops near input location nearbyStopURL = serverURL + "api/where/stops-for-location.xml?key=" + \ OBA_APIKey + "&lat=" + str(currLat) + "&lon=" + str(currLong) serverResponse = probeForResponse( nearbyStopURL ) stopDict = convertToDict( serverResponse ) # Store nearest stop information try: allStops = BusWorker.makeStops( stopDict ) except AssertionError as x: print( x ) sys.exit() # Display bus stops chosenStopNdx = BusWorker.displayBusStops( allStops ) print() print("--> You chose bus stop:") print(allStops[chosenStopNdx]) # Get arrival information for a stop stopURL = serverURL + "where/arrivals-and-departures-for-stop/" + \ allStops[chosenStopNdx].getStopID() + ".xml?key=" + OBA_APIKey timeServerResponse = probeForResponse( stopURL ) timeDict = convertToDict( timeServerResponse ) # Get route for chosen stop try: allRoutes = BusWorker.makeRoutes( timeDict ) except AssertionError as x: print( x ) sys.exit() # Display bus routes chosenRouteNdx = BusWorker.displayBusRoutes( allRoutes ) chosenStop = allStops[chosenStopNdx] stopLat = chosenStop.getLat() stopLong = chosenStop.getLong() # API Key for Google Maps GOOGLE_APIKey = getAPIKey( "keys/GOOGLE_API.key") distanceTimeURL = "https://maps.googleapis.com/maps/api/distancematrix/xml?" + \ "origins=" + str(currLat) + "," + str(currLong) + "&destinations=" + \ str(stopLat) + "," + str(stopLong) + "&mode=walking" + "&key=" + str(GOOGLE_APIKey) googleResponse = probeForResponse( distanceTimeURL ) googleDict = convertToDict( googleResponse ) print() print("----------------------- RESULTS -----------------------") print() print( "--> You are at location (" + str(currLat) + "," + str(currLong) + "), which is at address:" ) print( "\t" + str(googleDict['origin_address']) ) print( "--> The bus stop is at location (" + str( stopLat ) + \ "," + str( stopLong ) + "), which is at address:" ) print( "\t" + str(googleDict['destination_address']) ) print() curTime = makeEpochReadable( int(timeDict['currentTime']) ) print( "--> The current time is:\t" + str(curTime) ) arriv = int( allRoutes[chosenRouteNdx].getArrivalTime() ) busArrivalTime = makeEpochReadable( arriv ) print( "--> The bus will arrive at:\t" + str(busArrivalTime) ) print() # Time information in seconds busArrivalInterval = hmsToSeconds( str(busArrivalTime - curTime) ) getToBusInterval = int( googleDict['row']['element']['duration']['value'] ) print( "--> The bus will arrive in " + str( int(busArrivalInterval / 60) ) + " mins") print( "--> It will take you " + str( googleDict['row']['element']['duration']['text']) + \ " to walk to the bus stop.") # Print out final results if( getToBusInterval > ( busArrivalInterval + 60 ) ): print( "--> You probably won't make it in time! :(") elif ( (getToBusInterval >= (busArrivalInterval - 60)) and (getToBusInterval <= (busArrivalInterval + 60)) ): print( "--> It's a close one, you may or may not make it!!! :O") else: print( "--> You'll make it in time! :)")