コード例 #1
0
def default_values(priority_array, default_pv):
    priority_array_return = PriorityArray()
    present_value = default_pv
    for i in range(16, 0, -1):
        value = getattr(priority_array, f'_{i}')
        if not value:
            write = {'null': ()}
        else:
            present_value = value
            # TODO: Switch cases for different type of points
            write = {'real': value}
        priority_array_return.__setitem__(i, PriorityValue(**write))
    return [priority_array_return, present_value]
コード例 #2
0
    def __init__(self, init_value, **kwargs):
        if _debug:
            CommandableMixin._debug("__init__ %r, %r", init_value, kwargs)
        super(CommandableMixin, self).__init__(**kwargs)

        # if no present value given, give it the default value
        if ('presentValue' not in kwargs):
            if _debug:
                CommandableMixin._debug("    - initialize present value")
            self.presentValue = init_value

        # if no priority array given, give it an empty one
        if ('priorityArray' not in kwargs):
            if _debug:
                CommandableMixin._debug("    - initialize priority array")
            self.priorityArray = PriorityArray()
            for i in range(16):
                self.priorityArray.append(PriorityValue(null=Null()))

        # if no relinquish default value given, give it the default value
        if ('relinquishDefault' not in kwargs):
            if _debug:
                CommandableMixin._debug("    - initialize relinquish default")
            self.relinquishDefault = init_value

        # capture the present value property
        self._pv = self._properties['presentValue']
        if _debug: CommandableMixin._debug("    - _pv: %r", self._pv)

        # capture the datatype
        self._pv_datatype = self._pv.datatype
        if _debug:
            CommandableMixin._debug("    - _pv_datatype: %r",
                                    self._pv_datatype)

        # look up a matching priority value choice
        for element in PriorityValue.choiceElements:
            if element.klass is self._pv_datatype:
                self._pv_choice = element.name
                break
        else:
            self._pv_choice = 'constructedValue'
        if _debug:
            CommandableMixin._debug("    - _pv_choice: %r", self._pv_choice)
コード例 #3
0
def create_AO(oid=1, pv=0, name="AO", units=None, pv_writable=False):
    aoo = AnalogOutputObject(
        objectIdentifier=("analogOutput", oid),
        objectName=name,
        presentValue=pv,
        units=units,
        priorityArray=PriorityArray(),
    )
    aoo = _make_mutable(aoo, mutable=pv_writable)
    return aoo
コード例 #4
0
def create_CharStrValue(oid=1, pv="null", name="String", pv_writable=False):
    charval = CharacterStringValueObject(
        objectIdentifier=("characterstringValue", oid),
        objectName=name,
        priorityArray=PriorityArray(),
        statusFlags=StatusFlags(),
    )
    charval = _make_mutable(charval, mutable=pv_writable)
    charval.presentValue = CharacterString(pv)
    deprecate_msg()
    return charval
コード例 #5
0
ファイル: CommandableMixin.py プロジェクト: bacol89/bacpypes
        def __init__(self, **kwargs):
            super(_Commando, self).__init__(**kwargs)

            # build a default value in case one is needed
            default_value = datatype().value
            if issubclass(datatype, Enumerated):
                default_value = datatype._xlate_table[default_value]
            if _debug: Commandable._debug("    - default_value: %r", default_value)

            # see if a present value was provided
            if (presentValue not in kwargs):
                setattr(self, presentValue, default_value)

            # see if a priority array was provided
            if (priorityArray not in kwargs):
                new_priority_array = PriorityArray()
                for i in range(16):
                    new_priority_array.append(PriorityValue(null=()))
                setattr(self, priorityArray, new_priority_array)

            # see if a present value was provided
            if (relinquishDefault not in kwargs):
                setattr(self, relinquishDefault, default_value)
コード例 #6
0
def create_AV(oid=1, pv=0, name="AV", units=None, pv_writable=False):
    avo = AnalogValueObject(
        objectIdentifier=("analogValue", oid),
        objectName=name,
        presentValue=pv,
        units=units,
        relinquishDefault=0,
        priorityArray=PriorityArray(),
    )
    avo = _make_mutable(avo, mutable=pv_writable)
    avo = _make_mutable(avo,
                        identifier="relinquishDefault",
                        mutable=pv_writable)
    return avo
