From cac194e11cc24937180571bcb60c146b7d702bd5 Mon Sep 17 00:00:00 2001 From: Florian Bezannier Date: Wed, 23 Dec 2020 10:44:35 +0100 Subject: [PATCH] add fct get_db --- MyPSACC.py | 7 ++++++- web/callback.py | 5 +++-- web/db.py | 16 ++++++++-------- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/MyPSACC.py b/MyPSACC.py index c4b6664..2209b01 100644 --- a/MyPSACC.py +++ b/MyPSACC.py @@ -23,7 +23,7 @@ from threading import Semaphore, Timer from functools import wraps import sqlite3 -from web.db import conn +from web.db import get_db oauhth_url = {"clientsB2CPeugeot": "https://idpcvs.peugeot.com/am/oauth2/access_token", "clientsB2CCitroen": "https://idpcvs.citroen.com/am/oauth2/access_token", @@ -414,9 +414,11 @@ class MyPSACC: mileage = res.timed_odometer.mileage level = res.energy[0]["level"] try: + conn = get_db() conn.execute("INSERT INTO position(Timestamp,VIN,longitude,latitude,mileage,level) VALUES(?,?,?,?,?,?)", (date, vin, longitude, latitude, mileage, level)) conn.commit() + conn.close() except sqlite3.IntegrityError: logger.debug("position already saved") @@ -424,6 +426,7 @@ class MyPSACC: def get_recorded_position(): from geojson import Feature, Point, FeatureCollection from geojson import dumps as geo_dumps + conn = get_db() res = conn.execute('SELECT * FROM position ORDER BY Timestamp'); features_list = [] for row in res: @@ -432,10 +435,12 @@ class MyPSACC: "level": row["level"]}) features_list.append(feature) feature_collection = FeatureCollection(features_list) + conn.close() return geo_dumps(feature_collection, sort_keys=True) @staticmethod def get_trips() -> List[Trip]: + conn = get_db() res = conn.execute('SELECT * FROM position ORDER BY Timestamp').fetchall() start = res[0] end = res[1] diff --git a/web/callback.py b/web/callback.py index ec74143..7b93abf 100644 --- a/web/callback.py +++ b/web/callback.py @@ -13,6 +13,7 @@ from MyPSACC import MyPSACC from web import figures from web.app import app, dash_app, myp, chc, save_config +from web.db import get_db @dash_app.callback(Output('trips_map', 'figure'), @@ -157,7 +158,7 @@ dash_app.layout = dbc.Container(fluid=True, children=[ data_div ]) -conn = sqlite3.connect('info.db', detect_types=sqlite3.PARSE_DECLTYPES | sqlite3.PARSE_COLNAMES) +conn = get_db() conn.create_function("update_trips", 0, update_trips) -conn.execute("CREATE TEMP TRIGGER IF NOT EXISTS update_trigger AFTER INSERT ON position BEGIN SELECT update_trips(); END;") +conn.execute("CREATE TEMP TRIGGER IF NOT EXISTS update_trigger AFTER INSERT ON main.position BEGIN SELECT update_trips(); END;") conn.commit() diff --git a/web/db.py b/web/db.py index 9839062..e252e48 100644 --- a/web/db.py +++ b/web/db.py @@ -7,11 +7,11 @@ def convert_datetime(st): return datetime.strptime(st.decode("utf-8"), "%Y-%m-%d %H:%M:%S+00:00").replace(tzinfo=pytz.UTC) -sqlite3.register_converter("DATETIME", convert_datetime) -conn = sqlite3.connect('info.db', detect_types=sqlite3.PARSE_DECLTYPES | sqlite3.PARSE_COLNAMES) -conn.row_factory = sqlite3.Row -conn.execute("CREATE TABLE IF NOT EXISTS position (Timestamp DATETIME PRIMARY KEY, VIN TEXT, longitude REAL, " - "latitude REAL, mileage REAL, level INTEGER);") -conn.commit() - - +def get_db(): + sqlite3.register_converter("DATETIME", convert_datetime) + conn = sqlite3.connect('info.db', detect_types=sqlite3.PARSE_DECLTYPES | sqlite3.PARSE_COLNAMES) + conn.row_factory = sqlite3.Row + conn.execute("CREATE TABLE IF NOT EXISTS position (Timestamp DATETIME PRIMARY KEY, VIN TEXT, longitude REAL, " + "latitude REAL, mileage REAL, level INTEGER);") + conn.commit() + return conn