66 lines
2.5 KiB
Python
66 lines
2.5 KiB
Python
#!.venv/bin/python
|
|
from utils.database.connection import connetti_db
|
|
import logging
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
timestamp_cols = ['inserted_at', 'loaded_at', 'elaborated_at']
|
|
|
|
|
|
def load_data(cfg: object, matrice_valori: list) -> bool :
|
|
if not matrice_valori:
|
|
logger.info("Nulla da caricare.")
|
|
return True
|
|
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}, {timestamp_cols[status]} = now() where id = {id}')
|
|
conn.commit()
|
|
logging.info("Status updated.")
|
|
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}') |