Skip to content

Commit 44a7059

Browse files
authored
Merge pull request #259 from RWTH-EBC/255-ngsi_v2-tutorials-are-deprecated
fix: ngsi v2 tutorials are deprecated (#255)
2 parents 2162d59 + 80eaa74 commit 44a7059

16 files changed

+860
-731
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
- add: tutorials for multi-entity ([#260](https://github.com/RWTH-EBC/FiLiP/pull/260))
44
- add: add ``update_entity_relationships`` to allow relationship update ([#271](https://github.com/RWTH-EBC/FiLiP/pull/271))
55
- add: flag to determine the deletion of registration when clearing the CB ([#267](https://github.com/RWTH-EBC/FiLiP/pull/267))
6+
- fix: rework tutorials for pydantic v2 ([#259](https://github.com/RWTH-EBC/FiLiP/pull/259))
67

78
### v0.4.1
89
- fix: Session added as optional parameter to enable tls communication with clients ([#249](https://github.com/RWTH-EBC/FiLiP/pull/249))

filip/models/ngsi_v2/iot.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ def validate_cbHost(cls, value):
221221
Returns:
222222
timezone
223223
"""
224-
return str(value)
224+
return str(value) if value else value
225225
lazy: Optional[List[LazyDeviceAttribute]] = Field(
226226
default=[],
227227
desription="list of common lazy attributes of the device. For each "

tutorials/ngsi_v2/e1_virtual_weatherstation/e1_virtual_weatherstation.py

+19-20
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
44
# Create a virtual IoT device that simulates the ambient temperature and
55
# publishes it via MQTT. The simulation function is already predefined.
6-
# This exercise to give a simple introduction to the communication via MQTT.
6+
# This exercise gives a simple introduction to the communication via MQTT.
77
88
# The input sections are marked with 'ToDo'
99
@@ -31,16 +31,15 @@
3131
# import simulation model
3232
from tutorials.ngsi_v2.simulation_model import SimulationModel
3333

34-
3534
# ## Parameters
36-
# ToDo: Enter your mqtt broker url and port, e.g mqtt://test.mosquitto.org:1883
35+
# ToDo: Enter your mqtt broker url and port, e.g. mqtt://test.mosquitto.org:1883.
3736
MQTT_BROKER_URL = "mqtt://test.mosquitto.org:1883"
38-
# ToDo: If required enter your username and password
37+
# ToDo: If required, enter your username and password.
3938
MQTT_USER = ""
40-
MQTT_PW = ""
39+
MQTT_PW = ""
4140

4241
# ToDo: Create a unique topic that your weather station will publish on,
43-
# e.g. by using a uuid
42+
# e.g. by using a uuid.
4443
UNIQUE_ID = str(uuid4())
4544
TOPIC_WEATHER_STATION = f"fiware_workshop/{UNIQUE_ID}/weather_station"
4645

@@ -50,8 +49,7 @@
5049

5150
T_SIM_START = 0 # simulation start time in seconds
5251
T_SIM_END = 24 * 60 * 60 # simulation end time in seconds
53-
COM_STEP = 60 * 60 * 0.25 # 15 min communication step in seconds
54-
52+
COM_STEP = 60 * 60 # 60 min communication step in seconds
5553

5654
# ## Main script
5755
if __name__ == '__main__':
@@ -64,22 +62,23 @@
6462
# define a list for storing historical data
6563
history_weather_station = []
6664

67-
# ToDo: create a MQTTv5 client with paho-mqtt
65+
# ToDo: Create an MQTTv5 client with paho-mqtt.
6866
mqttc = ...
6967
# set user data if required
7068
mqttc.username_pw_set(username=MQTT_USER, password=MQTT_PW)
69+
7170
# ToDo: Define a callback function that will be executed when the client
7271
# receives message on a subscribed topic. It should decode your message
73-
# and store the information for later in our history
74-
# Note: do not change function's signature
72+
# and store the information for later in our history.
73+
# Note: Do not change the function's signature!
7574
def on_message(client, userdata, msg):
7675
"""
7776
Callback function for incoming messages
7877
"""
7978
# decode the payload
8079
payload = msg.payload.decode('utf-8')
8180
# ToDo: Parse the payload using the `json` package and write it to
82-
# the history
81+
# the history.
8382
...
8483

8584
pass
@@ -88,7 +87,7 @@ def on_message(client, userdata, msg):
8887
# or a topic specific callback with `mqttc.message_callback_add()`
8988
mqttc.on_message = on_message
9089

91-
# ToDO: connect to the mqtt broker and subscribe to your topic
90+
# ToDo: Connect to the mqtt broker and subscribe to your topic.
9291
mqtt_url = urlparse(MQTT_BROKER_URL)
9392
...
9493

@@ -98,27 +97,27 @@ def on_message(client, userdata, msg):
9897

9998

10099

101-
# ToDo: print and subscribe to the weather station topic
100+
# ToDo: Print and subscribe to the weather station topic.
102101
print(f"WeatherStation topic:\n {TOPIC_WEATHER_STATION}")
103102
mqttc.subscribe(topic=TOPIC_WEATHER_STATION)
104103

105104
# create a non-blocking thread for mqtt communication
106105
mqttc.loop_start()
107106

108-
# ToDo: Create a loop that publishes every second a message to the broker
107+
# ToDo: Create a loop that publishes every 0.2 seconds a message to the broker
109108
# that holds the simulation time "t_sim" and the corresponding temperature
110-
# "t_amb"
109+
# "t_amb".
111110
for t_sim in range(sim_model.t_start,
112111
int(sim_model.t_end + COM_STEP),
113112
int(COM_STEP)):
114-
# ToDo: publish the simulated ambient temperature
113+
# ToDo: Publish the simulated ambient temperature.
115114
...
116115

117116

118117

119118
# simulation step for next loop
120119
sim_model.do_step(int(t_sim + COM_STEP))
121-
time.sleep(1)
120+
time.sleep(0.2)
122121

123122
# close the mqtt listening thread
124123
mqttc.loop_stop()
@@ -127,9 +126,9 @@ def on_message(client, userdata, msg):
127126

128127
# plot results
129128
fig, ax = plt.subplots()
130-
t_simulation = [item["t_sim"] for item in history_weather_station]
129+
t_simulation = [item["t_sim"]/3600 for item in history_weather_station]
131130
temperature = [item["t_amb"] for item in history_weather_station]
132131
ax.plot(t_simulation, temperature)
133-
ax.set_xlabel('time in s')
132+
ax.set_xlabel('time in h')
134133
ax.set_ylabel('ambient temperature in °C')
135134
plt.show()

tutorials/ngsi_v2/e1_virtual_weatherstation/e1_virtual_weatherstation_solution.py

+21-23
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
44
# Create a virtual IoT device that simulates the ambient temperature and
55
# publishes it via MQTT. The simulation function is already predefined.
6-
# This exercise to give a simple introduction to the communication via MQTT.
6+
# This exercise gives a simple introduction to the communication via MQTT.
77
88
# The input sections are marked with 'ToDo'
99
@@ -31,16 +31,15 @@
3131
# import simulation model
3232
from tutorials.ngsi_v2.simulation_model import SimulationModel
3333

34-
3534
# ## Parameters
36-
# ToDo: Enter your mqtt broker url and port, e.g mqtt://test.mosquitto.org:1883
37-
MQTT_BROKER_URL = "mqtt://test.mosquitto.org:1883"
38-
# ToDo: If required enter your username and password
35+
# ToDo: Enter your mqtt broker url and port, e.g. mqtt://test.mosquitto.org:1883.
36+
MQTT_BROKER_URL = "mqtt://test.mosquitto.org:1883"
37+
# ToDo: If required, enter your username and password.
3938
MQTT_USER = ""
40-
MQTT_PW = ""
39+
MQTT_PW = ""
4140

4241
# ToDo: Create a unique topic that your weather station will publish on,
43-
# e.g. by using a uuid
42+
# e.g. by using a uuid.
4443
UNIQUE_ID = str(uuid4())
4544
TOPIC_WEATHER_STATION = f"fiware_workshop/{UNIQUE_ID}/weather_station"
4645

@@ -50,8 +49,7 @@
5049

5150
T_SIM_START = 0 # simulation start time in seconds
5251
T_SIM_END = 24 * 60 * 60 # simulation end time in seconds
53-
COM_STEP = 60 * 60 * 1 # 60 min communication step in seconds
54-
52+
COM_STEP = 60 * 60 # 60 min communication step in seconds
5553

5654
# ## Main script
5755
if __name__ == '__main__':
@@ -61,34 +59,34 @@
6159
temp_max=TEMPERATURE_MAX,
6260
temp_min=TEMPERATURE_MIN)
6361

64-
# define lists to store historical data
62+
# define a list for storing historical data
6563
history_weather_station = []
6664

67-
# ToDo: create a MQTTv5 client with paho-mqtt
65+
# ToDo: Create an MQTTv5 client with paho-mqtt.
6866
mqttc = mqtt.Client(protocol=mqtt.MQTTv5)
6967
# set user data if required
7068
mqttc.username_pw_set(username=MQTT_USER, password=MQTT_PW)
69+
7170
# ToDo: Define a callback function that will be executed when the client
7271
# receives message on a subscribed topic. It should decode your message
73-
# and store the information for later in our history
74-
# Note: do not change function's signature!
72+
# and store the information for later in our history.
73+
# Note: Do not change the function's signature!
7574
def on_message(client, userdata, msg):
7675
"""
7776
Callback function for incoming messages
7877
"""
7978
# decode the payload
8079
payload = msg.payload.decode('utf-8')
8180
# ToDo: Parse the payload using the `json` package and write it to
82-
# the history
81+
# the history.
8382
history_weather_station.append(json.loads(payload))
8483

8584

86-
8785
# add your callback function to the client. You can either use a global
8886
# or a topic specific callback with `mqttc.message_callback_add()`
8987
mqttc.on_message = on_message
9088

91-
# ToDO: connect to the mqtt broker and subscribe to your topic
89+
# ToDo: Connect to the mqtt broker and subscribe to your topic.
9290
mqtt_url = urlparse(MQTT_BROKER_URL)
9391
mqttc.connect(host=mqtt_url.hostname,
9492
port=mqtt_url.port,
@@ -98,27 +96,27 @@ def on_message(client, userdata, msg):
9896
clean_start=mqtt.MQTT_CLEAN_START_FIRST_ONLY,
9997
properties=None)
10098

101-
# ToDo: print and subscribe to the weather station topic
99+
# ToDo: Print and subscribe to the weather station topic.
102100
print(f"WeatherStation topic:\n {TOPIC_WEATHER_STATION}")
103101
mqttc.subscribe(topic=TOPIC_WEATHER_STATION)
104102

105103
# create a non-blocking thread for mqtt communication
106104
mqttc.loop_start()
107105

108-
# ToDo: Create a loop that publishes every second a message to the broker
106+
# ToDo: Create a loop that publishes every 0.2 seconds a message to the broker
109107
# that holds the simulation time "t_sim" and the corresponding temperature
110-
# "t_amb"
108+
# "t_amb".
111109
for t_sim in range(sim_model.t_start,
112110
int(sim_model.t_end + COM_STEP),
113111
int(COM_STEP)):
114-
# ToDo: publish the simulated ambient temperature
112+
# ToDo: Publish the simulated ambient temperature.
115113
mqttc.publish(topic=TOPIC_WEATHER_STATION,
116114
payload=json.dumps({"t_amb": sim_model.t_amb,
117115
"t_sim": sim_model.t_sim}))
118116

119117
# simulation step for next loop
120118
sim_model.do_step(int(t_sim + COM_STEP))
121-
time.sleep(1)
119+
time.sleep(0.2)
122120

123121
# close the mqtt listening thread
124122
mqttc.loop_stop()
@@ -127,9 +125,9 @@ def on_message(client, userdata, msg):
127125

128126
# plot results
129127
fig, ax = plt.subplots()
130-
t_simulation = [item["t_sim"] for item in history_weather_station]
128+
t_simulation = [item["t_sim"]/3600 for item in history_weather_station]
131129
temperature = [item["t_amb"] for item in history_weather_station]
132130
ax.plot(t_simulation, temperature)
133-
ax.set_xlabel('time in s')
131+
ax.set_xlabel('time in h')
134132
ax.set_ylabel('ambient temperature in °C')
135133
plt.show()
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
"""
22
# # Exercise 2: Service Health Check
33
4-
# Create one or multiple filip clients and check if the corresponding services
4+
# Create one or multiple FiLiP clients and check if the corresponding services
55
# are up and running by accessing their version information.
66
77
# The input sections are marked with 'ToDo'
88
99
# #### Steps to complete:
1010
# 1. Set up the missing parameters in the parameter section
11-
# 2. Create filip ngsi_v2 clients for the individual services and check for
11+
# 2. Create FiLiP ngsi_v2 clients for the individual services and check for
1212
# their version
1313
# 3. Create a config object for the ngsi_v2 multi client (HttpClient),
1414
# create the multi client and again check for services' versions
@@ -23,29 +23,32 @@
2323
QuantumLeapClient
2424

2525
# ## Parameters
26-
# ToDo: Enter your context broker url and port, e.g. http://localhost:1026
26+
# ToDo: Enter your context broker url and port, e.g. http://localhost:1026.
2727
CB_URL = "http://localhost:1026"
28-
# ToDo: Enter your IoT-Agent url and port, e.g. http://localhost:4041
28+
# ToDo: Enter your IoT-Agent url and port, e.g. http://localhost:4041.
2929
IOTA_URL = "http://localhost:4041"
30-
# ToDo: Enter your QuantumLeap url and port, e.g. http://localhost:8668
30+
# ToDo: Enter your QuantumLeap url and port, e.g. http://localhost:8668.
3131
QL_URL = "http://localhost:8668"
3232

3333
# ## Main script
3434
if __name__ == "__main__":
35-
# Create a single client for each service and check the service for
36-
# its version
35+
# ToDo: Create a single client for each service and check the service for
36+
# its version.
3737
cbc = ContextBrokerClient(url=CB_URL)
38-
print(cbc.get_version())
38+
print(f"Context Broker Client: {cbc.get_version()}")
3939

4040
iotac = ...
41+
print(f"IoTA Client: {iotac.get_version()}")
4142

4243
qlc = ...
44+
print(f"Quantum Leap Client: {qlc.get_version()}")
4345

44-
# ToDo: Create a configuration object for a multi client
46+
# ToDo: Create a configuration object for a multi client.
4547
config = HttpClientConfig(...)
4648

47-
# ToDo: Create a multi client check again all services for their version
49+
# ToDo: Create a multi client check again all services for their version.
4850
multic = HttpClient(config=config)
4951

50-
print(multic.cb.get_version())
51-
...
52+
print(f"Multi Client (Context Broker): {multic.cb.get_version()}\n"
53+
f"Multi Client (IoTA): {multic.iota.get_version()}\n"
54+
f"Multi Client (Quantum Leap): {multic.timeseries.get_version()}")
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
"""
22
# # Exercise 2: Service Health Check
33
4-
# Create one or multiple filip clients and check if the corresponding services
4+
# Create one or multiple FiLiP clients and check if the corresponding services
55
# are up and running by accessing their version information.
66
77
# The input sections are marked with 'ToDo'
88
99
# #### Steps to complete:
1010
# 1. Set up the missing parameters in the parameter section
11-
# 2. Create filip ngsi_v2 clients for the individual services and check for
11+
# 2. Create FiLiP ngsi_v2 clients for the individual services and check for
1212
# their version
1313
# 3. Create a config object for the ngsi_v2 multi client (HttpClient),
1414
# create the multi client and again check for services' versions
@@ -23,32 +23,32 @@
2323
QuantumLeapClient
2424

2525
# ## Parameters
26-
# ToDo: Enter your context broker url and port, e.g. http://localhost:1026
26+
# ToDo: Enter your context broker url and port, e.g. http://localhost:1026.
2727
CB_URL = "http://localhost:1026"
28-
# ToDo: Enter your IoT-Agent url and port, e.g. http://localhost:4041
28+
# ToDo: Enter your IoT-Agent url and port, e.g. http://localhost:4041.
2929
IOTA_URL = "http://localhost:4041"
30-
# ToDo: Enter your QuantumLeap url and port, e.g. http://localhost:8668
30+
# ToDo: Enter your QuantumLeap url and port, e.g. http://localhost:8668.
3131
QL_URL = "http://localhost:8668"
3232

3333
# ## Main script
3434
if __name__ == "__main__":
3535
# ToDo: Create a single client for each service and check the service for
36-
# its version
36+
# its version.
3737
cbc = ContextBrokerClient(url=CB_URL)
38-
print(cbc.get_version())
38+
print(f"Context Broker Client: {cbc.get_version()}")
3939

4040
iotac = IoTAClient(url=IOTA_URL)
41-
print(iotac.get_version())
41+
print(f"IoTA Client: {iotac.get_version()}")
4242

4343
qlc = QuantumLeapClient(url=QL_URL)
44-
print(qlc.get_version())
44+
print(f"Quantum Leap Client: {qlc.get_version()}")
4545

46-
# ToDo: Create a configuration object for a multi client
46+
# ToDo: Create a configuration object for a multi client.
4747
config = HttpClientConfig(cb_url=CB_URL, iota_url=IOTA_URL, ql_url=QL_URL)
4848

49-
# ToDo: Create a multi client check again all services for their version
49+
# ToDo: Create a multi client check again all services for their version.
5050
multic = HttpClient(config=config)
5151

52-
print(multic.cb.get_version())
53-
print(multic.iota.get_version())
54-
print(multic.timeseries.get_version())
52+
print(f"Multi Client (Context Broker): {multic.cb.get_version()}\n"
53+
f"Multi Client (IoTA): {multic.iota.get_version()}\n"
54+
f"Multi Client (Quantum Leap): {multic.timeseries.get_version()}")

0 commit comments

Comments
 (0)