Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ngsi v2 tutorials are deprecated (#255) #259

Merged
merged 34 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
1f03a38
chore: make exercise 2 solution more readable
Mar 2, 2024
8a58574
fix: change deprecated methods
Mar 3, 2024
30674f6
fix: change deprecated methods and fix typos
Mar 5, 2024
463ab68
attempt: fix deprecated methods
Mar 7, 2024
e5b7acd
feat: reduce time.sleep() and change x-axis plot result in exercise 1
Mar 7, 2024
4d16b24
fix: cbHost send "None" when no specifying
djs0109 Mar 19, 2024
50205bc
fix: replace "simtime" with "sim_time"
djs0109 Mar 19, 2024
c2fa9cf
chore: migrate to pydantic V2
djs0109 Mar 19, 2024
96d84a5
chore: change the convention for service_path and apikey
djs0109 Mar 19, 2024
529a89a
fix: fix parsing and deprecated methods in exercise 6 solution
Mar 27, 2024
78e9ad1
fix: fix parsing in exercise 7 solution
Mar 27, 2024
b910989
chore: make opening files consistent with as in other exercises
Mar 27, 2024
e4bfc59
chore: format e1_virtual_weatherstation exercise and solution
Apr 4, 2024
70112a6
chore: format e2_healthcheck exercise and solution
Apr 4, 2024
93e1665
chore: format e3_context_entities exercise and solution
Apr 4, 2024
b9acf77
chore: format e4_iot_thermal_zone_sensors exercise and solution
Apr 4, 2024
00ea4e0
chore: format e5_iot_thermal_zone_control exercise and solution
Apr 9, 2024
8416c70
chore: format e6_timeseries_data exercise and solution
Apr 10, 2024
17901f6
chore: format e7_semantic_iot exercise and solution
Apr 10, 2024
5fe9468
format: fix typo in exercise 1
Apr 10, 2024
0a610a5
fix: fix plotting in exercise 5
Apr 10, 2024
e029962
fix: syntax error
djs0109 Apr 11, 2024
cf1659c
chore: replace the deprecated qlc post_subscription with the one in cbc
djs0109 Apr 12, 2024
339a8f8
Merge branch 'master' into 255-ngsi_v2-tutorials-are-deprecated
Apr 16, 2024
3bd8e2f
fix: fix opening files for <python 3.10
Apr 16, 2024
17186e6
chore: make created files disjunct between exercises and solutions
Apr 16, 2024
01b6a90
chore: fix small typos and pep-8 errors in exercise 5 and 6
Apr 16, 2024
d2e228d
merge: solve merge conflicts
Apr 24, 2024
bc26f9e
fix: change the order of the clearing functions
djs0109 Apr 30, 2024
8317608
fix: revert changes to exercise 6
Apr 30, 2024
8a9a747
chore: remove some examples in e6
djs0109 Apr 30, 2024
3f816e9
Merge branch 'master' into 255-ngsi_v2-tutorials-are-deprecated
djs0109 Apr 30, 2024
fb18d6b
Merge branch 'master' into 255-ngsi_v2-tutorials-are-deprecated
djs0109 Apr 30, 2024
80eaa74
docs: changelog
djs0109 Apr 30, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
- add: tutorials for multi-entity ([#260](https://github.com/RWTH-EBC/FiLiP/pull/260))
- add: add ``update_entity_relationships`` to allow relationship update ([#271](https://github.com/RWTH-EBC/FiLiP/pull/271))
- add: flag to determine the deletion of registration when clearing the CB ([#267](https://github.com/RWTH-EBC/FiLiP/pull/267))
- fix: rework tutorials for pydantic v2 ([#259](https://github.com/RWTH-EBC/FiLiP/pull/259))

### v0.4.1
- fix: Session added as optional parameter to enable tls communication with clients ([#249](https://github.com/RWTH-EBC/FiLiP/pull/249))
Expand Down
2 changes: 1 addition & 1 deletion filip/models/ngsi_v2/iot.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ def validate_cbHost(cls, value):
Returns:
timezone
"""
return str(value)
return str(value) if value else value
lazy: Optional[List[LazyDeviceAttribute]] = Field(
default=[],
desription="list of common lazy attributes of the device. For each "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

# Create a virtual IoT device that simulates the ambient temperature and
# publishes it via MQTT. The simulation function is already predefined.
# This exercise to give a simple introduction to the communication via MQTT.
# This exercise gives a simple introduction to the communication via MQTT.

# The input sections are marked with 'ToDo'

Expand Down Expand Up @@ -31,16 +31,15 @@
# import simulation model
from tutorials.ngsi_v2.simulation_model import SimulationModel


# ## Parameters
# ToDo: Enter your mqtt broker url and port, e.g mqtt://test.mosquitto.org:1883
# ToDo: Enter your mqtt broker url and port, e.g. mqtt://test.mosquitto.org:1883.
MQTT_BROKER_URL = "mqtt://test.mosquitto.org:1883"
# ToDo: If required enter your username and password
# ToDo: If required, enter your username and password.
MQTT_USER = ""
MQTT_PW = ""
MQTT_PW = ""

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

Expand All @@ -50,8 +49,7 @@

T_SIM_START = 0 # simulation start time in seconds
T_SIM_END = 24 * 60 * 60 # simulation end time in seconds
COM_STEP = 60 * 60 * 0.25 # 15 min communication step in seconds

COM_STEP = 60 * 60 # 60 min communication step in seconds

# ## Main script
if __name__ == '__main__':
Expand All @@ -64,22 +62,23 @@
# define a list for storing historical data
history_weather_station = []

# ToDo: create a MQTTv5 client with paho-mqtt
# ToDo: Create an MQTTv5 client with paho-mqtt.
mqttc = ...
# set user data if required
mqttc.username_pw_set(username=MQTT_USER, password=MQTT_PW)

# ToDo: Define a callback function that will be executed when the client
# receives message on a subscribed topic. It should decode your message
# and store the information for later in our history
# Note: do not change function's signature
# and store the information for later in our history.
# Note: Do not change the function's signature!
def on_message(client, userdata, msg):
"""
Callback function for incoming messages
"""
# decode the payload
payload = msg.payload.decode('utf-8')
# ToDo: Parse the payload using the `json` package and write it to
# the history
# the history.
...

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

# ToDO: connect to the mqtt broker and subscribe to your topic
# ToDo: Connect to the mqtt broker and subscribe to your topic.
mqtt_url = urlparse(MQTT_BROKER_URL)
...

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



# ToDo: print and subscribe to the weather station topic
# ToDo: Print and subscribe to the weather station topic.
print(f"WeatherStation topic:\n {TOPIC_WEATHER_STATION}")
mqttc.subscribe(topic=TOPIC_WEATHER_STATION)

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

# ToDo: Create a loop that publishes every second a message to the broker
# ToDo: Create a loop that publishes every 0.2 seconds a message to the broker
# that holds the simulation time "t_sim" and the corresponding temperature
# "t_amb"
# "t_amb".
for t_sim in range(sim_model.t_start,
int(sim_model.t_end + COM_STEP),
int(COM_STEP)):
# ToDo: publish the simulated ambient temperature
# ToDo: Publish the simulated ambient temperature.
...



# simulation step for next loop
sim_model.do_step(int(t_sim + COM_STEP))
time.sleep(1)
time.sleep(0.2)

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

# plot results
fig, ax = plt.subplots()
t_simulation = [item["t_sim"] for item in history_weather_station]
t_simulation = [item["t_sim"]/3600 for item in history_weather_station]
temperature = [item["t_amb"] for item in history_weather_station]
ax.plot(t_simulation, temperature)
ax.set_xlabel('time in s')
ax.set_xlabel('time in h')
ax.set_ylabel('ambient temperature in °C')
plt.show()
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

# Create a virtual IoT device that simulates the ambient temperature and
# publishes it via MQTT. The simulation function is already predefined.
# This exercise to give a simple introduction to the communication via MQTT.
# This exercise gives a simple introduction to the communication via MQTT.

# The input sections are marked with 'ToDo'

Expand Down Expand Up @@ -31,16 +31,15 @@
# import simulation model
from tutorials.ngsi_v2.simulation_model import SimulationModel

djs0109 marked this conversation as resolved.
Show resolved Hide resolved

# ## Parameters
# ToDo: Enter your mqtt broker url and port, e.g mqtt://test.mosquitto.org:1883
MQTT_BROKER_URL = "mqtt://test.mosquitto.org:1883"
# ToDo: If required enter your username and password
# ToDo: Enter your mqtt broker url and port, e.g. mqtt://test.mosquitto.org:1883.
MQTT_BROKER_URL = "mqtt://test.mosquitto.org:1883"
# ToDo: If required, enter your username and password.
MQTT_USER = ""
MQTT_PW = ""
MQTT_PW = ""

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

Expand All @@ -50,8 +49,7 @@

T_SIM_START = 0 # simulation start time in seconds
T_SIM_END = 24 * 60 * 60 # simulation end time in seconds
COM_STEP = 60 * 60 * 1 # 60 min communication step in seconds

COM_STEP = 60 * 60 # 60 min communication step in seconds

# ## Main script
if __name__ == '__main__':
Expand All @@ -61,34 +59,34 @@
temp_max=TEMPERATURE_MAX,
temp_min=TEMPERATURE_MIN)

# define lists to store historical data
# define a list for storing historical data
history_weather_station = []

# ToDo: create a MQTTv5 client with paho-mqtt
# ToDo: Create an MQTTv5 client with paho-mqtt.
mqttc = mqtt.Client(protocol=mqtt.MQTTv5)
# set user data if required
mqttc.username_pw_set(username=MQTT_USER, password=MQTT_PW)

# ToDo: Define a callback function that will be executed when the client
# receives message on a subscribed topic. It should decode your message
# and store the information for later in our history
# Note: do not change function's signature!
# and store the information for later in our history.
# Note: Do not change the function's signature!
def on_message(client, userdata, msg):
"""
Callback function for incoming messages
"""
# decode the payload
payload = msg.payload.decode('utf-8')
# ToDo: Parse the payload using the `json` package and write it to
# the history
# the history.
history_weather_station.append(json.loads(payload))



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

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

# ToDo: print and subscribe to the weather station topic
# ToDo: Print and subscribe to the weather station topic.
print(f"WeatherStation topic:\n {TOPIC_WEATHER_STATION}")
mqttc.subscribe(topic=TOPIC_WEATHER_STATION)

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

# ToDo: Create a loop that publishes every second a message to the broker
# ToDo: Create a loop that publishes every 0.2 seconds a message to the broker
# that holds the simulation time "t_sim" and the corresponding temperature
# "t_amb"
# "t_amb".
for t_sim in range(sim_model.t_start,
int(sim_model.t_end + COM_STEP),
int(COM_STEP)):
# ToDo: publish the simulated ambient temperature
# ToDo: Publish the simulated ambient temperature.
mqttc.publish(topic=TOPIC_WEATHER_STATION,
payload=json.dumps({"t_amb": sim_model.t_amb,
"t_sim": sim_model.t_sim}))

# simulation step for next loop
sim_model.do_step(int(t_sim + COM_STEP))
time.sleep(1)
time.sleep(0.2)

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

# plot results
fig, ax = plt.subplots()
t_simulation = [item["t_sim"] for item in history_weather_station]
t_simulation = [item["t_sim"]/3600 for item in history_weather_station]
temperature = [item["t_amb"] for item in history_weather_station]
ax.plot(t_simulation, temperature)
ax.set_xlabel('time in s')
ax.set_xlabel('time in h')
ax.set_ylabel('ambient temperature in °C')
plt.show()
27 changes: 15 additions & 12 deletions tutorials/ngsi_v2/e2_healthcheck/e2_healthcheck.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
"""
# # Exercise 2: Service Health Check

# Create one or multiple filip clients and check if the corresponding services
# Create one or multiple FiLiP clients and check if the corresponding services
# are up and running by accessing their version information.

# The input sections are marked with 'ToDo'

# #### Steps to complete:
# 1. Set up the missing parameters in the parameter section
# 2. Create filip ngsi_v2 clients for the individual services and check for
# 2. Create FiLiP ngsi_v2 clients for the individual services and check for
# their version
# 3. Create a config object for the ngsi_v2 multi client (HttpClient),
# create the multi client and again check for services' versions
Expand All @@ -23,29 +23,32 @@
QuantumLeapClient

# ## Parameters
# ToDo: Enter your context broker url and port, e.g. http://localhost:1026
# ToDo: Enter your context broker url and port, e.g. http://localhost:1026.
CB_URL = "http://localhost:1026"
# ToDo: Enter your IoT-Agent url and port, e.g. http://localhost:4041
# ToDo: Enter your IoT-Agent url and port, e.g. http://localhost:4041.
IOTA_URL = "http://localhost:4041"
# ToDo: Enter your QuantumLeap url and port, e.g. http://localhost:8668
# ToDo: Enter your QuantumLeap url and port, e.g. http://localhost:8668.
QL_URL = "http://localhost:8668"

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

iotac = ...
print(f"IoTA Client: {iotac.get_version()}")

qlc = ...
print(f"Quantum Leap Client: {qlc.get_version()}")

# ToDo: Create a configuration object for a multi client
# ToDo: Create a configuration object for a multi client.
config = HttpClientConfig(...)

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

print(multic.cb.get_version())
...
print(f"Multi Client (Context Broker): {multic.cb.get_version()}\n"
f"Multi Client (IoTA): {multic.iota.get_version()}\n"
f"Multi Client (Quantum Leap): {multic.timeseries.get_version()}")
28 changes: 14 additions & 14 deletions tutorials/ngsi_v2/e2_healthcheck/e2_healthcheck_solution.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
"""
# # Exercise 2: Service Health Check

# Create one or multiple filip clients and check if the corresponding services
# Create one or multiple FiLiP clients and check if the corresponding services
# are up and running by accessing their version information.

# The input sections are marked with 'ToDo'

# #### Steps to complete:
# 1. Set up the missing parameters in the parameter section
# 2. Create filip ngsi_v2 clients for the individual services and check for
# 2. Create FiLiP ngsi_v2 clients for the individual services and check for
# their version
# 3. Create a config object for the ngsi_v2 multi client (HttpClient),
# create the multi client and again check for services' versions
Expand All @@ -23,32 +23,32 @@
QuantumLeapClient

# ## Parameters
# ToDo: Enter your context broker url and port, e.g. http://localhost:1026
# ToDo: Enter your context broker url and port, e.g. http://localhost:1026.
CB_URL = "http://localhost:1026"
# ToDo: Enter your IoT-Agent url and port, e.g. http://localhost:4041
# ToDo: Enter your IoT-Agent url and port, e.g. http://localhost:4041.
IOTA_URL = "http://localhost:4041"
# ToDo: Enter your QuantumLeap url and port, e.g. http://localhost:8668
# ToDo: Enter your QuantumLeap url and port, e.g. http://localhost:8668.
QL_URL = "http://localhost:8668"

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

iotac = IoTAClient(url=IOTA_URL)
print(iotac.get_version())
print(f"IoTA Client: {iotac.get_version()}")

qlc = QuantumLeapClient(url=QL_URL)
print(qlc.get_version())
print(f"Quantum Leap Client: {qlc.get_version()}")

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

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

print(multic.cb.get_version())
print(multic.iota.get_version())
print(multic.timeseries.get_version())
print(f"Multi Client (Context Broker): {multic.cb.get_version()}\n"
f"Multi Client (IoTA): {multic.iota.get_version()}\n"
f"Multi Client (Quantum Leap): {multic.timeseries.get_version()}")
Loading