Beispiel #1
0
 def __init__(self,
              paragraph_func=logger.info,
              heading_func=logger.info,
              debug=False):
     """Return an instance of the CheckUp class."""
     self.db_params = ConfigManager.get_db_params()
     self.paragraph_func = paragraph_func
     self.heading_func = heading_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]
Beispiel #2
0
def main(argv):
    """Import and analyze Microsoft Health data."""
    parser = argparse.ArgumentParser()
    parser.add_argument("-v", "--version", help="print the program's version", action='version', version=format_version(sys.argv[0]))
    parser.add_argument("-t", "--trace", help="Turn on debug tracing", type=int, default=0)
    modes_group = parser.add_argument_group('Modes')
    modes_group.add_argument("-i", "--input_file", help="Specifiy the CSV file to import into the database")
    modes_group.add_argument("--rebuild_db", help="Delete MSHealth db file and rebuild it.", action="store_true", default=False)
    modes_group.add_argument("--delete_db", help="Delete MSHealth db file.", action="store_true", default=False)
    args = parser.parse_args()

    root_logger = logging.getLogger()
    if args.trace:
        root_logger.setLevel(logging.DEBUG)
    else:
        root_logger.setLevel(logging.INFO)

    db_params = ConfigManager.get_db_params()

    if args.delete_db or args.rebuild_db:
        MSHealthDb.delete_db(db_params)
        if args.delete_db:
            sys.exit()

    mshealth_dir = ConfigManager.get_or_create_mshealth_dir()
    metric = ConfigManager.get_metric()

    msd = MSHealthData(args.input_file, mshealth_dir, db_params, metric, args.trace)
    if msd.file_count() > 0:
        msd.process_files()

    mshv = MSVaultData(args.input_file, mshealth_dir, db_params, metric, args.trace)
    if mshv.file_count() > 0:
        mshv.process_files()

    analyze = Analyze(db_params)
    analyze.get_years()
    analyze.summary()
Beispiel #3
0
def copy_data(overwite, latest, stats):
    """Copy data from a mounted Garmin USB device to files."""
    logger.info("___Copying Data___")
    copy = Copy(gc_config.device_mount_dir())

    settings_dir = ConfigManager.get_or_create_fit_files_dir()
    root_logger.info("Copying settings to %s", settings_dir)
    copy.copy_settings(settings_dir)

    if Statistics.activities in stats:
        activities_dir = ConfigManager.get_or_create_activities_dir()
        root_logger.info("Copying activities to %s", activities_dir)
        copy.copy_activities(activities_dir, latest)

    if Statistics.monitoring in stats:
        monitoring_dir = ConfigManager.get_or_create_monitoring_dir(datetime.datetime.now().year)
        root_logger.info("Copying monitoring to %s", monitoring_dir)
        copy.copy_monitoring(monitoring_dir, latest)

    if Statistics.sleep in stats:
        monitoring_dir = ConfigManager.get_or_create_monitoring_dir(datetime.datetime.now().year)
        root_logger.info("Copying sleep to %s", monitoring_dir)
        copy.copy_sleep(monitoring_dir, latest)
Beispiel #4
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)
Beispiel #5
0
 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)
Beispiel #6
0
 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)
Beispiel #7
0
 def test_copy_sleep(self):
     monitoring_dir = ConfigManager.get_or_create_monitoring_dir(
         datetime.datetime.now().year, test_dir=True)
     root_logger.info("Copying sleep to %s", monitoring_dir)
     self.copy.copy_sleep(monitoring_dir)
Beispiel #8
0
 def test_copy_settings(self):
     settings_dir = ConfigManager.get_or_create_fit_files_dir(test_dir=True)
     root_logger.info("Copying settings to %s", settings_dir)
     self.copy.copy_settings(settings_dir)
Beispiel #9
0
 def test_copy_activity(self):
     activities_dir = ConfigManager.get_or_create_activities_dir(
         test_dir=True)
     logger.info("Copying activities to %s", activities_dir)
     self.copy.copy_activities(activities_dir)
Beispiel #10
0
 def setUpClass(cls):
     cls.db_params = ConfigManager.get_db_params(test_db=True)
     cls.garmin_db = GarminDb(cls.db_params)
Beispiel #11
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))
Beispiel #12
0
from garmindb import GarminProfile, GarminWeightData, GarminSummaryData, GarminMonitoringFitData, GarminSleepData, GarminRhrData, GarminSettingsFitData, GarminHydrationData
from garmindb import GarminJsonSummaryData, GarminJsonDetailsData, GarminTcxData, GarminActivitiesFitData
from garmindb import ActivityExporter

from garmindb import ConfigManager, GarminConnectConfigManager, PluginManager
from garmindb import Statistics
from garmindb import OpenWithBaseCamp, OpenWithGoogleEarth


logging.basicConfig(filename='garmindb.log', filemode='w', level=logging.INFO)
logger = logging.getLogger(__file__)
logger.addHandler(logging.StreamHandler(stream=sys.stdout))
root_logger = logging.getLogger()

gc_config = GarminConnectConfigManager()
db_params_dict = ConfigManager.get_db_params()
plugin_manager = PluginManager(ConfigManager.get_or_create_plugins_dir(), db_params_dict)


stats_to_db_map = {
    Statistics.monitoring            : MonitoringDb,
    Statistics.steps                 : MonitoringDb,
    Statistics.itime                 : MonitoringDb,
    Statistics.sleep                 : GarminDb,
    Statistics.rhr                   : GarminDb,
    Statistics.weight                : GarminDb,
    Statistics.activities            : ActivitiesDb
}

summary_dbs = [GarminSummaryDb, SummaryDb]
Beispiel #13
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)