def __get_converter(self, config, need_uplink): if config is None: return BytesCanUplinkConverter( ) if need_uplink else BytesCanDownlinkConverter() else: if need_uplink: uplink = config.get("uplink") return BytesCanUplinkConverter() if uplink is None \ else TBUtility.check_and_import(self.__connector_type, uplink) else: downlink = config.get("downlink") return BytesCanDownlinkConverter() if downlink is None \ else TBUtility.check_and_import(self.__connector_type, downlink)
def __prepare_connectors_configuration(self, input_connector_config): try: self.__gateway.connectors_configs = {} for connector in input_connector_config['thingsboard'][ 'connectors']: for input_connector in input_connector_config[ connector['type']]: if input_connector['name'] == connector['name']: if not self.__gateway.connectors_configs.get( connector['type']): self.__gateway.connectors_configs[ connector['type']] = [] self.__gateway.connectors_configs[ connector['type']].append({ "name": connector["name"], "config": { connector['configuration']: input_connector["config"] } }) connector_class = TBUtility.check_and_import( connector["type"], self.__gateway._default_connectors.get( connector["type"], connector.get("class"))) self.__gateway._implemented_connectors[ connector["type"]] = connector_class except Exception as e: LOG.exception(e)
def __init__(self, gateway, config, connector_type): self.__connector_type = connector_type self.statistics = {'MessagesReceived': 0, 'MessagesSent': 0} super().__init__() self.__config = config self.setName( config.get( 'name', 'BACnet ' + ''.join(choice(ascii_lowercase) for _ in range(5)))) self.__devices = [] self.__device_indexes = {} self.__devices_address_name = {} self.__gateway = gateway self._application = TBBACnetApplication(self, self.__config) self.__bacnet_core_thread = Thread(target=run, name="BACnet core thread") self.__bacnet_core_thread.start() self.__stopped = False self.__config_devices = self.__config["devices"] self.default_converters = { "uplink_converter": TBUtility.check_and_import(self.__connector_type, "BACnetUplinkConverter"), "downlink_converter": TBUtility.check_and_import(self.__connector_type, "BACnetDownlinkConverter") } self.__request_functions = { "writeProperty": self._application.do_write_property, "readProperty": self._application.do_read_property } self.__available_object_resources = {} self.rpc_requests_in_progress = {} self.__connected = False self.daemon = True
def __load_converters(self, device): datatypes = [ "attributes", "telemetry", "attribute_updates", "server_side_rpc" ] for datatype in datatypes: for datatype_config in device.get(datatype, []): try: for converter_type in self.default_converters: converter_object = self.default_converters[ converter_type] if datatype_config.get( "class" ) is None else TBUtility.check_and_import( self.__connector_type, device.get("class")) datatype_config[converter_type] = converter_object( device) except Exception as e: log.exception(e)
def __fill_interest_devices(self): if self.__config.get('devices') is None: log.error( 'Devices not found in configuration file. BLE Connector stopped.' ) self._connected = False return None for interest_device in self.__config.get('devices'): keys_in_config = ['attributes', 'telemetry'] if interest_device.get('MACAddress') is not None: default_converter = BytesBLEUplinkConverter(interest_device) interest_uuid = {} for key_type in keys_in_config: for type_section in interest_device.get(key_type): if type_section.get("characteristicUUID") is not None: converter = None if type_section.get('converter') is not None: try: module = TBUtility.check_and_import( self.__connector_type, type_section['converter']) if module is not None: log.debug( 'Custom converter for device %s - found!', interest_device['MACAddress']) converter = module(interest_device) else: log.error( "\n\nCannot find extension module for device %s .\nPlease check your configuration.\n", interest_device['MACAddress']) except Exception as e: log.exception(e) else: converter = default_converter if converter is not None: if interest_uuid.get( type_section["characteristicUUID"]. upper()) is None: interest_uuid[type_section[ "characteristicUUID"].upper()] = [{ 'section_config': type_section, 'type': key_type, 'converter': converter }] else: interest_uuid[type_section[ "characteristicUUID"].upper()].append({ 'section_config': type_section, 'type': key_type, 'converter': converter }) else: log.error( "No characteristicUUID found in configuration section for %s:\n%s\n", key_type, pformat(type_section)) if self.__devices_around.get( interest_device['MACAddress'].upper()) is None: self.__devices_around[ interest_device['MACAddress'].upper()] = {} self.__devices_around[interest_device['MACAddress'].upper( )]['device_config'] = interest_device self.__devices_around[interest_device['MACAddress'].upper( )]['interest_uuid'] = interest_uuid else: log.error( "Device address not found, please check your settings.")