Skip to content

Commit

Permalink
Merge pull request #1213 from samson0v/master
Browse files Browse the repository at this point in the history
Fixed BACnet logging
  • Loading branch information
imbeacon authored Nov 3, 2023
2 parents bd6103e + 2752174 commit eaf1f36
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 27 deletions.
2 changes: 1 addition & 1 deletion thingsboard_gateway/connectors/bacnet/bacnet_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def __init__(self, gateway, config, connector_type):
self.__devices_address_name = {}
self.__gateway = gateway
self._log = init_logger(self.__gateway, self.name, self.__config.get('logLevel', 'INFO'))
self._application = TBBACnetApplication(self, self.__config)
self._application = TBBACnetApplication(self, self.__config, self._log)
self.__bacnet_core_thread = Thread(target=run, name="BACnet core thread", daemon=True,
kwargs={"sigterm": None, "sigusr1": None})
self.__bacnet_core_thread.start()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,26 @@
from bacpypes.primitivedata import Null, ObjectIdentifier, Atomic, Integer, Real, Unsigned

from thingsboard_gateway.connectors.bacnet.bacnet_utilities.tb_gateway_bacnet_device import TBBACnetDevice
from thingsboard_gateway.connectors.connector import log

LOG = None


class TBBACnetApplication(BIPSimpleApplication):
def __init__(self, connector, configuration):
def __init__(self, connector, configuration, logger):
try:
self.__config = configuration
self.__connector = connector
self._log = logger
global LOG
LOG = self._log
assert self.__config is not None
assert self.__config.get("general") is not None
self.requests_in_progress = {}
self.discovered_devices = {}
self.__device = TBBACnetDevice(self.__config["general"])
super().__init__(self.__device, self.__config["general"]["address"])
except Exception as e:
log.exception(e)
self._log.exception(e)

def do_whois(self, device=None):
try:
Expand All @@ -57,16 +61,16 @@ def do_whois(self, device=None):
deferred(self.request_io, iocb)

except Exception as e:
log.exception(e)
self._log.exception(e)

def indication(self, apdu: APDU):
if isinstance(apdu, IAmRequest):
log.debug("Received IAmRequest from device with ID: %i and address %s:%i",
self._log.debug("Received IAmRequest from device with ID: %i and address %s:%i",
apdu.iAmDeviceIdentifier[1],
apdu.pduSource.addrTuple[0],
apdu.pduSource.addrTuple[1]
)
log.debug(apdu.pduSource)
self._log.debug(apdu.pduSource)
request = ReadPropertyRequest(
destination=apdu.pduSource,
objectIdentifier=apdu.iAmDeviceIdentifier,
Expand All @@ -87,7 +91,7 @@ def do_read_property(self, device, mapping_type=None, config=None, callback=None
"config": config}})
iocb.add_callback(self.__general_cb)
except Exception as e:
log.exception(e)
self._log.exception(e)

def do_write_property(self, device, callback=None):
try:
Expand All @@ -96,7 +100,7 @@ def do_write_property(self, device, callback=None):
self.requests_in_progress.update({iocb: {"callback": callback}})
iocb.add_callback(self.__general_cb)
except Exception as error:
log.exception("exception: %r", error)
self._log.exception("exception: %r", error)

def do_binary_rising_edge(self, device, callback=None):
device["propertyValue"] = 1
Expand All @@ -114,18 +118,18 @@ def check_or_add(self, device):
def __on_mapping_response_cb(self, iocb: IOCB):
try:
if self.requests_in_progress.get(iocb) is not None:
log.debug(iocb)
log.debug(self.requests_in_progress[iocb])
self._log.debug(iocb)
self._log.debug(self.requests_in_progress[iocb])
if iocb.ioResponse:
apdu = iocb.ioResponse
value = self.__property_value_from_apdu(apdu)
callback_params = self.requests_in_progress[iocb]
if callback_params.get("callback") is not None:
self.__general_cb(iocb, callback_params, value)
elif iocb.ioError:
log.exception(iocb.ioError)
self._log.exception(iocb.ioError)
except Exception as e:
log.exception(e)
self._log.exception(e)
if self.requests_in_progress.get(iocb) is not None:
del self.requests_in_progress[iocb]

