121 lines
3.4 KiB
Python
121 lines
3.4 KiB
Python
|
#!/usr/bin/env python3
|
||
|
# -*- coding: utf-8 -*-
|
||
|
|
||
|
from fitparse import FitFile
|
||
|
from datetime import datetime
|
||
|
import inotify.adapters
|
||
|
import os, sys, shutil
|
||
|
import sqlite3
|
||
|
|
||
|
|
||
|
path = "/home/manuel/Dropbox/Apps/HealthFitExporter/"
|
||
|
#path = "/Users/manuel/Projekte/eBike-Influx-Calculation/test/"
|
||
|
ext = ('.fit')
|
||
|
db_file = "/www/wwwroot/www.manuelw.de/ebike/ebike.db"
|
||
|
#db_file = "/Users/manuel/Projekte/eBike-Influx-Calculation/ebike.db"
|
||
|
|
||
|
fitData = {}
|
||
|
|
||
|
##
|
||
|
### auf neue Files überwachen
|
||
|
def main():
|
||
|
i = inotify.adapters.InotifyTree(path)
|
||
|
|
||
|
for event in i.event_gen(yield_nones=False):
|
||
|
(_, type_names, fpath, filename) = event
|
||
|
|
||
|
if event != None:
|
||
|
print("PATH=[{}] FILENAME=[{}] EVENT_TYPES={}".format(path, filename, type_names))
|
||
|
if type_names[0] == "IN_MOVED_TO":
|
||
|
if filename.endswith(ext):
|
||
|
processFIT(filename)
|
||
|
|
||
|
pass
|
||
|
|
||
|
##
|
||
|
### parse FIT file und bestimme Handling
|
||
|
def processFIT(filename):
|
||
|
file = FitFile(path+filename)
|
||
|
for record in file.get_messages("session"):
|
||
|
## nur wenn Radfahren
|
||
|
if record.get_value("sport") == "cycling":
|
||
|
## Verarbeitung starten
|
||
|
doAll(file)
|
||
|
|
||
|
writeDB()
|
||
|
|
||
|
|
||
|
##
|
||
|
### todo on all fit files
|
||
|
def doAll(file):
|
||
|
global fitData
|
||
|
|
||
|
for record in file.get_messages("session"):
|
||
|
fitData["avg_cadence"] = record.get_value("avg_cadence")
|
||
|
#fitData["avg_speed"] += round(float(record.get_value("avg_speed")) / 0.27777777777778, 1)
|
||
|
fitData["avg_speed"] = record.get_value("avg_speed")
|
||
|
#fitData["total_ascent"] = float(record.get_value("total_ascent")) if record.get_value("total_ascent") != None else 0
|
||
|
fitData["total_distance"] = round(record.get_value("total_distance") / 1000, 1)
|
||
|
fitData["date"] = record.get_value("start_time").strftime("%d.%m.%Y")
|
||
|
fitData["total_calories"] = record.get_value("total_calories")
|
||
|
if record.get_value("max_altitude"):
|
||
|
fitData["max_altitude"] = round(float(record.get_value("max_altitude")), 1)
|
||
|
else:
|
||
|
fitData["max_altitude"] = 0
|
||
|
fitData["max_heart_rate"] = record.get_value("max_heart_rate")
|
||
|
fitData["avg_heart_rate"] = record.get_value("avg_heart_rate")
|
||
|
|
||
|
fitData["avg_speed"] = round(float(fitData["avg_speed"]), 1)
|
||
|
doHealthFit(file, record.get_value("min_altitude"))
|
||
|
|
||
|
|
||
|
def doHealthFit(file, min_alt=0.0):
|
||
|
global fitData
|
||
|
|
||
|
### Kadenz
|
||
|
i=0
|
||
|
cadence=0
|
||
|
last_elev=min_alt
|
||
|
ges_elev=0.0
|
||
|
|
||
|
for record in file.get_messages("record"):
|
||
|
## korrekte Kadenz ermitteln
|
||
|
if record.get_value("cadence") != None and record.get_value("cadence") > 0:
|
||
|
i+=1
|
||
|
cadence += record.get_value("cadence")
|
||
|
|
||
|
## korrekte Höhenmeter ermitteln
|
||
|
if record.get_value("altitude") != None:
|
||
|
if record.get_value("altitude") > last_elev:
|
||
|
ges_elev += record.get_value("altitude") - last_elev
|
||
|
|
||
|
last_elev = record.get_value("altitude")
|
||
|
|
||
|
#fitData["avg_cadence"] = int(cadence/i)
|
||
|
fitData["total_ascent"] = round(float(ges_elev), 1)
|
||
|
|
||
|
|
||
|
def writeDB():
|
||
|
global fitData, db_file
|
||
|
|
||
|
db = sqlite3.connect(db_file)
|
||
|
cursor = db.cursor()
|
||
|
|
||
|
data = [fitData["date"], fitData["total_distance"], fitData["total_ascent"], fitData["max_altitude"], fitData["avg_speed"],
|
||
|
fitData["avg_heart_rate"], fitData["total_calories"]]
|
||
|
|
||
|
cursor.executemany('INSERT INTO bike_drives VALUES (?)', data)
|
||
|
db.commit()
|
||
|
db.close()
|
||
|
|
||
|
|
||
|
#row.extend(["Date", "Total Distance", "Total Ascent", "Max Alt.", "AVG Speed", "AVG HR", "Calories"])
|
||
|
#row.extend([fitData["date"], fitData["total_distance"], fitData["total_ascent"], fitData["max_altitude"], fitData["avg_speed"],
|
||
|
# fitData["avg_heart_rate"], fitData["total_calories"] ])
|
||
|
|
||
|
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
main()
|
||
|
|
||
|
|