# Path: thingsboard_gateway/connectors/bacnet/bacnet_converter.py
# class BACnetConverter(Converter):
#     def __init__(self, config):
#     def convert(self, config, data):


from bacpypes.apdu import APDU, ReadPropertyACK
from bacpypes.constructeddata import ArrayOf
from bacpypes.primitivedata import Tag
from thingsboard_gateway.connectors.bacnet.bacnet_converter import BACnetConverter, log
#
#         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 BACnetUplinkConverter(BACnetConverter):
    def __init__(self, config):
        self.__config = config

    def convert(self, config, data):
        value = None
        if isinstance(data, ReadPropertyACK):
            value = self.__property_value_from_apdu(data)
        if config is not None:
            datatypes = {"attributes": "attributes",
                         "timeseries": "telemetry",
                         "telemetry": "telemetry"}
            dict_result = {"deviceName": None, "deviceType": None, "attributes": [], "telemetry": []}
            dict_result["deviceName"] = self.__config.get("deviceName", config[1].get("name", "BACnet device"))
            dict_result["deviceType"] = self.__config.get("deviceType", "default")
            dict_result[datatypes[config[0]]].append({config[1]["key"]: value})
        else:
            dict_result = value

# Path: thingsboard_gateway/connectors/bacnet/bacnet_converter.py
# class BACnetConverter(Converter):
#     def __init__(self, config):
#     def convert(self, config, data):


# Path: thingsboard_gateway/connectors/bacnet/bacnet_uplink_converter.py
from bacpypes.apdu import APDU, ReadPropertyACK
from bacpypes.constructeddata import ArrayOf
from bacpypes.primitivedata import Tag
from thingsboard_gateway.connectors.bacnet.bacnet_converter import BACnetConverter, log

#
#         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 BACnetUplinkConverter(BACnetConverter):
    def __init__(self, config):
        self.__config = config

    def convert(self, config, data):
        value = None
        if isinstance(data, ReadPropertyACK):
            value = self.__property_value_from_apdu(data)
        if config is not None:
            datatypes = {"attributes": "attributes",
                         "timeseries": "telemetry",
                         "telemetry": "telemetry"}
            dict_result = {"deviceName": None, "deviceType": None, "attributes": [], "telemetry": []}
            dict_result["deviceName"] = self.__config.get("deviceName", config[1].get("name", "BACnet device"))
            dict_result["deviceType"] = self.__config.get("deviceType", "default")
            dict_result[datatypes[config[0]]].append({config[1]["key"]: value})
        else:
            dict_result = value

        log.debug("%r %r", self, dict_result)