-
Notifications
You must be signed in to change notification settings - Fork 0
/
nlmanager.py
126 lines (106 loc) · 4.46 KB
/
nlmanager.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
#########################################################################
# Copyright (c) 2018 panStamp <[email protected]>
#
# This file is part of the panStamp project.
#
# panStamp is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# any later version.
#
# panStamp is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with panStamp; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
# USA
#
# Author: Daniel Berenguer
# Date: Nov 22 2018
#########################################################################
from config.config import NlConfig
from nldbclient import NlDbClient
from nlexception import NlException
import requests
import json
import threading
class NlManager(object):
"""
General FIWARE NGSIv2 managing class
"""
def get_auth_token(self):
"""
Retrieve auth token from server
"""
threading.Timer(NlConfig.TOKEN_INTERVAL, self.get_auth_token).start()
url = NlConfig.AUTH_TOKEN_URL + "/v3/auth/tokens"
payload = "{\"auth\": {\"identity\": {\"methods\": [\"password\"],\"password\": {\"user\": {\"domain\": {\"name\": \""
payload += NlConfig.SERVICE + "\"},\"name\": \"" + NlConfig.USER_NAME + "\",\"password\": \""
payload += NlConfig.USER_PASSWORD + "\"}}},\"scope\": {\"project\": {\"domain\": {\"name\": \""
payload += NlConfig.SERVICE+ "\"},\"name\": \"" + NlConfig.SUBSERVICE + "\"}}}}"
headers = {
'Content-Type': "application/json"
}
try:
response = requests.request("POST", url, data=payload, headers=headers, verify=False)
if "X-Subject-Token" in response.headers:
self.auth_token = response.headers["X-Subject-Token"]
else:
NlException("X-Subject-Token not available in response from server").log()
except requests.RequestException:
NlException("Retrieving auth token. No response from server").log()
except threading.ThreadError:
NlException("Unable to scheldule token update").log()
except:
NlException("Unable to parse token").log()
def get_entities(self):
"""
Retrieve list of entities from Context-broker
"""
threading.Timer(NlConfig.POLLING_INTERVAL, self.get_entities).start()
url = NlConfig.NGSIv2_URL + "/v2/entities"
headers = {
'Fiware-Service': NlConfig.SERVICE,
'Fiware-ServicePath': NlConfig.SUBSERVICE,
'X-Auth-Token': self.auth_token
}
try:
response = requests.request("GET", url, headers=headers, verify=False)
json_data = {"endpoints":{}}
entities = json.loads(response.text)
for sensor in entities:
json_data["id"] = sensor["id"]
json_data["type"] = sensor["type"]
json_data["timestamp"] = sensor["TimeInstant"]["value"]
for key, value in sensor.items():
if key not in ["id", "type", "TimeInstant"]:
val = value["value"]
try:
val = float(value["value"])
except ValueError:
pass
json_data["endpoints"][key] = val
print(json_data)
self.db_client.save_network_activity(json_data)
except requests.RequestException:
NlException("Retrieving entities. No response from server").log()
except threading.ThreadError:
NlException("Unable to scheldule entity update").log()
except NlException as ex:
ex.log()
except:
NlException("Unable to parse entities").log()
def __init__(self):
"""
Class constructor
"""
## Auth token
self.auth_token = None
## InfluxDB client
self.db_client = NlDbClient()
## HTTP requests
self.get_auth_token()
self.get_entities()