-
Notifications
You must be signed in to change notification settings - Fork 13
/
e10_ngsi_v2_quantumleap.py
179 lines (152 loc) · 5.4 KB
/
e10_ngsi_v2_quantumleap.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
"""
# Example for working with the QuantumLeapClient
"""
# ## Import packages
import logging
import time
import random
from filip.config import settings
from filip.models.ngsi_v2.subscriptions import Message, Subscription
from filip.models.ngsi_v2.context import ContextEntity
from filip.models.base import FiwareHeader
from filip.clients.ngsi_v2 import ContextBrokerClient, QuantumLeapClient
from filip.utils.cleanup import clear_all
# ## Parameters
#
# To run this example you need a working Fiware v2 setup with a
# Context Broker and QuantumLeap. Here you can set the addresses:
#
# Host address of Context Broker
CB_URL = settings.CB_URL
# Host address of QuantumLeap
QL_URL = settings.QL_URL
# Here you can also change FIWARE service and service path.
# FIWARE-Service
SERVICE = "filip"
# FIWARE-Service path
SERVICE_PATH = "/example"
# Setting up logging
logging.basicConfig(
level="INFO",
format="%(asctime)s %(name)s %(levelname)s: %(message)s",
datefmt="%d-%m-%Y %H:%M:%S",
)
logger = logging.getLogger(__name__)
if __name__ == "__main__":
# ## 1 Setup
#
# QuantumLeapClient and ContextBrokerClient are created to access
# FIWARE in the space given by the FiwareHeader.
#
# For more information see: e01_http_clients.py
fiware_header = FiwareHeader(service=SERVICE, service_path=SERVICE_PATH)
# clear all existing data
clear_all(fiware_header=fiware_header, cb_url=CB_URL, ql_url=QL_URL)
ql_client = QuantumLeapClient(url=QL_URL, fiware_header=fiware_header)
print(
f"Quantum Leap Client version: {ql_client.get_version()['version']}"
f" located at url: {ql_client.base_url}"
)
cb_client = ContextBrokerClient(url=CB_URL, fiware_header=fiware_header)
print(
f"Context Broker version: {cb_client.get_version()['orion']['version']}"
f" located at url: {cb_client.base_url}"
)
# ## 2 Interact with QL
#
# ### 2.1 Create a ContextEntity to work with
#
# For more information see: e01_ngsi_v2_context_basics.py
hall = {
"id": "Hall_1",
"type": "Room",
"temperature": {"value": random.randint(0, 100), "type": "Integer"},
}
hall_entity = ContextEntity(**hall)
cb_client.post_entity(hall_entity)
# ### 2.2 Manage subscriptions
#
# Create a subscription
# Note: The IPs must be the ones that Orion and quantumleap can access,
# e.g. service name or static IP, localhost will not work here.
subscription: Subscription = Subscription.model_validate(
{
"subject": {"entities": [{"id": hall_entity.id}]},
"notification": { # Notify QL automatically
"http": {"url": "http://quantumleap:8668/v2/notify"}
},
}
)
subscription_id = cb_client.post_subscription(subscription=subscription)
# get all subscriptions
subscription_list = cb_client.get_subscription_list()
# notify QL manually
for sub in subscription_list:
for entity in sub.subject.entities:
if entity.id == hall_entity.id:
try:
ql_client.post_notification(
notification=Message(data=[hall_entity], subscriptionId=sub.id)
)
subscription_id = sub.id
except:
logger.error("Can not notify QL")
# notify QL via Orion
for i in range(5, 10):
cb_client.update_attribute_value(
entity_id=hall_entity.id,
entity_type=hall_entity.type,
attr_name="temperature",
value=i,
)
time.sleep(1)
# get historical data as object and you can directly convert them to pandas dataframes
try:
print(
f"get_entity_by_id method converted to pandas:\n"
f"{ql_client.get_entity_by_id(hall_entity.id).to_pandas()}\n"
)
print(
f"get_entity_values_by_id method:\n"
f"{ql_client.get_entity_values_by_id(hall_entity.id)}\n"
)
print(
f"get_entity_attr_by_id method:\n"
f"{ql_client.get_entity_attr_by_id(hall_entity.id, 'temperature')}\n"
)
print(
f"get_entity_attr_values_by_id method:\n"
f"{ql_client.get_entity_attr_values_by_id(hall_entity.id, attr_name='temperature')}\n"
)
print(
f"get_entity_attr_by_type method:\n"
f"{ql_client.get_entity_attr_by_type(hall_entity.type, attr_name='temperature')}\n"
)
print(
f"get_entity_attr_values_by_type method:\n"
f"{ql_client.get_entity_attr_values_by_type(hall_entity.type, 'temperature')}"
)
except:
logger.info("There might be no historical data for some calls.")
# ## 2.3 Delete
#
# Delete entity in QL
try:
ql_client.delete_entity(entity_id=hall_entity.id, entity_type=hall_entity.type)
except:
logger.error("Can not delete data from QL")
# delete entity in CV
try:
cb_client.delete_entity(entity_id=hall_entity.id, entity_type=hall_entity.type)
except:
logger.error("Can not delete entity from context broker")
# delete the subscription
try:
cb_client.delete_subscription(subscription_id)
except:
logger.error("Can not delete subscription from context broker.")
# # 3 Clean up (Optional)
#
# Close clients
ql_client.close()
cb_client.close()