class Connect: os = __import__('os') """OS access :notes: Run sys commands """ connection = None """Connection instance :notes: HTTPS access variable """ headers = None """Connection headerssecurity and format :notes: Security and format headers """ thisGlobals = Globals() """Global parameter access :notes: Endpoint address, ports, token, id(s) """ def __init__(self): """Initialize connect find os set ssl context, get IP address :param None: :returns: None :todo: Add ifconfig command to try to get ip address """ try: if self.os.name == 'nt': ssl.create_default_context() elif self.os.name == 'posix': ssl._create_default_https_context = ssl._create_unverified_context else: ssl._create_default_https_context = ssl._create_unverified_context except Exception,e: pass try: #print socket.gethostname() thisIP = "IP Address: " + socket.gethostbyname(socket.gethostname()) #Globals.strPrint (self.thisGlobals, thisIP) except Exception,e: #Globals.strPrint (self.thisGlobals, "No IP Found...") pass
class Status: os = __import__('os') connection = None headers = None status_json = None localAdvisories = [] localProperties = [] localLevelDown = [] thisGlobals = Globals() def levelDown(self, data): self.localLevelDown = [] for Level in data: #print Level for entries in Level: self.localLevelDown.append(Properties(entries, Level[entries])) #print str(entries) + " : " + str(Level[entries]) def __init__(self): pass def get_status(self,gps_lat,gps_lon,weather): if Globals.AirConnected: try: self.connection = httplib.HTTPSConnection(Globals.httpsAddr, Globals.httpsPort, timeout=Globals.timeOut) self.headers = Globals.xapikey self.connection.request('GET', '/status/v2/point?latitude='+gps_lat+'&longitude='+gps_lon+'&weather='+weather, '', self.headers) result = self.connection.getresponse().read() self.status_json = json.loads(result) Globals.strPrint (self.thisGlobals,self.status_json) return (self.status_json['status'] == "success") except Exception,e: Globals.strPrint (self.thisGlobals,"Airmap: No Connection or slow connection ->Request Timeout...") #Globals.strPrint(self.thisGlobals,str(e)) return False else:
class Telemetry: os = __import__('os') connection = None headers = None thisGlobals = Globals() def __init__(self): pass def put_Telemetry(self, flightID, lat, lon, alt, ground_speed, heading, barometer, status, battery, drone, seq, logused): connectTelemetry = httplib.HTTPSConnection(Globals.telemetryAddr, Globals.httpsPort, timeout=Globals.timeOut) headers = Globals.xapikey #print "..............................................." try: connectTelemetry.request( 'PUT', '/livemap/telemetry/{}'.format(flightID), json.dumps({ "lat": float(lat), "lon": float(lon), "alt": float(alt), "timestamp": "{}".format(datetime.datetime.now()), "gs_ms": float(ground_speed), "true": float(heading), "baro": float(barometer), "status": "{}".format(status), "battery": float(battery), "drone": "{}".format(drone), "seq": int(seq), "log": float(logused) }), headers) result = connectTelemetry.getresponse().read() #print result parsed_json = json.loads(result) #print parsed_json return parsed_json except: #traceback.print_exc() print "No response from telemetry..." def post_Telemetry(self, flightID, lat, lon, alt, ground_speed, heading, barometer, status, battery, drone, seq, logused): connectTelemetry = httplib.HTTPSConnection(Globals.telemetryAddr, Globals.httpsPort, timeout=Globals.timeOut) headers = Globals.xapikey #print "..............................................." try: connectTelemetry.request( 'POST', '/livemap/telemetry/{}'.format(flightID), json.dumps({ "lat": float(lat), "lon": float(lon), "alt": float(alt), "timestamp": "{}".format(datetime.datetime.now()), "gs_ms": float(ground_speed), "true": float(heading), "baro": float(barometer), "status": "{}".format(status), "battery": float(battery), "drone": "{}".format(drone), "seq": int(seq), "log": float(logused) }), headers) result = connectTelemetry.getresponse().read() #print result parsed_json = json.loads(result) #print parsed_json return parsed_json except: #traceback.print_exc() print "No response from telemetry..."
class Flight: os = __import__('os') connection = None headers = None thisGlobals = Globals() def __init__(self): pass def get_FlightList(self, pilotID): connection = httplib.HTTPSConnection(Globals.httpsAddr, Globals.httpsPort, timeout=Globals.timeOut) headers = Globals.xapikey try: connection.request( 'GET', '/flight/v2/?pilot_id=' + str(pilotID) + '&enhance=true', '', headers) result = connection.getresponse().read() parsed_json = json.loads(result) #print(result) flight_collection = parsed_json['data']['results'] #print flight_collection return flight_collection except: traceback.print_exc() def cmd_KillFlights(self, pilotID): connection = httplib.HTTPSConnection(Globals.httpsAddr, Globals.httpsPort, timeout=Globals.timeOut) headers = Globals.xapikey try: connection.request( 'GET', '/flight/v2/?pilot_id=' + str(pilotID) + '&enhance=true', '', headers) result = connection.getresponse().read() parsed_json = json.loads(result) #print(result) flight_collection = parsed_json['data']['results'] #print flight_collection for flights in flight_collection: endFlight = flights['id'] #destroy flight print "deleting {}".format(endFlight) try: connectFlight = httplib.HTTPSConnection( Globals.httpsAddr, Globals.httpsPort, timeout=Globals.timeOut) headers = Globals.xapikey headers['Authorization'] = "Bearer {}".format( Globals.myToken) connectFlight.request( 'POST', '/flight/v2/{}/delete'.format(endFlight), '', headers) result = connectFlight.getresponse().read() #print(result) except: print "Kill Flights Error..." traceback.print_exc() except: traceback.print_exc() def get_PilotID(self): if Globals.pilotIDValid == True: return Globals.pilot_id else: return False def create_FlightPoint(self, time, lat, lon, public, notify): startTime = datetime.datetime.now() endTime = startTime + datetime.timedelta(0, (time * 60)) startTime = startTime.isoformat() + "-07:00" endTime = endTime.isoformat() + "-07:00" try: connectFlight = httplib.HTTPSConnection(Globals.httpsAddr, Globals.httpsPort, timeout=Globals.timeOut) headers = Globals.xapikey headers['Authorization'] = "Bearer {}".format(Globals.myToken) connectFlight.request( 'POST', '/flight/v2/point', json.dumps({ "latitude": float(lat), "longitude": float(lon), "max_altitude": 100, "start_time": "{}".format(startTime), "end_time": "" + endTime + "", "public": bool(public), "notify": bool(notify) }), headers) result = connectFlight.getresponse().read() #Globals.strPrint(self.thisGlobals,result) try: parsed_json = json.loads(result) parsed_status = parsed_json['status'] Globals.pilot_id = parsed_json['data']['pilot_id'] Globals.pilotIDValid = True #Globals.strPrint (self.thisGlobals,Globals.pilot_id) except: Globals.strPrint(self.thisGlobals, "Pilot ID not found...Retry!") Globals.strPrint(self.thisGlobals, result) return False if parsed_status != "success": return False Globals.myFlightID = parsed_json['data']['id'] except: print "Create Flight Error..." traceback.print_exc() return Globals.myFlightID def end_Flight(self, flightID): try: connectFlight = httplib.HTTPSConnection(Globals.httpsAddr, Globals.httpsPort, timeout=Globals.timeOut) headers = Globals.xapikey headers['Authorization'] = "Bearer {}".format(Globals.myToken) connectFlight.request('POST', '/flight/v2/{}/end'.format(flightID), '', headers) result = connectFlight.getresponse().read() parsed_json = json.loads(result) parsed_status = parsed_json['status'] if parsed_status != "success": return False else: return True except: print "End Flight Error..." traceback.print_exc() def delete_Flight(self, flightID): try: connectFlight = httplib.HTTPSConnection(Globals.httpsAddr, Globals.httpsPort, timeout=Globals.timeOut) headers = Globals.xapikey headers['Authorization'] = "Bearer {}".format(Globals.myToken) connectFlight.request('POST', '/flight/v2/{}/delete'.format(flightID), '', headers) result = connectFlight.getresponse().read() parsed_json = json.loads(result) parsed_status = parsed_json['status'] if parsed_status != "success": return False else: return True except: print "End Flight Error..." traceback.print_exc()
class Flight: os = __import__('os') connection = None headers = None thisGlobals = Globals() def __init__(self): pass def get_FlightList(self, pilotID): connection = httplib.HTTPSConnection(Globals.httpsAddr, Globals.httpsPort, timeout=Globals.timeOut) headers = Globals.xapikey try: connection.request('GET', '/flight/v2/?pilot_id='+str(pilotID)+'&enhance=true', '', headers) result = connection.getresponse().read() parsed_json = json.loads(result) flight_collection = parsed_json['data']['results'] return flight_collection except: traceback.print_exc() def cmd_KillFlights(self, pilotID): connection = httplib.HTTPSConnection(Globals.httpsAddr, Globals.httpsPort, timeout=Globals.timeOut) headers = Globals.xapikey try: connection.request('GET', '/flight/v2/?pilot_id='+str(pilotID)+'&enhance=true', '', headers) result = connection.getresponse().read() parsed_json = json.loads(result) flight_collection = parsed_json['data']['results'] for flights in flight_collection: endFlight = flights['id'] #destroy flight print "deleting {}".format(endFlight) try: connectFlight = httplib.HTTPSConnection(Globals.httpsAddr, Globals.httpsPort, timeout=Globals.timeOut) headers = Globals.xapikey headers['Authorization'] = "Bearer {}".format(Globals.myToken) connectFlight.request('POST', '/flight/v2/{}/delete'.format(endFlight), '', headers) result = connectFlight.getresponse().read() #print(result) except: print "Kill Flights Error..." traceback.print_exc() except: traceback.print_exc() def get_PilotID(self): if Globals.pilotIDValid == True: return Globals.pilot_id else: return False def create_FlightPoint(self, time, lat, lon, public, notify): startTime = datetime.datetime.utcnow() endTime = startTime + datetime.timedelta(0,(time*60)) startTime = startTime.isoformat() + "-00:00" endTime = endTime.isoformat() + "-00:00" print startTime print endTime try: connectFlight = httplib.HTTPSConnection(Globals.httpsAddr, Globals.httpsPort, timeout=Globals.timeOut) headers = Globals.xapikey headers['Authorization'] = "Bearer {}".format(Globals.myToken) connectFlight.request('POST', '/flight/v2/point', json.dumps({"latitude":float(lat),"longitude":float(lon),"max_altitude":100,"start_time":"{}".format(startTime),"end_time":"" + endTime + "","public":bool(public),"notify":bool(notify)}), headers) result = connectFlight.getresponse().read() #Globals.strPrint(self.thisGlobals,result) try: parsed_json = json.loads(result) parsed_status = parsed_json['status'] print parsed_status Globals.pilot_id = parsed_json['data']['pilot_id'] Globals.pilotIDValid = True #Globals.strPrint (self.thisGlobals,Globals.pilot_id) except: Globals.strPrint (self.thisGlobals,"Pilot ID not found...Retry!") Globals.strPrint (self.thisGlobals,result) return False if parsed_status != "success": return False Globals.myFlightID = parsed_json['data']['id'] except: print "Create Flight Error..." traceback.print_exc() return Globals.myFlightID def create_FlightPolygon(self, time, lat, lon,thisBounds, public, notify): startTime = datetime.datetime.utcnow() endTime = startTime + datetime.timedelta(0,(time*60)) startTime = startTime.isoformat() + "-00:00" endTime = endTime.isoformat() + "-00:00" thisBounds = json.loads(thisBounds) try: connectFlight = httplib.HTTPSConnection(Globals.httpsAddr, Globals.httpsPort, timeout=Globals.timeOut) headers = Globals.xapikey headers['Authorization'] = "Bearer {}".format(Globals.myToken) connectFlight.request('POST', '/flight/v2/polygon', json.dumps({"latitude":float(lat),"longitude":float(lon),"max_altitude":100,"start_time":"{}".format(startTime),"end_time":"" + endTime + "","public":bool(public),"notify":bool(notify),"geometry":{"type":"Polygon","coordinates": thisBounds}}), headers) result = connectFlight.getresponse().read() #Globals.strPrint(self.thisGlobals,result) try: parsed_json = json.loads(result) parsed_status = parsed_json['status'] print parsed_status Globals.pilot_id = parsed_json['data']['pilot_id'] Globals.pilotIDValid = True #Globals.strPrint (self.thisGlobals,Globals.pilot_id) except: Globals.strPrint (self.thisGlobals,"Pilot ID not found...Retry!") Globals.strPrint (self.thisGlobals,result) return False if parsed_status != "success": return False Globals.myFlightID = parsed_json['data']['id'] except: print "Create Flight Error..." traceback.print_exc() return Globals.myFlightID def end_Flight(self, flightID): try: connectFlight = httplib.HTTPSConnection(Globals.httpsAddr, Globals.httpsPort, timeout=Globals.timeOut) headers = Globals.xapikey headers['Authorization'] = "Bearer {}".format(Globals.myToken) connectFlight.request('POST', '/flight/v2/{}/end'.format(flightID), '', headers) result = connectFlight.getresponse().read() parsed_json = json.loads(result) parsed_status = parsed_json['status'] if parsed_status != "success": return False else: return True except: print "End Flight Error..." traceback.print_exc() def delete_Flight(self, flightID): try: connectFlight = httplib.HTTPSConnection(Globals.httpsAddr, Globals.httpsPort, timeout=Globals.timeOut) headers = Globals.xapikey headers['Authorization'] = "Bearer {}".format(Globals.myToken) connectFlight.request('POST', '/flight/v2/{}/delete'.format(flightID), '', headers) result = connectFlight.getresponse().read() parsed_json = json.loads(result) parsed_status = parsed_json['status'] if parsed_status != "success": return False else: return True except: print "End Flight Error..." traceback.print_exc() def start_comm(self, flightID): try: connectFlight = httplib.HTTPSConnection(Globals.httpsAddr, Globals.httpsPort, timeout=Globals.timeOut) headers = Globals.xapikey headers['Authorization'] = "Bearer {}".format(Globals.myToken) connectFlight.request('POST', '/flight/v2/{}/start-comm'.format(flightID), '', headers) result = connectFlight.getresponse().read() parsed_json = json.loads(result) print parsed_json #parsed_status = parsed_json['data']['key']['data'] parsed_status = parsed_json['data']['key'] print "H:" + parsed_status #thisKey = (''.join(str(hex(i)[2:].zfill(2)) for i in parsed_status)).decode('hex') thisKey = parsed_status.decode('base64') return thisKey except: print "Could Not Start Comms..." traceback.print_exc() def end_comm(self, flightID): try: connectFlight = httplib.HTTPSConnection(Globals.httpsAddr, Globals.httpsPort, timeout=Globals.timeOut) headers = Globals.xapikey headers['Authorization'] = "Bearer {}".format(Globals.myToken) connectFlight.request('POST', '/flight/v2/{}/start-comm'.format(flightID), '', headers) result = connectFlight.getresponse().read() parsed_json = json.loads(result) parsed_status = parsed_json['status'] if parsed_status != "success": return False else: return True except: print "Could Not End Comms..." traceback.print_exc() def recover_Pilot(self): try: connectFlight = httplib.HTTPSConnection(Globals.httpsAddr, Globals.httpsPort, timeout=Globals.timeOut) headers = Globals.xapikey headers['Authorization'] = "Bearer {}".format(Globals.myToken) connectFlight.request('GET', '/pilot/v2/profile', "", headers) result = connectFlight.getresponse().read() try: parsed_json = json.loads(result) parsed_status = parsed_json['status'] print parsed_status Globals.pilot_id = parsed_json['data']['id'] Globals.pilotIDValid = True except: Globals.strPrint (self.thisGlobals,"Pilot Recover ID not found...Retry!") Globals.strPrint (self.thisGlobals,result) return False if parsed_status != "success": return False except: print "Create Flight Error..." traceback.print_exc() return Globals.pilot_id