コード例 #7
0
class TestBACnetCoderMixin:
    @pytest.mark.parametrize(
        "priority_array, expected",
        [
            (PriorityArray([PriorityValue(null=Null())] * 16), [None] * 16),
            (
                PriorityArray([
                    *[PriorityValue(null=Null())] * 7,
                    PriorityValue(real=3.3),
                    *[PriorityValue(null=Null())] * 8,
                ]),
                [*[None] * 7, 3.3, *[None] * 8],
            ),
        ],
    )
    def test__decode_priority_array(self, priority_array, expected):
        bacnet_decoder = BACnetCoderMixin()
        assert (bacnet_decoder._decode_priority_array(
            priority_array=priority_array) == expected)

    @pytest.mark.parametrize(
        "value, expected",
        [
            (1, "active"),
            (0, "inactive"),
            (1.0, "active"),
            (True, "active"),
            (False, "inactive"),
            (None, "inactive"),
            ("1", "active"),
            ("0", "active"),
        ],
    )
    def test__encode_binary_present_value(self, value, expected):
        bacnet_decoder = BACnetCoderMixin()
        assert bacnet_decoder._encode_binary_present_value(
            value=value) == expected
コード例 #8
0
def create_MV(oid=1,
              pv=0,
              name="MV",
              states=["red", "green", "blue"],
              pv_writable=False):
    msvo = MultiStateValueObject(
        objectIdentifier=("multiStateValue", oid),
        objectName=name,
        presentValue=pv,
        numberOfStates=len(states),
        stateText=ArrayOf(CharacterString)(states),
        priorityArray=PriorityArray(),
    )
    msvo = _make_mutable(msvo, mutable=pv_writable)
    return msvo
コード例 #9
0
def create_BV(oid=1,
              pv=0,
              name="BV",
              activeText="On",
              inactiveText="Off",
              pv_writable=False):
    bvo = BinaryValueObject(
        objectIdentifier=("binaryValue", oid),
        objectName=name,
        presentValue=pv,
        activeText=activeText,
        inactiveText=inactiveText,
        priorityArray=PriorityArray(),
    )
    bvo = _make_mutable(bvo, mutable=pv_writable)
    return bvo
コード例 #10
0
    def __init__(self, init_value, **kwargs):
        if _debug:
            CommandableMixin._debug("__init__ %r, %r", init_value, kwargs)
        super(CommandableMixin, self).__init__(**kwargs)

        # if no present value given, give it the default value
        if "presentValue" not in kwargs:
            if _debug:
                CommandableMixin._debug("    - initialize present value")
            self.presentValue = init_value

        # if no priority array given, give it an empty one
        if "priorityArray" not in kwargs:
            if _debug:
                CommandableMixin._debug("    - initialize priority array")
            self.priorityArray = PriorityArray()
            for i in range(16):
                self.priorityArray.append(PriorityValue(null=Null()))

        # if no relinquish default value given, give it the default value
        if "relinquishDefault" not in kwargs:
            if _debug:
                CommandableMixin._debug("    - initialize relinquish default")
            self.relinquishDefault = init_value

        # capture the present value property
        self._pv = self._properties["presentValue"]
        if _debug:
            CommandableMixin._debug("    - _pv: %r", self._pv)

        # capture the datatype
        self._pv_datatype = self._pv.datatype
        if _debug:
            CommandableMixin._debug("    - _pv_datatype: %r", self._pv_datatype)

        # look up a matching priority value choice
        for element in PriorityValue.choiceElements:
            if element.klass is self._pv_datatype:
                self._pv_choice = element.name
                break
        else:
            self._pv_choice = "constructedValue"
        if _debug:
            CommandableMixin._debug("    - _pv_choice: %r", self._pv_choice)
コード例 #11
0
ファイル: CommandableMixin.py プロジェクト: i-DRC/BAC0
        def __init__(self, **kwargs):
            super(_Commando, self).__init__(**kwargs)

            # build a default value in case one is needed
            default_value = datatype().value
            if issubclass(datatype, Enumerated):
                default_value = datatype._xlate_table[default_value]
            if _debug:
                Commandable._debug("    - default_value: %r", default_value)

            # see if a present value was provided
            if presentValue not in kwargs:
                setattr(self, presentValue, default_value)

            # see if a priority array was provided
            if priorityArray not in kwargs:
                setattr(self, priorityArray, PriorityArray())

            # see if a present value was provided
            if relinquishDefault not in kwargs:
                setattr(self, relinquishDefault, default_value)
コード例 #12
0
 def default_properties(objectType,
                        properties,
                        is_commandable=False,
                        relinquish_default=None):
     _properties = properties or {}
     if "statusFlags" not in _properties.keys():
         _properties["statusFlags"] = [0, 0, 0, 0]
     if "reliability" not in _properties.keys():
         _properties["reliability"] = Reliability(0)
     if ("analog" in objectType.__name__.lower()
             and "units" not in _properties.keys()):
         raise ValueError("Provide Engineering Units in properties")
     if is_commandable and ("input" in objectType.__name__.lower()
                            or "output" in objectType.__name__.lower()):
         _properties["outOfService"] = False
     if is_commandable:
         _properties["priorityArray"] = PriorityArray()
         _properties[
             "relinquishDefault"] = ObjectFactory.relinquish_default_value(
                 objectType, relinquish_default)
     return _properties
コード例 #13
0
class CommandableMixin(object):

    def __init__(self, init_value, **kwargs):
        if _debug: CommandableMixin._debug("__init__ %r, %r", init_value, kwargs)
        super(CommandableMixin, self).__init__(**kwargs)

        # if no present value given, give it the default value
        if ('presentValue' not in kwargs):
            if _debug: CommandableMixin._debug("    - initialize present value")
            self.presentValue = init_value

        # if no priority array given, give it an empty one
        if ('priorityArray' not in kwargs):
            if _debug: CommandableMixin._debug("    - initialize priority array")
            self.priorityArray = PriorityArray()
            for i in range(16):
                self.priorityArray.append(PriorityValue(null=Null()))

        # if no relinquish default value given, give it the default value
        if ('relinquishDefault' not in kwargs):
            if _debug: CommandableMixin._debug("    - initialize relinquish default")
            self.relinquishDefault = init_value

        # capture the present value property
        self._pv = self._properties['presentValue']
        if _debug: CommandableMixin._debug("    - _pv: %r", self._pv)

        # capture the datatype
        self._pv_datatype = self._pv.datatype
        if _debug: CommandableMixin._debug("    - _pv_datatype: %r", self._pv_datatype)

        # look up a matching priority value choice
        for element in PriorityValue.choiceElements:
            if element.klass is self._pv_datatype:
                self._pv_choice = element.name
                break
        else:
            self._pv_choice = 'constructedValue'
        if _debug: CommandableMixin._debug("    - _pv_choice: %r", self._pv_choice)

    def WriteProperty(self, property, value, arrayIndex=None, priority=None, direct=False):
        if _debug: CommandableMixin._debug("WriteProperty %r %r arrayIndex=%r priority=%r direct=%r", property, value, arrayIndex, priority, direct)

        # when writing to the presentValue with a priority
        if (property == 'presentValue'):
            # default (lowest) priority
            if priority is None:
                priority = 16
            if _debug: CommandableMixin._debug("    - translate to array index %d", priority)

            # translate to updating the priority array
            property = 'priorityArray'
            arrayIndex = priority
            priority = None

        # update the priority array entry
        if (property == 'priorityArray') and (arrayIndex is not None):
            # check the bounds
            if arrayIndex == 0:
                raise ExecutionError(errorClass='property', errorCode='writeAccessDenied')
            if (arrayIndex < 1) or (arrayIndex > 16):
                raise ExecutionError(errorClass='property', errorCode='invalidArrayIndex')

            # update the specific priorty value element
            priority_value = self.priorityArray[arrayIndex]
            if _debug: CommandableMixin._debug("    - priority_value: %r", priority_value)

            # the null or the choice has to be set, the other clear
            if value is ():
                if _debug: CommandableMixin._debug("    - write a null")
                priority_value.null = value
                setattr(priority_value, self._pv_choice, None)
            else:
                if _debug: CommandableMixin._debug("    - write a value")
                priority_value.null = None
                setattr(priority_value, self._pv_choice, value)

            # look for the highest priority value
            for i in range(1, 17):
                priority_value = self.priorityArray[i]
                if priority_value.null is None:
                    if (i < arrayIndex):
                        if _debug: CommandableMixin._debug("    - existing higher priority value")
                        return
                    value = getattr(priority_value, self._pv_choice)
                    break
            else:
                value = self.relinquishDefault
            if _debug: CommandableMixin._debug("    - new present value: %r", value)

            property = 'presentValue'
            arrayIndex = priority = None

        # allow the request to pass through
        if _debug: CommandableMixin._debug("    - super: %r %r arrayIndex=%r priority=%r", property, value, arrayIndex, priority)
        super(CommandableMixin, self).WriteProperty(
            property, value,
            arrayIndex=arrayIndex, priority=priority, direct=direct,
            )
