# Path: thingsboard_gateway/connectors/connector.py
# class Connector(ABC):
#     def open(self):
#     def close(self):
#     def get_name(self):
#     def is_connected(self):
#     def on_attributes_update(self, content):
#     def server_side_rpc_handler(self, content):
# 
# Path: thingsboard_gateway/tb_utility/tb_loader.py
# class TBModuleLoader:
#     PATHS = []
#     LOADED_CONNECTORS = {}
# 
#     @staticmethod
#     def find_paths():
#         root_path = path.abspath(path.dirname(path.dirname(__file__)))
#         log.debug("Root path is: " + root_path)
#         if path.exists(DEB_INSTALLATION_EXTENSION_PATH):
#             log.debug("Debian installation extensions folder exists.")
#             TBModuleLoader.PATHS.append(DEB_INSTALLATION_EXTENSION_PATH)
#         TBModuleLoader.PATHS.append(root_path + EXTENSIONS_FOLDER)
#         TBModuleLoader.PATHS.append(root_path + CONNECTORS_FOLDER)
# 
#     @staticmethod
#     def import_module(extension_type, module_name):
#         if len(TBModuleLoader.PATHS) == 0:
#             TBModuleLoader.find_paths()
#         buffered_module_name = extension_type + module_name
#         if TBModuleLoader.LOADED_CONNECTORS.get(buffered_module_name) is not None:
#             return TBModuleLoader.LOADED_CONNECTORS[buffered_module_name]
#         try:
#             for current_path in TBModuleLoader.PATHS:
#                 current_extension_path = current_path + path.sep + extension_type
#                 if path.exists(current_extension_path):
#                     for file in listdir(current_extension_path):
#                         if not file.startswith('__') and file.endswith('.py'):
#                             try:
#                                 module_spec = spec_from_file_location(module_name, current_extension_path + path.sep + file)
#                                 log.debug(module_spec)
# 
#                                 if module_spec is None:
#                                     continue
# 
#                                 module = module_from_spec(module_spec)
#                                 module_spec.loader.exec_module(module)
#                                 for extension_class in getmembers(module, isclass):
#                                     if module_name in extension_class:
#                                         log.info("Import %s from %s.", module_name, current_extension_path)
#                                         TBModuleLoader.LOADED_CONNECTORS[buffered_module_name] = extension_class[1]
#                                         return extension_class[1]
#                             except ImportError:
#                                 continue
#         except Exception as e:
#             log.exception(e)


import time
import serial
from random import choice
from string import ascii_lowercase
from threading import Thread
from thingsboard_gateway.connectors.connector import Connector, log  # Import base class for connector and logger
from thingsboard_gateway.tb_utility.tb_loader import TBModuleLoader
#     Copyright 2022. ThingsBoard
#
#     Licensed under the Apache License, Version 2.0 (the "License");
#     you may not use this file except in compliance with the License.
#     You may obtain a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#     Unless required by applicable law or agreed to in writing, software
#     distributed under the License is distributed on an "AS IS" BASIS,
#     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#     See the License for the specific language governing permissions and
#     limitations under the License.

"""Import libraries"""





class CustomSerialConnector(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  # Save configuration from the configuration file.
        self.__gateway = gateway  # Save gateway object, we will use some gateway methods for adding devices and saving data from them.
        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.

# Path: thingsboard_gateway/connectors/connector.py
# class Connector(ABC):
#     def open(self):
#     def close(self):
#     def get_name(self):
#     def is_connected(self):
#     def on_attributes_update(self, content):
#     def server_side_rpc_handler(self, content):
# 
# Path: thingsboard_gateway/tb_utility/tb_loader.py
# class TBModuleLoader:
#     PATHS = []
#     LOADED_CONNECTORS = {}
# 
#     @staticmethod
#     def find_paths():
#         root_path = path.abspath(path.dirname(path.dirname(__file__)))
#         log.debug("Root path is: " + root_path)
#         if path.exists(DEB_INSTALLATION_EXTENSION_PATH):
#             log.debug("Debian installation extensions folder exists.")
#             TBModuleLoader.PATHS.append(DEB_INSTALLATION_EXTENSION_PATH)
#         TBModuleLoader.PATHS.append(root_path + EXTENSIONS_FOLDER)
#         TBModuleLoader.PATHS.append(root_path + CONNECTORS_FOLDER)
# 
#     @staticmethod
#     def import_module(extension_type, module_name):
#         if len(TBModuleLoader.PATHS) == 0:
#             TBModuleLoader.find_paths()
#         buffered_module_name = extension_type + module_name
#         if TBModuleLoader.LOADED_CONNECTORS.get(buffered_module_name) is not None:
#             return TBModuleLoader.LOADED_CONNECTORS[buffered_module_name]
#         try:
#             for current_path in TBModuleLoader.PATHS:
#                 current_extension_path = current_path + path.sep + extension_type
#                 if path.exists(current_extension_path):
#                     for file in listdir(current_extension_path):
#                         if not file.startswith('__') and file.endswith('.py'):
#                             try:
#                                 module_spec = spec_from_file_location(module_name, current_extension_path + path.sep + file)
#                                 log.debug(module_spec)
# 
#                                 if module_spec is None:
#                                     continue
# 
#                                 module = module_from_spec(module_spec)
#                                 module_spec.loader.exec_module(module)
#                                 for extension_class in getmembers(module, isclass):
#                                     if module_name in extension_class:
#                                         log.info("Import %s from %s.", module_name, current_extension_path)
#                                         TBModuleLoader.LOADED_CONNECTORS[buffered_module_name] = extension_class[1]
#                                         return extension_class[1]
#                             except ImportError:
#                                 continue
#         except Exception as e:
#             log.exception(e)


# Path: thingsboard_gateway/extensions/serial/custom_serial_connector.py
import time
import serial
from random import choice
from string import ascii_lowercase
from threading import Thread
from thingsboard_gateway.connectors.connector import Connector, log  # Import base class for connector and logger
from thingsboard_gateway.tb_utility.tb_loader import TBModuleLoader

#     Copyright 2022. ThingsBoard
#
#     Licensed under the Apache License, Version 2.0 (the "License");
#     you may not use this file except in compliance with the License.
#     You may obtain a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#     Unless required by applicable law or agreed to in writing, software
#     distributed under the License is distributed on an "AS IS" BASIS,
#     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#     See the License for the specific language governing permissions and
#     limitations under the License.

"""Import libraries"""





class CustomSerialConnector(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  # Save configuration from the configuration file.
        self.__gateway = gateway  # Save gateway object, we will use some gateway methods for adding devices and saving data from them.
        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.

        log.info("Starting Custom %s connector", self.get_name())  # Send message to logger