Skip to content

Commit

Permalink
Add DCB data only if available to avoid crash (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
eikowagenknecht authored Oct 14, 2023
1 parent a199eb4 commit af96ae2
Showing 1 changed file with 30 additions and 16 deletions.
46 changes: 30 additions & 16 deletions e3dc/_e3dc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1209,8 +1209,6 @@ def get_battery_data(self, batIndex=None, dcbs=None, keepAlive=False):
"maxChargeCurrent": <max charge current>,
"maxDischargeCurrent": <max discharge current>,
"maxDcbCellTemp": <max DCB cell temp>,
"measuredResistance": <measured resistance>,
"measuredResistanceRun": <measure resistance (RUN)>,
"minDcbCellTemp": <min DCB cell temp>,
"moduleVoltage": <module voltage>,
"rc": <rc>,
Expand Down Expand Up @@ -1380,23 +1378,39 @@ def get_battery_data(self, batIndex=None, dcbs=None, keepAlive=False):
)

info = rscpFindTag(req, RscpTag.BAT_DCB_INFO)
# For some devices, no info for the DCBs exists. Skip those.
if info is None or len(info) < 3 or info[1] == "Error":
continue

temperatures_raw = rscpFindTagIndex(
rscpFindTag(req, RscpTag.BAT_DCB_ALL_CELL_TEMPERATURES),
RscpTag.BAT_DATA,
)
# Initialize default values for DCB
sensorCount = 0
temperatures = []
sensorCount = rscpFindTagIndex(info, RscpTag.BAT_DCB_NR_SENSOR)
for sensor in range(0, sensorCount):
temperatures.append(round(temperatures_raw[sensor][2], 2))

voltages_raw = rscpFindTagIndex(
rscpFindTag(req, RscpTag.BAT_DCB_ALL_CELL_VOLTAGES), RscpTag.BAT_DATA
)
seriesCellCount = 0
voltages = []
seriesCellCount = rscpFindTagIndex(info, RscpTag.BAT_DCB_NR_SERIES_CELL)
for cell in range(0, seriesCellCount):
voltages.append(round(voltages_raw[cell][2], 2))

# Set temperatures, if available for the device
temps = rscpFindTag(req, RscpTag.BAT_DCB_ALL_CELL_TEMPERATURES)
if temps is not None and len(temps) == 3 and temps[1] != "Error":
temperatures_raw = rscpFindTagIndex(
rscpFindTag(req, RscpTag.BAT_DCB_ALL_CELL_TEMPERATURES),
RscpTag.BAT_DATA,
)
temperatures = []
sensorCount = rscpFindTagIndex(info, RscpTag.BAT_DCB_NR_SENSOR)
for sensor in range(0, sensorCount):
temperatures.append(round(temperatures_raw[sensor][2], 2))

# Set voltages, if available for the device
voltages = rscpFindTag(req, RscpTag.BAT_DCB_ALL_CELL_VOLTAGES)
if voltages is not None and len(voltages) == 3 and voltages[1] != "Error":
voltages_raw = rscpFindTagIndex(
rscpFindTag(req, RscpTag.BAT_DCB_ALL_CELL_VOLTAGES),
RscpTag.BAT_DATA,
)
voltages = []
seriesCellCount = rscpFindTagIndex(info, RscpTag.BAT_DCB_NR_SERIES_CELL)
for cell in range(0, seriesCellCount):
voltages.append(round(voltages_raw[cell][2], 2))

dcbobj = {
"current": rscpFindTagIndex(info, RscpTag.BAT_DCB_CURRENT),
Expand Down

0 comments on commit af96ae2

Please sign in to comment.