Repository navigation
Expand file tree
/
Copy pathBACpypes_applications.py
More file actions
569 lines (463 loc) · 19 KB
/
Copy pathBACpypes_applications.py
File metadata and controls
569 lines (463 loc) · 19 KB
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
# -*- coding: utf-8 -*-
"""
Created on Mon Nov 7 12:31:36 2016
@author: erdongwei
"""
import sys
import time
import ConfigParser
from bacpypes.debugging import bacpypes_debugging, ModuleLogger
from bacpypes.consolelogging import ConfigArgumentParser
from bacpypes.core import run, stop, deferred
from bacpypes.pdu import Address, GlobalBroadcast
from bacpypes.app import BIPSimpleApplication
from bacpypes.local.device import LocalDeviceObject
from bacpypes.apdu import ReadPropertyMultipleRequest, PropertyReference, \
ReadAccessSpecification, ReadPropertyMultipleACK, \
WritePropertyMultipleRequest, WriteAccessSpecification
from bacpypes.apdu import Error, AbortPDU, SimpleAckPDU, WhoIsRequest, IAmRequest, \
ReadPropertyRequest, ReadPropertyACK, WritePropertyRequest
from bacpypes.object import get_object_class, get_datatype
from bacpypes.basetypes import ServicesSupported, PropertyIdentifier, PropertyValue
from bacpypes.errors import DecodingError
from bacpypes.constructeddata import Array, Any
from bacpypes.primitivedata import Null, Atomic, Integer, Unsigned, Real
# some debugging
_debug = 0
_log = ModuleLogger(globals())
#
# WhoIsApplication
#
global valueRead
global this_app
global deviceList
@bacpypes_debugging
class Applications(BIPSimpleApplication):
def __init__(self, *args):
if _debug: Applications._debug("__init__ %r", args)
BIPSimpleApplication.__init__(self, *args)
# keep track of requests to line up responses
self._request = None
def request(self, apdu):
global deviceList
global valueRead
valueRead = None
if _debug: Applications._debug("request %r", apdu)
# save a copy of the request
self._request = apdu
self._initialTime = time.strftime("%s")
if (isinstance(self._request, WhoIsRequest)):
deviceList = []
# forward it along
BIPSimpleApplication.request(self, apdu)
def indication(self, apdu):
if _debug: Applications._debug("indication %r", apdu)
if (isinstance(self._request, WhoIsRequest)) and (isinstance(apdu, IAmRequest)):
device_type, device_instance = apdu.iAmDeviceIdentifier
if device_type != 'device':
raise DecodingError("invalid object type")
if (self._request.deviceInstanceRangeLowLimit is not None) and \
(device_instance < self._request.deviceInstanceRangeLowLimit):
pass
elif (self._request.deviceInstanceRangeHighLimit is not None) and \
(device_instance > self._request.deviceInstanceRangeHighLimit):
pass
else:
# print out the contents
#sys.stdout.write('pduSource = ' + repr(apdu.pduSource) + '\n')
'''sys.stdout.write('iAmDeviceIdentifier = ' + str(apdu.iAmDeviceIdentifier) + '\n')
sys.stdout.write('maxAPDULengthAccepted = ' + str(apdu.maxAPDULengthAccepted) + '\n')
sys.stdout.write('segmentationSupported = ' + str(apdu.segmentationSupported) + '\n')
sys.stdout.write('vendorID = ' + str(apdu.vendorID) + '\n')
sys.stdout.flush()'''
deviceList.append(apdu)
# forward it along
BIPSimpleApplication.indication(self, apdu)
def confirmation(self, apdu):
global valueRead
valueRead = None
if _debug: Applications._debug("confirmation %r", apdu)
if isinstance(apdu, Error):
sys.stdout.write("error: %s\n" % (apdu.errorCode,))
sys.stdout.flush()
valueRead = apdu
elif isinstance(apdu, AbortPDU):
apdu.debug_contents()
valueRead = apdu
elif isinstance(apdu, SimpleAckPDU):
sys.stdout.write("ack\n")
sys.stdout.flush()
elif (isinstance(self._request, ReadPropertyRequest)) and (isinstance(apdu, ReadPropertyACK)):
# find the datatype
datatype = get_datatype(apdu.objectIdentifier[0], apdu.propertyIdentifier)
if _debug: Applications._debug(" - datatype: %r", datatype)
if not datatype:
raise TypeError("unknown datatype")
# special case for array parts, others are managed by cast_out
if issubclass(datatype, Array) and (apdu.propertyArrayIndex is not None):
if apdu.propertyArrayIndex == 0:
value = apdu.propertyValue.cast_out(Unsigned)
else:
value = apdu.propertyValue.cast_out(datatype.subtype)
else:
value = apdu.propertyValue.cast_out(datatype)
if _debug: Applications._debug(" - value: %r", value)
valueRead = value
### readPropertyMulti
elif (isinstance(self._request, ReadPropertyMultipleRequest)) and (isinstance(apdu, ReadPropertyMultipleACK)):
valueRead = []
# loop through the results
for result in apdu.listOfReadAccessResults:
# here is the object identifier
objectIdentifier = result.objectIdentifier
if _debug: Applications._debug(" - objectIdentifier: %r", objectIdentifier)
# now come the property values per object
for element in result.listOfResults:
# get the property and array index
propertyIdentifier = element.propertyIdentifier
if _debug: Applications._debug(" - propertyIdentifier: %r", propertyIdentifier)
propertyArrayIndex = element.propertyArrayIndex
if _debug: Applications._debug(" - propertyArrayIndex: %r", propertyArrayIndex)
# here is the read result
readResult = element.readResult
#sys.stdout.write(propertyIdentifier)
if propertyArrayIndex is not None:
pass
#sys.stdout.write("[" + str(propertyArrayIndex) + "]")
# check for an error
if readResult.propertyAccessError is not None:
sys.stdout.write(" ! " + str(readResult.propertyAccessError) + '\n')
valueRead.append(None)
else:
# here is the value
propertyValue = readResult.propertyValue
# find the datatype
datatype = get_datatype(objectIdentifier[0], propertyIdentifier)
if _debug: Applications._debug(" - datatype: %r", datatype)
if not datatype:
valueRead.append(None)
raise TypeError("unknown datatype")
# special case for array parts, others are managed by cast_out
if issubclass(datatype, Array) and (propertyArrayIndex is not None):
if propertyArrayIndex == 0:
value = propertyValue.cast_out(Unsigned)
else:
value = propertyValue.cast_out(datatype.subtype)
else:
value = propertyValue.cast_out(datatype)
if _debug: Applications._debug(" - value: %r", value)
#sys.stdout.write(" = " + str(value) + '\n')
valueRead.append(value)
sys.stdout.flush()
### finished this reading
stop()
def Init(ini_filename):
global this_app
### read initialization file
opts = ConfigParser.ConfigParser()
opts.read(ini_filename)
objectName = opts.get('BACpypes', 'objectName')
objectIdentifier = opts.get('BACpypes', 'objectIdentifier')
maxApduLengthAccepted = opts.get('BACpypes', 'maxApduLengthAccepted')
segmentationSupported = opts.get('BACpypes', 'segmentationSupported')
vendorIdentifier = opts.get('BACpypes', 'vendorIdentifier')
address = opts.get('BACpypes', 'address')
if _debug: _log.debug("initialization")
if _debug: _log.debug(" - args: %r", args)
### create a local device
this_device = LocalDeviceObject(
objectName=objectName,
objectIdentifier=int(objectIdentifier),
maxApduLengthAccepted=int(maxApduLengthAccepted),
segmentationSupported=segmentationSupported,
vendorIdentifier=int(vendorIdentifier),
)
# pss = ServicesSupported()
# pss['whoIs'] = 1
# pss['iAm'] = 1
# pss['readProperty'] = 1
# pss['writeProperty'] = 1
# pss['readPropertyMultiple'] = 1
# pss['writePropertyMultiple'] = 1
# this_device.protocolServicesSupported = pss.value
this_app = Applications(this_device, str(address))
def Request_whois(args):
args = args.split()
# build a request
request = WhoIsRequest()
if (len(args) == 1) or (len(args) == 3):
request.pduDestination = Address(args[0])
del args[0]
else:
request.pduDestination = GlobalBroadcast()
if len(args) == 2:
request.deviceInstanceRangeLowLimit = int(args[0])
request.deviceInstanceRangeHighLimit = int(args[1])
### return the request
return request
def Request_read(args):
if _debug: print("build read reqeust: ", args)
addr, obj_type, obj_inst, prop_id = args[:4]
if obj_type.isdigit():
obj_type = int(obj_type)
elif not get_object_class(obj_type):
raise ValueError("unknown object type")
obj_inst = int(obj_inst)
datatype = get_datatype(obj_type, prop_id)
if not datatype:
raise ValueError("invalid property for object type")
# build a request
request = ReadPropertyRequest(
objectIdentifier=(obj_type, obj_inst),
propertyIdentifier=prop_id,
)
request.pduDestination = Address(addr)
if len(args) == 5:
request.propertyArrayIndex = int(args[4])
if _debug: print(" - request: %r" % request)
### return the request
return request
def request_readMulti(args):
if _debug: print ("build readMulti reqeust: ", args)
i = 0
addr = args[i]
i += 1
read_access_spec_list = []
while i < len(args):
obj_type = args[i]
i += 1
if obj_type.isdigit():
obj_type = int(obj_type)
elif not get_object_class(obj_type):
raise ValueError("unknown object type")
obj_inst = int(args[i])
i += 1
prop_reference_list = []
while i < len(args):
### bug here, if the name of next object is a kind of property,
### then there will be a bug, for example the object notificationClass
prop_id = args[i]
if prop_id not in PropertyIdentifier.enumerations:
break
i += 1
if prop_id in ('all', 'required', 'optional'):
pass
else:
datatype = get_datatype(obj_type, prop_id)
if not datatype:
raise ValueError("invalid property for object type")
# build a property reference
prop_reference = PropertyReference(
propertyIdentifier=prop_id,
)
# check for an array index
if (i < len(args)) and args[i].isdigit():
prop_reference.propertyArrayIndex = int(args[i])
i += 1
# add it to the list
prop_reference_list.append(prop_reference)
# check for at least one property
if not prop_reference_list:
raise ValueError("provide at least one property")
# build a read access specification
read_access_spec = ReadAccessSpecification(
objectIdentifier=(obj_type, obj_inst),
listOfPropertyReferences=prop_reference_list,
)
# add it to the list
read_access_spec_list.append(read_access_spec)
# check for at least one
if not read_access_spec_list:
raise RuntimeError("at least one read access specification required")
# build the request
request = ReadPropertyMultipleRequest(
listOfReadAccessSpecs=read_access_spec_list,
)
request.pduDestination = Address(addr)
if _debug: print(" - request: %r" % request)
### return the request
return request
def request_write(args):
if _debug: ("build write request: %r" % args)
addr, obj_type, obj_inst, prop_id = args[:4]
if obj_type.isdigit():
obj_type = int(obj_type)
obj_inst = int(obj_inst)
value = args[4]
indx = None
if len(args) >= 6:
if args[5] != "-":
indx = int(args[5])
if _debug: print(" - indx: %r" % indx)
priority = None
if len(args) >= 7:
priority = int(args[6])
if _debug: print(" - priority: %r" % priority)
# get the datatype
datatype = get_datatype(obj_type, prop_id)
if _debug: print(" - datatype: %r" % datatype)
# change atomic values into something encodeable, null is a special case
if (value == 'null'):
value = Null()
elif issubclass(datatype, Atomic):
if datatype is Integer:
value = int(value)
elif datatype is Real:
value = float(value)
elif datatype is Unsigned:
value = int(value)
value = datatype(value)
elif issubclass(datatype, Array) and (indx is not None):
if indx == 0:
value = Integer(value)
elif issubclass(datatype.subtype, Atomic):
value = datatype.subtype(value)
elif not isinstance(value, datatype.subtype):
raise TypeError("invalid result datatype, expecting %s" % (datatype.subtype.__name__,))
elif not isinstance(value, datatype):
raise TypeError("invalid result datatype, expecting %s" % (datatype.__name__,))
if _debug: print(" - encodeable value: %r %s" % value, type(value))
# build a request
request = WritePropertyRequest(
objectIdentifier=(obj_type, obj_inst),
propertyIdentifier=prop_id
)
request.pduDestination = Address(addr)
# save the value
request.propertyValue = Any()
try:
request.propertyValue.cast_in(value)
except Exception as error:
print("WriteProperty cast error: %r" % error)
# optional array index
if indx is not None:
request.propertyArrayIndex = indx
# optional priority
if priority is not None:
request.priority = priority
if _debug: print (" - request: %r" % request)
### return the request
return request
def request_writeMulti(args):
if _debug: print ("build write request: %r" % args)
i = 0
addr = args[i]
i += 1
write_access_spec_list = []
while i < len(args):
obj_type = args[i]
i += 1
if obj_type.isdigit():
obj_type = int(obj_type)
obj_inst = args[i]
i += 1
obj_inst = int(obj_inst)
prop_value_list = []
while i < len(args):
prop_id = args[i]
if prop_id not in PropertyIdentifier.enumerations:
break
i += 1
if prop_id in ('all', 'required', 'optional'):
pass
else:
datatype = get_datatype(obj_type, prop_id)
if not datatype:
raise ValueError("invalid property for object type")
value = args[i]
i += 1
# check for an array index
indx = None
if (i < len(args)) and args[i].isdigit():
indx = int(args[i])
i += 1
elif args[i] == '-':
i += 1
# check for an priority
priority = None
if (i < len(args)) and args[i].isdigit():
priority = int(args[i])
i += 1
# change atomic values into something encodeable, null is a special case
if (value == 'null'):
value = Null()
elif issubclass(datatype, Atomic):
if datatype is Integer:
value = int(value)
elif datatype is Real:
value = float(value)
elif datatype is Unsigned:
value = int(value)
value = datatype(value)
elif issubclass(datatype, Array) and (indx is not None):
if indx == 0:
value = Integer(value)
elif issubclass(datatype.subtype, Atomic):
value = datatype.subtype(value)
elif not isinstance(value, datatype.subtype):
raise TypeError("invalid result datatype, expecting %s" % (datatype.subtype.__name__,))
elif not isinstance(value, datatype):
raise TypeError("invalid result datatype, expecting %s" % (datatype.__name__,))
if _debug: print(" - encodeable value: %r %s" % value, type(value))
# build a property value
prop_value = PropertyValue(
propertyIdentifier=prop_id,
)
prop_value.value = Any()
try:
prop_value.value.cast_in(value)
except Exception as error:
print("WriteProperty cast error: %r" % error)
# optional array index
if indx is not None:
prop_value.propertyArrayIndex = indx
# optional priority
if indx is not None:
prop_value.priority = priority
# add it to the list
prop_value_list.append(prop_value)
# check for at least one property
if not prop_value_list:
raise ValueError("provide at least one property")
# build a read access specification
write_access_spec = WriteAccessSpecification(
objectIdentifier=(obj_type, obj_inst),
listOfProperties=prop_value_list,
)
# add it to the list
write_access_spec_list.append(write_access_spec)
# check for at least one
if not write_access_spec_list:
raise RuntimeError("at least one read access specification required")
# build the request
request = WritePropertyMultipleRequest(
listOfWriteAccessSpecs=write_access_spec_list,
)
request.pduDestination = Address(addr)
if _debug: print(" - request: %r" % request)
### return the request
return request
def write_prop(args):
request = request_write(args)
deferred(this_app.request, request)
run()
def write_multi(args):
request = request_writeMulti(args)
### do the service request
deferred(this_app.request, request)
run()
def read_prop(args):
request = Request_read(args)
deferred(this_app.request, request)
run()
return valueRead
def read_multi(args):
request = request_readMulti(args)
deferred(this_app.request, request)
run()
return valueRead
def whois(args, timer=5):
request = Request_whois(args)
deferred(this_app.request, request)
run()
return deviceList