# Path: BAC0/core/io/Read.py
# def cast_datatype_from_tag(propertyValue, obj_id, prop_id):
#     try:
#         tag_list = propertyValue.tagList.tagList
#         if tag_list[0].tagClass == 0:
#             tag = tag_list[0].tagNumber
# 
#             datatype = Tag._app_tag_class[tag]
#         else:
#             from bacpypes.constructeddata import ArrayOf
# 
#             subtype_tag = propertyValue.tagList.tagList[0].tagList[0].tagNumber
#             datatype = ArrayOf(Tag._app_tag_class[subtype_tag])
#         value = {"{}_{}".format(obj_id, prop_id): propertyValue.cast_out(datatype)}
#     except:
#         value = {"{}_{}".format(obj_id, prop_id): propertyValue}
#     return value


from bacpypes.apdu import SubscribeCOVRequest, SimpleAckPDU, RejectPDU, AbortPDU
from bacpypes.iocb import IOCB
from bacpypes.core import deferred
from bacpypes.pdu import Address
from bacpypes.object import get_object_class, get_datatype
from bacpypes.constructeddata import Array
from bacpypes.primitivedata import Tag, ObjectIdentifier, Unsigned
from BAC0.core.io.Read import cast_datatype_from_tag

class SubscriptionContext:
    next_proc_id = 1

    def __init__(self, address, objectID, confirmed=None, lifetime=None, callback=None):
        self.address = address
        self.subscriberProcessIdentifier = SubscriptionContext.next_proc_id
        SubscriptionContext.next_proc_id += 1
        self.monitoredObjectIdentifier = objectID
        self.issueConfirmedNotifications = confirmed
        self.lifetime = lifetime
        self.callback = callback

    def cov_notification(self, apdu):
        # make a rash assumption that the property value is going to be
        # a single application encoded tag
        source = apdu.pduSource
        object_changed = apdu.monitoredObjectIdentifier

        elements = {
            "source": source,
            "object_changed": object_changed,
            "properties": {},
        }
        for element in apdu.listOfValues:
            prop_id = element.propertyIdentifier
            datatype = get_datatype(object_changed[0], prop_id)
            value = element.value

            if not datatype:

# Path: BAC0/core/io/Read.py
# def cast_datatype_from_tag(propertyValue, obj_id, prop_id):
#     try:
#         tag_list = propertyValue.tagList.tagList
#         if tag_list[0].tagClass == 0:
#             tag = tag_list[0].tagNumber
# 
#             datatype = Tag._app_tag_class[tag]
#         else:
#             from bacpypes.constructeddata import ArrayOf
# 
#             subtype_tag = propertyValue.tagList.tagList[0].tagList[0].tagNumber
#             datatype = ArrayOf(Tag._app_tag_class[subtype_tag])
#         value = {"{}_{}".format(obj_id, prop_id): propertyValue.cast_out(datatype)}
#     except:
#         value = {"{}_{}".format(obj_id, prop_id): propertyValue}
#     return value


# Path: BAC0/core/functions/cov.py
from bacpypes.apdu import SubscribeCOVRequest, SimpleAckPDU, RejectPDU, AbortPDU
from bacpypes.iocb import IOCB
from bacpypes.core import deferred
from bacpypes.pdu import Address
from bacpypes.object import get_object_class, get_datatype
from bacpypes.constructeddata import Array
from bacpypes.primitivedata import Tag, ObjectIdentifier, Unsigned
from BAC0.core.io.Read import cast_datatype_from_tag


class SubscriptionContext:
    next_proc_id = 1

    def __init__(self, address, objectID, confirmed=None, lifetime=None, callback=None):
        self.address = address
        self.subscriberProcessIdentifier = SubscriptionContext.next_proc_id
        SubscriptionContext.next_proc_id += 1
        self.monitoredObjectIdentifier = objectID
        self.issueConfirmedNotifications = confirmed
        self.lifetime = lifetime
        self.callback = callback

    def cov_notification(self, apdu):
        # make a rash assumption that the property value is going to be
        # a single application encoded tag
        source = apdu.pduSource
        object_changed = apdu.monitoredObjectIdentifier

        elements = {
            "source": source,
            "object_changed": object_changed,
            "properties": {},
        }
        for element in apdu.listOfValues:
            prop_id = element.propertyIdentifier
            datatype = get_datatype(object_changed[0], prop_id)
            value = element.value

            if not datatype:

                value = cast_datatype_from_tag(