# Path: thingsboard_gateway/connectors/converter.py
# class Converter(ABC):
#     def convert(self, config, data):
# 
# Path: thingsboard_gateway/connectors/odbc/odbc_converter.py
# class OdbcConverter(ABC):
#     @abstractmethod
#     def convert(self, config, data):
#         pass


from thingsboard_gateway.connectors.converter import log
from thingsboard_gateway.connectors.odbc.odbc_converter import OdbcConverter
#
#     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 OdbcUplinkConverter(OdbcConverter):
    def convert(self, config, data):
        if isinstance(config, str) and config == "*":
            return data

        converted_data = {}
        for config_item in config:
            try:
                if isinstance(config_item, str):
                    converted_data[config_item] = data[config_item]
                elif isinstance(config_item, dict):
                    if "nameExpression" in config_item:
                        name = eval(config_item["nameExpression"], globals(), data)
                    else:
                        name = config_item["name"]

                    if "column" in config_item:
                        converted_data[name] = data[config_item["column"]]
                    elif "value" in config_item:
                        converted_data[name] = eval(config_item["value"], globals(), data)
                    else:

# Path: thingsboard_gateway/connectors/converter.py
# class Converter(ABC):
#     def convert(self, config, data):
# 
# Path: thingsboard_gateway/connectors/odbc/odbc_converter.py
# class OdbcConverter(ABC):
#     @abstractmethod
#     def convert(self, config, data):
#         pass


# Path: thingsboard_gateway/connectors/odbc/odbc_uplink_converter.py
from thingsboard_gateway.connectors.converter import log
from thingsboard_gateway.connectors.odbc.odbc_converter import OdbcConverter

#
#     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 OdbcUplinkConverter(OdbcConverter):
    def convert(self, config, data):
        if isinstance(config, str) and config == "*":
            return data

        converted_data = {}
        for config_item in config:
            try:
                if isinstance(config_item, str):
                    converted_data[config_item] = data[config_item]
                elif isinstance(config_item, dict):
                    if "nameExpression" in config_item:
                        name = eval(config_item["nameExpression"], globals(), data)
                    else:
                        name = config_item["name"]

                    if "column" in config_item:
                        converted_data[name] = data[config_item["column"]]
                    elif "value" in config_item:
                        converted_data[name] = eval(config_item["value"], globals(), data)
                    else:

                        log.error("Failed to convert SQL data to TB format: no column/value configuration item")