Skip to content

Commit

Permalink
Changed: more detailed error output when an exception happens
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-manuel committed Jan 12, 2024
1 parent 6da7dad commit 6222121
Show file tree
Hide file tree
Showing 17 changed files with 191 additions and 34 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
## v1.0.x

* Changed: Fix issue on first driver startup, when no device setting in dbus exists by @mr-manuel
* Changed: More detailed error output when an exception happens by @mr-manuel


## v1.0.20240102beta
Expand Down
14 changes: 12 additions & 2 deletions etc/dbus-serialbattery/bms/ant.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from utils import read_serial_data, logger
import utils
from struct import unpack_from
import sys


class ANT(Battery):
Expand All @@ -32,8 +33,17 @@ def test_connection(self):
try:
result = self.read_status_data()
result = result and self.refresh_data()
except Exception as err:
logger.error(f"Unexpected {err=}, {type(err)=}")
except Exception:
(
exception_type,
exception_object,
exception_traceback,
) = sys.exc_info()
file = exception_traceback.tb_frame.f_code.co_filename
line = exception_traceback.tb_lineno
logger.error(
f"Exception occurred: {repr(exception_object)} of type {exception_type} in {file} line #{line}"
)
result = False

return result
Expand Down
14 changes: 12 additions & 2 deletions etc/dbus-serialbattery/bms/battery_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from utils import is_bit_set, read_serial_data, logger
import utils
from struct import unpack_from
import sys


class BatteryTemplate(Battery):
Expand All @@ -29,8 +30,17 @@ def test_connection(self):
result = self.read_status_data()
# get first data to show in startup log, only if result is true
result = result and self.refresh_data()
except Exception as err:
logger.error(f"Unexpected {err=}, {type(err)=}")
except Exception:
(
exception_type,
exception_object,
exception_traceback,
) = sys.exc_info()
file = exception_traceback.tb_frame.f_code.co_filename
line = exception_traceback.tb_lineno
logger.error(
f"Exception occurred: {repr(exception_object)} of type {exception_type} in {file} line #{line}"
)
result = False

return result
Expand Down
14 changes: 12 additions & 2 deletions etc/dbus-serialbattery/bms/daly.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from time import sleep, time
from datetime import datetime
from re import sub
import sys


class Daly(Battery):
Expand Down Expand Up @@ -66,8 +67,17 @@ def test_connection(self):
self.read_soc_data(ser)
self.read_battery_code(ser)

except Exception as err:
logger.error(f"Unexpected {err=}, {type(err)=}")
except Exception:
(
exception_type,
exception_object,
exception_traceback,
) = sys.exc_info()
file = exception_traceback.tb_frame.f_code.co_filename
line = exception_traceback.tb_lineno
logger.error(
f"Exception occurred: {repr(exception_object)} of type {exception_type} in {file} line #{line}"
)
result = False

# give the user a feedback that no BMS was found
Expand Down
14 changes: 12 additions & 2 deletions etc/dbus-serialbattery/bms/ecs.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from utils import logger
import utils
import minimalmodbus
import sys


class Ecs(Battery):
Expand Down Expand Up @@ -58,8 +59,17 @@ def test_connection(self):

except IOError:
result = False
except Exception as err:
logger.error(f"Unexpected {err=}, {type(err)=}")
except Exception:
(
exception_type,
exception_object,
exception_traceback,
) = sys.exc_info()
file = exception_traceback.tb_frame.f_code.co_filename
line = exception_traceback.tb_lineno
logger.error(
f"Exception occurred: {repr(exception_object)} of type {exception_type} in {file} line #{line}"
)
result = False

# give the user a feedback that no BMS was found
Expand Down
14 changes: 12 additions & 2 deletions etc/dbus-serialbattery/bms/hlpdatabms4s.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import utils
import serial
from time import sleep
import sys


class HLPdataBMS4S(Battery):
Expand All @@ -20,8 +21,17 @@ def test_connection(self):
result = False
try:
result = self.read_test_data()
except Exception as err:
logger.error(f"Unexpected {err=}, {type(err)=}")
except Exception:
(
exception_type,
exception_object,
exception_traceback,
) = sys.exc_info()
file = exception_traceback.tb_frame.f_code.co_filename
line = exception_traceback.tb_lineno
logger.error(
f"Exception occurred: {repr(exception_object)} of type {exception_type} in {file} line #{line}"
)
result = False

# give the user a feedback that no BMS was found
Expand Down
14 changes: 12 additions & 2 deletions etc/dbus-serialbattery/bms/jkbms.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import utils
from struct import unpack_from
from re import sub
import sys


class Jkbms(Battery):
Expand All @@ -25,8 +26,17 @@ def test_connection(self):
# Return True if success, False for failure
try:
return self.read_status_data()
except Exception as err:
logger.error(f"Unexpected {err=}, {type(err)=}")
except Exception:
(
exception_type,
exception_object,
exception_traceback,
) = sys.exc_info()
file = exception_traceback.tb_frame.f_code.co_filename
line = exception_traceback.tb_lineno
logger.error(
f"Exception occurred: {repr(exception_object)} of type {exception_type} in {file} line #{line}"
)
return False

def get_settings(self):
Expand Down
14 changes: 12 additions & 2 deletions etc/dbus-serialbattery/bms/jkbms_ble.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from time import sleep, time
from bms.jkbms_brn import Jkbms_Brn
import os
import sys

# from bleak import BleakScanner, BleakError
# import asyncio
Expand Down Expand Up @@ -69,8 +70,17 @@ def test_connection(self):
if not result:
logger.error("No BMS found at " + self.address)

