-
Notifications
You must be signed in to change notification settings - Fork 130
/
Copy pathReadWriteBBMD.py
executable file
·138 lines (105 loc) · 3.7 KB
/
ReadWriteBBMD.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
#!/usr/bin/env python
"""
This application presents a 'console' prompt to the user asking for read commands
to read the BDT and FDT from a BBMD.
"""
from bacpypes.debugging import bacpypes_debugging, ModuleLogger
from bacpypes.consolecmd import ConsoleCmd
from bacpypes.consolelogging import ArgumentParser
from bacpypes.comm import bind, Client
from bacpypes.core import run, enable_sleeping
from bacpypes.pdu import Address
from bacpypes.bvll import (
ReadBroadcastDistributionTable,
ReadBroadcastDistributionTableAck,
ReadForeignDeviceTable,
ReadForeignDeviceTableAck,
WriteBroadcastDistributionTable,
Result,
)
from bacpypes.bvllservice import AnnexJCodec, UDPMultiplexer
# some debugging
_debug = 0
_log = ModuleLogger(globals())
# globals
this_application = None
@bacpypes_debugging
class ReadWriteBBMDConsoleClient(ConsoleCmd, Client):
def do_readbdt(self, args):
"""readbdt <addr>"""
args = args.split()
if _debug:
ReadWriteBBMDConsoleClient._debug("do_readbdt %r", args)
# build a request and send it downstream
read_bdt = ReadBroadcastDistributionTable(destination=Address(args[0]))
if _debug:
ReadWriteBBMDConsoleClient._debug(" - read_bdt: %r", read_bdt)
self.request(read_bdt)
def do_readfdt(self, args):
"""readfdt <addr>"""
args = args.split()
if _debug:
ReadWriteBBMDConsoleClient._debug("do_readfdt %r", args)
# build a request and send it downstream
read_fdt = ReadForeignDeviceTable(destination=Address(args[0]))
if _debug:
ReadWriteBBMDConsoleClient._debug(" - read_fdt: %r", read_fdt)
self.request(read_fdt)
def do_writebdt(self, args):
"""writebdt <addr> <entry> ..."""
args = args.split()
if _debug:
ReadWriteBBMDConsoleClient._debug("do_writebdt %r", args)
# build a list of broadcast distribution table entries which just so
# happen to be BACpypes IPv4 addresses
bdt = []
for addr in args[1:]:
bdte = Address(addr)
bdt.append(bdte)
# build a request and send it downstream
write_bdt = WriteBroadcastDistributionTable(
destination=Address(args[0]), bdt=bdt
)
if _debug:
ReadWriteBBMDConsoleClient._debug(" - write_bdt: %r", write_bdt)
self.request(write_bdt)
def confirmation(self, pdu):
"""Filter for the acks and errors."""
if _debug:
ReadWriteBBMDConsoleClient._debug("confirmation %r", pdu)
if isinstance(
pdu, (ReadBroadcastDistributionTableAck, ReadForeignDeviceTableAck, Result)
):
pdu.debug_contents()
def main():
global this_application
# parse the command line arguments
parser = ArgumentParser(description=__doc__)
parser.add_argument(
"local_address",
help="IPv4 address",
)
args = parser.parse_args()
if _debug:
_log.debug("initialization")
_log.debug(" - args: %r", args)
local_address = Address(args.local_address)
if _debug:
_log.debug(" - local_address: %r", local_address)
# make a console
this_console = ReadWriteBBMDConsoleClient()
if _debug:
_log.debug(" - this_console: %r", this_console)
# create an Annex J codec, bound to the Annex J server
# of the UDP multiplexer
annexj = AnnexJCodec()
mux = UDPMultiplexer(local_address)
# bind the layers
bind(this_console, annexj, mux.annexJ)
# enable sleeping will help with threads
enable_sleeping()
_log.debug("running")
run()
_log.debug("fini")
if __name__ == "__main__":
main()