-
Notifications
You must be signed in to change notification settings - Fork 2
/
b2b
executable file
·343 lines (302 loc) · 12.4 KB
/
b2b
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
#!/usr/bin/env python
from copy import deepcopy
import time
import logging
import sys
from typing import Union
import threading
import json
import argparse
from multiprocessing import Process, Pipe
from pdb import set_trace as bp
from rdflib import Namespace, Graph, Literal, URIRef
from bacpypes.core import run as bacpypes_run
from brickbacnet.discovery import BacnetDiscovery
from brickbacnet.bacnet_wrapper import get_port_from_ini
from brickbacnet.brickserver import BrickServer
from brickbacnet.namespaces import BACNET, BRICK_NS_TEMPLATE, OWL, RDF, RDFS
from brickbacnet.connector import Connector, run_read_daemon
from brickbacnet.common import make_src_id, make_obj_id
from brickbacnet.sqlite_wrapper import SqliteWrapper
from brickbacnet.dummy_ds import DummyDs
def str2ilist(s):
s.replace(' ', '')
return [int(c) for c in s.split(',')]
def str2slist(s):
s.replace(' ', '')
return [str(c) for c in s.split(',')]
class BacnetConn(object):
def __init__(self):
parser = argparse.ArgumentParser(
description="BACnet connector with Brick Server or something similar.",
)
parser.add_argument(
"command", help="The command to run", choices=["discovery", "connector"],
)
args = parser.parse_args(sys.argv[1:2])
getattr(self, 'run_' + args.command)()
def run_discovery(self):
parser = argparse.ArgumentParser(
description="Record changes to the repository. The priority of conflicting configuration aparameters is `b2b_config.json` > `argument parameters` > `default values`.",
)
parser = self._add_shared_args(parser)
parser.add_argument(
"--bacnet-devices-file",
help="The target file to store bacnet devices' metadata.",
default="results/bacnet_devices.csv",
)
parser.add_argument(
"--bacnet-objects-files-template",
help="The target file to store bacnet objects' metadata.",
default="results/bacnet_objects_{0}.csv",
)
parser.add_argument(
"--register-brickserver",
action="store_const",
const=True,
default=False,
help="Regsiter discovered objects in a Brick Server",
)
parser.add_argument(
"--register-plastser",
action="store_const",
const=True,
default=False,
help="Regsiter discovered objects in a Plaster Web Service (TODO)",
)
parser.add_argument(
"--base-namespace",
default='http://example.com#',
help="The name space associated with discovered entities",
type=str,
)
# Set parameters
args = parser.parse_args(sys.argv[2:])
with open(args.b2b_config, 'r') as fp:
config = json.load(fp)
self.BRICK = Namespace(BRICK_NS_TEMPLATE.format(version=config['brick_version']))
self.Point = self.BRICK.Point
self.BACnet_Device = BACNET.BACnet_Device
self.property_filters = config["property_filters"]
self.sqlite_db = SqliteWrapper(config['sqlite_db'])
# Set up threads for BACpypes
bacnet_discovery = BacnetDiscovery(args.bacpypes_ini, config, self.sqlite_db)
time.sleep(1)
run_thread = threading.Thread(target=bacpypes_run)
run_thread.daemon = True
run_thread.start()
# Discover BACnet devices
devices = bacnet_discovery.discover_devices()
# TODO: Dump the table.
# Discover BACnet objects for identified devices
target_device_ids = args.target_devices
if target_device_ids:
target_devices = {dev_id: devices[int(dev_id)] for dev_id in target_device_ids}
else:
target_devices = devices
device_objs = bacnet_discovery.discover_objects(target_devices)
if args.register_brickserver:
self.ds_if = BrickServer(config['brickserver']['hostname'],
config['brickserver']['jwt_token'],
config['brick_version'],
)
else:
self.ds_if = DummyDs() #TODO
# Serialize the discovered resources into an RDF graph
g = self.make_brick_graph(target_devices, device_objs)
# Register entities to get theri uuids or just get their uuids
self.update_uuids(g)
# Register the identifeid devies and objects to Brick Server
if args.register_brickserver:
self.ds_if.register_graph(g)
self.sqlite_db.export_devices(args.bacnet_devices_file)
for device_id, objs in device_objs.items():
self.sqlite_db.export_objects(device_id, args.bacnet_objects_files_template.format(device_id))
def update_uuids(self, g):
# map devices
qstr = """
select ?entity ?dev_id where {
?entity a bacnet:BACnet_Device.
?dev bacnet:device_id ?dev_id.
}
"""
for [entity_id, dev_id] in g.query(qstr):
self.sqlite_db.update_dev_property(
dev_id=dev_id,
prop='uuid',
val=entity_id,
)
# map objects
qstr = """
select ?entity ?obj_type ?obj_instance ?dev_id where {
?entity a brick:Point.
?entity bacnet:object_type ?obj_type.
?entity bacnet:instance ?obj_instance.
?dev brick:hasPoint ?entity.
?dev bacnet:device_id ?dev_id.
}
"""
res = g.query(qstr)
for [entity_id, obj_type, obj_instance, dev_id] in res:
self.sqlite_db.update_obj_property(
dev_id=dev_id,
obj_instance=obj_instance,
prop='uuid',
val=entity_id,
)
def create_uuid_maps(self, g):
# map devices
qstr = """
select ?entity ?dev_id where {
?entity a bacnet:BACnet_Device.
?dev bacnet:device_identifier ?dev_id.
}
"""
uuid_dev_id_map = {}
for [entity_id, dev_id] in g.query(qstr):
uuid_dev_id_map[entity_id] = dev_id
# map objects
qstr = """
select ?entity ?obj_type ?obj_instance ?dev_id where {
?entity a brick:Point.
?entity bacnet:object_type ?obj_type.
?entity bacnet:instance ?obj_instance.
?dev brick:hasPoint ?entity.
?dev bacnet:device_identifier ?dev_id.
}
"""
res = g.query(qstr)
uuid_src_id_map = {}
for [entity_id, obj_type, obj_instance, dev_id] in res:
obj_id = make_obj_id(obj_type, obj_instance)
src_id = make_src_id(dev_id, obj_id)
uuid_src_id_map[entity_id] = src_id
return uuid_dev_id_map, uuid_src_id_map
def _get_default_graph(self):
g = Graph()
g.bind('brick', self.BRICK)
g.bind('bacnet', BACNET)
return g
def get_obj_uri(self, dev_id: str, instance: int, entity_type: Union[str, URIRef]):
# Check if the src_id exists in the local db
entity_id = self.sqlite_db.find_obj_uuid(dev_id, instance)
if not entity_id:
props = {
'instance': instance,
'device_ref': dev_id,
}
entity_ids = self.ds_if.query_entities(props)
if entity_ids:
if len(entity_ids) > 1:
print(f'There are more than 1 entities found at Brick Server for Device {instance}')
entity_id = entity_ids[0]
# Check if the src_id exists in Brick Server if register_brickserver is enabled.
if entity_id:
uri = URIRef(entity_id)
else:
uri = URIRef(self.ds_if.create_entity(str(entity_type)))
return uri
def get_dev_uri(self, instance: int, entity_type: Union[str, URIRef]):
# Check if the src_id exists in the local db
entity_id = self.sqlite_db.find_dev_uuid(instance)
if not entity_id:
props = {
'device_id': instance,
}
entity_ids = self.ds_if.query_entities(props)
if entity_ids:
if len(entity_ids) > 1:
print(f'There are more than 1 entities found at Brick Server for Device {instance}')
entity_id = entity_ids[0]
# Check if the src_id exists in Brick Server if register_brickserver is enabled.
if entity_id:
uri = URIRef(entity_id)
else:
uri = URIRef(self.ds_if.create_entity(str(entity_type)))
return uri
def make_brick_graph(self, devices, device_objs, target_file='results/b2b.ttl'):
g = self._get_default_graph()
for dev_props in devices.values():
dev_id = dev_props['device_id']
dev_uri = self.get_dev_uri(dev_id, BACNET.BACnet_Device)
g.add((dev_uri, RDF.type, BACNET.BACnet_Device))
for prop, val in dev_props.items():
if prop in self.property_filters:
continue
g.add((dev_uri, BACNET[prop], Literal(val)))
for obj_props in device_objs[dev_id].values():
obj_uri = self.get_obj_uri(dev_id, obj_props['instance'], 'Point')
g.add((obj_uri, RDF.type, self.BRICK.Point))
g.add((dev_uri, self.BRICK.hasPoint, obj_uri))
g.add((obj_uri, self.BRICK.isPointOf, dev_uri))
for prop, val in obj_props.items():
if prop in self.property_filters:
continue
g.add((obj_uri, BACNET[prop], Literal(val)))
g.serialize(target_file, format='turtle')
return g
def _add_shared_args(self, parser):
parser.register('type','ilist', str2ilist)
parser.register('type','slist', str2slist)
parser.add_argument(
"--target-devices",
default=None,
help="Indicate the target devices for which you'd like to get objects. By default, brickbacnet queries all the discoverable devices. Comma-separated integers are accepted (e.g., --target-devices 505,506",
type='ilist',
)
parser.add_argument(
"--b2b-config",
help="The location of brickbacnet config file in JSON.",
default="configs/b2b_config.json"
)
parser.add_argument(
"--bacpypes-ini",
help="The location of BACpypes.ini",
default="configs/BACpypes.ini",
)
return parser
def run_connector(self):
parser = argparse.ArgumentParser(
description="Run a Connector for BACnet and Brick Server",
)
parser = self._add_shared_args(parser)
parser.add_argument(
"--run-actuation-server",
action="store_const",
const=True,
default=False,
help="Regsiter discovered objects in a Brick Server",
)
args = parser.parse_args(sys.argv[2:])
config = json.load(open(args.b2b_config))
self.sqlite_db = SqliteWrapper(config['sqlite_db'])
if args.target_devices:
config['bacnet_device_ids'] = args.target_devices
else: # If not specified, read all devices.
config['bacnet_device_ids'] = self.sqlite_db.get_device_ids()
ds_if = BrickServer(config['brickserver']['hostname'],
config['brickserver']['jwt_token'],
config['brick_version']
)
bacpypes_ini = config['bacpypes_ini']
if args.run_actuation_server:
raise NotImplementedError()
base_port = get_port_from_ini(bacpypes_ini)
procs = []
for i, dev_id in enumerate(config['bacnet_device_ids']):
to_child, to_self = Pipe()
connector_params = deepcopy(config['connector'])
connector_params.update({
'bacpypes_ini': bacpypes_ini,
'ds_if': ds_if,
'bacnet_device_ids': [dev_id],
'sqlite_db': config['sqlite_db'],
'overriding_bacnet_port': base_port + i,
})
p = Process(target=run_read_daemon, args=(connector_params, dev_id))
procs.append(p)
run_read_daemon(connector_params, dev_id)
for p in procs:
p.start()
if __name__ == "__main__":
BacnetConn()