add fct get_db

This commit is contained in:
Florian Bezannier
2020-12-23 10:44:35 +01:00
parent 3b50ecf4bb
commit cac194e11c
3 changed files with 17 additions and 11 deletions
+6 -1
View File
@@ -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]
+3 -2
View File
@@ -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()
+8 -8
View File
@@ -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