-
Notifications
You must be signed in to change notification settings - Fork 0
/
mqtt2ip.py
executable file
·160 lines (122 loc) · 4.15 KB
/
mqtt2ip.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
#!/usr/bin/env python
"""
This sample application presents itself as a router between a BACnet/MQTT
and BACnet/IP network. Note that the length of the B/MQTT address is set
in the bacpypes_mqtt module. As a router, this does not have an application
layer.
"""
from bacpypes.debugging import bacpypes_debugging, ModuleLogger
from bacpypes.consolelogging import ArgumentParser
from bacpypes.core import run
from bacpypes.comm import bind
from bacpypes.pdu import Address
from bacpypes.netservice import NetworkServiceAccessPoint, NetworkServiceElement
from bacpypes.bvllservice import BIPSimple, AnnexJCodec, UDPMultiplexer
import bacpypes_mqtt
# some debugging
_debug = 0
_log = ModuleLogger(globals())
# globals
args = None
#
# MQTT2IPRouter
#
@bacpypes_debugging
class MQTT2IPRouter:
def __init__(self, lan, mqtt_addr, mqtt_net, ip_addr, ip_net):
if _debug:
MQTT2IPRouter._debug(
"__init__ %r %r %r %r %r", lan, mqtt_addr, mqtt_net, ip_addr, ip_net
)
global args
# a network service access point will be needed
self.nsap = NetworkServiceAccessPoint()
# give the NSAP a generic network layer service element
self.nse = NetworkServiceElement()
bind(self.nse, self.nsap)
# == First stack
# create an MQTT client
self.s1_msap = bacpypes_mqtt.MQTTClient(
lan,
mqtt_addr,
args.host,
port=args.port,
username=args.username,
password=args.password,
keepalive=args.keepalive,
)
# create a service element for the client
self.s1_mse = bacpypes_mqtt.MQTTServiceElement()
bind(self.s1_mse, self.s1_msap)
# bind to the MQTT network
self.nsap.bind(self.s1_msap, mqtt_net)
# == Second stack
# create a generic BIP stack, bound to the Annex J server
# on the UDP multiplexer
self.s2_bip = BIPSimple()
self.s2_annexj = AnnexJCodec()
self.s2_mux = UDPMultiplexer(ip_addr)
# bind the bottom layers
bind(self.s2_bip, self.s2_annexj, self.s2_mux.annexJ)
# bind the BIP stack to the local network
self.nsap.bind(self.s2_bip, ip_net)
#
# __main__
#
def main():
global args
# parse the command line arguments
parser = ArgumentParser(description=__doc__)
# arguments for first network
parser.add_argument("lan", type=str, help="MQTT network name")
parser.add_argument("mqtt_addr", type=str, help="address on the MQTT network")
parser.add_argument("mqtt_net", type=int, help="network number of MQTT network")
# arguments for B/IP network
parser.add_argument("ip_addr", type=str, help="address on the IPv4 network")
parser.add_argument("ip_net", type=int, help="network number of IPv4 network")
# additional options for the MQTT client
parser.add_argument(
"--host",
type=str,
default=bacpypes_mqtt.default_broker_host,
help="broker host address",
)
parser.add_argument(
"--port",
type=int,
default=bacpypes_mqtt.default_broker_port,
help="broker port",
)
parser.add_argument("--username", type=str, default=None, help="broker username")
parser.add_argument("--password", type=str, default=None, help="broker password")
parser.add_argument(
"--keepalive",
type=int,
default=bacpypes_mqtt.default_broker_keepalive,
help="maximum period in seconds allowed between communications with the broker",
)
# now parse the arguments
args = parser.parse_args()
if _debug:
_log.debug("initialization")
if _debug:
_log.debug(" - args: %r", args)
# create the router
router = MQTT2IPRouter(
args.lan,
Address(args.mqtt_addr),
args.mqtt_net,
Address(args.ip_addr),
args.ip_net,
)
if _debug:
_log.debug(" - router: %r", router)
# start up the client
router.s1_mse.startup()
_log.debug("running")
run()
# shutdown the client
router.s1_mse.shutdown()
_log.debug("fini")
if __name__ == "__main__":
main()