-
Notifications
You must be signed in to change notification settings - Fork 130
/
Copy pathReadObjectList.py
executable file
·239 lines (174 loc) · 7.02 KB
/
ReadObjectList.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
#!/usr/bin/env python
"""
This application is given the device instance number of a device and its
address read the object list, then for each object, read the object name.
"""
from collections import deque
from bacpypes.debugging import bacpypes_debugging, ModuleLogger
from bacpypes.consolelogging import ConfigArgumentParser
from bacpypes.core import run, deferred, stop
from bacpypes.iocb import IOCB
from bacpypes.primitivedata import ObjectIdentifier, CharacterString
from bacpypes.constructeddata import ArrayOf
from bacpypes.pdu import Address
from bacpypes.apdu import ReadPropertyRequest, ReadPropertyACK
from bacpypes.app import BIPSimpleApplication
from bacpypes.local.device import LocalDeviceObject
# some debugging
_debug = 0
_log = ModuleLogger(globals())
# globals
this_device = None
this_application = None
# convenience definition
ArrayOfObjectIdentifier = ArrayOf(ObjectIdentifier)
#
# ObjectListContext
#
class ObjectListContext:
def __init__(self, device_id, device_addr):
self.device_id = device_id
self.device_addr = device_addr
self.object_list = []
self.object_names = []
self._object_list_queue = None
def completed(self, had_error=None):
if had_error:
print("had error: %r" % (had_error,))
else:
for objid, objname in zip(self.object_list, self.object_names):
print("%s: %s" % (objid, objname))
stop()
#
# ReadObjectListApplication
#
@bacpypes_debugging
class ReadObjectListApplication(BIPSimpleApplication):
def __init__(self, *args):
if _debug: ReadObjectListApplication._debug("__init__ %r", args)
BIPSimpleApplication.__init__(self, *args)
def read_object_list(self, device_id, device_addr):
if _debug: ReadObjectListApplication._debug("read_object_list %r %r", device_id, device_addr)
# create a context to hold the results
context = ObjectListContext(device_id, device_addr)
# build a request for the object name
request = ReadPropertyRequest(
destination=context.device_addr,
objectIdentifier=context.device_id,
propertyIdentifier='objectList',
)
if _debug: ReadObjectListApplication._debug(" - request: %r", request)
# make an IOCB, reference the context
iocb = IOCB(request)
iocb.context = context
if _debug: ReadObjectListApplication._debug(" - iocb: %r", iocb)
# let us know when its complete
iocb.add_callback(self.object_list_results)
# give it to the application
self.request_io(iocb)
def object_list_results(self, iocb):
if _debug: ReadObjectListApplication._debug("object_list_results %r", iocb)
# extract the context
context = iocb.context
# do something for error/reject/abort
if iocb.ioError:
context.completed(iocb.ioError)
return
# do something for success
apdu = iocb.ioResponse
# should be an ack
if not isinstance(apdu, ReadPropertyACK):
if _debug: ReadObjectListApplication._debug(" - not an ack")
context.completed(RuntimeError("read property ack expected"))
return
# pull out the content
object_list = apdu.propertyValue.cast_out(ArrayOfObjectIdentifier)
if _debug: ReadObjectListApplication._debug(" - object_list: %r", object_list)
# store it in the context
context.object_list = object_list
# make a queue of the identifiers to read, start reading them
context._object_list_queue = deque(object_list)
deferred(self.read_next_object, context)
def read_next_object(self, context):
if _debug: ReadObjectListApplication._debug("read_next_object %r", context)
# if there's nothing more to do, we're done
if not context._object_list_queue:
if _debug: ReadObjectListApplication._debug(" - all done")
context.completed()
return
# pop off the next object identifier
object_id = context._object_list_queue.popleft()
if _debug: ReadObjectListApplication._debug(" - object_id: %r", object_id)
# build a request for the object name
request = ReadPropertyRequest(
destination=context.device_addr,
objectIdentifier=object_id,
propertyIdentifier='objectName',
)
if _debug: ReadObjectListApplication._debug(" - request: %r", request)
# make an IOCB, reference the context
iocb = IOCB(request)
iocb.context = context
if _debug: ReadObjectListApplication._debug(" - iocb: %r", iocb)
# let us know when its complete
iocb.add_callback(self.object_name_results)
# give it to the application
self.request_io(iocb)
def object_name_results(self, iocb):
if _debug: ReadObjectListApplication._debug("object_name_results %r", iocb)
# extract the context
context = iocb.context
# do something for error/reject/abort
if iocb.ioError:
context.completed(iocb.ioError)
return
# do something for success
apdu = iocb.ioResponse
# should be an ack
if not isinstance(apdu, ReadPropertyACK):
if _debug: ReadObjectListApplication._debug(" - not an ack")
context.completed(RuntimeError("read property ack expected"))
return
# pull out the name
object_name = apdu.propertyValue.cast_out(CharacterString)
if _debug: ReadObjectListApplication._debug(" - object_name: %r", object_name)
# store it in the context
context.object_names.append(object_name)
# read the next one
deferred(self.read_next_object, context)
#
# __main__
#
def main():
global this_device
global this_application
# parse the command line arguments
parser = ConfigArgumentParser(description=__doc__)
# add an argument for the device identifier
parser.add_argument('device_id', type=int,
help='device identifier',
)
# add an argument for the address of the device
parser.add_argument('device_addr', type=str,
help='device address',
)
# parse the args
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 simple application
this_application = ReadObjectListApplication(this_device, args.ini.address)
# build a device object identifier
device_id = ('device', args.device_id)
# translate the address
device_addr = Address(args.device_addr)
# kick off the process after the core is up and running
deferred(this_application.read_object_list, device_id, device_addr)
_log.debug("running")
run()
_log.debug("fini")
if __name__ == "__main__":
main()