except Exception as err:
logger.error(f"Unexpected {err=}, {type(err)=}")
except Exception:
(
exception_type,
exception_object,
exception_traceback,
) = sys.exc_info()
file = exception_traceback.tb_frame.f_code.co_filename
line = exception_traceback.tb_lineno
logger.error(
f"Exception occurred: {repr(exception_object)} of type {exception_type} in {file} line #{line}"
)
result = False

return result
Expand Down
26 changes: 21 additions & 5 deletions etc/dbus-serialbattery/bms/jkbms_brn.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,19 +460,35 @@ async def asy_connect_and_scrape(self):
self.trigger_soc_reset = False
await self.reset_soc_jk(client)
await asyncio.sleep(0.01)
except Exception as err:
self.run = False
except Exception:
(
exception_type,
exception_object,
exception_traceback,
) = sys.exc_info()
file = exception_traceback.tb_frame.f_code.co_filename
line = exception_traceback.tb_lineno
logger.info(
f"--> asy_connect_and_scrape(): error while connecting to bt: {err}"
f"--> asy_connect_and_scrape(): error while connecting to bt: {repr(exception_object)} "
+ f"of type {exception_type} in {file} line #{line}"
)
self.run = False
finally:
self.run = False
if client.is_connected:
try:
await client.disconnect()
except Exception as err:
except Exception:
(
exception_type,
exception_object,
exception_traceback,
) = sys.exc_info()
file = exception_traceback.tb_frame.f_code.co_filename
line = exception_traceback.tb_lineno
logger.info(
f"--> asy_connect_and_scrape(): error while disconnecting: {err}"
f"--> asy_connect_and_scrape(): error while disconnecting: {repr(exception_object)} "
+ f"of type {exception_type} in {file} line #{line}"
)

logger.info("--> asy_connect_and_scrape(): Exit")
Expand Down
14 changes: 12 additions & 2 deletions etc/dbus-serialbattery/bms/lifepower.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import utils
from struct import unpack_from
import re
import sys


class Lifepower(Battery):
Expand All @@ -28,8 +29,17 @@ def test_connection(self):
result = False
try:
result = self.read_status_data()
except Exception as err:
logger.error(f"Unexpected {err=}, {type(err)=}")
except Exception:
(
exception_type,
exception_object,
exception_traceback,
) = sys.exc_info()
file = exception_traceback.tb_frame.f_code.co_filename
line = exception_traceback.tb_lineno
logger.error(
f"Exception occurred: {repr(exception_object)} of type {exception_type} in {file} line #{line}"
)
result = False

return result
Expand Down
14 changes: 12 additions & 2 deletions etc/dbus-serialbattery/bms/lltjbd.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import utils
from struct import unpack_from, pack
import struct
import sys

# Protocol registers
REG_ENTER_FACTORY = 0x00
Expand Down Expand Up @@ -268,8 +269,17 @@ def test_connection(self):
and self.get_settings()
and self.refresh_data()
)
except Exception as err:
logger.error(f"Unexpected {err=}, {type(err)=}")
except Exception:
(
exception_type,
exception_object,
exception_traceback,
) = sys.exc_info()
file = exception_traceback.tb_frame.f_code.co_filename
line = exception_traceback.tb_lineno
logger.error(
f"Exception occurred: {repr(exception_object)} of type {exception_type} in {file} line #{line}"
)
result = False

return result
Expand Down
14 changes: 12 additions & 2 deletions etc/dbus-serialbattery/bms/mnb.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from battery import Protection, Battery, Cell
from utils import logger
from bms.mnb_utils_max17853 import data_cycle, init_max
import sys

# from struct import *
# from bms.mnb_test_max17853 import * # use test for testing
Expand Down Expand Up @@ -97,8 +98,17 @@ def test_connection(self):
result = False
try:
result = self.read_status_data()
except Exception as err:
logger.error(f"Unexpected {err=}, {type(err)=}")
except Exception:
(
exception_type,
exception_object,
exception_traceback,
) = sys.exc_info()
file = exception_traceback.tb_frame.f_code.co_filename
line = exception_traceback.tb_lineno
logger.error(
f"Exception occurred: {repr(exception_object)} of type {exception_type} in {file} line #{line}"
)
result = False

return result
Expand Down
14 changes: 12 additions & 2 deletions etc/dbus-serialbattery/bms/renogy.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import utils
from struct import unpack
import struct
import sys


class Renogy(Battery):
Expand Down Expand Up @@ -49,8 +50,17 @@ def test_connection(self):
result = self.read_gen_data()
# get first data to show in startup log
result = result and self.refresh_data()
except Exception as err:
logger.error(f"Unexpected {err=}, {type(err)=}")
except Exception:
(
exception_type,
exception_object,
exception_traceback,
) = sys.exc_info()
file = exception_traceback.tb_frame.f_code.co_filename
line = exception_traceback.tb_lineno
logger.error(
f"Exception occurred: {repr(exception_object)} of type {exception_type} in {file} line #{line}"
)
result = False

return result
Expand Down
14 changes: 12 additions & 2 deletions etc/dbus-serialbattery/bms/revov.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from utils import *
from struct import *
import struct
import sys

# Author: L Sheed
# Date: 3 May 2022
Expand Down Expand Up @@ -57,8 +58,17 @@ def test_connection(self):
# get first data to show in startup log
if result:
self.refresh_data()
except Exception as err:
logger.error(f"Unexpected {err=}, {type(err)=}")
except Exception:
(
exception_type,
exception_object,
exception_traceback,
) = sys.exc_info()
file = exception_traceback.tb_frame.f_code.co_filename
line = exception_traceback.tb_lineno
logger.error(
f"Exception occurred: {repr(exception_object)} of type {exception_type} in {file} line #{line}"
)
result = False

return result
Expand Down
Loading

0 comments on commit 6222121

Please sign in to comment.