Expand All @@ -136,32 +140,32 @@ def __general_cb(self, iocb, callback_params=None, value=None):
if iocb.ioResponse:
apdu = iocb.ioResponse
if isinstance(apdu, SimpleAckPDU):
log.debug("Write to %s - successfully.", str(apdu.pduSource))
self._log.debug("Write to %s - successfully.", str(apdu.pduSource))
else:
log.debug("Received response: %r", apdu)
self._log.debug("Received response: %r", apdu)
elif iocb.ioError:
log.exception(iocb.ioError)
self._log.exception(iocb.ioError)
else:
log.error("There are no data in response and no errors.")
self._log.error("There are no data in response and no errors.")
if isinstance(callback_params, dict) and callback_params.get("callback"):
try:
callback_params["callback"](iocb, callback_params)
except TypeError:
callback_params["callback"](iocb)
except Exception as e:
log.exception("During processing callback, exception has been raised:")
log.exception(e)
self._log.exception("During processing callback, exception has been raised:")
self._log.exception(e)
if self.requests_in_progress.get(iocb) is not None:
del self.requests_in_progress[iocb]

def __iam_cb(self, iocb: IOCB, vendor_id=None):
if iocb.ioResponse:
apdu = iocb.ioResponse
log.debug("Received IAm Response: %s", str(apdu))
self._log.debug("Received IAm Response: %s", str(apdu))
if self.discovered_devices.get(apdu.pduSource) is None:
self.discovered_devices[apdu.pduSource] = {}
value = self.__connector.default_converters["uplink_converter"]("{}").convert(None, apdu)
log.debug("Property: %s is %s", apdu.propertyIdentifier, value)
self._log.debug("Property: %s is %s", apdu.propertyIdentifier, value)
self.discovered_devices[apdu.pduSource].update({apdu.propertyIdentifier: value})
data_to_connector = {
"address": apdu.pduSource,
Expand All @@ -171,7 +175,7 @@ def __iam_cb(self, iocb: IOCB, vendor_id=None):
}
self.__connector.add_device(data_to_connector)
elif iocb.ioError:
log.exception(iocb.ioError)
self._log.exception(iocb.ioError)

@staticmethod
def form_iocb(device, config=None, request_type="readProperty"):
Expand All @@ -196,7 +200,7 @@ def form_iocb(device, config=None, request_type="readProperty"):
request.propertyArrayIndex = int(property_index)
iocb = IOCB(request)
except Exception as e:
log.exception(e)
LOG.exception(e)
elif request_type == "writeProperty":
datatype = get_datatype(object_id.value[0], property_id, vendor)
if (isinstance(value, str) and value.lower() == 'null') or value is None:
Expand All @@ -210,7 +214,7 @@ def form_iocb(device, config=None, request_type="readProperty"):
value = int(value)
value = datatype(value)
elif not isinstance(value, datatype):
log.error("invalid result datatype, expecting %s" % (datatype.__name__,))
LOG.error("invalid result datatype, expecting %s" % (datatype.__name__,))
request = WritePropertyRequest(
objectIdentifier=object_id,
propertyIdentifier=property_id
Expand All @@ -220,14 +224,14 @@ def form_iocb(device, config=None, request_type="readProperty"):
try:
request.propertyValue.cast_in(value)
except AttributeError as e:
log.debug(e)
LOG.debug(e)
except Exception as error:
log.exception("WriteProperty cast error: %r", error)
LOG.exception("WriteProperty cast error: %r", error)
if property_index is not None:
request.propertyArrayIndex = property_index
if priority is not None:
request.priority = priority
iocb = IOCB(request)
else:
log.error("Request type is not found or not implemented")
LOG.error("Request type is not found or not implemented")
return iocb

0 comments on commit eaf1f36

Please sign in to comment.