# Path: thingsboard_gateway/connectors/modbus/modbus_converter.py
# class ModbusConverter(Converter):
#     def convert(self, config, data):


from pymodbus.constants import Endian
from pymodbus.exceptions import ModbusIOException
from pymodbus.payload import BinaryPayloadDecoder
from pymodbus.pdu import ExceptionResponse
from thingsboard_gateway.connectors.modbus.modbus_converter import ModbusConverter, log
                    response = data[config_data][tag]["input_data"]
                    if configuration.get("byteOrder"):
                        byte_order = configuration["byteOrder"]
                    elif config.get("byteOrder"):
                        byte_order = config["byteOrder"]
                    else:
                        byte_order = "LITTLE"
                    if configuration.get("wordOrder"):
                        word_order = configuration["wordOrder"]
                    elif config.get("wordOrder"):
                        word_order = config.get("wordOrder", "LITTLE")
                    else:
                        word_order = "LITTLE"
                    endian_order = Endian.Little if byte_order.upper() == "LITTLE" else Endian.Big
                    word_endian_order = Endian.Little if word_order.upper() == "LITTLE" else Endian.Big
                    decoded_data = None
                    if not isinstance(response, ModbusIOException) and not isinstance(response, ExceptionResponse):
                        if configuration["functionCode"] == 1:
                            decoder = None
                            coils = response.bits
                            try:
                                decoder = BinaryPayloadDecoder.fromCoils(coils, byteorder=endian_order, wordorder=word_endian_order)
                            except TypeError:
                                decoder = BinaryPayloadDecoder.fromCoils(coils, wordorder=word_endian_order)
                            assert decoder is not None

                            decoded_data = decoder.decode_bits()[0]
                        elif configuration["functionCode"] in [2, 3, 4]:
                            decoder = None
                            registers = response.registers

# Path: thingsboard_gateway/connectors/modbus/modbus_converter.py
# class ModbusConverter(Converter):
#     def convert(self, config, data):


# Path: thingsboard_gateway/connectors/modbus/bytes_modbus_uplink_converter.py
from pymodbus.constants import Endian
from pymodbus.exceptions import ModbusIOException
from pymodbus.payload import BinaryPayloadDecoder
from pymodbus.pdu import ExceptionResponse
from thingsboard_gateway.connectors.modbus.modbus_converter import ModbusConverter, log

                    response = data[config_data][tag]["input_data"]
                    if configuration.get("byteOrder"):
                        byte_order = configuration["byteOrder"]
                    elif config.get("byteOrder"):
                        byte_order = config["byteOrder"]
                    else:
                        byte_order = "LITTLE"
                    if configuration.get("wordOrder"):
                        word_order = configuration["wordOrder"]
                    elif config.get("wordOrder"):
                        word_order = config.get("wordOrder", "LITTLE")
                    else:
                        word_order = "LITTLE"
                    endian_order = Endian.Little if byte_order.upper() == "LITTLE" else Endian.Big
                    word_endian_order = Endian.Little if word_order.upper() == "LITTLE" else Endian.Big
                    decoded_data = None
                    if not isinstance(response, ModbusIOException) and not isinstance(response, ExceptionResponse):
                        if configuration["functionCode"] == 1:
                            decoder = None
                            coils = response.bits
                            try:
                                decoder = BinaryPayloadDecoder.fromCoils(coils, byteorder=endian_order, wordorder=word_endian_order)
                            except TypeError:
                                decoder = BinaryPayloadDecoder.fromCoils(coils, wordorder=word_endian_order)
                            assert decoder is not None

                            decoded_data = decoder.decode_bits()[0]
                        elif configuration["functionCode"] in [2, 3, 4]:
                            decoder = None
                            registers = response.registers

                            log.debug("Tag: %s Config: %s registers: %s", tag, str(configuration), str(registers))