# Path: thingsboard_gateway/connectors/can/can_converter.py
# class CanConverter(ABC):
#     @abstractmethod
#     def convert(self, config, data):
#         pass
# 
# Path: thingsboard_gateway/connectors/converter.py
# class Converter(ABC):
#     def convert(self, config, data):


import struct
from thingsboard_gateway.connectors.can.can_converter import CanConverter
from thingsboard_gateway.connectors.converter import log
#     Copyright 2020. 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.




class BytesCanDownlinkConverter(CanConverter):
    def convert(self, config, data):
        try:
            if config.get("dataInHex", ""):
                return list(bytearray.fromhex(config["dataInHex"]))

            if not isinstance(data, dict) or not data:

# Path: thingsboard_gateway/connectors/can/can_converter.py
# class CanConverter(ABC):
#     @abstractmethod
#     def convert(self, config, data):
#         pass
# 
# Path: thingsboard_gateway/connectors/converter.py
# class Converter(ABC):
#     def convert(self, config, data):


# Path: thingsboard_gateway/connectors/can/bytes_can_downlink_converter.py
import struct
from thingsboard_gateway.connectors.can.can_converter import CanConverter
from thingsboard_gateway.connectors.converter import log

#     Copyright 2020. 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.




class BytesCanDownlinkConverter(CanConverter):
    def convert(self, config, data):
        try:
            if config.get("dataInHex", ""):
                return list(bytearray.fromhex(config["dataInHex"]))

            if not isinstance(data, dict) or not data:

                log.error("Failed to convert TB data to CAN payload: data is empty or not a dictionary")