-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy pathWrite.py
321 lines (260 loc) · 10.7 KB
/
Write.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2015 by Christian Tremblay, P.Eng <christian.tremblay@servisys.com>
# Licensed under LGPLv3, see file LICENSE in this source tree.
#
"""
Write.py - creation of WriteProperty requests
Used while defining an app
Example::
class BasicScript(WhoisIAm, WriteProperty)
Class::
WriteProperty()
def write()
"""
import re
from bacpypes3.apdu import (
ErrorRejectAbortNack,
)
from bacpypes3.app import Application
from bacpypes3.basetypes import PropertyIdentifier
# --- 3rd party modules ---
from bacpypes3.debugging import ModuleLogger
from bacpypes3.pdu import Address
from bacpypes3.primitivedata import ObjectIdentifier, Null
from BAC0.tasks.DoOnce import DoOnce
from ..app.asyncApp import BAC0Application
from ..utils.notes import note_and_log
# --- this application's modules ---
from .IOExceptions import (
ApplicationNotStarted,
NoResponseFromController,
WritePropertyException,
)
# ------------------------------------------------------------------------------
# some debugging
_debug = 0
_LOG = ModuleLogger(globals())
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]))?"
write_pattern = re.compile(WRITE_REGEX)
@note_and_log
class WriteProperty:
"""
Defines BACnet Write functions: WriteProperty [WritePropertyMultiple not supported]
"""
def write(self, args, vendor_id=0, timeout=10):
# asyncio.create_task(
# self._write(args=args, vendor_id=vendor_id, timeout=timeout)
# )
# loop = asyncio.get_event_loop()
# loop.run_until_complete(self._write(args=args, vendor_id=vendor_id, timeout=timeout))
write_task = DoOnce((self._write, [args, vendor_id, timeout]))
write_task.start()
async def _write(self, args, vendor_id=0, timeout=10):
"""Build a WriteProperty request, wait for an answer, and return status [True if ok, False if not].
:param args: String with <addr> <type> <inst> <prop> <value> [ <indx> ] - [ <priority> ]
:returns: return status [True if ok, False if not]
*Example*::
import BAC0
bacnet = BAC0.lite()
bacnet.write('2:5 analogValue 1 presentValue 100 - 8')
Direct the controller at (Network 2, address 5) to write 100 to the presentValues of
its analogValue 1 (AV:1) at priority 8
"""
if not self._started:
raise ApplicationNotStarted("BACnet stack not running - use startApp()")
_this_application: BAC0Application = self.this_application
_app: Application = _this_application.app
self.log_title("Write property", args)
(
device_address,
object_identifier,
property_identifier,
value,
property_array_index,
priority,
) = self.build_wp_request(args)
try:
response = await _app.write_property(
device_address,
object_identifier,
property_identifier,
value,
property_array_index,
priority,
)
except ErrorRejectAbortNack as err:
self.log(f"exception: {err!r}", level="error")
raise NoResponseFromController(f"APDU Abort Reason : {response}")
except ValueError as err:
self.log(f"exception: {err!r}", level="error")
raise ValueError(f"Invalid value for property : {err} | {response}")
except WritePropertyException as error:
# construction error
self._log.exception(f"exception: {error!r}")
@classmethod
def _parse_wp_args(cls, args):
"""
Utility to parse the string of the request.
Supports @obj_ and @prop_ syntax for objest type and property id, useful with proprietary objects and properties.
"""
global write_pattern
match = write_pattern.search(args)
try:
address = match.group("address")
objId = match.group("objId")
prop_id = match.group("propId")
value = match.group("value")
indx = match.group("indx")
priority = match.group("priority")
except AttributeError:
raise ValueError(f"Invalid request | {args}")
if ":" in objId:
obj_type, obj_inst = objId.split(":")
else:
obj_type, obj_inst = objId.split(" ")
if obj_type.isdigit():
obj_type = int(obj_type)
elif "@obj_" in obj_type:
obj_type = int(obj_type.split("_")[1])
obj_inst = int(obj_inst)
if "@prop_" in prop_id:
prop_id = prop_id.split("_")[1]
if prop_id.isdigit():
prop_id = int(prop_id)
value = Null(()) if value == "null" else value
indx = None if indx == "-" or indx is None or indx == "" else int(indx)
priority = None if priority is None else int(priority)
return (address, obj_type, obj_inst, prop_id, value, priority, indx)
def build_wp_request(self, args, vendor_id=0):
vendor_id = vendor_id
(
address,
obj_type,
obj_inst,
prop_id,
value,
priority,
indx,
) = WriteProperty._parse_wp_args(args)
object_identifier = ObjectIdentifier((obj_type, obj_inst))
property_identifier = PropertyIdentifier(prop_id)
device_address = Address(address)
request = (
device_address,
object_identifier,
property_identifier,
value,
indx,
priority,
)
self.log_subtitle("Creating Request")
self.log(
f"{'indx':<20} {'priority':<20} {'datatype':<20} {'value':<20}",
level="debug",
)
self.log(f"{'REQUEST':<20} {request}", level="debug")
return request
'''
def writeMultiple(self, addr=None, args=None, vendor_id=0, timeout=10):
"""Build a WritePropertyMultiple request, wait for an answer
:param addr: destination of request (ex. '2:3' or '192.168.1.2')
:param args: list of String with <type> <inst> <prop> <value> [ <indx> ] - [ <priority> ]
:param vendor_id: Mandatory for registered proprietary object and properties
:param timeout: used by IOCB to discard request if timeout reached
:returns: return status [True if ok, False if not]
*Example*::
import BAC0
bacnet = BAC0.lite()
r = ['analogValue 1 presentValue 100','analogValue 2 presentValue 100','analogValue 3 presentValue 100 - 8','@obj_142 1 @prop_1042 True']
bacnet.writeMultiple(addr='2:5',args=r,vendor_id=842)
# or
# bacnet.writeMultiple('2:5',r)
"""
if not self._started:
raise ApplicationNotStarted("BACnet stack not running - use startApp()")
self.log_title("Write property multiple", args)
try:
# build a WritePropertyMultiple request
iocb = IOCB(self.build_wpm_request(args, vendor_id=vendor_id, addr=addr))
iocb.set_timeout(timeout)
# pass to the BACnet stack
deferred(self.this_application.request_io, iocb)
self.log(f"{'iocb':<20} {iocb!r}", level='debug')
except WritePropertyException as error:
# construction error
self._log.exception(f"exception: {error!r}")
iocb.wait() # Wait for BACnet response
if iocb.ioResponse: # successful response
apdu = iocb.ioResponse
if not isinstance(apdu, SimpleAckPDU): # expect an ACK
self.log("Not an ack, see debug for more infos.", level='warning')
self.log(f"Not an ack. | APDU : {apdu} / {type(apdu)}", level='debug')
return
if iocb.ioError: # unsuccessful: error/reject/abort
apdu = iocb.ioError
reason = find_reason(apdu)
raise NoResponseFromController(f"APDU Abort Reason : {reason}")
def build_wpm_request(self, args, vendor_id=0, addr=None):
if not addr:
raise ValueError("Please provide addr")
address = Address(addr)
self.log_subtitle("Creating Write Multiple Request")
self.log(f"{'indx':<20} {'priority':<20} {'datatype':<20} {'value':<20}", level='debug')
was = []
listOfObjects = []
for each in args:
property_values = []
if isinstance(each, str):
(
object_identifier,
property_identifier,
value,
priority,
indx,
) = self._parse_wp_args(each)
elif isinstance(each, tuple):
# Supported but not really the best choice as the parser will
# catch some edge cases for you
object_identifier, property_identifier, value, priority, indx = each
else:
raise ValueError("Wrong encoding of request")
existingObject = next(
(obj for obj in was if obj.objectIdentifier == (obj_type, obj_inst)),
None,
)
if existingObject is None:
property_values.append(
PropertyValue(
propertyIdentifier=prop_id,
propertyArrayIndex=indx,
value=value,
priority=priority,
)
)
was.append(
WriteAccessSpecification(
objectIdentifier=(obj_type, obj_inst),
listOfProperties=property_values,
)
)
else:
existingObject.listOfProperties.append(
PropertyValue(
propertyIdentifier=prop_id,
propertyArrayIndex=indx,
value=value,
priority=priority,
)
)
datatype = get_datatype(obj_type, prop_id, vendor_id=vendor_id)
self._log.debug(
f"{indx!r:<20} {priority!r:<20} {datatype!r:<20} {value!r:<20}"
)
# build a request
request = WritePropertyMultipleRequest(listOfWriteAccessSpecs=was)
request.pduDestination = Address(addr)
self.log(f"{'REQUEST':<20} {request}", level='debug')
return request
'''