def read_login(filename, login_object): val_return = 0 if os.path.isfile(filename) and os.access(filename, os.R_OK): #print ("file exists and is readable") with open(filename, 'r') as f: try: login_string = f.read() log_split = login_string.split(',') login_object.username = log_split[0] login_object.password = log_split[1] login_object.longitude = log_split[2] login_object.latitude = log_split[3] login_object.radius = log_split[4] val_return = 1 except IOExeption: # whatever reader errors you care about logging.add_log( 2, "Problem reading user info ---> solve this later on") # handle error finally: f.close() return val_return else: logging.add_log(2, "File whit login non existent") return val_return
def update_height_of_flight(active_plane_flarms, package, database_con): package_flarm_id = helpers.get_flarm_id(package) try: for e in active_plane_flarms: if e[0] is not None and e[0] == package_flarm_id and ( e[3] is None or e[3] < helpers.get_value_converted_int(package.altitude)): if not database.update_glider_height( database_con, helpers.long_to_hex_str(package_flarm_id), helpers.get_value_converted_int(package.altitude)): return False return True elif e[1] is not None and e[1] == package_flarm_id and ( e[4] is None or e[4] < helpers.get_value_converted_int(package.altitude)): if not database.update_tow_height( database_con, helpers.long_to_hex_str(package_flarm_id), helpers.get_value_converted_int(package.altitude)): return False return True except Exception as e: logging.add_log( 2, "Something went wrong when trying to update flight height ---> %s" % e) print "Something went wrong when trying to update flight height ---> %s" % e return False logging.add_log(0, "plane height not updated") return True
def get_flarm_id(packet): try: flarm_id = hex_string_to_int(packet.src_callsign[3:]) except Exception as e: logging.add_log(1, "failed to get flarm id ----> %s" %e) flarm_id = -1 return flarm_id
def create_socket_file(socket): try: sock_file = socket.makefile() except Exception as e: logging.add_log(2, "Problem creating socket file -> %s" % e) return -1 return sock_file
def keepalive(connection_file): try: connection_file.write("Keepalive") connection_file.flush() # here we send a log message of when the keepalive was sent so we can trace what happens except Exception, e: #for now we just print, but in the long run we log to file logging.add_log(2, "Problem keeping the connection alive -> %s" % e) return 0
def raw_timestamp_to_seconds(raw_timestamp): try: sec = int(raw_timestamp[0:2])*3600 sec += int(raw_timestamp[2:4])*60 sec += int(raw_timestamp[4:6]) return sec except Exception as e: logging.add_log(1, "failed CONVERTING timestamp to int----> %s" %e) return None
def get_message(connection_file): try: # Read packet string from socket packet_str = connection_file.readline() #print "packet string length is: ", len(packet_str), " packet is: ", packet_str except socket.error as e: logging.add_log(2, ("Socket error on readline, %s" % e)) return "" return packet_str
def close(sock): # close socket -- must be closed to avoid buffer overflow try: sock.shutdown(0) sock.close() logging.add_log(0, "Closing socket") except Exception as e: logging.add_log(1, "Failed to close socket -> %s" % e) return -1 return 1
def assign_flight_type(connection, aircraft_id, flight_type): val_return = 0 try: with connection.cursor() as cursor: sql = "UPDATE Flight_Data SET Flight_Type = %s where Glider_id = %s OR Towing_id = %s" #TODO update so the specific flight gets affected cursor.execute(sql, (flight_type, aircraft_id, aircraft_id)) val_return = 1 except Exception as e: print('Could not assign tow plane to flight') logging.add_log( 1, 'Failed to add a tow plane to a flight at database.assign_tow_plane() - %s' % e)
def get_plane_ids(connection): try: a1 = get_glider_ids(connection) a2 = get_tow_plane_ids(connection) array = a1 + a2 return array except Exception as e: print('Could not list all aircrafts') logging.add_log( 1, 'Failed to list all aircrafts registered in the database at database.get_plane_ids() - %s' % e) return (-1)
def end_flight(connection, flarm_id, time): val_return = 0 try: with connection.cursor() as cursor: sql = "UPDATE Flight_Data SET Glider_Landing = %s, Flight_Status = 'Finished', Flight_Time = TIMEDIFF(%s, Takeoff) WHERE Glider_id = %s AND Flight_Status = 'Ongoing'" cursor.execute(sql, (time, time, flarm_id)) connection.commit() val_return = 1 except Exception as e: print('Could not end flight') logging.add_log( 2, 'Failed to end flight at database.end_flight() - %s' % e) return val_return
def relevant_package(array_whit_id, package): if package.src_callsign != None and len(package.src_callsign) > 6: try: id_hex = helpers.get_flarm_id(package) if helpers.array_contains(array_whit_id, id_hex): return True except Exception as e: logging.add_log( 1, ("Galet->packets, funk relevant_package, %s ->>> %s" % (e, package[0].orig_packet))) return False
def list_pilot(connection): try: with connection.cursor() as cursor: sql = "SELECT * FROM Pilot" cursor.execute(sql) result = cursor.fetchall() return result except Exception as e: print('Could not list all pilots') logging.add_log( 1, 'Failed to list all pilots registered in the database at database.list_pilot() - %s' % e)
def add_pilot(connection, id, name): val_return = 0 try: with connection.cursor() as cursor: sql = "INSERT INTO Pilot(Pilot_ID, Name) VALUES (%s, %s)" cursor.execute(sql, (id, name)) connection.commit() val_return = 1 except Exception as e: print('Could not add a new pilot') logging.add_log( 2, 'Failed to add a pilot at database.add_pilot() - %s' % e) return val_return
def add_glider(connection, aircraft_id, glider_id): val_return = 0 try: with connection.cursor() as cursor: sql = "INSERT INTO Glider(Glider_ID, Flarm_ID, Daily_Surveillance_Performed) VALUES (%s, %s, 'No')" cursor.execute(sql, (aircraft_id, glider_id)) connection.commit() val_return = 1 except Exception as e: print('Could not add a new glider') logging.add_log( 2, 'Failed to add a glider at database.add_glider() - %s' % e) return val_return
def new_flight(connection, glider_id, towing_id, time): val_return = 0 try: with connection.cursor() as cursor: sql = "INSERT INTO Flight_Data(Takeoff, Logged_Date, Glider_id, Towing_id, Flight_Status) VALUES (%s, CURRENT_DATE, %s, %s, 'Ongoing')" cursor.execute(sql, (time, glider_id, towing_id)) connection.commit() val_return = 1 except Exception as e: print('Could not start a new flight') logging.add_log( 2, 'Failed to start a new flight at database.new_flight() - %s' % e) return val_return
def end_towplane_flight(connection, flarm_id): val_return = 0 try: with connection.cursor() as cursor: sql = "UPDATE Flight_Data SET Flight_Status = 'Finished' WHERE Towing_id = %s AND Flight_Status = 'Ongoing' AND Glider_id is Null" cursor.execute(sql, flarm_id) connection.commit() val_return = 1 except Exception as e: print('Could not end flight') logging.add_log( 2, 'Failed to end flight at database.end_towplane_flight() - %s' % e) return val_return
def logout(connection): val_return = 0 try: connection.close() logging.add_log(0, 'Logging out from database successful') val_return = 1 except Exception as e: print('Could not end connection to database') logging.add_log( 2, 'Failed to logout from the database at database.logout() - %s' % e) return val_return
def assign_glider_pilot(connection, flight_number, pilot_id): val_return = 0 try: with connection.cursor() as cursor: sql = "UPDATE Flight_Data SET Glider_Pilot = %s WHERE Flight_No = %s" cursor.execute(sql, (pilot_id, flight_number)) connection.commit() val_return = 1 except Exception as e: print('Could not assign glider pilot to flight') logging.add_log( 2, 'Failed to add a pilot to a flight at database.assign_glider_flight() - %s' % e) return val_return
def assign_flight(connection, flight_number, pilot_id, flight_type): val_return = 0 try: with connection.cursor() as cursor: sql = "INSERT INTO Flew(Flight_No, Pilot_ID, Flight_Type) VALUES (%s, %s, %s)" cursor.execute(sql, (flight_number, pilot_id, flight_type)) connection.commit() val_return = 1 except Exception as e: print('Could not assign pilot to flight') logging.add_log( 2, 'Failed to add a pilot to a flight at database.assign_flight() - %s' % e) return val_return
def update_glider_height(connection, flarm_id, height): val_return = 0 try: with connection.cursor() as cursor: sql = "UPDATE Flight_Data SET Max_Height = %s WHERE Glider_id = %s AND Flight_Status = %s" cursor.execute(sql, (height, flarm_id, "Ongoing")) connection.commit() val_return = 1 except Exception as e: print('Could not update gliders max height') logging.add_log( 2, 'Failed to update gliders max height at database.update_glider_height() - %s' % e) return val_return
def assign_tow_plane(connection, glider_id, towing_id): val_return = 0 try: with connection.cursor() as cursor: sql = "UPDATE Flight_Data SET Towing_id = %s where Glider_id = %s AND Flight_Status = %s AND Towing_id is NUll" cursor.execute(sql, (towing_id, glider_id, 'Ongoing')) connection.commit() val_return = 1 except Exception as e: print('Could not assign tow plane to flight') logging.add_log( 1, 'Failed to add a tow plane to a flight at database.assign_tow_plane() - %s' % e) return val_return
def add_flight_notes(connection, flight_number, note): val_return = 0 try: with connection.cursor() as cursor: sql = "UPDATE Flight_Data SET Notes = %s WHERE Flight_No = %s" cursor.execute(sql, (note, flight_number)) connection.commit() val_return = 1 except Exception as e: Print('Could not add comment to flight log') logging.add_log( 1, 'Failed to add a note to a flight log at database.add_flight_notes() - %s' % e) return val_return
def tow_plane_landing(connection, flarm_id, time): val_return = 0 try: with connection.cursor() as cursor: sql = "UPDATE Flight_Data SET Towing_Landing = %s, Towing_Time = TIMEDIFF(%s, Takeoff) WHERE Towing_id = %s AND Towing_Landing is NULL" cursor.execute(sql, (time, time, flarm_id)) connection.commit() val_return = 1 except Exception as e: print('Could not land towing plane') logging.add_log( 2, 'Failed to land towing plane at database.tow_plane_landing() - %s' % e) return val_return
def update_tow_height(connection, flarm_id, height): val_return = 0 try: with connection.cursor() as cursor: sql = "UPDATE Flight_Data SET Towing_Height = %s WHERE Towing_id = %s AND Towing_Landing IS %s" cursor.execute(sql, (height, flarm_id, None)) connection.commit() val_return = 1 except Exception as e: print('Could not update towing height') logging.add_log( 2, 'Failed to update towing height at database.update_tow_height() - %s' % e) return val_return
def get_tow_plane_ids(connection): try: with connection.cursor() as cursor: sql = "SELECT Flarm_ID FROM Tow_Plane" cursor.execute(sql) result = cursor.fetchall() array = list(itertools.chain.from_iterable(result)) ret_array = [helpers.hex_string_to_int(e) for e in array] return ret_array except Exception as e: print('Could not list all tow planes') logging.add_log( 1, 'Failed to list all tow planes registered in the database at database.get_tow_plane_ids() - %s' % e) return (-1)
def reset_surveillance(connection): val_return = 0 try: with connection.cursor() as cursor: sql1 = "UPDATE Glider SET Daily_Surveillance_Performed = 'No'" sql2 = "UPDATE Tow_Plane SET Daily_Surveillance_Performed = 'No'" cursor.execute(sql1) cursor.execute(sql2) connection.commit() val_return = 1 except Exception as e: print('Could not reset daily surveillance') logging.add_log( 2, 'Failed to reset daily surveillances at database.reset_surveillance() - %s' % e) return val_return
def main_func(libfap, glider_id_array, towingplane_id_array, active_database_connection, packet_str): # Parse packet using libfap.py into fields to process, eg: packet_parsed = libfap.fap_parseaprs(packet_str, len(packet_str), 0) if packets.relevant_package(glider_id_array+towingplane_id_array, packet_parsed[0]): if not logging.log_packet(packet_str): logging.add_log(1, "Logging the flight packets went wrong, %s" % packet_str) if not packets.processing(glider_id_array, towingplane_id_array, packet_parsed[0], active_database_connection): logging.add_log(2, "Main -> processing packet went wrong") return False if len(packet_str) == 0: print "Read returns zero length string. Failure. Orderly closeout" return -2 return True
def get_started_flight(connection): try: with connection.cursor() as cursor: sql = "SELECT Glider_id, Towing_id, Takeoff, Max_Height, Towing_Height, Towing_Landing FROM Flight_Data WHERE Flight_Status = %s" cursor.execute(sql, 'Ongoing') result = cursor.fetchall() array = list(result) new_array = [(helpers.hex_string_to_int(e[0]), helpers.hex_string_to_int(e[1]), e[2].total_seconds(), e[3], e[4], e[5] is None) for e in array] return new_array except Exception as e: print('Could not list ongoing flights') logging.add_log( 1, 'Failed to list all ongoing flights registered in the database at database.get_started_flight() - %s' % e) return (-1)
def surveilled_tow_plane(connection, towing_id, pilot_id, notes): val_return = 0 try: with connection.cursor() as cursor: sql1 = "INSERT INTO Daily_Surveillance(Flarm_ID, Logged_Date, Note) VALUES (%s, CURRENT_DATE, %s)" cursor.execute(sql1, (towing_id, notes)) sql2 = "INSERT INTO Surveilled(Flarm_ID, Logged_Date, Pilot_ID) VALUES (%s, CURRENT_DATE, %s)" cursor.execute(sql2, (towing_id, pilot_id)) sql3 = "UPDATE Tow_Plane SET Daily_Surveillance_Performed = 'Yes' WHERE Flarm_ID = %s" cursor.execute(sql3, (towing_id)) connection.commit() val_return = 1 except Exception as e: print('Could not perform surveillance') logging.add_log( 2, 'Failed to perform surveillance at database.surveilled_glider() - %s' % e) return val_return