コード例 #14
0
ファイル: CommandableMixin.py プロジェクト: yaztown/bacpypes
class CommandableMixin(object):

    def __init__(self, init_value, **kwargs):
        if _debug: CommandableMixin._debug("__init__ %r, %r", init_value, kwargs)
        super(CommandableMixin, self).__init__(**kwargs)

        # if no present value given, give it the default value
        if ('presentValue' not in kwargs):
            if _debug: CommandableMixin._debug("    - initialize present value")
            self.presentValue = init_value

        # if no priority array given, give it an empty one
        if ('priorityArray' not in kwargs):
            if _debug: CommandableMixin._debug("    - initialize priority array")
            self.priorityArray = PriorityArray()
            for i in range(16):
                self.priorityArray.append(PriorityValue(null=Null()))

        # if no relinquish default value given, give it the default value
        if ('relinquishDefault' not in kwargs):
            if _debug: CommandableMixin._debug("    - initialize relinquish default")
            self.relinquishDefault = init_value

        # capture the present value property
        self._pv = self._properties['presentValue']
        if _debug: CommandableMixin._debug("    - _pv: %r", self._pv)

        # capture the datatype
        self._pv_datatype = self._pv.datatype
        if _debug: CommandableMixin._debug("    - _pv_datatype: %r", self._pv_datatype)

        # look up a matching priority value choice
        for element in PriorityValue.choiceElements:
            if element.klass is self._pv_datatype:
                self._pv_choice = element.name
                break
        else:
            self._pv_choice = 'constructedValue'
        if _debug: CommandableMixin._debug("    - _pv_choice: %r", self._pv_choice)

    def WriteProperty(self, property, value, arrayIndex=None, priority=None, direct=False):
        if _debug: CommandableMixin._debug("WriteProperty %r %r arrayIndex=%r priority=%r direct=%r", property, value, arrayIndex, priority, direct)

        # when writing to the presentValue with a priority
        if (property == 'presentValue'):
            # default (lowest) priority
            if priority is None:
                priority = 16
            if _debug: CommandableMixin._debug("    - translate to array index %d", priority)

            # translate to updating the priority array
            property = 'priorityArray'
            arrayIndex = priority
            priority = None

        # update the priority array entry
        if (property == 'priorityArray') and (arrayIndex is not None):
            # check the bounds
            if arrayIndex == 0:
                raise ExecutionError(errorClass='property', errorCode='writeAccessDenied')
            if (arrayIndex < 1) or (arrayIndex > 16):
                raise ExecutionError(errorClass='property', errorCode='invalidArrayIndex')

            # update the specific priorty value element
            priority_value = self.priorityArray[arrayIndex]
            if _debug: CommandableMixin._debug("    - priority_value: %r", priority_value)

            # the null or the choice has to be set, the other clear
            if value is ():
                if _debug: CommandableMixin._debug("    - write a null")
                priority_value.null = value
                setattr(priority_value, self._pv_choice, None)
            else:
                if _debug: CommandableMixin._debug("    - write a value")
                priority_value.null = None
                setattr(priority_value, self._pv_choice, value)

            # look for the highest priority value
            for i in range(1, 17):
                priority_value = self.priorityArray[i]
                if priority_value.null is None:
                    if (i < arrayIndex):
                        if _debug: CommandableMixin._debug("    - existing higher priority value")
                        return
                    value = getattr(priority_value, self._pv_choice)
                    break
            else:
                value = self.relinquishDefault
            if _debug: CommandableMixin._debug("    - new present value: %r", value)

            property = 'presentValue'
            arrayIndex = priority = None

        # allow the request to pass through
        if _debug: CommandableMixin._debug("    - super: %r %r arrayIndex=%r priority=%r", property, value, arrayIndex, priority)
        super(CommandableMixin, self).WriteProperty(
            property, value,
            arrayIndex=arrayIndex, priority=priority, direct=direct,
            )