fix, logger, except
This commit is contained in:
@@ -74,7 +74,6 @@ def list_clients(datas, userdata):
|
|||||||
[userdata['cur_clients'].remove(item) for item in delta_clients_del]
|
[userdata['cur_clients'].remove(item) for item in delta_clients_del]
|
||||||
|
|
||||||
def ctrl_client_mod(client, userdata, message):
|
def ctrl_client_mod(client, userdata, message):
|
||||||
|
|
||||||
command_functions = {
|
command_functions = {
|
||||||
"createClient": create_client,
|
"createClient": create_client,
|
||||||
"deleteClient": delete_client,
|
"deleteClient": delete_client,
|
||||||
|
|||||||
@@ -7,6 +7,10 @@ import sys
|
|||||||
import os
|
import os
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
# Configurazione Logging
|
||||||
|
logging.basicConfig(level=logging.INFO, format='- PID: %(process)d %(levelname)8s: %(message)s', stream=sys.stderr)
|
||||||
|
logger = logging.getLogger()
|
||||||
|
|
||||||
# Configurazione connessione PostgreSQL
|
# Configurazione connessione PostgreSQL
|
||||||
DB_CONFIG = {
|
DB_CONFIG = {
|
||||||
"dbname": os.getenv("DB_NAME"),
|
"dbname": os.getenv("DB_NAME"),
|
||||||
@@ -26,7 +30,7 @@ def get_credentials(args):
|
|||||||
}
|
}
|
||||||
response = requests.post(url, json=data)
|
response = requests.post(url, json=data)
|
||||||
if response.status_code != 200:
|
if response.status_code != 200:
|
||||||
logging.error(f"Error to get pwd from wallet.")
|
logger.error(f"Error to get pwd from wallet.")
|
||||||
exit(1)
|
exit(1)
|
||||||
|
|
||||||
return response.json().get('password')
|
return response.json().get('password')
|
||||||
@@ -66,11 +70,11 @@ def init_db(args):
|
|||||||
""")
|
""")
|
||||||
conn.commit()
|
conn.commit()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.error(f"Errore durante l'inizializzazione del database: {e}")
|
logger.error(f"Errore durante l'inizializzazione del database: {e}")
|
||||||
exit(1)
|
exit(1)
|
||||||
finally:
|
finally:
|
||||||
conn.close()
|
conn.close()
|
||||||
logging.info("Database inizializzato.")
|
logger.info("Database inizializzato.")
|
||||||
|
|
||||||
def create_nested_json(path, data):
|
def create_nested_json(path, data):
|
||||||
keys = path.split('/')[1:]
|
keys = path.split('/')[1:]
|
||||||
@@ -94,7 +98,7 @@ def receive_data(client, userdata, message):
|
|||||||
""")
|
""")
|
||||||
conn.commit()
|
conn.commit()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.error(f"Errore durante l'inserimento dei dati nel database: {e}")
|
logger.error(f"Errore durante l'inserimento dei dati nel database: {e}")
|
||||||
finally:
|
finally:
|
||||||
conn.close()
|
conn.close()
|
||||||
|
|
||||||
@@ -107,15 +111,9 @@ def main():
|
|||||||
parser.add_argument('-P', '--port', type=int, default=1883)
|
parser.add_argument('-P', '--port', type=int, default=1883)
|
||||||
parser.add_argument('-c', '--client')
|
parser.add_argument('-c', '--client')
|
||||||
parser.add_argument('-w', '--wallet', default="http://mqtt:5000/")
|
parser.add_argument('-w', '--wallet', default="http://mqtt:5000/")
|
||||||
parser.add_argument('-L', '--log_level', choices=['DEBUG', 'INFO', 'WARNING', 'ERROR'], default='INFO')
|
|
||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
logging.basicConfig(
|
|
||||||
format="- PID: %(process)d %(levelname)8s: %(message)s ",
|
|
||||||
level=args.log_level,
|
|
||||||
filename=f"/var/log/{args.client.removesuffix('_ase')}_receiver.log"
|
|
||||||
)
|
|
||||||
init_db(args)
|
init_db(args)
|
||||||
|
|
||||||
auth = {'username': args.client, 'password': get_credentials(args)}
|
auth = {'username': args.client, 'password': get_credentials(args)}
|
||||||
@@ -128,7 +126,7 @@ def main():
|
|||||||
auth=auth, client_id=f'{args.client.removesuffix("_ase")}_client_ase',
|
auth=auth, client_id=f'{args.client.removesuffix("_ase")}_client_ase',
|
||||||
userdata=userdata)
|
userdata=userdata)
|
||||||
except (KeyboardInterrupt, Exception) as e:
|
except (KeyboardInterrupt, Exception) as e:
|
||||||
logging.info(f"Terminating: ....{e}")
|
logger.info(f"Terminating: ....{e}")
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
Reference in New Issue
Block a user