Exemple #1
0
def download_data(overwite, latest, stats):
    """Download selected activity types from Garmin Connect and save the data in files. Overwrite previously downloaded data if indicated."""
    logger.info("___Downloading %s Data___", 'Latest' if latest else 'All')

    download = Download()
    if not download.login():
        logger.error("Failed to login!")
        sys.exit()

    if Statistics.activities in stats:
        if latest:
            activity_count = gc_config.latest_activity_count()
        else:
            activity_count = gc_config.all_activity_count()
        activities_dir = ConfigManager.get_or_create_activities_dir()
        root_logger.info("Fetching %d activities to %s", activity_count, activities_dir)
        download.get_activity_types(activities_dir, overwite)
        download.get_activities(activities_dir, activity_count, overwite)

    if Statistics.monitoring in stats:
        date, days = __get_date_and_days(MonitoringDb(db_params_dict), latest, MonitoringHeartRate, MonitoringHeartRate.heart_rate, 'monitoring')
        if days > 0:
            root_logger.info("Date range to update: %s (%d) to %s", date, days, ConfigManager.get_monitoring_base_dir())
            download.get_daily_summaries(ConfigManager.get_or_create_monitoring_dir, date, days, overwite)
            download.get_hydration(ConfigManager.get_or_create_monitoring_dir, date, days, overwite)
            download.get_monitoring(ConfigManager.get_or_create_monitoring_dir, date, days)
            root_logger.info("Saved monitoring files for %s (%d) to %s for processing", date, days, ConfigManager.get_monitoring_base_dir())

    if Statistics.sleep in stats:
        date, days = __get_date_and_days(GarminDb(db_params_dict), latest, Sleep, Sleep.total_sleep, 'sleep')
        if days > 0:
            sleep_dir = ConfigManager.get_or_create_sleep_dir()
            root_logger.info("Date range to update: %s (%d) to %s", date, days, sleep_dir)
            download.get_sleep(sleep_dir, date, days, overwite)
            root_logger.info("Saved sleep files for %s (%d) to %s for processing", date, days, sleep_dir)

    if Statistics.weight in stats:
        date, days = __get_date_and_days(GarminDb(db_params_dict), latest, Weight, Weight.weight, 'weight')
        if days > 0:
            weight_dir = ConfigManager.get_or_create_weight_dir()
            root_logger.info("Date range to update: %s (%d) to %s", date, days, weight_dir)
            download.get_weight(weight_dir, date, days, overwite)
            root_logger.info("Saved weight files for %s (%d) to %s for processing", date, days, weight_dir)

    if Statistics.rhr in stats:
        date, days = __get_date_and_days(GarminDb(db_params_dict), latest, RestingHeartRate, RestingHeartRate.resting_heart_rate, 'rhr')
        if days > 0:
            rhr_dir = ConfigManager.get_or_create_rhr_dir()
            root_logger.info("Date range to update: %s (%d) to %s", date, days, rhr_dir)
            download.get_rhr(rhr_dir, date, days, overwite)
            root_logger.info("Saved rhr files for %s (%d) to %s for processing", date, days, rhr_dir)
Exemple #2
0
def export_activity(debug, directory, export_activity_id):
    """Export an activity given its database id."""
    garmin_db = GarminDb(db_params_dict)
    measurement_system = Attributes.measurements_type(garmin_db)
    ae = ActivityExporter(directory, export_activity_id, measurement_system, debug)
    ae.process(db_params_dict)
    return ae.write('activity_%s.tcx' % export_activity_id)
Exemple #3
0
 def __init__(self, output_func=logger.info, debug=False):
     """Return an instance of the CheckUp class."""
     self.db_params = ConfigManager.get_db_params()
     self.output_func = output_func
     self.debug = debug
     self.garmin_db = GarminDb(self.db_params)
     self.measurement_system = Attributes.measurements_type(self.garmin_db)
     self.unit_strings = fitfile.units.unit_strings[self.measurement_system]
 def test_parse_uprofile(self):
     db_params = ConfigManager.get_db_params(test_db=True)
     gp = GarminProfile(db_params, self.file_path, debug=2)
     if gp.file_count() > 0:
         gp.process()
     gdb = GarminDb(db_params)
     measurement_system = Attributes.measurements_type(gdb)
     self.assertEqual(
         measurement_system, fitfile.field_enums.DisplayMeasure.statute,
         'DisplayMeasure expected %r found %r' %
         (fitfile.field_enums.DisplayMeasure.statute, measurement_system))
