From 6867c9904dcc04e201e1b5a5fedebcc218f43f5f Mon Sep 17 00:00:00 2001 From: Florian Bezannier Date: Wed, 23 Dec 2020 10:05:50 +0100 Subject: [PATCH] fix sql conn --- MyPSACC.py | 16 ++-------------- web/callback.py | 7 ++----- web/db.py | 20 ++++++++++++++++++++ 3 files changed, 24 insertions(+), 19 deletions(-) create mode 100644 web/db.py diff --git a/MyPSACC.py b/MyPSACC.py index 125efa7..c4b6664 100644 --- a/MyPSACC.py +++ b/MyPSACC.py @@ -9,7 +9,6 @@ from json import JSONEncoder from hashlib import md5 from time import sleep -import pytz from oauth2_client.credentials_manager import CredentialManager, ServiceInformation import paho.mqtt.client as mqtt from requests import Response @@ -24,6 +23,8 @@ from threading import Semaphore, Timer from functools import wraps import sqlite3 +from web.db import conn + oauhth_url = {"clientsB2CPeugeot": "https://idpcvs.peugeot.com/am/oauth2/access_token", "clientsB2CCitroen": "https://idpcvs.citroen.com/am/oauth2/access_token", "clientsB2CDS": "https://idpcvs.driveds.com/am/oauth2/access_token", @@ -407,9 +408,6 @@ class MyPSACC: @staticmethod def record_position(vin, res: psac.models.status.Status): - conn = sqlite3.connect('info.db') - conn.execute( - "CREATE TABLE IF NOT EXISTS position (Timestamp DATETIME PRIMARY KEY, VIN TEXT, longitude REAL, latitude REAL, mileage REAL, level INTEGER);") longitude = res.last_position.geometry.coordinates[0] latitude = res.last_position.geometry.coordinates[1] date = res.last_position.properties.updated_at @@ -421,16 +419,11 @@ class MyPSACC: conn.commit() except sqlite3.IntegrityError: logger.debug("position already saved") - finally: - conn.close() @staticmethod def get_recorded_position(): - import sqlite3 from geojson import Feature, Point, FeatureCollection from geojson import dumps as geo_dumps - conn = sqlite3.connect('info.db') - conn.row_factory = sqlite3.Row res = conn.execute('SELECT * FROM position ORDER BY Timestamp'); features_list = [] for row in res: @@ -443,9 +436,6 @@ class MyPSACC: @staticmethod def get_trips() -> List[Trip]: - sqlite3.register_converter("DATETIME", convert_datetime) - conn = sqlite3.connect('info.db', detect_types=sqlite3.PARSE_DECLTYPES | sqlite3.PARSE_COLNAMES) - conn.row_factory = sqlite3.Row res = conn.execute('SELECT * FROM position ORDER BY Timestamp').fetchall() start = res[0] end = res[1] @@ -494,5 +484,3 @@ class MyPeugeotEncoder(JSONEncoder): return mpd -def convert_datetime(st): - return datetime.strptime(st.decode("utf-8"), "%Y-%m-%d %H:%M:%S+00:00").replace(tzinfo=pytz.UTC) \ No newline at end of file diff --git a/web/callback.py b/web/callback.py index 27e8d77..a045b4c 100644 --- a/web/callback.py +++ b/web/callback.py @@ -88,7 +88,7 @@ def charge_control(): charge_control.set_stop_hour([int(request.args["hour"]), int(request.args["minute"])]) if 'percentage' in request.args: charge_control.percentage_threshold = int(request.args['percentage']) - save_config() + save_config(myp) return jsonify(charge_control.get_dict()) @@ -154,7 +154,4 @@ dash_app.layout = dbc.Container(fluid=True, children=[ html.H1('My car info'), data_div ]) -conn = sqlite3.connect('info.db') -conn.create_function("update_trips",0,update_trips) -conn.execute("CREATE TRIGGER IF NOT EXISTS update_trigger AFTER INSERT ON position BEGIN SELECT update_trips(); END;") -conn.commit() + diff --git a/web/db.py b/web/db.py new file mode 100644 index 0000000..e6fb786 --- /dev/null +++ b/web/db.py @@ -0,0 +1,20 @@ +import sqlite3 +from datetime import datetime + +import pytz + +from web.callback import update_trips + + +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.create_function("update_trips",0,update_trips) +conn.execute("CREATE TRIGGER IF NOT EXISTS update_trigger AFTER INSERT ON position BEGIN SELECT update_trips(); END;") +conn.commit()