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.74k stars 844 forks source link

[BUG] Unique iterator for ODBC connector #466

Closed murp-C2D2 closed 3 years ago

murp-C2D2 commented 3 years ago

I would like to use multiple ODBC connectors to read and transmit data from different tables in the same database. This is currently not possible in the current version if the column names providing the iterator in all the tables are same. In most real time cases it is possible that there exists some primary-key columns and possibly with same names across several tables in database. It is possible to uniquely identify the iterator using its name in the code. Since there has to be a uniquely named connector for every connection, using the connector name when hashing can generate uniquely identifiable iterator file name.

Please find the patch below how I did this.

diff --git a/thingsboard_gateway/connectors/odbc/odbc_connector.py b/thingsboard_gateway/connectors/odbc/odbc_connector.py
index a519112..3137022 100644
--- a/thingsboard_gateway/connectors/odbc/odbc_connector.py
+++ b/thingsboard_gateway/connectors/odbc/odbc_connector.py
@@ -324,6 +324,7 @@ class OdbcConnector(Connector, Thread):
             file_name += self.__connection.getinfo(pyodbc.SQL_DRIVER_NAME)
             file_name += self.__connection.getinfo(pyodbc.SQL_SERVER_NAME)
             file_name += self.__connection.getinfo(pyodbc.SQL_DATABASE_NAME)
+            file_name += self.get_name()
             file_name += self.__config["polling"]["iterator"]["column"]

             self.__iterator_file_name = sha1(file_name.encode()).hexdigest() + ".json"
imbeacon commented 3 years ago

Hi @murp-C2D2 ,

Thank you for your participating in the project life. I have added your changes into the code and they will be available in the next release.