1
0
mirror of https://github.com/thingsboard/thingsboard-gateway synced 2025-10-26 22:31:42 +08:00

Added ability to use clientId/username/password to connect to the ThingsBoard instance

This commit is contained in:
zbeacon
2022-08-11 11:54:39 +03:00
parent e479d3afec
commit f7b2ab5811
3 changed files with 17 additions and 9 deletions

View File

@@ -42,14 +42,22 @@ class TBClient(threading.Thread):
self.__ca_cert = None self.__ca_cert = None
self.__private_key = None self.__private_key = None
self.__cert = None self.__cert = None
self.__token = None self.__client_id = ""
self.__username = None
self.__password = None
self.__is_connected = False self.__is_connected = False
self.__stopped = False self.__stopped = False
self.__paused = False self.__paused = False
self._last_cert_check_time = 0 self._last_cert_check_time = 0
if credentials.get("accessToken") is not None: if credentials.get("accessToken") is not None:
self.__token = str(credentials["accessToken"]) self.__username = str(credentials["accessToken"])
self.client = TBGatewayMqttClient(self.__host, self.__port, self.__token, self, quality_of_service=self.__default_quality_of_service) if credentials.get("username") is not None:
self.__username = str(credentials["username"])
if credentials.get("password") is not None:
self.__password = str(credentials["password"])
if credentials.get("clientId") is not None:
self.__client_id = str(credentials["clientId"])
self.client = TBGatewayMqttClient(self.__host, self.__port, self.__username, self.__password, self, quality_of_service=self.__default_quality_of_service, client_id=self.__client_id)
if self.__tls: if self.__tls:
self.__ca_cert = self.__config_folder_path + credentials.get("caCert") if credentials.get("caCert") is not None else None self.__ca_cert = self.__config_folder_path + credentials.get("caCert") if credentials.get("caCert") is not None else None
self.__private_key = self.__config_folder_path + credentials.get("privateKey") if credentials.get("privateKey") is not None else None self.__private_key = self.__config_folder_path + credentials.get("privateKey") if credentials.get("privateKey") is not None else None

View File

@@ -76,15 +76,15 @@ class TBPublishInfo:
class TBDeviceMqttClient: class TBDeviceMqttClient:
def __init__(self, host, port=1883, token=None, quality_of_service=None): def __init__(self, host, port=1883, username=None, password=None, quality_of_service=None, client_id=""):
self._client = paho.Client(protocol=4) self._client = paho.Client(protocol=4, client_id=client_id)
self.quality_of_service = quality_of_service if quality_of_service is not None else 1 self.quality_of_service = quality_of_service if quality_of_service is not None else 1
self.__host = host self.__host = host
self.__port = port self.__port = port
if token == "": if username == "":
log.warning("token is not set, connection without tls wont be established") log.warning("token is not set, connection without tls wont be established")
else: else:
self._client.username_pw_set(token) self._client.username_pw_set(username, password=password)
self._lock = RLock() self._lock = RLock()
self._attr_request_dict = {} self._attr_request_dict = {}

View File

@@ -32,8 +32,8 @@ log = logging.getLogger("tb_connection")
class TBGatewayMqttClient(TBDeviceMqttClient): class TBGatewayMqttClient(TBDeviceMqttClient):
def __init__(self, host, port, token=None, gateway=None, quality_of_service=1): def __init__(self, host, port, username=None, password=None, gateway=None, quality_of_service=1, client_id=""):
super().__init__(host, port, token, quality_of_service) super().__init__(host, port, username, password, quality_of_service, client_id)
self.quality_of_service = quality_of_service self.quality_of_service = quality_of_service
self.__max_sub_id = 0 self.__max_sub_id = 0
self.__sub_dict = {} self.__sub_dict = {}