Whitespace cleanups; rename InfluxDB field from "gauge" to "value"

... this abandons Prometheus "compatibility" for a better fit with InfluxDB naming.
This commit is contained in:
Timothy Allen 2021-07-23 21:29:57 +02:00
parent 4e68925d34
commit 10bf0b4f1e

89
bms.py
View File

@ -216,7 +216,7 @@ def parse_03_response(response):
if data_len > 0:
vtot = bytes_to_digits(response[4], response[5]) * 0.01
data['bms_voltage_total_volts'] = dict()
data['bms_voltage_total_volts']['help'] = "Total Voltage"
data['bms_voltage_total_volts']['help'] = "Total Voltage"
data['bms_voltage_total_volts']['raw_value'] = vtot
data['bms_voltage_total_volts']['value'] = "{:.2f}".format(vtot)
data['bms_voltage_total_volts']['units'] = "V"
@ -225,23 +225,23 @@ def parse_03_response(response):
current = bytes_to_digits(response[6], response[7])
current = convert_to_signed(current) * 0.01
data["bms_current_amps"] = dict()
data["bms_current_amps"]['help'] = "Current"
data["bms_current_amps"]['raw_value'] = current
data["bms_current_amps"]['value'] = "{:.2f}".format(current)
data["bms_current_amps"]['units'] = "A"
data['bms_current_amps'] = dict()
data['bms_current_amps']['help'] = "Current"
data['bms_current_amps']['raw_value'] = current
data['bms_current_amps']['value'] = "{:.2f}".format(current)
data['bms_current_amps']['units'] = "A"
if debug > 1:
print("Current: {:.2f}A".format(current))
res_cap = bytes_to_digits(response[8], response[9]) * 0.01
nom_cap = bytes_to_digits(response[10], response[11]) * 0.01
data['bms_capacity_remaining_ah'] = dict()
data['bms_capacity_remaining_ah']['help'] = "Remaining Capacity"
data['bms_capacity_remaining_ah']['help'] = "Remaining Capacity"
data['bms_capacity_remaining_ah']['raw_value'] = res_cap
data['bms_capacity_remaining_ah']['value'] = "{:.2f}".format(res_cap)
data['bms_capacity_remaining_ah']['units'] = "Ah"
data['bms_capacity_nominal_ah'] = dict()
data['bms_capacity_nominal_ah']['help'] = "Nominal Capacity"
data['bms_capacity_nominal_ah']['help'] = "Nominal Capacity"
data['bms_capacity_nominal_ah']['raw_value'] = nom_cap
data['bms_capacity_nominal_ah']['value'] = "{:.2f}".format(nom_cap)
data['bms_capacity_nominal_ah']['units'] = "Ah"
@ -251,7 +251,7 @@ def parse_03_response(response):
cycle_times = bytes_to_digits(response[12], response[13])
data['bms_charge_cycles'] = dict()
data['bms_charge_cycles']['help'] = "Charge Cycles"
data['bms_charge_cycles']['help'] = "Charge Cycles"
data['bms_charge_cycles']['raw_value'] = cycle_times
data['bms_charge_cycles']['value'] = "{0}".format(cycle_times)
if debug > 1:
@ -266,7 +266,7 @@ def parse_03_response(response):
cells = response[25] # 4S
data['bms_cell_number'] = dict()
data['bms_cell_number']['help'] = "Cells"
data['bms_cell_number']['help'] = "Cells"
data['bms_cell_number']['raw_value'] = cells
data['bms_cell_number']['value'] = "{0}".format(cells)
if debug > 1:
@ -293,8 +293,8 @@ def parse_03_response(response):
# 0xF (cells 1 + 2 + 3 + 4 balancing) 15
#data["Balancing"] = dict()
data['bms_cells_balancing'] = dict()
data['bms_cells_balancing']['help'] = "Cells balancing"
data['bms_cells_balancing']['label'] = 'cell'
data['bms_cells_balancing']['help'] = "Cells balancing"
data['bms_cells_balancing']['label'] = 'cell'
data['bms_cells_balancing']['raw_values'] = dict()
data['bms_cells_balancing']['values'] = dict()
for cell in range(cells):
@ -334,51 +334,51 @@ def parse_03_response(response):
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']['help'] = "Single undervoltage protection"
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']['help'] = "Whole group overvoltage protection"
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']['help'] = "Whole group undervoltage protection"
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']['help'] = "Charging over-temperature protection"
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']['help'] = "Charging under-temperature protection"
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']['help'] = "Discharging over-temperature protection"
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']['help'] = "Discharging under-protection"
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']['help'] = "Charging over-current protection"
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']['help'] = "Discharging over-current protection"
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']['help'] = "Short-circuit protection"
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']['help'] = "Front detection IC error"
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']['help'] = "Software lock MOS"
data['bms_protection_slmos_bool']['raw_value'] = bool(slm)
data['bms_protection_slmos_bool']['value'] = "{0}".format(int(bool(slm)))
if debug > 2:
@ -402,7 +402,7 @@ def parse_03_response(response):
# percent of capacity remaining, converted to a per mille ratio between 0 and 1
rsoc = response[23] * 0.01
data['bms_capacity_charge_ratio'] = dict()
data['bms_capacity_charge_ratio']['help'] = "Percent Charge"
data['bms_capacity_charge_ratio']['help'] = "Percent Charge"
data['bms_capacity_charge_ratio']['raw_value'] = rsoc
data['bms_capacity_charge_ratio']['value'] = "{0}".format(rsoc)
data['bms_capacity_charge_ratio']['units'] = "\u2030"
@ -412,11 +412,11 @@ def parse_03_response(response):
# bit0 = charging; bit1 = discharging; 0 = MOS closing; 1 = MOS opening
control_status = response[24]
data['bms_charge_is_charging'] = dict()
data['bms_charge_is_charging']['help'] = "MOSFET charging"
data['bms_charge_is_charging']['help'] = "MOSFET charging"
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']['help'] = "MOSFET discharging"
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:
@ -435,13 +435,13 @@ def parse_03_response(response):
for i in range(ntc_num):
temperatures.append((bytes_to_digits(response[27+(2*i)], response[28+(2*i)]) - 2731) * 0.1)
data['bms_temperature_sensor_num'] = dict()
data['bms_temperature_sensor_num']['help'] = "Temperature Sensors"
data['bms_temperature_sensor_num']['raw_value'] = ntc_num
data['bms_temperature_sensor_num']['help'] = "Temperature Sensors"
data['bms_temperature_sensor_num']['raw_value'] = ntc_num
data['bms_temperature_sensor_num']['value'] = "{0}".format(ntc_num)
data['bms_temperature_celcius'] = dict()
data['bms_temperature_celcius']['help'] = "Temperature"
data['bms_temperature_celcius']['units'] = "\u00B0C"
data['bms_temperature_celcius']['label'] = 'sensor'
data['bms_temperature_celcius']['help'] = "Temperature"
data['bms_temperature_celcius']['units'] = "\u00B0C"
data['bms_temperature_celcius']['label'] = 'sensor'
data['bms_temperature_celcius']['raw_values'] = dict()
data['bms_temperature_celcius']['values'] = dict()
for i, temp in enumerate(temperatures):
@ -494,17 +494,17 @@ def parse_04_response(response):
if data_len > 0:
data['bms_voltage_cells_volts'] = dict()
data['bms_voltage_cells_volts']['help'] = "Cell Voltages"
data['bms_voltage_cells_volts']['units'] = "V"
data['bms_voltage_cells_volts']['label'] = "cell"
data['bms_voltage_cells_volts']['help'] = "Cell Voltages"
data['bms_voltage_cells_volts']['units'] = "V"
data['bms_voltage_cells_volts']['label'] = "cell"
data['bms_voltage_cells_volts']['raw_values'] = dict()
data['bms_voltage_cells_volts']['values'] = dict()
for cell in range(int(data_len / 2)):
first = (cell * 2) + 4
second = (cell * 2) + 5
cellv = bytes_to_digits(response[first], response[second]) * 0.001
data['bms_voltage_cells_volts']['raw_values'][cell+1] = cellv
data['bms_voltage_cells_volts']['values'][cell+1] = "{:.3f}".format(cellv)
data['bms_voltage_cells_volts']['raw_values'][cell+1] = cellv
data['bms_voltage_cells_volts']['values'][cell+1] = "{:.3f}".format(cellv)
if debug > 1:
print("Cell {:.0f}: {:.3f}V".format(cell+1, cellv))
return data
@ -672,7 +672,7 @@ def influxdb_export(bucket, url=None, org=None, token=None, daemonize=True):
influxclient.close()
return
def influxdb_write_snapshot(bucket,data):
def influxdb_write_snapshot(bucket, data):
global debug
global influxclient
global writeapi
@ -689,15 +689,15 @@ def influxdb_create_snapshot(data):
helpmsg = ''
units = ''
now = datetime.datetime.now(datetime.timezone.utc).isoformat()
''' Note that the fieldname is set to "gauge" in order to retain
compatibility with data imported from Prometheus (and it's
as good a name as any). '''
for kind, contains in data.items():
helpmsg = None
if contains.get('help'):
helpmsg = contains.get('help')
units = None
if contains.get('units'):
units = contains.get('units')
# Simple values
value = None
if contains.get('raw_value') is not None:
value = contains.get('raw_value')
if debug > 2:
@ -705,10 +705,11 @@ def influxdb_create_snapshot(data):
point = Point(kind) \
.tag("units", units) \
.tag("help", helpmsg) \
.field("gauge", value) \
.field("value", value) \
.time(now)
points.append(point)
# Doesn't have a value, but multiple values, each with a label:
label = None
if contains.get('raw_values') is not None and isinstance(contains.get('raw_values'), dict):
label = contains.get('label')
for idx, label_value in contains.get('raw_values').items():
@ -718,7 +719,7 @@ def influxdb_create_snapshot(data):
.tag(label, idx) \
.tag("units", units) \
.tag("help", helpmsg) \
.field("gauge", label_value) \
.field("value", label_value) \
.time(now)
points.append(point)
# Information (like a manufacturing date or a serial number)
@ -729,7 +730,7 @@ def influxdb_create_snapshot(data):
point = Point(kind) \
.tag("units", units) \
.tag("help", helpmsg) \
.field("gauge", value) \
.field("value", value) \
.time(now)
points.append(point)
else: