Created
January 3, 2021 16:45
-
-
Save JoelBender/a1c5acdf523c83010622c04f2c999172 to your computer and use it in GitHub Desktop.
Local Objects
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 is a server that has a local, writable, Analog Value | |
and Binary Value object. | |
On a network with two IPv4 workstations make a copy of the BACpypes~.ini file | |
called in BACpypes.ini and set the 'address:' to the CIDR address of the | |
workstation. For example, the first one is 192.168.10.11/24 and the second | |
is 192.168.10.21/24. | |
On the first workstation, run this sample application: | |
$ python local_objects.py | |
On the second workstation, run the ReadWriteProperty.py application, the debug | |
and color flags are optional but provide interesting output: | |
$ python ReadWriteProprty.py --debug --color | |
Now change the AnalogValueObject in the first applicatin from the console in | |
the second: | |
> write 192.168.10.11 analogValue:1 12.3 | |
ack | |
In the first window will be a notice that the value changed: | |
'av' changed from 0.0 to 12.300000190734863 | |
Changing the binary value is similar: | |
> write 192.168.10.11 binaryValue:1 active | |
ack | |
And this should appear: | |
'bv' changed from 'inactive' to 'active' | |
Enjoy! | |
""" | |
import time | |
from threading import Thread | |
from functools import partial | |
from bacpypes.debugging import bacpypes_debugging, ModuleLogger | |
from bacpypes.consolelogging import ConfigArgumentParser | |
from bacpypes.consolecmd import ConsoleCmd | |
from bacpypes.core import run, deferred, enable_sleeping | |
from bacpypes.task import RecurringTask | |
from bacpypes.app import BIPSimpleApplication | |
from bacpypes.primitivedata import Real | |
from bacpypes.object import ( | |
WritableProperty, | |
AnalogValueObject, | |
BinaryValueObject, | |
register_object_type, | |
) | |
from bacpypes.local.device import LocalDeviceObject | |
# some debugging | |
_debug = 0 | |
_log = ModuleLogger(globals()) | |
# test globals | |
test_av = None | |
test_bv = None | |
test_application = None | |
@register_object_type | |
class WritableAnalogValueObject(AnalogValueObject): | |
properties = [WritableProperty("presentValue", Real)] | |
def something_changed(thing, old_value, new_value): | |
print("%r changed from %r to %r" % (thing, old_value, new_value)) | |
def main(): | |
global test_av, test_bv, test_application | |
# make a parser | |
parser = ConfigArgumentParser(description=__doc__) | |
parser.add_argument( | |
"--console", action="store_true", default=False, help="create a console", | |
) | |
# parse the command line arguments | |
args = parser.parse_args() | |
if _debug: | |
_log.debug("initialization") | |
if _debug: | |
_log.debug(" - args: %r", args) | |
# make a device object | |
this_device = LocalDeviceObject(ini=args.ini) | |
if _debug: | |
_log.debug(" - this_device: %r", this_device) | |
# make a sample application | |
test_application = BIPSimpleApplication(this_device, args.ini.address) | |
# make an analog value object | |
test_av = WritableAnalogValueObject( | |
objectIdentifier=("analogValue", 1), | |
objectName="av", | |
presentValue=0.0, | |
statusFlags=[0, 0, 0, 0], | |
covIncrement=1.0, | |
) | |
_log.debug(" - test_av: %r", test_av) | |
# add it to the device | |
test_application.add_object(test_av) | |
_log.debug(" - object list: %r", this_device.objectList) | |
# add a very simple monitor | |
test_av._property_monitors["presentValue"].append(partial(something_changed, "av"),) | |
# make a binary value object | |
test_bv = BinaryValueObject( | |
objectIdentifier=("binaryValue", 1), | |
objectName="bv", | |
presentValue="inactive", | |
statusFlags=[0, 0, 0, 0], | |
) | |
_log.debug(" - test_bv: %r", test_bv) | |
# add it to the device | |
test_application.add_object(test_bv) | |
# add a very simple monitor | |
test_bv._property_monitors["presentValue"].append(partial(something_changed, "bv"),) | |
_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