From 4e68925d34727f59fb1f010146f3590f6b45a536 Mon Sep 17 00:00:00 2001 From: tim Date: Thu, 22 Jul 2021 14:55:14 +0200 Subject: [PATCH] Fix some issues with raw values --- bms.py | 60 +++++++++++++++++++++++++++++----------------------------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/bms.py b/bms.py index d601563..2592a41 100755 --- a/bms.py +++ b/bms.py @@ -310,7 +310,7 @@ def parse_03_response(response): # Cells are recorded as groups of 4 bits (0x0-0xF) per 4 cells g = int(hilo_cell / 4) b = 2**(hilo_cell - (g * 4 )) - data['bms_cells_balancing']['raw_values'][cell+1] = int(bool((state >> g) & b)) + data['bms_cells_balancing']['raw_values'][cell+1] = bool((state >> g) & b) data['bms_cells_balancing']['values'][cell+1] = "{0}".format(int(bool((state >> g) & b))) if debug > 1: print("Balancing cell {0}: {1}".format(cell, bool((state >> g & b)))) @@ -331,56 +331,56 @@ def parse_03_response(response): slm = protection_state & 4096 data['bms_protection_sop_bool'] = dict() data['bms_protection_sop_bool']['help'] = "Single overvoltage protection" - data['bms_protection_sop_bool']['raw_value'] = int(bool(sop)) + data['bms_protection_sop_bool']['raw_value'] = bool(sop) data['bms_protection_sop_bool']['value'] = "{0}".format(int(bool(sop))) data['bms_protection_sup_bool'] = dict() data['bms_protection_sup_bool']['help'] = "Single undervoltage protection" - data['bms_protection_sup_bool']['raw_value'] = int(bool(sup)) + data['bms_protection_sup_bool']['raw_value'] = bool(sup) data['bms_protection_sup_bool']['value'] = "{0}".format(int(bool(sup))) data['bms_protection_wgop_bool'] = dict() data['bms_protection_wgop_bool']['help'] = "Whole group overvoltage protection" - data['bms_protection_wgop_bool']['raw_value'] = int(bool(gop)) + data['bms_protection_wgop_bool']['raw_value'] = bool(gop) data['bms_protection_wgop_bool']['value'] = "{0}".format(int(bool(gop))) data['bms_protection_wgup_bool'] = dict() data['bms_protection_wgup_bool']['help'] = "Whole group undervoltage protection" - data['bms_protection_wgup_bool']['raw_value'] = int(bool(gup)) - data['bms_protection_wgup_bool']['value'] = int(bool(gup)) + data['bms_protection_wgup_bool']['raw_value'] = bool(gup) + data['bms_protection_wgup_bool']['value'] = "{0}".format(int(bool(gup))) data['bms_protection_cotp_bool'] = dict() data['bms_protection_cotp_bool']['help'] = "Charging over-temperature protection" - data['bms_protection_cotp_bool']['raw_value'] = \ + data['bms_protection_cotp_bool']['raw_value'] = bool(cotp) data['bms_protection_cotp_bool']['value'] = "{0}".format(int(bool(cotp))) data['bms_protection_cutp_bool'] = dict() data['bms_protection_cutp_bool']['help'] = "Charging under-temperature protection" - data['bms_protection_cutp_bool']['raw_value'] = int(bool(cutp)) + data['bms_protection_cutp_bool']['raw_value'] = bool(cutp) data['bms_protection_cutp_bool']['value'] = "{0}".format(int(bool(cutp))) data['bms_protection_dotp_bool'] = dict() data['bms_protection_dotp_bool']['help'] = "Discharging over-temperature protection" - data['bms_protection_dotp_bool']['raw_value'] = int(bool(dotp)) + data['bms_protection_dotp_bool']['raw_value'] = bool(dotp) data['bms_protection_dotp_bool']['value'] = "{0}".format(int(bool(dotp))) data['bms_protection_dutp_bool'] = dict() data['bms_protection_dutp_bool']['help'] = "Discharging under-protection" - data['bms_protection_dutp_bool']['raw_value'] = int(bool(dutp)) + data['bms_protection_dutp_bool']['raw_value'] = bool(dutp) data['bms_protection_dutp_bool']['value'] = "{0}".format(int(bool(dutp))) data['bms_protection_cocp_bool'] = dict() data['bms_protection_cocp_bool']['help'] = "Charging over-current protection" - data['bms_protection_cocp_bool']['raw_value'] = int(bool(cocp)) + data['bms_protection_cocp_bool']['raw_value'] = bool(cocp) data['bms_protection_cocp_bool']['value'] = "{0}".format(int(bool(cocp))) data['bms_protection_docp_bool'] = dict() data['bms_protection_docp_bool']['help'] = "Discharging over-current protection" - data['bms_protection_docp_bool']['raw_value'] = int(bool(docp)) + data['bms_protection_docp_bool']['raw_value'] = bool(docp) data['bms_protection_docp_bool']['value'] = "{0}".format(int(bool(docp))) data['bms_protection_scp_bool'] = dict() data['bms_protection_scp_bool']['help'] = "Short-circuit protection" - data['bms_protection_scp_bool']['raw_value'] = int(bool(scp)) + data['bms_protection_scp_bool']['raw_value'] = bool(scp) data['bms_protection_scp_bool']['value'] = "{0}".format(int(bool(scp))) data['bms_protection_fdic_bool'] = dict() data['bms_protection_fdic_bool']['help'] = "Front detection IC error" - data['bms_protection_fdic_bool']['raw_value'] = int(bool(fdic)) + data['bms_protection_fdic_bool']['raw_value'] = bool(fdic) data['bms_protection_fdic_bool']['value'] = "{0}".format(int(bool(fdic))) data['bms_protection_slmos_bool'] = dict() data['bms_protection_slmos_bool']['help'] = "Software lock MOS" - data['bms_protection_slmos_bool']['raw_value'] = int(bool(slm)) - data['bms_protection_slmos_bool']['value'] = int(bool(slm)) + data['bms_protection_slmos_bool']['raw_value'] = bool(slm) + data['bms_protection_slmos_bool']['value'] = "{0}".format(int(bool(slm))) if debug > 2: print("Protection state: {0}".format(protection_state)) print("Single overvoltage protection: {0}".format(bool(sop))) @@ -403,7 +403,7 @@ def parse_03_response(response): rsoc = response[23] * 0.01 data['bms_capacity_charge_ratio'] = dict() data['bms_capacity_charge_ratio']['help'] = "Percent Charge" - data['bms_capacity_charge_ratio']['raw_value'] = format(rsoc) + data['bms_capacity_charge_ratio']['raw_value'] = rsoc data['bms_capacity_charge_ratio']['value'] = "{0}".format(rsoc) data['bms_capacity_charge_ratio']['units'] = "\u2030" if debug > 1: @@ -413,11 +413,11 @@ def parse_03_response(response): control_status = response[24] data['bms_charge_is_charging'] = dict() data['bms_charge_is_charging']['help'] = "MOSFET charging" - data['bms_charge_is_charging']['raw_value'] = int(bool(control_status & 1)) - data['bms_charge_is_charging']['value'] = int(bool(control_status & 1)) + data['bms_charge_is_charging']['raw_value'] = bool(control_status & 1) + data['bms_charge_is_charging']['value'] = "{0}".format(int(bool(control_status & 1))) data['bms_charge_is_discharging'] = dict() data['bms_charge_is_discharging']['help'] = "MOSFET discharging" - data['bms_charge_is_discharging']['raw_value'] = int(bool(control_status & 1)) + data['bms_charge_is_discharging']['raw_value'] = bool(control_status & 1) data['bms_charge_is_discharging']['value'] = "{0}".format(int(bool(control_status & 1))) if debug > 1: if (control_status & 1): @@ -565,7 +565,7 @@ def main(): pp.pprint(data) -def prometheus_export(daemonize=True, filename=False): +def prometheus_export(daemonize=True, filename=None): global debug if not can_export_prometheus: return @@ -582,7 +582,7 @@ def prometheus_export(daemonize=True, filename=False): # Populate the metric data structure this period prometheus_populate_metric(metric, data) - if (daemonize): + if daemonize: prometheus_client.start_http_server(9999, registry=registry) while True: @@ -596,7 +596,7 @@ def prometheus_export(daemonize=True, filename=False): prometheus_populate_metric(metric, data) prometheus_client.generate_latest(registry) else: - if not filename: + if filename is None: print("Invalid filename supplied"); return False prometheus_client.write_to_textfile(filename, registry=registry) @@ -606,19 +606,19 @@ def prometheus_create_metric(registry, data): metric = dict() for name, contains in data.items(): helpmsg = '' - if contains.get('help'): + if contains.get('help') is not None: helpmsg = contains.get('help') if contains.get('units'): helpmsg += ' (' + contains.get('units') + ')' - if contains.get('value'): + if contains.get('value') is not None: metric[name] = prometheus_client.Gauge(name, helpmsg, registry=registry) # Has multiple values, each a different label - elif contains.get('values'): + elif contains.get('values') is not None: if contains.get('label') is None: print("ERROR: no label for {0} specified".format(name)) label = contains.get('label') metric[name] = prometheus_client.Gauge(name, helpmsg, [label], registry=registry) - elif contains.get('info'): + elif contains.get('info') is not None: metric[name] = prometheus_client.Info(name, helpmsg, registry=registry) else: pass @@ -626,11 +626,11 @@ def prometheus_create_metric(registry, data): def prometheus_populate_metric(metric, data): for name, contains in data.items(): - if contains.get('value'): + if contains.get('value') is not None: value = contains.get('value') metric[name].set(value) # doesn't have a value, but has [1-4]: - if contains.get('values') and isinstance(contains.get('values'), dict): + if contains.get('values') is not None and isinstance(contains.get('values'), dict): for idx, label_value in contains.get('values').items(): metric[name].labels(idx).set(label_value) if contains.get('info'): @@ -659,7 +659,7 @@ def influxdb_export(bucket, url=None, org=None, token=None, daemonize=True): influxclient = InfluxDBClient.from_env_properties() influxdb_write_snapshot(bucket, data) - if (daemonize): + if daemonize: while True: # Delay, collect new data, and start again time.sleep(DAEMON_UPDATE_PERIOD)