Skip to content

Commit bf1cbde

Browse files
migrating and improving manual_test_create_device
leads to improving write REGEX
1 parent 642a41f commit bf1cbde

File tree

2 files changed

+62
-38
lines changed

2 files changed

+62
-38
lines changed

BAC0/core/io/Write.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def write()
4949
# some debugging
5050
_debug = 0
5151
_LOG = ModuleLogger(globals())
52-
WRITE_REGEX = r"(?P<address>\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b|(\b\d+:\d+\b)) (?P<objId>(@obj_)?[-\w:]*[: ]*\d*) (?P<propId>(@prop_)?\w*(-\w*)?)[ ]?(?P<value>-*\w*)?[ ]?(?P<indx>-|\d*)?[ ]?(?P<priority>(1[0-6]|[0-9]))?"
52+
WRITE_REGEX = r"(?P<address>\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(?::\d+)?\b|(\b\d+:\d+\b)) (?P<objId>(@obj_)?[-\w:]*[: ]*\d*) (?P<propId>(@prop_)?\w*(-\w*)?)[ ]?(?P<value>-*\w*)?[ ]?(?P<indx>-|\d*)?[ ]?(?P<priority>(1[0-6]|[0-9]))?"
5353
write_pattern = re.compile(WRITE_REGEX)
5454

5555

tests/manual_test_create_device.py

+61-37
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import asyncio
2+
import random
23

34
import BAC0
45
from BAC0.core.devices.local.factory import (
@@ -14,7 +15,11 @@
1415
make_state_text,
1516
multistate_input,
1617
multistate_output,
18+
multistate_value,
1719
)
20+
from BAC0.scripts.script_runner import run
21+
22+
bacnet = None
1823

1924

2025
def add_points(qty_per_type, device):
@@ -36,17 +41,17 @@ def add_points(qty_per_type, device):
3641
)
3742

3843
states = make_state_text(["Normal", "Alarm", "Super Emergency"])
39-
# _new_objects = multistate_value(
40-
# description="An Alarm Value",
41-
# properties={"stateText": states},
42-
# name="BIG-ALARM",
43-
# is_commandable=False,
44-
# )
44+
_new_objects = multistate_value(
45+
description="An Alarm Value",
46+
properties={"stateText": states},
47+
name="BIG-ALARM",
48+
is_commandable=False,
49+
)
4550

4651
# All others using default implementation
4752
for _ in range(qty_per_type):
4853
# _new_objects = analog_output(presentValue=89.9)
49-
_new_objects = analog_value(presentValue=79.9)
54+
_new_objects = analog_value(presentValue=79.9, is_commandable=True)
5055
_new_objects = binary_input()
5156
_new_objects = binary_output()
5257
_new_objects = binary_value()
@@ -60,36 +65,55 @@ def add_points(qty_per_type, device):
6065

6166

6267
async def main():
63-
bacnet = BAC0.lite()
64-
65-
# We'll use 3 devices with our first instance
66-
device_app = BAC0.lite(port=47809, deviceId=101, localObjName="App1")
67-
device30_app = BAC0.lite(port=47810, deviceId=102, localObjName="App2")
68-
device300_app = BAC0.lite(port=47811, deviceId=103, localObjName="App3")
69-
70-
add_points(2, device_app)
71-
add_points(3, device30_app)
72-
add_points(4, device300_app)
73-
74-
ip = device_app.localIPAddr.addrTuple[0]
75-
boid = device_app.Boid
76-
77-
ip_30 = device30_app.localIPAddr.addrTuple[0]
78-
boid_30 = device30_app.Boid
79-
80-
ip_300 = device300_app.localIPAddr.addrTuple[0]
81-
boid_300 = device300_app.Boid
82-
83-
# Connect to test device using main network
84-
test_device = BAC0.device("{}:47809".format(ip), boid, bacnet, poll=10)
85-
test_device_30 = BAC0.device("{}:47810".format(ip_30), boid_30, bacnet, poll=0)
86-
test_device_300 = BAC0.device("{}:47811".format(ip_300), boid_300, bacnet, poll=0)
87-
while True:
88-
await asyncio.sleep(0.01)
89-
# print(test_device.points)
90-
# (test_device_30.points)
91-
# (test_device_300.points)
68+
# We'll use 3 devices plus our main instance
69+
async with BAC0.start(localObjName="bacnet") as bacnet:
70+
async with BAC0.start(port=47809, localObjName="App1") as device_app:
71+
async with BAC0.start(port=47810, localObjName="App2") as device30_app:
72+
async with BAC0.start(port=47811, localObjName="App3") as device300_app:
73+
add_points(2, device_app)
74+
add_points(3, device30_app)
75+
add_points(4, device300_app)
76+
77+
ip = device_app.localIPAddr.addrTuple[0]
78+
boid = device_app.Boid
79+
80+
ip_30 = device30_app.localIPAddr.addrTuple[0]
81+
boid_30 = device30_app.Boid
82+
83+
ip_300 = device300_app.localIPAddr.addrTuple[0]
84+
boid_300 = device300_app.Boid
85+
86+
# Connect to test device using main network
87+
test_device = await BAC0.device(
88+
f"{ip}:47809", boid, bacnet, poll=10
89+
)
90+
test_device_30 = await BAC0.device(
91+
f"{ip_30}:47810", boid_30, bacnet, poll=0
92+
)
93+
test_device_300 = await BAC0.device(
94+
f"{ip_300}:47811", boid_300, bacnet, poll=0
95+
)
96+
bacnet._log.info("CTRL-C to exit")
97+
98+
while True:
99+
await asyncio.sleep(2)
100+
bacnet._log.info(f"{test_device['BIG-ALARM']}")
101+
new_val_for_av1 = random.randint(1, 100)
102+
bacnet._log.info(f"Setting AV-1 to {new_val_for_av1}")
103+
# test_device_30['AV-1'] = new_val_for_av1
104+
await test_device_30["AV-1"].write(new_val_for_av1, priority=16)
105+
await asyncio.sleep(2)
106+
bacnet._log.info(
107+
f"Forcing a read on test_device_30/AV-1 : {await test_device_30['AV-1'].value}"
108+
)
109+
test_device_300["AV-1"] = new_val_for_av1
110+
await asyncio.sleep(2)
111+
bacnet._log.info(
112+
f"Forcing a read on test_device_300/AV-1 : {await test_device_300['AV-1'].value}"
113+
)
114+
# (test_device_300.points)
92115

93116

94117
if __name__ == "__main__":
95-
asyncio.run(main())
118+
run(main, bacnet)
119+
# asyncio.run(main())

0 commit comments

Comments
 (0)