DB config

This commit is contained in:
Manuel Weiser 2024-05-11 09:26:03 +02:00
parent 14486c2e1c
commit 57102208bc
2 changed files with 83 additions and 132 deletions

View File

@ -1,153 +1,103 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from fitparse import FitFile
from datetime import datetime, timedelta from datetime import datetime, timedelta
import os, sys, shutil import sqlite3
from os import walk
import csv
import pandas as pd
path = "/home/manuel/Dropbox/Apps/HealthFitExporter/" db_file = "/www/wwwroot/www.manuelw.de/ebike/ebike.db"
#path = "/Users/manuel/Projekte/eBike-Influx-Calculation/"
ext = ('.fit')
csv_file = "/www/wwwroot/www.manuelw.de/ebike/ebike_data.csv"
output = {}
fitData = {}
fitData["total_distance"] = 0
fitData["total_ascent"] = 0
fitData["avg_speed"] = 0
fitData["avg_speed_count"] = 0
fitFilesCount = 0 fitFilesCount = 0
loading = sys.argv[1] try:
watt = sys.argv[2] loading = sys.argv[1]
row = [] watt = sys.argv[2]
except:
print("Fehlende Command Line Inputs")
## ##
### auf neue Files überwachen ### auf neue Files überwachen
def main(): def main():
f = []
for base, dirs, files in os.walk(path):
files = [ fi for fi in files if fi.endswith(".fit") ]
if files:
processFIT(files)
##
### parse FIT file und bestimme Handling
def processFIT(files):
global fitFilesCount
print("Files:", len(files))
fitFilesCount = len(files)
for filename in files:
#print(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)
showEnd()
readCSV()
deleteFiles()
##
### todo on all fit files
def doAll(file):
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["avg_speed_count"] +=1
#fitData["total_ascent"] = float(record.get_value("total_ascent")) if record.get_value("total_ascent") != None else 0
fitData["total_distance"] += float(round(record.get_value("total_distance") / 1000, 1))
fitData["avg_speed"] = round(float(fitData["avg_speed"] / fitData["avg_speed_count"]), 1)
doHealthFit(file, record.get_value("min_altitude"))
def doHealthFit(file, min_alt=0.0):
### 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)
fitData["total_ascent"] = round(float(fitData["total_ascent"]), 1)
def showEnd():
print("Akku geladen",loading)
print("Ladung Wh",watt)
print("Total Range", fitData["total_distance"])
print("Total Ascent", fitData["total_ascent"])
print("AVG Speed", fitData["avg_speed"])
def readCSV():
global fitData, fitFilesCount
with open(csv_file, "r", encoding="utf-8", errors="ignore") as scraped:
row = []
try: try:
row.extend(scraped.readlines()[-1].replace("\n", "").split(",")) db = sqlite3.connect(db_file)
row = row[0:-5] cursor = db.cursor()
dropCSV() print("Connected to SQLite")
row.extend([fitData["total_distance"], fitData["total_ascent"], fitData["avg_speed"], watt, fitFilesCount])
writeCSV(row)
except: updateLast(cursor)
print("EXCEPTION") insertNew(cursor)
row.extend(["Date", "Loading", "Total Range", "Total Ascent", "AVG Speed", "Loaded Wh", "Fahrten"])
writeCSV(row) except sqlite3.Error as error:
print("Failed to insert data into sqlite table", error)
finally:
if db:
db.close()
print("The SQLite connection is closed")
def writeCSV(row): def updateLast(cursor):
with open(csv_file, 'a', newline='') as csvfile: global watt
spamwriter = csv.writer(csvfile, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
spamwriter.writerow(row) ## hole letztes Ladungs-Datum
spamwriter.writerow([datetime.today().strftime('%d-%m-%Y'), loading, 0, 0, 0, 0, 0]) sql = '''
SELECT id, l_date FROM bike_loading ORDER BY d_date DESC LIMIT 1;
'''
cursor.execute(sql)
row = cursor.fetchone()
ins_id = row["id"]
## hole alle Fahrten nach letzter Ladung
sql = '''
SELECT * FROM bike_drives WHERE d_date >= 'row["l_date"]';
'''
cursor.execute(sql)
rows = cursor.fetchall()
total_range = 0
total_ascent = 0
avg_speed = 0
fahrten = 0
for row in rows:
fahrten += 1
total_range += row["d_total_distance"]
total_ascent += row["d_total_ascent"]
avg_speed += row["d_avg_speed"]
avg_speed = round(avg_speed / fahrten, 1)
data = (total_range, total_ascent, watt, fahrten)
sql = '''
UPDATE bike_loading SET
(l_total_range, l_total_ascent, l_loaded_wh, l_fahrten)
VALUES (?, ?, ?, ?)
WHERE id = ins_id
'''
cursor.execute(sql, data)
db.commit()
print("Data updated successfully into bike_loading table")
def dropCSV(): def insertNew(cursor):
df = pd.read_csv(csv_file, index_col='Date', on_bad_lines='skip') global loading
df = df.iloc[:-1]
df.to_csv(csv_file, index=True)
date = datetime.today().strftime('%Y-%m-%d %H:%M:%S')
data = (date, loading)
sql = '''
INSERT INTO bike_loading
(l_date, l_loading)
VALUES
(?, ?)
'''
def deleteFiles(): cursor.execute(sql, data)
for filename in os.listdir(path): db.commit()
file_path = os.path.join(path, filename) print("Data inserted successfully into bike_drives table")
try: cursor.close()
if os.path.isfile(file_path) or os.path.islink(file_path):
os.unlink(file_path)
#print("lösche... test")
elif os.path.isdir(file_path):
shutil.rmtree(file_path)
except Exception as e:
print('Failed to delete %s. Reason: %s' % (file_path, e))
if __name__ == '__main__': if __name__ == '__main__':
main() main()

View File

@ -102,6 +102,7 @@ def writeDB():
try: try:
db = sqlite3.connect(db_file) db = sqlite3.connect(db_file)
cursor = db.cursor() cursor = db.cursor()
print("Connected to SQLite")
sql = ''' sql = '''
INSERT INTO bike_drives INSERT INTO bike_drives