2024-05-11 08:03:49 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
from datetime import datetime, timedelta
|
2024-05-11 09:26:03 +02:00
|
|
|
import sqlite3
|
2024-05-11 08:03:49 +02:00
|
|
|
|
|
|
|
|
2024-05-11 09:26:03 +02:00
|
|
|
db_file = "/www/wwwroot/www.manuelw.de/ebike/ebike.db"
|
2024-05-11 08:03:49 +02:00
|
|
|
|
|
|
|
fitFilesCount = 0
|
|
|
|
|
2024-05-11 09:26:03 +02:00
|
|
|
try:
|
|
|
|
loading = sys.argv[1]
|
|
|
|
watt = sys.argv[2]
|
|
|
|
except:
|
|
|
|
print("Fehlende Command Line Inputs")
|
|
|
|
|
2024-05-11 08:03:49 +02:00
|
|
|
|
|
|
|
##
|
|
|
|
### auf neue Files überwachen
|
|
|
|
def main():
|
2024-05-11 09:26:03 +02:00
|
|
|
try:
|
|
|
|
db = sqlite3.connect(db_file)
|
|
|
|
cursor = db.cursor()
|
|
|
|
print("Connected to SQLite")
|
|
|
|
|
|
|
|
updateLast(cursor)
|
|
|
|
insertNew(cursor)
|
|
|
|
|
|
|
|
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 updateLast(cursor):
|
|
|
|
global watt
|
|
|
|
|
|
|
|
## hole letztes Ladungs-Datum
|
|
|
|
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 insertNew(cursor):
|
|
|
|
global loading
|
|
|
|
|
|
|
|
date = datetime.today().strftime('%Y-%m-%d %H:%M:%S')
|
|
|
|
data = (date, loading)
|
|
|
|
sql = '''
|
|
|
|
INSERT INTO bike_loading
|
|
|
|
(l_date, l_loading)
|
|
|
|
VALUES
|
|
|
|
(?, ?)
|
|
|
|
'''
|
|
|
|
|
|
|
|
cursor.execute(sql, data)
|
|
|
|
db.commit()
|
|
|
|
print("Data inserted successfully into bike_drives table")
|
|
|
|
cursor.close()
|
2024-05-11 08:03:49 +02:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|
|
|
|
|
2024-05-11 09:26:03 +02:00
|
|
|
|