eBike-SQLite/insertLoading.py

110 lines
1.9 KiB
Python
Raw Normal View History

2024-05-11 08:03:49 +02:00
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from datetime import datetime, timedelta
2024-05-11 09:36:15 +02:00
import sys
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
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 09:36:15 +02:00
exit(0)
2024-05-11 09:26:03 +02:00
2024-05-11 08:03:49 +02:00
##
### auf neue Files überwachen
def main():
2024-05-11 10:13:00 +02:00
global cursor
2024-05-11 09:26:03 +02:00
try:
db = sqlite3.connect(db_file)
cursor = db.cursor()
print("Connected to SQLite")
2024-05-11 10:13:00 +02:00
updateLast()
2024-05-11 10:10:25 +02:00
db.commit()
print("Data updated successfully into bike_loading table")
2024-05-11 10:13:00 +02:00
insertNew()
2024-05-11 10:10:25 +02:00
db.commit()
print("Data inserted successfully into bike_drives table")
cursor.close()
2024-05-11 09:26:03 +02:00
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")
2024-05-11 10:13:00 +02:00
def updateLast():
global cursor, watt
2024-05-11 09:26:03 +02:00
## hole letztes Ladungs-Datum
sql = '''
2024-05-11 09:36:15 +02:00
SELECT id, l_date FROM bike_loading ORDER BY l_date DESC LIMIT 1;
2024-05-11 09:26:03 +02:00
'''
cursor.execute(sql)
row = cursor.fetchone()
2024-05-11 09:52:44 +02:00
ins_id = row[0]
2024-05-11 09:26:03 +02:00
## hole alle Fahrten nach letzter Ladung
sql = '''
2024-05-11 09:57:25 +02:00
SELECT d_total_distance, d_total_ascent, d_avg_speed FROM bike_drives WHERE d_date >= ?;
2024-05-11 09:26:03 +02:00
'''
2024-05-11 09:59:07 +02:00
cursor.execute(sql, (row[1],))
2024-05-11 09:26:03 +02:00
rows = cursor.fetchall()
total_range = 0
total_ascent = 0
avg_speed = 0
fahrten = 0
2024-05-11 09:54:50 +02:00
print("Anzahl:", len(rows))
2024-05-11 09:26:03 +02:00
for row in rows:
fahrten += 1
2024-05-11 09:52:44 +02:00
total_range += row[0]
total_ascent += row[1]
avg_speed += row[2]
2024-05-11 09:26:03 +02:00
avg_speed = round(avg_speed / fahrten, 1)
2024-05-11 10:06:20 +02:00
data = (total_range, total_ascent, watt, fahrten, ins_id)
2024-05-11 09:26:03 +02:00
sql = '''
UPDATE bike_loading SET
2024-05-11 10:02:28 +02:00
l_total_range = ?,
l_total_ascent = ?,
l_loaded_wh = ?,
2024-05-11 10:02:03 +02:00
l_fahrten = ?
2024-05-11 10:06:20 +02:00
WHERE id = ?
2024-05-11 09:26:03 +02:00
'''
cursor.execute(sql, data)
2024-05-11 10:13:00 +02:00
def insertNew():
global cursor, loading
2024-05-11 09:26:03 +02:00
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)
2024-05-11 08:03:49 +02:00
if __name__ == '__main__':
main()
2024-05-11 09:26:03 +02:00