def process(self, db_params): """Process database data for an activity into a an XML tree in TCX format.""" garmin_act_db = GarminDB.ActivitiesDB(db_params, self.debug - 1) with garmin_act_db.managed_session() as garmin_act_db_session: activity = GarminDB.Activities.s_get(garmin_act_db_session, self.activity_id) self.tcx = GarminDbTcx() self.tcx.create(activity.sport, activity.start_time) laps = GarminDB.ActivityLaps.s_get_activity( garmin_act_db_session, self.activity_id) records = GarminDB.ActivityRecords.s_get_activity( garmin_act_db_session, self.activity_id) for lap in laps: distance = Distance.from_meters_or_feet( lap.distance, self.measurement_system) track = self.tcx.add_lap(lap.start_time, lap.stop_time, distance, lap.calories) for record in records: if record.timestamp >= lap.start_time and record.timestamp <= lap.stop_time: alititude = Distance.from_meters_or_feet( record.altitude, self.measurement_system) speed = Speed.from_kph_or_mph(record.speed, self.measurement_system) self.tcx.add_point(track, record.timestamp, record.position, alititude, record.hr, speed) garmindb = GarminDB.GarminDB(db_params) with garmindb.managed_session() as garmin_db_session: file = GarminDB.File.s_get(garmin_db_session, self.activity_id) device = GarminDB.Device.s_get(garmin_db_session, file.serial_number) self.tcx.add_creator(device.product, file.serial_number)
def test_distance(self): distance = Distance.from_mm(1000000) self.assertEqual(distance.value, 1000.0) self.assertEqual(distance.to_mm(), 1000000.0) self.assertEqual(distance.to_meters(), 1000.0) self.assertEqual(distance.to_kms(), 1.0) self.assertEqual(distance.to_mm(), 1000000) self.assertEqual(distance.to_miles(), 0.6213712)
def test_loop(self): sport = Sport.boating.name start_time = datetime.datetime.now() stop_time = start_time + datetime.timedelta(hours=1) product = GarminProduct.Fenix.name serial_number = 123412341234 version = (1, 2, 3, 4) lap_distance = Distance.from_meters(10) lap_calories = 100 position1 = Location(1, 2) position2 = Location(3, 4) record_alititude = Distance.from_meters(100) record_hr = 100 record_speed = Speed.from_mph(6.0) # create a TCX XML tree tcx = GarminDbTcx() tcx.create(sport, start_time) tcx.add_creator(product, serial_number, version=version) track = tcx.add_lap(start_time, stop_time, lap_distance, lap_calories) tcx.add_point(track, start_time, position1, record_alititude, record_hr, record_speed) tcx.add_point(track, stop_time, position2, record_alititude, record_hr, record_speed) # now read it back tcx.update() self.assertEqual(tcx.sport, sport) self.assertEqual(tcx.start_time, start_time) self.assertEqual(tcx.end_time, stop_time) self.assertEqual(tcx.calories, lap_calories) self.assertEqual(tcx.distance, lap_distance) self.assertEqual(tcx.duration, 60 * 60) self.assertEqual(tcx.start_loc, position1) self.assertEqual(tcx.end_loc, position2) self.assertEqual(tcx.creator_product, product) self.assertEqual(int(tcx.creator_serialnumber), serial_number) self.assertEqual(tcx.creator_version, version) logger.info('hr avg: %f', tcx.hr_avg) logger.info('hr max: %f', tcx.hr_max)
def get_point_altitude(self, point): """Return the altitude of the trackpoint.""" return Distance.from_meters(super().get_point_altitude(point))
def get_lap_distance(self, lap): """Return the recorded distance for the lap.""" return Distance.from_meters(super().get_lap_distance(lap))
def descent(self): """Return the total descent over the activity.""" return Distance.from_meters(super().descent)
def distance(self): """Return the total distance recorded for the activity.""" return Distance.from_meters(super().distance)