def __process_file(self, file_name):
     tcx = GarminDbTcx()
     tcx.read(file_name)
     start_time = tcx.start_time
     (manufacturer, product) = tcx.get_manufacturer_and_product()
     serial_number = tcx.serial_number
     device = {
         'serial_number': serial_number,
         'timestamp': start_time,
         'manufacturer': manufacturer,
         'product': product,
         'hardware_version': None,
     }
     GarminDB.Device.s_insert_or_update(self.garmin_db_session,
                                        device,
                                        ignore_none=True)
     root_logger.info(
         "Processing file: %s for manufacturer %s product %s device %s",
         file_name, manufacturer, product, serial_number)
     (file_id, file_name) = GarminDB.File.name_and_id_from_path(file_name)
     file = {
         'id': file_id,
         'name': file_name,
         'type': GarminDB.File.FileType.tcx,
         'serial_number': serial_number,
     }
     GarminDB.File.s_insert_or_update(self.garmin_db_session, file)
     activity = {
         'activity_id': file_id,
         'start_time': start_time,
         'stop_time': tcx.end_time,
         'laps': tcx.lap_count,
         'sport': tcx.sport,
         'calories': tcx.calories,
         'distance': tcx.distance.kms_or_miles(self.measurement_system),
         'avg_hr': tcx.hr_avg,
         'max_hr': tcx.hr_max,
         'max_cadence': tcx.cadence_max,
         'avg_cadence': tcx.cadence_avg,
         'ascent': tcx.ascent.meters_or_feet(self.measurement_system),
         'descent': tcx.descent.meters_or_feet(self.measurement_system)
     }
     start_loc = tcx.start_loc
     if start_loc is not None:
         activity.update({
             'start_lat': start_loc.lat_deg,
             'start_long': start_loc.long_deg
         })
     end_loc = tcx.end_loc
     if end_loc is not None:
         activity.update({
             'stop_lat': end_loc.lat_deg,
             'stop_long': end_loc.long_deg
         })
     GarminDB.Activities.s_insert_or_update(self.garmin_act_db_session,
                                            activity,
                                            ignore_none=True,
                                            ignore_zero=True)
     for lap_number, lap in enumerate(tcx.laps):
         self.__process_lap(tcx, file_id, lap_number, lap)
 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)
Beispiel #3
0
 def check_activity_file(self, filename):
     tcx = GarminDbTcx()
     tcx.read(filename)
     (manufacturer, product) = tcx.get_manufacturer_and_product()
     logger.info(
         "%s: sport %r end_time %r start_time %r manufacturer %r product %r serial_number %r laps %r distance %r calories %r start %r end %r "
         "hr %r, %r max speed %r cadence %r, %r", filename, tcx.sport,
         tcx.end_time, tcx.start_time, manufacturer, product,
         tcx.serial_number, tcx.lap_count, tcx.distance, tcx.calories,
         tcx.start_loc, tcx.end_loc, tcx.hr_avg, tcx.hr_max, tcx.speed_max,
         tcx.cadence_avg, tcx.cadence_max)
     self.assertGreater(tcx.end_time, tcx.start_time)
     self.assertGreater(tcx.lap_count, 0)
class ActivityExporter(object):
    """Export activities as TCX files from database data."""
    def __init__(self, directory, activity_id, measurement_system, debug):
        """Return a instance of ActivityExporter ready to write a TCX file."""
        self.directory = directory
        self.activity_id = activity_id
        self.measurement_system = measurement_system
        self.debug = debug

    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 write(self, filename):
        """Write the TCX file to disk."""
        full_path = self.directory + os.path.sep + filename
        self.tcx.write(full_path)
        return full_path
Beispiel #5
0
 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)