1
1
import asyncio
2
+ import random
2
3
3
4
import BAC0
4
5
from BAC0 .core .devices .local .factory import (
14
15
make_state_text ,
15
16
multistate_input ,
16
17
multistate_output ,
18
+ multistate_value ,
17
19
)
20
+ from BAC0 .scripts .script_runner import run
21
+
22
+ bacnet = None
18
23
19
24
20
25
def add_points (qty_per_type , device ):
@@ -36,17 +41,17 @@ def add_points(qty_per_type, device):
36
41
)
37
42
38
43
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
+ )
45
50
46
51
# All others using default implementation
47
52
for _ in range (qty_per_type ):
48
53
# _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 )
50
55
_new_objects = binary_input ()
51
56
_new_objects = binary_output ()
52
57
_new_objects = binary_value ()
@@ -60,36 +65,55 @@ def add_points(qty_per_type, device):
60
65
61
66
62
67
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)
92
115
93
116
94
117
if __name__ == "__main__" :
95
- asyncio .run (main ())
118
+ run (main , bacnet )
119
+ # asyncio.run(main())
0 commit comments