-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathagent.py
327 lines (258 loc) · 9.43 KB
/
agent.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
#!/usr/bin/env python3
""" HIAS Bluetooth IoT Agent Class
HIAS Bluetooth IoT Agents are bridges between HIAS devices that support
Bluetooth/Bluetooth Low Energy protocol and the HIASCDI Context Broker.
MIT License
Copyright (c) 2021 Asociación de Investigacion en Inteligencia Artificial
Para la Leucemia Peter Moss
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files(the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Contributors:
- Adam Milton-Barker
"""
from gevent import monkey
monkey.patch_all()
import json
import os
import psutil
import signal
import sys
import time
import threading
from abc import ABC, abstractmethod
from bluepy import btle
from datetime import datetime, timedelta
from flask import Flask, request, Response
from threading import Thread
from modules.AbstractAgent import AbstractAgent
class Agent(AbstractAgent):
""" HIAS Bluetooth IoT Agent Class
HIAS Bluetooth IoT Agents are bridges between HIAS
devices that support Bluetooth/Bluetooth Low Energy
protocol and the HIASCDI Context Broker.
"""
def get_ble_devices(self):
""" Returns a list of HIAS BLE devices """
bles = self.hiascdi.get_ble_devices()
for ble in bles:
self.bles.append((
ble["bluetoothAddress"]["value"],
ble["bluetoothServiceUUID"]["value"],
ble["bluetoothCharacteristicUUID"]["value"],
ble["networkLocation"]["value"],
ble["networkZone"]["value"],
ble["id"]))
def check_ble_devices(self):
""" Checks for disconnected HIAS BLE devices """
for device in self.ble_tracker:
if self.ble_tracker[device]["last_seen"] != "" and self.ble_tracker[device]["last_seen"] < datetime.now() - timedelta(minutes=5):
self.mqtt.device_status_publish(self.ble_tracker[device]["location"],
self.ble_tracker[device]["zone"],
self.ble_tracker[device]["device"],
"OFFLINE")
self.ble_tracker[device]["last_seen"] = ""
self.helpers.logger.info(
"BLE device " + self.ble_tracker[device]["address"] + " disconnected from iotJumpWay")
threading.Timer(100.0, self.check_ble_devices).start()
def ble_connection(self, addr, service, characteristic):
""" Connects to a HIAS BLE device """
while True:
try:
self.helpers.logger.info(
"Attempting BLE connection to "+addr)
peripheral = btle.Peripheral(addr)
peripheral.setMTU(512)
delegate = BtAgentDelegate()
peripheral.withDelegate(delegate)
serv = peripheral.getServiceByUUID(service)
charac = serv.getCharacteristics(characteristic)[0]
peripheral.writeCharacteristic(charac.valHandle + 1, b"\x01\x00")
self.helpers.logger.info(
"BLE connection to " + addr + " established")
if addr in self.ble_tracker:
self.ble_tracker[addr]["last_seen"] = datetime.now()
self.helpers.logger.info(
addr + " connection timestamp updated")
self.notification_loop(peripheral)
except Exception as e:
self.helpers.logger.info(
"BLE connection to " + addr + " failed")
time.sleep(1.0)
continue
def notification_loop(self, peripheral):
""" Notification loop """
try:
if peripheral.waitForNotifications(2.0):
self.helpers.logger.info(
"Awaiting notifications...")
except Exception as e:
pass
finally:
self.helpers.logger.info(
"Disconnecting from HIAS BLE device")
try:
peripheral.disconnect()
time.sleep(4)
except Exception as e:
self.helpers.logger.info(
"Failed to disconnect from HIAS BLE device")
pass
def parse_data(self, data):
""" Parses the data dictionary """
entity_type = data["EntityType"]
entity = data["Entity"]
data_type = data["Type"]
data_value = data["Value"]
data_message = data["Message"]
return entity_type, entity, data_type, data_value, data_message
def respond(self, response_code, response):
""" Returns the request repsonse """
return Response(response=json.dumps(response, indent=4),
status=response_code,
mimetype="application/json")
def signal_handler(self, signal, frame):
self.helpers.logger.info("Disconnecting")
self.mqtt.disconnect()
sys.exit(1)
app = Flask(__name__)
agent = Agent()
class BtAgentDelegate(btle.DefaultDelegate):
def __init__(self):
btle.DefaultDelegate.__init__(self)
def handleNotification(self, cHandle, data):
data = json.loads(data.decode("utf-8"))
if "EntityType" not in data:
return
if "Entity" not in data:
return
(entity_type, entity, data_type,
data_value, data_message) = agent.parse_data(data)
agent.helpers.logger.info(
"Received " + entity_type + " data via BLE: " + str(data))
attrs = agent.get_attributes(entity_type, entity)
bch = attrs["blockchain"]
if not agent.hiasbch.iotjumpway_check(bch):
return
_id = attrs["id"]
location = attrs["location"]
zone = attrs["zone"] if "zone" in attrs else "NA"
sensor = data["Sensor"] if "Sensor" in data else "NA"
actuator = data["Actuator"] if "Actuator" in data else "NA"
if sensor != "NA":
agent.helpers.logger.info("Processing sensors")
sensors = agent.hiascdi.get_sensors(
entity, entity_type)
sensorData = sensors["sensors"]
i = 0
for sensor in sensorData["value"]:
for prop in sensor["properties"]["value"]:
if data["Type"].lower() in prop:
sensorData["value"][i]["properties"]["value"][data["Type"].lower()] = {
"value": data["Value"],
"timestamp": {
"value": datetime.now().isoformat()
}
}
i = i + 1
updateResponse = agent.hiascdi.update_entity(
entity, entity_type, {
"networkStatus": {"value": "ONLINE"},
"networkStatus.metadata": {"timestamp": {
"value": datetime.now().isoformat()
}},
"dateModified": {"value": datetime.now().isoformat()},
"sensors": sensorData
})
if updateResponse:
_id = agent.hiashdi.insert_data("Sensors", {
"Use": entity_type,
"Location": location,
"Zone": zone,
"Device": entity if entity_type == "Device" else "NA",
"HIASCDI": entity if entity_type == "HIASCDI" else "NA",
"Agent": entity if entity_type == "Agent" else "NA",
"Application": entity if entity_type == "Application" else "NA",
"Device": entity if entity_type == "Device" else "NA",
"Staff": entity if entity_type == "Staff" else "NA",
"Sensor": data["Sensor"],
"Type": data["Type"],
"Value": data["Value"],
"Message": data["Message"],
"Time": datetime.now().strftime('%Y-%m-%d %H:%M:%S')
})
if _id != False:
agent.helpers.logger.info(
entity_type + " " + entity + " sensors update OK")
agent.mqtt.publish("Integrity", {
"_id": str(_id),
"Sensor": data["Sensor"],
"Type": data["Type"],
"Value": data["Value"],
"Message": data["Message"]
})
else:
agent.helpers.logger.error(
entity_type + " " + entity + " sensors update KO")
else:
agent.helpers.logger.error(
entity_type + " " + entity + " sensors update KO")
@app.route('/About', methods=['GET'])
def about():
"""
Returns Agent details
Responds to GET requests sent to the North Port About API endpoint.
"""
return agent.respond(200, {
"Identifier": agent.credentials["iotJumpWay"]["entity"],
"Host": agent.credentials["server"]["ip"],
"NorthPort": agent.credentials["server"]["port"],
"CPU": psutil.cpu_percent(),
"Memory": psutil.virtual_memory()[2],
"Diskspace": psutil.disk_usage('/').percent,
"Temperature": psutil.sensors_temperatures()['coretemp'][0].current
})
def main():
signal.signal(signal.SIGINT, agent.signal_handler)
signal.signal(signal.SIGTERM, agent.signal_handler)
agent.hiascdi_connection()
agent.get_ble_devices()
agent.hiashdi_connection()
agent.hiasbch_connection()
agent.mqtt_connection({
"host": agent.credentials["iotJumpWay"]["host"],
"port": agent.credentials["iotJumpWay"]["port"],
"location": agent.credentials["iotJumpWay"]["location"],
"zone": agent.credentials["iotJumpWay"]["zone"],
"entity": agent.credentials["iotJumpWay"]["entity"],
"name": agent.credentials["iotJumpWay"]["name"],
"un": agent.credentials["iotJumpWay"]["un"],
"up": agent.credentials["iotJumpWay"]["up"]
})
for ble in agent.bles:
agent.ble_tracker[ble[0]] = {
"location": ble[3],
"zone": ble[4],
"device": ble[5],
"address": ble[0],
"last_seen": ""
}
Thread(target=agent.ble_connection, args=(ble[0], ble[1], ble[2]), daemon=True).start()
Thread(target=agent.life, args=(), daemon=True).start()
Thread(target=agent.check_ble_devices, args=(), daemon=True).start()
app.run(host=agent.helpers.credentials["server"]["ip"],
port=agent.helpers.credentials["server"]["port"])
if __name__ == "__main__":
main()