thingsboard / thingsboard-gateway

Open-source IoT Gateway - integrates devices connected to legacy and third-party systems with ThingsBoard IoT Platform using Modbus, CAN bus, BACnet, BLE, OPC-UA, MQTT, ODBC and REST protocols
https://thingsboard.io/docs/iot-gateway/what-is-iot-gateway/
Apache License 2.0
1.76k stars 846 forks source link

[HELP] custom connector: |ERROR| - [tb_logger.py] - tb_logger - exception - 127 - 'config' #1489

Open Godsgrav3 opened 3 months ago

Godsgrav3 commented 3 months ago

Describe the issue Hi, I'm trying to make a barebones/simpel custom connector that just writes some messages in the logger. But when doing so I keep running into an error message and I'm not sure how I can fix this. Any help with this is appreciated. |ERROR| - [tb_logger.py] - tb_logger - exception - 127 - 'config'

I use the following configuration json:

{
  "connection": {
    "name": "Testing gRPC connection",
    "host": [HOST],
    "apiToken": [TOKEN]
    "port": 8080
  },
  "attributeUpdates": [
    {
      "retain": false,
      "deviceNameFilter": ".*",
      "attributeFilter": "firmwareVersion",
      "topicExpression": "sensor/${deviceName}/${attributeKey}",
      "valueExpression": "{\"${attributeKey}\":\"${attributeValue}\"}"
    }
  ],
  "logLevel": "DEBUG",
  "name": "CustomGRPCConnector",
  "enableRemoteLogging": false,
  "id": "bf945f76-7d9d-47f7-8a23-544dcfbb5732"
}

in tb_gateway.json I have added the following:

{
      "type": "CustomGRPC",
      "name": "CustomGRPCConnector",
      "configuration": "customGRPCConnector.json",
      "class": "CustomGRPCConnector"
}

Error traceback (If it was raised):

tb-gateway                              | 2024-08-07 11:34:12 - |ERROR| - [tb_logger.py] - tb_logger - exception - 127 - 'config'
tb-gateway                              | Traceback (most recent call last):
tb-gateway                              |   File "/thingsboard_gateway/gateway/tb_gateway_service.py", line 589, in __init_remote_configuration
tb-gateway                              |     self.__remote_configurator = RemoteConfigurator(self, self.__config)
tb-gateway                              |                                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
tb-gateway                              |   File "/thingsboard_gateway/tb_utility/tb_gateway_remote_configurator.py", line 42, in __init__
tb-gateway                              |     self._load_connectors_configuration()
tb-gateway                              |   File "/thingsboard_gateway/tb_utility/tb_gateway_remote_configurator.py", line 180, in _load_connectors_configuration
tb-gateway                              |     config = connector.pop('config')[general_connector_config['configuration']]
tb-gateway                              |              ^^^^^^^^^^^^^^^^^^^^^^^
tb-gateway                              | KeyError: 'config'

And the extensions folder I have added the folder CustomGRPC folder and added the following python file (partially copies as the rest is standard, if required I will update it):

from threading import Thread
from random import choice
from string import ascii_lowercase
from thingsboard_gateway.tb_utility.tb_utility import TBUtility

# Libraries required for custom connector
import grpc
try:
    from chirpstack_api import api
except ImportError:
    print("tb-gateway - chirpstack_api not found - installing....")
    TBUtility.install_package("chirpstack-api")
    from chirpstack_api import api

from thingsboard_gateway.connectors.connector import Connector    # Import base class for connector and logger
from thingsboard_gateway.tb_utility.tb_loader import TBModuleLoader
from thingsboard_gateway.tb_utility.tb_logger import init_logger

class CustomGRPCConnector(Thread, Connector):    # Define a connector class, it should inherit from "Connector" class.
    def __init__(self, gateway,  config, connector_type):
        super().__init__()    # Initialize parents classes
        self.statistics = {'MessagesReceived': 0,
                           'MessagesSent': 0}    # Dictionary, will save information about count received and sent messages.
        self.__config = config
        self.__id = self.__config.get('id')
        self.__gateway = gateway    # Save gateway object, we will use some gateway methods for adding devices and saving data from them.
        #self.__connector_type = connector_type    # Saving type for connector, need for loading converter
        self.setName(self.__config.get("name",
                                       "Custom %s connector " % self.get_name() + ''.join(choice(ascii_lowercase) for _ in range(5))))    # get from the configuration or create name for logs.
        self._log = init_logger(self.__gateway, self.name, level=self.__config.get('logLevel'),
                                enable_remote_logging=self.__config.get('enableRemoteLogging', False))
        #self._log.info("\n")
        #self._log.info("Starting Custom %s connector", self.get_name())    # Send message to logger
        #self._log.info("\n")
        #self.daemon = True    # Set self thread as daemon
        #self.stopped = True    # Service variable for check state
        #self.connected = False    # Service variable for check connection to device
        #self.devices = {}    # Dictionary with devices, will contain devices configurations, converters for devices and serial port objects
        #self.__load_converters()    # Call function to load converters and save it into devices dictionary
        #self.__connect_to_host()    # Call function for connect to devices
        #self._log.info('Custom connector %s initialization success.', self.get_name())    # Message to logger
        #self._log.info("Devices in configuration file found: %s ", '\n'.join(device for device in self.devices))    # Message to logger

A lot I have commented to try and locate the origin of the issue but was unsuccessful, hence the ticket. Versions (please complete the following information):