forked from MarieluiseOden/FiLiP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorion_example.py
196 lines (152 loc) · 6.26 KB
/
orion_example.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
import logging
from typing import List
from requests import RequestException
from filip.clients.ngsi_v2 import ContextBrokerClient
from filip.models.base import FiwareHeader
from filip.models.ngsi_v2.context import ContextEntity, ContextAttribute
from filip.utils.simple_ql import QueryString
# Setting up logging
logging.basicConfig(
level='INFO',
format='%(asctime)s %(name)s %(levelname)s: %(message)s')
logger = logging.getLogger(__name__)
def setup():
"""
Get version of context broker
"""
logger.info("------Setting up clients------")
with ContextBrokerClient(
fiware_header=FiwareHeader(service='filip',
service_path='/testing')) as cb_client:
for key, value in cb_client.get_version().items():
print("Context broker version" + value["version"] + "at url " +
cb_client.base_url)
def create_entities():
"""
Create entities
"""
with ContextBrokerClient(
fiware_header=FiwareHeader(service='filip',
service_path='/testing')) as cb_client:
room1 = {"id": "Room1",
"type": "Room",
"temperature": {"value": 11,
"type": "Float"},
"pressure": {"value": 111,
"type": "Integer"}
}
room2 = {"id": "Room2",
"type": "Room",
"temperature": {"value": 22,
"type": "Float"},
"pressure": {"value": 222,
"type": "Integer"}
}
room3 = {"id": "Room3",
"type": "Room",
"temperature": {"value": 33,
"type": "Float"},
"pressure": {"value": 333,
"type": "Integer"}
}
logger.info("------Creating example entities------")
room1_entity = ContextEntity(**room1)
room2_entity = ContextEntity(**room2)
room3_entity = ContextEntity(**room3)
logger.info("------Posting example entities------")
cb_client.post_entity(entity=room1_entity)
cb_client.post_entity(entity=room2_entity)
cb_client.post_entity(entity=room3_entity)
return [room1_entity, room2_entity, room3_entity]
def filter_entities():
"""
Retrieve and filter entities
Returns:
"""
with ContextBrokerClient(
fiware_header=FiwareHeader(service='filip',
service_path='/testing')) as cb_client:
logger.info("------Get all entities from context broker------")
logger.info(cb_client.get_entity_list())
logger.info("------Get entities by id------")
logger.info(cb_client.get_entity_list(entity_ids=["Room1"]))
logger.info("------Get entities by type------")
logger.info(cb_client.get_entity_list(entity_types=["Room"]))
logger.info("------Get entities by id pattern------")
logger.info(cb_client.get_entity_list(id_pattern="^Room[2-5]"))
logger.info("------Get entities by query expression------")
query = QueryString(qs=[('temperature', '>', 22)])
logger.info(cb_client.get_entity_list(q=query))
logger.info("------Get attributes of entities------")
logger.info(cb_client.get_entity_attributes(entity_id="Room1"))
def update_entity(entity: ContextEntity):
"""
Update entitites
Args:
entity:
Returns:
"""
with ContextBrokerClient(
fiware_header=FiwareHeader(service='filip',
service_path='/testing')) as cb_client:
entity.add_properties({'Space': ContextAttribute(
type='Number', value=111)})
logger.info("------Updating value of an attribute of an entity------")
logger.info(cb_client.update_attribute_value(entity_id=entity.id,
attr_name="temperature",
value=12))
logger.info("------Adding a new attribute to an entity------")
logger.info(cb_client.update_entity(entity=entity))
logger.info("------Checking if new attribute is added------")
logger.info(cb_client.get_entity_attributes(entity_id=entity.id))
def error_check(entity: ContextEntity):
"""
Check error messages
Args:
entity:
Returns:
"""
with ContextBrokerClient(
fiware_header=FiwareHeader(service='filip',
service_path='/testing')) as cb_client:
try:
logger.info("-----Should return an error "
"for non existing id------")
logger.info(cb_client.get_entity(entity_id="Room5"))
except RequestException:
try:
logger.info("-----Should return an error "
"for non existing type-----")
logger.info(cb_client.get_entity(entity_id=entity.id,
entity_type="Surface"))
except RequestException:
try:
logger.info("------Should return an error for non "
"existing attribute name------")
logger.info(
cb_client.get_attribute_value(entity_id=entity.id,
attr_name="area"))
except RequestException:
pass
def delete_entities(entities_to_delete: List[ContextEntity]):
"""
Clean up
Args:
entities_to_delete:
Returns:
"""
with ContextBrokerClient(
fiware_header=FiwareHeader(service='filip',
service_path='/testing')) as cb_client:
logger.info("Deleting all test entities")
for entity in entities_to_delete:
cb_client.delete_entity(entity_id=entity.id)
return
if __name__ == "__main__":
logger.info("------EXAMPLE ORION------")
setup()
entities = create_entities()
filter_entities()
update_entity(entities[0])
error_check(entities[0])
delete_entities(entities)