From ae26cdf3c65242e5c36646982857e8b522d701b7 Mon Sep 17 00:00:00 2001 From: tim Date: Thu, 21 Mar 2024 20:58:30 +0200 Subject: [PATCH] Shift functions around for poetry --- bmspy/bms.py | 96 ++++++++++++++++++++++++++++------------------------ 1 file changed, 51 insertions(+), 45 deletions(-) diff --git a/bmspy/bms.py b/bmspy/bms.py index d65c12d..ef2149c 100755 --- a/bmspy/bms.py +++ b/bmspy/bms.py @@ -29,6 +29,7 @@ try: writeapi = None except: can_export_influxdb = False + influxclient = None DAEMON_UPDATE_PERIOD = 30 SERIALPORT = "/dev/ttyUSB0" @@ -550,21 +551,6 @@ def collect_data(): return data -def main(): - global debug - data = dict() - while bool(data) is False: - data = collect_data() - time.sleep(1) - - if args.report_json: - print(json.dumps(data)) - - elif args.report_print: - pp = pprint.PrettyPrinter(indent=4) - pp.pprint(data) - - def prometheus_export(daemonize=True, filename=None): global debug if not can_export_prometheus: @@ -738,37 +724,39 @@ def influxdb_create_snapshot(data): return points +def parse_args(): + parser = argparse.ArgumentParser( + description='Query JBD BMS and report status', + add_help=True, + ) + parser.add_argument('--json', '-j', dest='report_json', action='store_true', + default=False, help='Report data as JSON') + parser.add_argument('--prometheus', '-p', dest='report_prometheus', action='store_true', + default=False, help='Daemonize and report data to Prometheus') + parser.add_argument('--file', '-f', dest='report_textfile', type=str, action='store', + default=False, help='Report data to Prometheus using textfile ') + parser.add_argument('--influxdb', '-i', dest='report_influxdb', action='store_true', + default=False, help='Daemonize and report data to InfluxDB using INFLUXDB_V2_URL, INFLUXDB_V2_ORG and INFLUXDB_V2_TOKEN environment variables') + parser.add_argument('--bucket', '-b', dest='influx_bucket', type=str, action='store', + default="ups", help='Set the bucket name when sending data to influxdb (defaults to "ups")') + parser.add_argument('--url', '-u', dest='influx_url', type=str, action='store', + default=False, help='Set the URL when sending data to influxdb (overrides INFLUXDB environment variables)') + parser.add_argument('--org', '-o', dest='influx_org', type=str, action='store', + default=False, help='Set the influx organization when sending data to influxdb (overrides INFLUXDB environment variables)') + parser.add_argument('--token', '-t', dest='influx_token', type=str, action='store', + default=False, help='Set the influx token when sending data to influxdb (overrides INFLUXDB environment variables)') + parser.add_argument('--print', dest='report_print', action='store_true', + default=True, help='Report data as text') + parser.add_argument('--verbose', '-v', action='count', + default=0, help='Print more verbose information (can be specified multiple times)') + args = parser.parse_args() + return args -if __name__ == '__main__': - debug = 0 - atexit.register(cleanup) - atexit.register(shutdown) + +def main(): + global debug try: - parser = argparse.ArgumentParser( - description='Query JBD BMS and report status', - add_help=True, - ) - parser.add_argument('--json', '-j', dest='report_json', action='store_true', - default=False, help='Report data as JSON') - parser.add_argument('--prometheus', '-p', dest='report_prometheus', action='store_true', - default=False, help='Daemonize and report data to Prometheus') - parser.add_argument('--file', '-f', dest='report_textfile', type=str, action='store', - default=False, help='Report data to Prometheus using textfile ') - parser.add_argument('--influxdb', '-i', dest='report_influxdb', action='store_true', - default=False, help='Daemonize and report data to InfluxDB using INFLUXDB_V2_URL, INFLUXDB_V2_ORG and INFLUXDB_V2_TOKEN environment variables') - parser.add_argument('--bucket', '-b', dest='influx_bucket', type=str, action='store', - default="ups", help='Set the bucket name when sending data to influxdb (defaults to "ups")') - parser.add_argument('--url', '-u', dest='influx_url', type=str, action='store', - default=False, help='Set the URL when sending data to influxdb (overrides INFLUXDB environment variables)') - parser.add_argument('--org', '-o', dest='influx_org', type=str, action='store', - default=False, help='Set the influx organization when sending data to influxdb (overrides INFLUXDB environment variables)') - parser.add_argument('--token', '-t', dest='influx_token', type=str, action='store', - default=False, help='Set the influx token when sending data to influxdb (overrides INFLUXDB environment variables)') - parser.add_argument('--print', dest='report_print', action='store_true', - default=True, help='Report data as text') - parser.add_argument('--verbose', '-v', action='count', - default=0, help='Print more verbose information (can be specified multiple times)') - args = parser.parse_args() + args = parse_args() debug=args.verbose @@ -791,6 +779,24 @@ if __name__ == '__main__': elif args.report_textfile: prometheus_export(daemonize=False, filename=args.report_textfile) else: - main() + data = dict() + while bool(data) is False: + data = collect_data() + time.sleep(1) + + if args.report_json: + print(json.dumps(data)) + + elif args.report_print: + pp = pprint.PrettyPrinter(indent=4) + pp.pprint(data) except KeyboardInterrupt: cleanup() + + + +if __name__ == '__main__': + debug = 0 + atexit.register(cleanup) + atexit.register(shutdown) + main()