|
|
|
|
@@ -7,6 +7,10 @@ import sys
|
|
|
|
|
import os
|
|
|
|
|
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
|
|
|
|
|
DB_CONFIG = {
|
|
|
|
|
"dbname": os.getenv("DB_NAME"),
|
|
|
|
|
@@ -26,7 +30,7 @@ def get_credentials(args):
|
|
|
|
|
}
|
|
|
|
|
response = requests.post(url, json=data)
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
return response.json().get('password')
|
|
|
|
|
@@ -66,11 +70,11 @@ def init_db(args):
|
|
|
|
|
""")
|
|
|
|
|
conn.commit()
|
|
|
|
|
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)
|
|
|
|
|
finally:
|
|
|
|
|
conn.close()
|
|
|
|
|
logging.info("Database inizializzato.")
|
|
|
|
|
logger.info("Database inizializzato.")
|
|
|
|
|
|
|
|
|
|
def create_nested_json(path, data):
|
|
|
|
|
keys = path.split('/')[1:]
|
|
|
|
|
@@ -94,7 +98,7 @@ def receive_data(client, userdata, message):
|
|
|
|
|
""")
|
|
|
|
|
conn.commit()
|
|
|
|
|
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:
|
|
|
|
|
conn.close()
|
|
|
|
|
|
|
|
|
|
@@ -107,15 +111,9 @@ def main():
|
|
|
|
|
parser.add_argument('-P', '--port', type=int, default=1883)
|
|
|
|
|
parser.add_argument('-c', '--client')
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
|
|
logging.basicConfig(
|
|
|
|
|
format="- PID: %(process)d %(levelname)8s: %(message)s ",
|
|
|
|
|
level=args.log_level,
|
|
|
|
|
stream=sys.stderr
|
|
|
|
|
)
|
|
|
|
|
init_db(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',
|
|
|
|
|
userdata=userdata)
|
|
|
|
|
except (KeyboardInterrupt, Exception) as e:
|
|
|
|
|
logging.info(f"Terminating: ....{e}")
|
|
|
|
|
logger.info(f"Terminating: ....{e}")
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
main()
|