-
Notifications
You must be signed in to change notification settings - Fork 98
/
ScriptApplication.py
458 lines (362 loc) · 14.3 KB
/
ScriptApplication.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
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
#!/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.
#
"""
SimpleApplication
=================
A basic BACnet application (bacpypes BIPSimpleApplication) for interacting with
the bacpypes BACnet stack. It enables the base-level BACnet functionality
(a.k.a. device discovery) - meaning it can send & receive WhoIs & IAm messages.
Additional functionality is enabled by inheriting this application, and then
extending it with more functions. [See BAC0.scripts for more examples of this.]
"""
# --- standard Python modules ---
from collections import defaultdict
# --- 3rd party modules ---
from bacpypes.app import ApplicationIOController
from bacpypes.pdu import Address
from bacpypes.service.object import ReadWritePropertyMultipleServices
from bacpypes.service.cov import ChangeOfValueServices
from bacpypes.netservice import NetworkServiceAccessPoint, NetworkServiceElement
from bacpypes.bvllservice import (
BIPSimple,
BIPForeign,
BIPBBMD,
AnnexJCodec,
UDPMultiplexer,
)
from bacpypes.apdu import SubscribeCOVRequest, SimpleAckPDU, RejectPDU, AbortPDU
from bacpypes.appservice import StateMachineAccessPoint, ApplicationServiceAccessPoint
from bacpypes.comm import ApplicationServiceElement, bind, Client
from bacpypes.iocb import IOCB
from bacpypes.core import deferred
# basic services
from bacpypes.service.device import WhoIsIAmServices, WhoHasIHaveServices
from bacpypes.service.object import ReadWritePropertyServices
# --- this application's modules ---
from ..utils.notes import note_and_log
from ..functions.Discover import NetworkServiceElementWithRequests
# ------------------------------------------------------------------------------
class common_mixin:
"""
They take message coming from the network that are not generated from
a request we made.
"""
def do_IAmRequest(self, apdu):
"""Given an I-Am request, cache it."""
self._log.debug("do_IAmRequest {!r}".format(apdu))
# build a key from the source, just use the instance number
key = (str(apdu.pduSource), apdu.iAmDeviceIdentifier[1])
self.i_am_counter[key] += 1
self._last_i_am_received.append(key)
def do_IHaveRequest(self, apdu):
"""Given an I-Have request, cache it."""
self._log.debug("do_IHaveRequest {!r}".format(apdu))
# build a key from the source, using object name
key = (str(apdu.pduSource), apdu.objectName)
self.i_have_counter[key] += 1
self._last_i_have_received.append(key)
def do_WhoIsRequest(self, apdu):
"""Respond to a Who-Is request."""
# build a key from the source and parameters
key = (
str(apdu.pduSource),
apdu.deviceInstanceRangeLowLimit,
apdu.deviceInstanceRangeHighLimit,
)
self._log.debug(
"do_WhoIsRequest from {} | {} to {}".format(key[0], key[1], key[2])
)
# count the times this has been received
self.who_is_counter[key] += 1
low_limit = key[1]
high_limit = key[2]
# count the times this has been received
self.who_is_counter[key] += 1
if low_limit is not None and self.localDevice.objectIdentifier[1] < low_limit:
return
if high_limit is not None and self.localDevice.objectIdentifier[1] > high_limit:
return
# generate an I-Am
self._log.debug("Responding to Who is by a Iam")
self.iam_req.pduDestination = apdu.pduSource
iocb = IOCB(self.iam_req) # make an IOCB
deferred(self.request_io, iocb)
def do_ConfirmedCOVNotificationRequest(self, apdu):
# look up the process identifier
context = self.subscription_contexts.get(apdu.subscriberProcessIdentifier, None)
if not context or apdu.pduSource != context.address:
self._log.warning(
"Unsollicited COV Notification received from {} ({}). Have you restarted the application recently ?".format(
apdu.pduSource, apdu
)
)
# this is turned into cancel_cov request and sent back to the client
else:
# now tell the context object
elements = context.cov_notification(apdu)
# success
response = SimpleAckPDU(context=apdu)
# send a confirmation
self.response(response)
self._log.debug("Confirmed COV Notification: {}".format(elements))
self.subscription_contexts["context_callback"](elements)
# execute callback
if context.callback is not None:
context.callback(elements=elements)
def do_UnconfirmedCOVNotificationRequest(self, apdu):
# look up the process identifier
context = self.subscription_contexts.get(apdu.subscriberProcessIdentifier, None)
if not context or apdu.pduSource != context.address:
return
# now tell the context object
elements = context.cov_notification(apdu)
self._log.debug("Unconfirmed COV Notification: {}".format(elements))
self.subscription_contexts["context_callback"](elements)
# execute callback
if context.callback is not None:
context.callback(elements=elements)
@note_and_log
class BAC0Application(
common_mixin,
ApplicationIOController,
WhoIsIAmServices,
WhoHasIHaveServices,
ReadWritePropertyServices,
ReadWritePropertyMultipleServices,
ChangeOfValueServices,
):
"""
Defines a basic BACnet/IP application to process BACnet requests.
:param *args: local object device, local IP address
See BAC0.scripts.BasicScript for more details.
"""
def __init__(
self,
localDevice,
localAddress,
bbmdAddress=None,
bbmdTTL=0,
deviceInfoCache=None,
aseID=None,
iam_req=None,
subscription_contexts=None,
):
ApplicationIOController.__init__(
self, localDevice, deviceInfoCache, aseID=aseID
)
self.iam_req = iam_req
# local address might be useful for subclasses
if isinstance(localAddress, Address):
self.localAddress = localAddress
else:
self.localAddress = Address(localAddress)
# include a application decoder
self.asap = ApplicationServiceAccessPoint()
# pass the device object to the state machine access point so it
# can know if it should support segmentation
self.smap = StateMachineAccessPoint(localDevice)
# the segmentation state machines need access to the same device
# information cache as the application
self.smap.deviceInfoCache = self.deviceInfoCache
# a network service access point will be needed
self.nsap = NetworkServiceAccessPoint()
# give the NSAP a generic network layer service element
self.nse = NetworkServiceElementWithRequests()
bind(self.nse, self.nsap)
# bind the top layers
bind(self, self.asap, self.smap, self.nsap)
# create a generic BIP stack, bound to the Annex J server
# on the UDP multiplexer
self.bip = BIPSimple()
self.annexj = AnnexJCodec()
self.mux = UDPMultiplexer(self.localAddress)
# bind the bottom layers
bind(self.bip, self.annexj, self.mux.annexJ)
# bind the BIP stack to the network, no network number
self.nsap.bind(self.bip, address=self.localAddress)
self.i_am_counter = defaultdict(int)
self.i_have_counter = defaultdict(int)
self.who_is_counter = defaultdict(int)
# keep track of requests to line up responses
self._request = None
self._last_i_am_received = []
self._last_i_have_received = []
# to support CoV
self.subscription_contexts = subscription_contexts
def close_socket(self):
# pass to the multiplexer, then down to the sockets
self.mux.close_socket()
def request(self, apdu):
# save a copy of the request
self._request = apdu
# forward it along
super(BAC0Application, self).request(apdu)
@note_and_log
class BAC0ForeignDeviceApplication(
common_mixin,
ApplicationIOController,
WhoIsIAmServices,
WhoHasIHaveServices,
ReadWritePropertyServices,
ReadWritePropertyMultipleServices,
ChangeOfValueServices,
):
"""
Defines a basic BACnet/IP application to process BACnet requests.
:param *args: local object device, local IP address
See BAC0.scripts.BasicScript for more details.
"""
def __init__(
self,
localDevice,
localAddress,
bbmdAddress=None,
bbmdTTL=0,
deviceInfoCache=None,
aseID=None,
iam_req=None,
subscription_contexts=None,
):
ApplicationIOController.__init__(
self, localDevice, deviceInfoCache, aseID=aseID
)
self.iam_req = iam_req
# local address might be useful for subclasses
if isinstance(localAddress, Address):
self.localAddress = localAddress
else:
self.localAddress = Address(localAddress)
# include a application decoder
self.asap = ApplicationServiceAccessPoint()
# pass the device object to the state machine access point so it
# can know if it should support segmentation
self.smap = StateMachineAccessPoint(localDevice)
# the segmentation state machines need access to the same device
# information cache as the application
self.smap.deviceInfoCache = self.deviceInfoCache
# a network service access point will be needed
self.nsap = NetworkServiceAccessPoint()
# give the NSAP a generic network layer service element
self.nse = NetworkServiceElementWithRequests()
bind(self.nse, self.nsap)
# bind the top layers
bind(self, self.asap, self.smap, self.nsap)
# create a generic BIP stack, bound to the Annex J server
# on the UDP multiplexer
self.bip = BIPForeign(bbmdAddress, bbmdTTL)
self.annexj = AnnexJCodec()
self.mux = UDPMultiplexer(self.localAddress, noBroadcast=True)
# bind the bottom layers
bind(self.bip, self.annexj, self.mux.annexJ)
# bind the NSAP to the stack, no network number
self.nsap.bind(self.bip)
self.i_am_counter = defaultdict(int)
self.i_have_counter = defaultdict(int)
self.who_is_counter = defaultdict(int)
# keep track of requests to line up responses
self._request = None
self._last_i_am_received = []
self._last_i_have_received = []
# to support CoV
self.subscription_contexts = subscription_contexts
def close_socket(self):
# pass to the multiplexer, then down to the sockets
self.mux.close_socket()
class NullClient(Client):
def __init__(self, cid=None):
Client.__init__(self, cid=cid)
def confirmation(self, *args, **kwargs):
pass
@note_and_log
class BAC0BBMDDeviceApplication(
common_mixin,
ApplicationIOController,
WhoIsIAmServices,
WhoHasIHaveServices,
ReadWritePropertyServices,
ReadWritePropertyMultipleServices,
ChangeOfValueServices,
):
"""
Defines a basic BACnet/IP application to process BACnet requests.
:param *args: local object device, local IP address
See BAC0.scripts.BasicScript for more details.
"""
bdt = []
def __init__(
self,
localDevice,
localAddress,
bdtable=[],
deviceInfoCache=None,
aseID=None,
iam_req=None,
subscription_contexts=None,
):
self.bdtable = bdtable
null_client = NullClient()
ApplicationIOController.__init__(
self, localDevice, deviceInfoCache, aseID=aseID
)
self.iam_req = iam_req
# local address might be useful for subclasses
if isinstance(localAddress, Address):
self.localAddress = localAddress
else:
self.localAddress = Address(localAddress)
# include a application decoder
self.asap = ApplicationServiceAccessPoint()
# pass the device object to the state machine access point so it
# can know if it should support segmentation
self.smap = StateMachineAccessPoint(localDevice)
# the segmentation state machines need access to the same device
# information cache as the application
self.smap.deviceInfoCache = self.deviceInfoCache
# a network service access point will be needed
self.nsap = NetworkServiceAccessPoint()
# give the NSAP a generic network layer service element
self.nse = NetworkServiceElementWithRequests()
bind(self.nse, self.nsap)
# bind the top layers
bind(self, self.asap, self.smap, self.nsap)
# create a generic BIP stack, bound to the Annex J server
# on the UDP multiplexer
self.bip = BIPBBMD(self.localAddress)
self.annexj = AnnexJCodec()
self.mux = UDPMultiplexer(self.localAddress, noBroadcast=False)
# bind the bottom layers
# bind(self.bip, self.annexj, self.mux.annexJ)
bind(null_client, self.bip, self.annexj, self.mux.annexJ)
if self.bdtable:
for bdtentry in self.bdtable:
self.add_peer(bdtentry)
# bind the NSAP to the stack, no network number
self.nsap.bind(self.bip)
self.i_am_counter = defaultdict(int)
self.i_have_counter = defaultdict(int)
self.who_is_counter = defaultdict(int)
# keep track of requests to line up responses
self._request = None
self._last_i_am_received = []
self._last_i_have_received = []
# to support CoV
self.subscription_contexts = subscription_contexts
def add_peer(self, address):
try:
bdt_address = Address(address)
self.bip.add_peer(bdt_address)
except Exception:
raise
def remove_peer(self, address):
try:
bdt_address = Address(address)
self.bip.remove_peer(bdt_address)
except Exception:
raise
def close_socket(self):
# pass to the multiplexer, then down to the sockets
self.mux.close_socket()