Created
August 13, 2021 22:15
-
-
Save JoelBender/bd5318907a1d7caabd39eb2d577a7335 to your computer and use it in GitHub Desktop.
Sometimes write access is denied
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" | |
This sample application shows how to extend one of the basic objects, an Analog | |
Value Object in this case, to provide a present value that is writable but | |
occasionally returns an error. | |
""" | |
import os | |
import random | |
from bacpypes.debugging import bacpypes_debugging, ModuleLogger | |
from bacpypes.consolelogging import ConfigArgumentParser | |
from bacpypes.core import run | |
from bacpypes.primitivedata import Real | |
from bacpypes.object import AnalogValueObject, Property, register_object_type | |
from bacpypes.errors import ExecutionError | |
from bacpypes.app import BIPSimpleApplication | |
from bacpypes.local.device import LocalDeviceObject | |
# some debugging | |
_debug = 0 | |
_log = ModuleLogger(globals()) | |
# settings | |
WRITABLE_OBJECT_COUNT = int(os.getenv("WRITABLE_OBJECT_COUNT", 10)) | |
# | |
# WritableValueProperty | |
# | |
@bacpypes_debugging | |
class WritableValueProperty(Property): | |
def __init__(self, identifier): | |
if _debug: | |
WritableValueProperty._debug("__init__ %r", identifier) | |
Property.__init__( | |
self, identifier, Real, default=0.0, optional=True, mutable=True | |
) | |
def WriteProperty(self, obj, value, arrayIndex=None, priority=None, direct=False): | |
if _debug: | |
WritableValueProperty._debug( | |
"WriteProperty %r %r arrayIndex=%r priority=%r direct=%r", | |
obj, | |
value, | |
arrayIndex, | |
priority, | |
direct, | |
) | |
# check for some condition when it should be denied | |
dice_roll = random.random() * 100.0 | |
if _debug: | |
WritableValueProperty._debug(" - dice_roll: %r", dice_roll) | |
if (not direct) and (dice_roll < 10.0): | |
if _debug: | |
WritableValueProperty._debug(" - denied!") | |
raise ExecutionError(errorClass="property", errorCode="writeAccessDenied") | |
# continue along | |
return super().WriteProperty(obj, value, arrayIndex, priority, direct) | |
# | |
# Writable Value Object Type | |
# | |
@bacpypes_debugging | |
@register_object_type | |
class WritableAnalogValueObject(AnalogValueObject): | |
properties = [ | |
WritableValueProperty("presentValue"), | |
] | |
def __init__(self, **kwargs): | |
if _debug: | |
WritableAnalogValueObject._debug("__init__ %r", kwargs) | |
AnalogValueObject.__init__(self, **kwargs) | |
# | |
# __main__ | |
# | |
def main(): | |
# parse the command line arguments | |
args = ConfigArgumentParser(description=__doc__).parse_args() | |
if _debug: | |
_log.debug("initialization") | |
if _debug: | |
_log.debug(" - args: %r", args) | |
# make a device object | |
this_device = LocalDeviceObject( | |
objectName=args.ini.objectname, | |
objectIdentifier=("device", int(args.ini.objectidentifier)), | |
maxApduLengthAccepted=int(args.ini.maxapdulengthaccepted), | |
segmentationSupported=args.ini.segmentationsupported, | |
vendorIdentifier=int(args.ini.vendoridentifier), | |
) | |
# make a sample application | |
this_application = BIPSimpleApplication(this_device, args.ini.address) | |
# make some writable objects | |
for i in range(1, WRITABLE_OBJECT_COUNT + 1): | |
wavo = WritableAnalogValueObject( | |
objectIdentifier=("analogValue", i), | |
objectName="Writable-%d" % (i,), | |
) | |
_log.debug(" - wavo: %r", wavo) | |
this_application.add_object(wavo) | |
# make sure they are all there | |
_log.debug(" - object list: %r", this_device.objectList) | |
_log.debug("running") | |
run() | |
_log.debug("fini") | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment