Skip to content

Commit 463ab68

Browse files
author
Walther
committed
attempt: fix deprecated methods
1 parent 30674f6 commit 463ab68

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

tutorials/ngsi_v2/e5_iot_thermal_zone_control/e5_iot_thermal_zone_control_solution.py

+17-9
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
from urllib.parse import urlparse
3939
from uuid import uuid4
4040
import paho.mqtt.client as mqtt
41-
from pydantic import parse_file_as
41+
from pydantic import TypeAdapter
4242
import matplotlib.pyplot as plt
4343

4444
# import from filip
@@ -94,6 +94,12 @@
9494
READ_DEVICES_FILEPATH = \
9595
Path("../e4_iot_thermal_zone_sensors_solution_devices.json")
9696

97+
with open(READ_GROUPS_FILEPATH, 'r') as file:
98+
json_groups = json.load(file)
99+
100+
with open(READ_DEVICES_FILEPATH, 'r') as file:
101+
json_devices = json.load(file)
102+
97103
# set parameters for the temperature simulation
98104
TEMPERATURE_MAX = 10 # maximal ambient temperature
99105
TEMPERATURE_MIN = -5 # minimal ambient temperature
@@ -125,8 +131,10 @@
125131
history_heater = []
126132

127133
# Create clients and restore devices and groups from file
128-
groups = parse_file_as(List[ServiceGroup], READ_GROUPS_FILEPATH)
129-
devices = parse_file_as(List[Device], READ_DEVICES_FILEPATH)
134+
ta1 = TypeAdapter(List[ServiceGroup])
135+
groups = ta1.validate_python(json_groups)
136+
ta2 = TypeAdapter(List[Device])
137+
devices = ta2.validate_python(json_devices)
130138
cbc = ContextBrokerClient(url=CB_URL, fiware_header=fiware_header)
131139
iotac = IoTAClient(url=IOTA_URL, fiware_header=fiware_header)
132140
iotac.post_groups(service_groups=groups)
@@ -167,7 +175,7 @@
167175
heater_entity = cbc.get_entity(entity_id=heater.entity_name,
168176
entity_type=heater.entity_type)
169177
print(f"Your device entity before running the simulation: \n "
170-
f"{heater_entity.json(indent=2)}")
178+
f"{heater_entity.model_dump_json(indent=2)}")
171179

172180
# create a MQTTv5 client with paho-mqtt and the known groups and devices.
173181
mqttc = IoTAMQTTClient(protocol=mqtt.MQTTv5,
@@ -237,7 +245,7 @@ def on_measurement(client, obj, msg):
237245
"""
238246
Callback for measurement notifications
239247
"""
240-
message = Message.parse_raw(msg.payload)
248+
message = Message.model_validate_json(msg.payload)
241249
updated_zone_temperature_sensor = message.data[0]
242250

243251
# ToDo: retrieve the value of temperature attribute
@@ -340,7 +348,7 @@ def on_measurement(client, obj, msg):
340348
mqttc.disconnect()
341349

342350
print(cbc.get_entity(entity_id=heater.entity_name,
343-
entity_type=heater.entity_type).json(indent=2))
351+
entity_type=heater.entity_type).model_dump_json(indent=2))
344352

345353
# plot results
346354
fig, ax = plt.subplots()
@@ -372,21 +380,21 @@ def on_measurement(client, obj, msg):
372380
f"Wrong file extension! {WRITE_DEVICES_FILEPATH.suffix}"
373381
WRITE_DEVICES_FILEPATH.touch(exist_ok=True)
374382
with WRITE_DEVICES_FILEPATH.open('w', encoding='utf-8') as f:
375-
devices = [item.dict() for item in iotac.get_device_list()]
383+
devices = [item.model_dump() for item in iotac.get_device_list()]
376384
json.dump(devices, f, ensure_ascii=False, indent=2)
377385

378386
assert WRITE_GROUPS_FILEPATH.suffix == '.json', \
379387
f"Wrong file extension! {WRITE_GROUPS_FILEPATH.suffix}"
380388
WRITE_GROUPS_FILEPATH.touch(exist_ok=True)
381389
with WRITE_GROUPS_FILEPATH.open('w', encoding='utf-8') as f:
382-
groups = [item.dict() for item in iotac.get_group_list()]
390+
groups = [item.model_dump() for item in iotac.get_group_list()]
383391
json.dump(groups, f, ensure_ascii=False, indent=2)
384392

385393
assert WRITE_SUBSCRIPTIONS_FILEPATH.suffix == '.json', \
386394
f"Wrong file extension! {WRITE_SUBSCRIPTIONS_FILEPATH.suffix}"
387395
WRITE_SUBSCRIPTIONS_FILEPATH.touch(exist_ok=True)
388396
with WRITE_SUBSCRIPTIONS_FILEPATH.open('w', encoding='utf-8') as f:
389-
subs = [item.dict() for item in cbc.get_subscription_list()]
397+
subs = [item.model_dump() for item in cbc.get_subscription_list()]
390398
json.dump(subs, f, ensure_ascii=False, indent=2)
391399

392400
clear_iot_agent(url=IOTA_URL, fiware_header=fiware_header)

0 commit comments

Comments
 (0)