def string_to_message(raw_string, reference_date): global receivers try: message = parse(raw_string, reference_date) except NotImplementedError as e: app.logger.error( "No parser implemented for message: {}".format(raw_string)) return None except ParseError as e: app.logger.error("Parsing error with message: {}".format(raw_string)) return None except TypeError as e: app.logger.error("TypeError with message: {}".format(raw_string)) return None except Exception as e: app.logger.error("Other Exception with string: {}".format(raw_string)) return None # update reference receivers and distance to the receiver if message["aprs_type"] == "position": if message[ "beacon_type"] in AIRCRAFT_BEACON_TYPES + RECEIVER_BEACON_TYPES: latitude = message["latitude"] longitude = message["longitude"] location = Location(longitude, latitude) message["location"] = location.to_wkt() location_mgrs = myMGRS.toMGRS(latitude, longitude).decode("utf-8") message["location_mgrs"] = location_mgrs message["location_mgrs_short"] = location_mgrs[ 0:5] + location_mgrs[5:7] + location_mgrs[10:12] if message[ "beacon_type"] in AIRCRAFT_BEACON_TYPES and "gps_quality" in message: if message["gps_quality"] is not None and "horizontal" in message[ "gps_quality"]: message["gps_quality_horizontal"] = message["gps_quality"][ "horizontal"] message["gps_quality_vertical"] = message["gps_quality"][ "vertical"] del message["gps_quality"] # TODO: Fix python-ogn-client 0.91 if "senders_messages" in message and message[ "senders_messages"] is not None: message["senders_messages"] = int(message["senders_messages"]) if "good_senders" in message and message["good_senders"] is not None: message["good_senders"] = int(message["good_senders"]) if "good_and_bad_senders" in message and message[ "good_and_bad_senders"] is not None: message["good_and_bad_senders"] = int(message["good_and_bad_senders"]) return message
def get_soaringspot_contests(url, client_id, secret): import math document = get_soaringspot_document(url, client_id, secret) print(document) contests = list() for item in document.items(): if item[0] == "contests": for contest_row in item[1]: parameters = { 'category': contest_row['category'], 'country': contest_row['country'], 'end_date': datetime.strptime(contest_row['end_date'], "%Y-%m-%d"), 'featured': contest_row['featured'], 'name': contest_row['name'], 'start_date': datetime.strptime(contest_row['start_date'], "%Y-%m-%d"), 'time_zone': contest_row['time_zone'] } contest = Contest(**parameters) location_row = contest_row['location'] parameters = { 'altitude': location_row['altitude'], 'country': location_row['country'], 'latitude': math.degrees(location_row['latitude']), 'longitude': math.degrees(location_row['longitude']), 'name': location_row['name'], 'time_zone': location_row['time_zone'] } location = Location(**parameters) contest.location = location for contest_class_row in contest_row['classes']: parameters = { 'category': contest_class_row['category'], 'type': contest_class_row['type'] } contest_class = ContestClass(**parameters) contest_class.contest = contest contestants_doc = get_soaringspot_document( contest_class_row.links['contestants'].url, client_id, secret) print(contestants_doc) if 'code' in contestants_doc and contestants_doc[ 'code'] == 404: print("No contestant") else: for contestant_row in contestants_doc['contestants']: parameters = { 'aircraft_model': contestant_row['aircraft_model'], 'aircraft_registration': contestant_row['aircraft_registration'], 'club': contestant_row['club'] if 'club' in contestant_row else None, 'contestant_number': contestant_row['contestant_number'], 'handicap': contestant_row['handicap'], 'live_track_id': contestant_row['live_track_id'], 'name': contestant_row['name'], 'not_competing': contestant_row['not_competing'], 'pure_glider': contestant_row['pure_glider'], 'sponsors': contestant_row['sponsors'] if 'sponsors' in contestant_row else None } contestant = Contestant(**parameters) contestant.contest_class = contest_class for pilot_row in contestant_row['pilot']: parameters = { 'civl_id': pilot_row['civl_id'], 'email': pilot_row['email'], 'first_name': pilot_row['first_name'], 'igc_id': pilot_row['igc_id'], 'last_name': pilot_row['last_name'], 'nationality': pilot_row['nationality'] } pilot = Pilot(**parameters) pilot.contestant = contestant tasks_doc = get_soaringspot_document( contest_class_row.links['tasks'].url, client_id, secret) print(tasks_doc) if 'code' in tasks_doc and tasks_doc['code'] == 404: print("No task") else: for task_row in tasks_doc['tasks']: parameters = { 'images': task_row['images'], 'no_start': datetime.strptime(task_row['no_start'], "%Y-%m-%dT%H:%M:%S"), 'result_status': task_row['result_status'], 'start_on_entry': task_row['start_on_entry'], 'task_date': datetime.strptime(task_row['task_date'], "%Y-%m-%d"), 'task_distance': task_row['task_distance'], 'task_distance_max': task_row['task_distance_max'], 'task_distance_min': task_row['task_distance_min'], 'task_duration': task_row['task_duration'], 'task_number': task_row['task_number'], 'task_type': task_row['task_type'], 'task_value': task_row['task_value'] } task = Task(**parameters) task.contest_class = contest_class points_doc = get_soaringspot_document( task_row.links['points'].url, client_id, secret) print(points_doc) if 'code' in points_doc and points_doc[ 'code'] == 404: print("No points") else: for point_row in points_doc['points']: parameters = { 'name': point_row['name'], 'latitude': math.degrees(point_row['latitude']), 'longitude': math.degrees(point_row['longitude']), 'elevation': point_row['elevation'], 'point_index': point_row['point_index'], 'type': point_row['type'], 'multiple_start': point_row['multiple_start'], 'distance': point_row['distance'], 'course_in': point_row['course_in'], 'course_out': point_row['course_out'], 'oz_type': point_row['oz_type'], 'oz_radius1': point_row['oz_radius1'], 'oz_radius2': point_row['oz_radius2'], 'oz_angle1': math.degrees(point_row['oz_angle1']), 'oz_angle12': math.degrees(point_row['oz_angle12']), 'oz_angle2': math.degrees(point_row['oz_angle2']), 'oz_line': point_row['oz_line'], 'oz_max_altitude': point_row['oz_max_altitude'], 'oz_move': point_row['oz_move'], 'oz_reduce': point_row['oz_reduce'], 'speed_section_type': point_row['speed_section_type'] } turnpoint = Turnpoint(**parameters) turnpoint.task = task contests.append(contest) return contests
def get_strepla_contest_body(competition_id): from app.utils import ddb_import contest_url = "http://www.strepla.de/scs/ws/competition.ashx?cmd=info&cId=" + str( competition_id) + "&daysPeriod=700" # print(contest_url) r = requests.get(contest_url) # Test if contest is present if (len(r.text)) == 2: raise ValueError("This contest does not exist: '{}' Aborting.".format( competition_id)) return contest_data = json.loads(r.text.encode('utf-8'))[0] # Process contest location and date info parameters = { 'end_date': datetime.strptime(contest_data['lastDay'], "%Y-%m-%dT%H:%M:%S"), 'name': contest_data['name'], 'start_date': datetime.strptime(contest_data['firstDay'], "%Y-%m-%dT%H:%M:%S") } contest = Contest(**parameters) parameters = {'name': contest_data['Location']} location = Location(**parameters) contest.location = location # Process contest class info contest_class_url = "http://www.strepla.de/scs/ws/compclass.ashx?cmd=overview&cid=" + str( competition_id) # print(contest_class_url) r = requests.get(contest_class_url) contest_class_data = json.loads(r.text.encode('utf-8')) ddb_entries = ddb_import() for contest_class_row in contest_class_data: parameters = { 'category': contest_class_row['rulename'], 'type': contest_class_row['name'] } contest_class = ContestClass(**parameters) contest_class.contest = contest # Process pilots of class contestant_url = "http://www.strepla.de/scs/ws/pilot.ashx?cmd=competitors&cId=" + str( competition_id) + "&cc=" + str(contest_class_row['name']) r = requests.get(contestant_url) contestant_data = json.loads(r.text.encode('utf-8')) if (len(contestant_data) == 0): print("Class name not recognized. Aborting.") return for contestant_row in contestant_data: parameters = { 'aircraft_model': contestant_row['glider_name'], 'aircraft_registration': contestant_row['glider_callsign'], 'contestant_number': contestant_row['glider_cid'], 'handicap': contestant_row['glider_index'], 'live_track_id': contestant_row['flarm_ID'] } contestant = Contestant(**parameters) contestant.contest_class = contest_class # Do some checks of the live_track_id if parameters['live_track_id']: print("Live_track_id defined via StrePla API") # Check if live_track_id is also in OGN DDB if parameters['live_track_id'] in ddb_entries.keys(): if parameters['aircraft_registration'] == ddb_entries[ parameters['live_track_id']]: print( "Live_track_id is also in OGN DDB. Registrations match. Plausible id." ) else: print( "Live_track_id is also in OGN DDB. Registrations DOES NOT match. Check id." ) else: print( "Live_track_id is not in OGN DDB. Plausibility check not possible." ) else: # Live_track_id not provided. Performing Lookup on OGN DDB. if contestant_row['glider_callsign'] in ddb_entries.values(): live_track_id = list(ddb_entries.keys())[list( ddb_entries.values()).index( contestant_row['glider_callsign'])] print( parameters['aircraft_registration'], "Live_track_id not provided. OGN DDB lookup found: ", live_track_id) parameters = {'live_track_id': live_track_id} else: print(parameters['aircraft_registration'], "Aircraft registration not found in OGN DDB.") parameters = { 'first_name': contestant_row['name'].rsplit(',', 1)[0], 'last_name': contestant_row['name'].split(',', 1)[0], 'nationality': contestant_row['country'] } pilot = Pilot(**parameters) pilot.contestant = contestant return contest
def test_insert_data(test_client): ##New user db.session.add( User(username="******", email="*****@*****.**", password=bcrypt.generate_password_hash("test").decode('utf-8'))) db.session.add( User(username="******", email="*****@*****.**", password=bcrypt.generate_password_hash("test").decode('utf-8'))) db.session.add( User(username="******", email="*****@*****.**", password=bcrypt.generate_password_hash("test").decode('utf-8'))) ##New Business db.session.add( Business(owner=User.query.get(1), name="Business1", description="Some description")) db.session.add( Business(owner=User.query.get(2), name="Business2", description="Some description")) db.session.add( Business(owner=User.query.get(3), name="Business3", description="Some description")) ##Add location db.session.add( Location(business=Business.query.get(1), county='Kiambu', region='Lari', location='Some location')) db.session.add( Location(business=Business.query.get(2), county='Nairobi', region='Ruiru', location='Membley')) db.session.add( Location(business=Business.query.get(3), county='Kajiado', region='Some region', location='Some location')) ##Add Category db.session.add( Category(business=Business.query.get(1), category='Some category')) db.session.add( Category(business=Business.query.get(2), category='Some category')) db.session.add( Category(business=Business.query.get(3), category='Some category')) ##Add review db.session.add( Review(business=Business.query.get(1), email='*****@*****.**', message='Some message')) db.session.add( Review(business=Business.query.get(2), email='*****@*****.**', message='Some message')) db.session.add( Review(business=Business.query.get(3), email='*****@*****.**', message='Some message')) db.session.commit()
def add(self, raw_string): try: message = parse(raw_string, reference_timestamp=self.reference_timestamp) except NotImplementedError as e: current_app.logger.error( "No parser implemented for message: {}".format(raw_string)) return except ParseError as e: current_app.logger.error( "Parsing error with message: {}".format(raw_string)) return except TypeError as e: current_app.logger.error( "TypeError with message: {}".format(raw_string)) return except Exception as e: current_app.logger.error( "Other Exception with string: {}".format(raw_string)) return if message['aprs_type'] not in ('server', 'position'): return elif message[ 'aprs_type'] == 'server' and self.auto_update_timestamp is True: self.reference_timestamp = message['timestamp'] return elif message['aprs_type'] == 'position': latitude = message["latitude"] longitude = message["longitude"] location = Location(longitude, latitude) message["location"] = location.to_wkt() location_mgrs = self.mgrs.toMGRS(latitude, longitude).decode("utf-8") message["location_mgrs"] = location_mgrs message["location_mgrs_short"] = location_mgrs[ 0:5] + location_mgrs[5:7] + location_mgrs[10:12] if "aircraft_type" in message: message["aircraft_type"] = AircraftType( message["aircraft_type"] ).name if message["aircraft_type"] in AircraftType.list( ) else AircraftType.UNKNOWN.name if "gps_quality" in message: if message[ "gps_quality"] is not None and "horizontal" in message[ "gps_quality"]: message["gps_quality_horizontal"] = message["gps_quality"][ "horizontal"] message["gps_quality_vertical"] = message["gps_quality"][ "vertical"] del message["gps_quality"] if message["beacon_type"] in RECEIVER_BEACON_TYPES: complete_message = ",".join([ str(message[k]) if k in message and message[k] is not None else "\\N" for k in BEACON_KEY_FIELDS + RECEIVER_BEACON_FIELDS ]) self.receiver_buffer.write(complete_message) self.receiver_buffer.write("\n") elif message["beacon_type"] in AIRCRAFT_BEACON_TYPES: complete_message = ",".join([ str(message[k]) if k in message and message[k] is not None else "\\N" for k in BEACON_KEY_FIELDS + AIRCRAFT_BEACON_FIELDS ]) self.aircraft_buffer.write(complete_message) self.aircraft_buffer.write("\n") else: current_app.logger.error("Ignore beacon_type: {}".format( message["beacon_type"])) return if datetime.utcnow() - self.last_flush >= timedelta(seconds=5): self._flush() self.last_flush = datetime.utcnow()
def create_simple_contest(): contest = Contest() contest.name = "My Fake Contest" contest.category = None contest.country = "FR" contest.end_date = date(2005, 9, 12) contest.featured = None contest.start_date = date(2005, 9, 3) contest.time_zone = None location = Location() location.name = "St. Auban" location.country = "FR" location.latitude = 44.1959 location.longitude = 5.98849 location.altitude = None contest.location = location open_class = ContestClass() open_class.category = "glider" open_class.type = "Open" open_class.contest = contest m18_class = ContestClass() m18_class.category = "glider" m18_class.type = "18-meter" m18_class.contest = contest contestant_open_1 = Contestant() contestant_open_1.aircraft_model = "Eta" contestant_open_1.aircraft_registration = "D-KONI" contestant_open_1.contestant_number = "KG" contestant_open_1.live_track_id = "FLRDD0815" contestant_open_1.contest_class = open_class pilot_open_1 = Pilot() pilot_open_1.first_name = "Konstantin" pilot_open_1.last_name = "Gründger" pilot_open_1.contestant = contestant_open_1 contestant_open_2 = Contestant() contestant_open_2.aircraft_model = "Nimeta" contestant_open_2.aircraft_registration = "D-1900" contestant_open_2.contestant_number = "XX" contestant_open_2.live_track_id = "FLRDD4711" contestant_open_2.contest_class = open_class pilot_open_2 = Pilot() pilot_open_2.first_name = "Dagobert" pilot_open_2.last_name = "Duck" pilot_open_2.contestant = contestant_open_2 contestant_18m_1 = Contestant() contestant_18m_1.aircraft_model = "ASG 29" contestant_18m_1.aircraft_registration = "D-KUGL" contestant_18m_1.contestant_number = "GL" contestant_18m_1.live_track_id = "OGN0123" contestant_18m_1.contest_class = m18_class pilot_18m_1 = Pilot() pilot_18m_1.first_name = "Gundel" pilot_18m_1.last_name = "Gaukelei" pilot_18m_1.contestant = contestant_18m_1 task_1 = Task() task_1.no_start = datetime(2015, 9, 3, 10, 0, 0) task_1.task_type = "High speed" task_1.task_date = date(2005, 9, 3) task_1.contest_class = open_class task_2 = Task() task_2.no_start = datetime(2015, 9, 4, 10, 0, 0) task_2.task_date = date(2005, 9, 4) task_2.contest_class = open_class task_3 = Task() task_3.no_start = datetime(2015, 9, 7, 10, 0, 0) task_3.task_date = date(2005, 9, 7) task_3.contest_class = open_class return contest