evol 5
This commit is contained in:
63
utils/database/loader_action.py
Normal file
63
utils/database/loader_action.py
Normal file
@@ -0,0 +1,63 @@
|
||||
#!.venv/bin/python
|
||||
from utils.database.connection import connetti_db
|
||||
import logging
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
CSV_RECEIVED = 0
|
||||
DATA_LOADED = 1
|
||||
DATA_ELABORATED = 2
|
||||
|
||||
def load_data(cfg: object, matrice_valori: list) -> bool :
|
||||
sql_insert_RAWDATA = f'''
|
||||
INSERT IGNORE INTO {cfg.dbname}.{cfg.dbrawdata} (
|
||||
`UnitName`,`ToolNameID`,`NodeNum`,`EventDate`,`EventTime`,`BatLevel`,`Temperature`,
|
||||
`Val0`,`Val1`,`Val2`,`Val3`,`Val4`,`Val5`,`Val6`,`Val7`,
|
||||
`Val8`,`Val9`,`ValA`,`ValB`,`ValC`,`ValD`,`ValE`,`ValF`,
|
||||
`BatLevelModule`,`TemperatureModule`, `RssiModule`
|
||||
)
|
||||
VALUES (
|
||||
%s, %s, %s, %s, %s, %s, %s,
|
||||
%s, %s, %s, %s, %s, %s, %s, %s,
|
||||
%s, %s, %s, %s, %s, %s, %s, %s,
|
||||
%s, %s, %s
|
||||
)
|
||||
'''
|
||||
with connetti_db(cfg) as conn:
|
||||
cur = conn.cursor()
|
||||
try:
|
||||
cur.executemany(sql_insert_RAWDATA, matrice_valori)
|
||||
conn.commit()
|
||||
logging.info("Data loaded.")
|
||||
rc = True
|
||||
except Exception as e:
|
||||
conn.rollback()
|
||||
logging.error(f"Error: {e}.")
|
||||
rc = False
|
||||
finally:
|
||||
conn.close()
|
||||
return rc
|
||||
|
||||
def update_status(cfg: object, id: int, status: int) -> None:
|
||||
with connetti_db(cfg) as conn:
|
||||
cur = conn.cursor()
|
||||
try:
|
||||
cur.execute(f'update {cfg.dbname}.{cfg.dbrectable} set locked = 0, status = {status} where id = {id}')
|
||||
conn.commit()
|
||||
except Exception as e:
|
||||
conn.rollback()
|
||||
logging.error(f'Error: {e}')
|
||||
|
||||
def get_matlab_cmd(cfg: object, unit: str, tool: str) -> tuple:
|
||||
with connetti_db(cfg) as conn:
|
||||
cur = conn.cursor()
|
||||
try:
|
||||
cur.execute(f'''select m.matcall, t.ftp_send , t.unit_id, s.`desc` as statustools, t.api_send, u.inoltro_api, u.inoltro_api_url, u.inoltro_api_bearer_token, IFNULL(u.duedate, "") as duedate
|
||||
from matfuncs as m
|
||||
inner join tools as t on t.matfunc = m.id
|
||||
inner join units as u on u.id = t.unit_id
|
||||
inner join statustools as s on t.statustool_id = s.id
|
||||
where t.name = "{tool}" and u.name = "{unit}"''')
|
||||
return cur.fetchone()
|
||||
except Exception as e:
|
||||
logging.error(f'Error: {e}')
|
||||
Reference in New Issue
Block a user