From b936311e59f86ae05de82c5c31a8722b99ec825e Mon Sep 17 00:00:00 2001 From: Pim van den Berg Date: Thu, 8 Apr 2021 14:19:37 +0200 Subject: [PATCH] refactor: put all export related stuff in itho_export.py --- itho-wpu.py | 25 +------------------------ itho_export.py | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 24 deletions(-) create mode 100644 itho_export.py diff --git a/itho-wpu.py b/itho-wpu.py index eb6eef0..0dacab9 100755 --- a/itho-wpu.py +++ b/itho-wpu.py @@ -6,7 +6,6 @@ import queue import sys import time import os -import datetime import json import db from collections import namedtuple @@ -19,29 +18,6 @@ stdout_log_handler.setFormatter(logging.Formatter("%(message)s")) logger.addHandler(stdout_log_handler) -def export_to_influxdb(action, measurements): - from influxdb import InfluxDBClient - - influx_client = InfluxDBClient( - host=os.getenv('INFLUXDB_HOST', 'localhost'), - port=os.getenv('INFLUXDB_PORT', 8086), - username=os.getenv('INFLUXDB_USERNAME', 'root'), - password=os.getenv('INFLUXDB_PASSWORD', 'root'), - database=os.getenv('INFLUXDB_DATABASE') - ) - json_body = [ - { - "measurement": action, - "time": datetime.datetime.utcnow().replace(microsecond=0).isoformat(), - "fields": measurements, - } - ] - try: - influx_client.write_points(json_body) - except Exception as e: - print('Failed to write to influxdb: ', e) - - actions = { "getnodeid": [0x90, 0xE0], "getserial": [0x90, 0xE1], @@ -217,6 +193,7 @@ def process_response(action, response, args, wpu): if action == "getdatalog": measurements = process_datalog(response, wpu) if args.export_to_influxdb: + from itho_export import export_to_influxdb export_to_influxdb(action, measurements) elif action == "getnodeid": process_nodeid(response) diff --git a/itho_export.py b/itho_export.py new file mode 100644 index 0000000..6dd7802 --- /dev/null +++ b/itho_export.py @@ -0,0 +1,25 @@ +import datetime +import os + + +def export_to_influxdb(action, measurements): + from influxdb import InfluxDBClient + + influx_client = InfluxDBClient( + host=os.getenv('INFLUXDB_HOST', 'localhost'), + port=os.getenv('INFLUXDB_PORT', 8086), + username=os.getenv('INFLUXDB_USERNAME', 'root'), + password=os.getenv('INFLUXDB_PASSWORD', 'root'), + database=os.getenv('INFLUXDB_DATABASE') + ) + json_body = [ + { + "measurement": action, + "time": datetime.datetime.utcnow().replace(microsecond=0).isoformat(), + "fields": measurements, + } + ] + try: + influx_client.write_points(json_body) + except Exception as e: + print('Failed to write to influxdb: ', e)