def test_WebApi_Update_Success(self): #Note that this only checks the response json to ensure it matches #the update; we don't wait and then query to verify, but just trust it. v = WebApi() j = v.Update('open', '12:34') self.assertTrue( j['status'] == 'open' and j['openUntil'].find('12:34:00') == 11 ) #TODO: Timezone is different server-side now, so this check fails j = v.Update('closed') self.assertTrue(j['status'] == 'closed' and j.get('openUntil') == None)
def setup(): #Connects to the internet, updates local IP address #on VHS Api, connects to SpaceTime Serial, updates #Web Api variables, and queries SpaceTime's clock #(to trigger an update upon its response). #returns initialized (WebAPI, SpaceTime) print('Initializing SpaceTime...') vhs = VHSApi() web = WebApi() st = SpaceTime() print('Connecting to the internet...') web.WaitForConnect() print('Connected!') #Update the machine's local IP on the VHS Api. The timestamp can serve as a boot history. vhs.Update(api_var_ip, GetLocalIP()) print('Initializing Serial connection with SpaceTime (' + st.serial.name + ')...') while not st.IsConnected(): print( 'Failed to init Serial connection with SpaceTime. Trying again...') print('Initialized!') print('Initializing webserver for REST API (only available to LAN)') RestServ(st) #Query Closing time (this is the only time we do this) #in case RPi was rebooted but SpaceTime wasn't. st.GetTime(1) #Closing time is ID 1 st.Read() #Ignore the echo of the GetTime command ct = st.Read() #Read the response #It is an unlabeled time, but we know it should be Closing time if ct.type == 'AmbiguousTime': ct.type = 'Closing' #Update Web Api if necessary ProcessSerialMsg(ct, web, st) #Query SpaceTime's clock. Its response will trigger us to update it if necessary. st.GetTime(0) #Current time is ID 0 return web, st
def test_WebApi_Update_BadUrl(self): v = WebApi(dataURL='http://isvhsopen.com/') self.assertFalse(v.Update('test1', 'v1'))
def test_WebApi_Update_NoJSON(self): #If querying a different server, we shouldn't be using our actual API key! v = WebApi(dataURL='http://www.google.ca/?q=', apiKey='BADBADBAD') self.assertFalse(v.Update('test1', 'v1'))
def test_WebApi_Update_ConnectionError(self): #If querying a different server, we shouldn't be using our actual API key! v = WebApi(dataURL='http://doesnotexist.isvhsopen.com/', apiKey='BADBADBAD') self.assertFalse(v.Update('open', '12:34'))
def test_WebApi_Update_Timeout(self): v = WebApi(timeout=0) self.assertFalse(v.Update('open', '12:34'))
def test_WebApi_Bad_Key(self): v = WebApi(apiKey='BADBADBAD') self.assertFalse(v.Update('open', '12:34'))
def test_WebApi_Query_BadUrl(self): v = WebApi(dataURL='http://isvhsopen.com/') self.assertFalse(v.Query('status'))
def test_WebApi_Query_Timeout(self): v = WebApi(timeout=0) self.assertFalse(v.Query('status'))
def test_WebApi_Query_Success(self): v = WebApi() r = v.Query('status') self.assertTrue(r == 'open' or r == 'closed')
if __name__=="__main__": appname="main" work_dir=os.getcwd() log_dir=work_dir+os.sep+"log" log=Log(log_dir,appname,3) logger=log.get_instance() log.start() logger.debug("{} {}, pid:{}".format(appname, _g_version, os.getpid())) try: filename=os.getcwd()+os.sep+"setting.json" config=readConfig(filename,logger) take_req=WebApi(logger) if config is not None: url,data=makeUserIdRequest(config,logger) result,content=take_req.http_post(url,data,True) if result == True: useruuid=getUserData(content,logger) url,data=makeEventRequest(config,logger) result, content=take_req.http_post(url,data,True) if result == True: destination,mqURL=getMQData(content,logger) ## connecting to activemq # host_and_port=[("127.0.0.1",61613)] # destination="/topic/{}".format("ADSB.Receiver") receive_from_topic(mqURL,destination,logger,config["rabbitmq"]) # host_and_port=[("127.0.0.1",61613)]