Exemple #5
0
 def setUpClass(cls):
     db_params = ConfigManager.get_db_params()
     cls.garmin_db = GarminDb(db_params)
     table_dict = {
         'attributes_table': Attributes,
         'device_table': Device,
         'device_info_table': DeviceInfo,
         'file_table': File,
         'weight_table': Weight,
         'stress_table': Stress,
         'sleep_table': Sleep,
         'sleep_events_table': SleepEvents,
         'resting_heart_rate_table': RestingHeartRate
     }
     super().setUpClass(cls.garmin_db, table_dict)
 def test_summary_json_file_import(self):
     db_params = ConfigManager.get_db_params(test_db=True)
     gjsd = GarminSummaryData(
         db_params,
         'test_files/json/monitoring/summary',
         latest=False,
         measurement_system=fitfile.field_enums.DisplayMeasure.statute,
         debug=2)
     if gjsd.file_count() > 0:
         gjsd.process()
     table_not_none_cols_dict = {
         DailySummary: [
             DailySummary.rhr, DailySummary.distance, DailySummary.steps,
             DailySummary.floors_goal
         ]
     }
     self.check_not_none_cols(GarminDb(db_params), table_not_none_cols_dict)
 def test_fit_file_import(self):
     db_params = ConfigManager.get_db_params(test_db=True)
     self.profile_function('fit_mon_import', self.fit_file_import,
                           db_params)
     test_mon_db = GarminDb(db_params)
     self.check_db_tables_exists(test_mon_db, {'device_table': Device})
     self.check_db_tables_exists(test_mon_db, {
         'file_table': File,
         'device_info_table': DeviceInfo
     }, self.gfd_file_count)
     table_not_none_cols_dict = {
         Monitoring: [
             Monitoring.timestamp, Monitoring.activity_type,
             Monitoring.duration
         ]
     }
     self.check_not_none_cols(MonitoringDb(db_params),
                              table_not_none_cols_dict)
 def setUpClass(cls):
     cls.garmin_act_db = ActivitiesDb(ConfigManager.get_db_params())
     table_dict = {
         'activities_table': Activities,
         'activity_laps_table': ActivityLaps,
         'activity_records_table': ActivityRecords,
         'run_activities_table': StepsActivities,
         'paddle_activities_table': PaddleActivities,
         'cycle_activities_table': CycleActivities,
     }
     super().setUpClass(cls.garmin_act_db, table_dict,
                        {Activities: [Activities.activity_id]})
     cls.test_db_params = ConfigManager.get_db_params(test_db=True)
     cls.plugin_manager = PluginManager(
         ConfigManager.get_or_create_plugins_dir(), cls.test_db_params)
     cls.test_mon_db = GarminDb(cls.test_db_params)
     cls.test_act_db = ActivitiesDb(cls.test_db_params, debug_level=1)
     cls.measurement_system = fitfile.field_enums.DisplayMeasure.statute
     print(f"db params {repr(cls.test_db_params)}")
Exemple #9
0
 def setUpClass(cls):
     cls.db_params = ConfigManager.get_db_params(test_db=True)
     cls.garmin_db = GarminDb(cls.db_params)
Exemple #10
0
def import_data(debug, latest, stats):
    """Import previously downloaded Garmin data into the database."""
    logger.info("___Importing %s Data___", 'Latest' if latest else 'All')

    # Import the user profile and/or settings FIT file first so that we can get the measurement system and some other things sorted out first.
    fit_files_dir = ConfigManager.get_or_create_fit_files_dir()
    gp = GarminProfile(db_params_dict, fit_files_dir, debug)
    if gp.file_count() > 0:
        gp.process()

    gsfd = GarminSettingsFitData(fit_files_dir, debug)
    if gsfd.file_count() > 0:
        gsfd.process_files(FitFileProcessor(db_params_dict, plugin_manager, debug))

    gdb = GarminDb(db_params_dict)
    measurement_system = Attributes.measurements_type(gdb)

    if Statistics.weight in stats:
        weight_dir = ConfigManager.get_or_create_weight_dir()
        gwd = GarminWeightData(db_params_dict, weight_dir, latest, measurement_system, debug)
        if gwd.file_count() > 0:
            gwd.process()

    if Statistics.monitoring in stats:
        monitoring_dir = ConfigManager.get_or_create_monitoring_base_dir()
        gsd = GarminSummaryData(db_params_dict, monitoring_dir, latest, measurement_system, debug)
        if gsd.file_count() > 0:
            gsd.process()

        ghd = GarminHydrationData(db_params_dict, monitoring_dir, latest, measurement_system, debug)
        if ghd.file_count() > 0:
            ghd.process()

        gfd = GarminMonitoringFitData(monitoring_dir, latest, measurement_system, debug)
        if gfd.file_count() > 0:
            gfd.process_files(MonitoringFitFileProcessor(db_params_dict, plugin_manager, debug))

    if Statistics.sleep in stats:
        sleep_dir = ConfigManager.get_or_create_sleep_dir()
        gsd = GarminSleepData(db_params_dict, sleep_dir, latest, debug)
        if gsd.file_count() > 0:
            gsd.process()

    if Statistics.rhr in stats:
        rhr_dir = ConfigManager.get_or_create_rhr_dir()
        grhrd = GarminRhrData(db_params_dict, rhr_dir, latest, debug)
        if grhrd.file_count() > 0:
            grhrd.process()

    if Statistics.activities in stats:
        activities_dir = ConfigManager.get_or_create_activities_dir()
        # Tcx fields are less precise than the JSON files, so load Tcx first and overwrite with better JSON values.
        gtd = GarminTcxData(activities_dir, latest, measurement_system, debug)
        if gtd.file_count() > 0:
            gtd.process_files(db_params_dict)

        gjsd = GarminJsonSummaryData(db_params_dict, activities_dir, latest, measurement_system, debug)
        if gjsd.file_count() > 0:
            gjsd.process()

        gdjd = GarminJsonDetailsData(db_params_dict, activities_dir, latest, measurement_system, debug)
        if gdjd.file_count() > 0:
            gdjd.process()

        gfd = GarminActivitiesFitData(activities_dir, latest, measurement_system, debug)
        if gfd.file_count() > 0:
            gfd.process_files(ActivityFitFileProcessor(db_params_dict, plugin_manager, debug))