from thingsboard_gateway.connectors.converter import Converter, log


class EthernetIPConverter(Converter):
	def __init__(self, config):
		self.__config = config
		self.result_dict = {
			'deviceName': config.get('name', 'EthernetIPDevice'),
			'deviceType': config.get('deviceType', 'default'),
			'attributes': [],
			'telemetry': []
			}
	
	def convert(self, config, data):
		keys = ["attributes", "telemetry"]
		for k in keys:
			self.result_dict[k] = []
			if self.__config.get(k) is not None and data is not None:
				for datapoint in data:
					if datapoint[3] == None:
						try:
							log.debug(config)
							converted_data = {config[k][datapoint[0]]["key"]: datapoint[1]}
							self.result_dict[k].append(converted_data)
						except KeyError:
							continue
		return self.result_dict
