diff --git a/insertLoading.py b/insertLoading.py index f40ee19..b31cbcc 100644 --- a/insertLoading.py +++ b/insertLoading.py @@ -46,50 +46,52 @@ def updateLast(db, cursor): cursor.execute(sql) row = cursor.fetchone() - ins_id = row[0] - - ## hole alle Fahrten nach letzter Ladung - sql = ''' - SELECT d_total_distance, d_total_ascent, d_avg_speed FROM bike_drives WHERE d_date >= ?; - ''' - cursor.execute(sql, (row[1],)) - rows = cursor.fetchall() - - total_range = 0 - total_ascent = 0 - avg_speed = 0 - fahrten = 0 - - print("Anzahl:", len(rows)) - if len(rows) < 1: - cursor.close() - if db: - db.close() - print("The SQLite connection is closed") - exit(0) - - for row in rows: - fahrten += 1 - total_range += row[0] - total_ascent += row[1] - avg_speed += row[2] + if row[0]: + ins_id = row[0] + + ## hole alle Fahrten nach letzter Ladung + sql = ''' + SELECT d_total_distance, d_total_ascent, d_avg_speed FROM bike_drives WHERE d_date >= ?; + ''' + cursor.execute(sql, (row[1],)) + rows = cursor.fetchall() + + total_range = 0 + total_ascent = 0 + avg_speed = 0 + fahrten = 0 + + print("Anzahl:", len(rows)) + if len(rows) < 1: + print("Keine neuen Daten vorhanden, beende jetzt.") + cursor.close() + if db: + db.close() + print("The SQLite connection is closed") + exit(0) + + for row in rows: + fahrten += 1 + total_range += row[0] + total_ascent += row[1] + avg_speed += row[2] + + avg_speed = round(avg_speed / fahrten, 1) + + data = (total_range, total_ascent, avg_speed, watt, fahrten, ins_id) + sql = ''' + UPDATE bike_loading SET + l_total_range = ?, + l_total_ascent = ?, + l_avg_speed = ?, + l_loaded_wh = ?, + l_fahrten = ? + WHERE id = ? + ''' + cursor.execute(sql, data) + db.commit() + print("Data updated successfully into bike_loading table") - avg_speed = round(avg_speed / fahrten, 1) - - data = (total_range, total_ascent, avg_speed, watt, fahrten, ins_id) - sql = ''' - UPDATE bike_loading SET - l_total_range = ?, - l_total_ascent = ?, - l_avg_speed = ?, - l_loaded_wh = ?, - l_fahrten = ? - WHERE id = ? - ''' - cursor.execute(sql, data) - db.commit() - print("Data updated successfully into bike_loading table") - def insertNew(db, cursor): global loading diff --git a/parseFitFiles.py b/parseFitFiles.py index 747d8b9..6f1406d 100644 --- a/parseFitFiles.py +++ b/parseFitFiles.py @@ -29,6 +29,7 @@ def main(): if type_names[0] == "IN_MOVED_TO": if filename.endswith(ext): processFIT(filename) + deleteFiles(filename) pass @@ -124,13 +125,16 @@ def writeDB(): if db: db.close() print("The SQLite connection is closed") - - - #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"] ]) +def deleteFiles(filename): + try: + file = filename.remove(ext) + os.unlink(path+file+".*") + print("lösche", file) + except Exception as e: + print('Failed to delete %s. Reason: %s' % (path+filename, e)) + if __name__ == '__main__': main()