|
38 | 38 | from urllib.parse import urlparse
|
39 | 39 | from uuid import uuid4
|
40 | 40 | import paho.mqtt.client as mqtt
|
41 |
| -from pydantic import parse_file_as |
| 41 | +from pydantic import TypeAdapter |
42 | 42 | import matplotlib.pyplot as plt
|
43 | 43 |
|
44 | 44 | # import from filip
|
|
94 | 94 | READ_DEVICES_FILEPATH = \
|
95 | 95 | Path("../e4_iot_thermal_zone_sensors_solution_devices.json")
|
96 | 96 |
|
| 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 | + |
97 | 103 | # set parameters for the temperature simulation
|
98 | 104 | TEMPERATURE_MAX = 10 # maximal ambient temperature
|
99 | 105 | TEMPERATURE_MIN = -5 # minimal ambient temperature
|
|
125 | 131 | history_heater = []
|
126 | 132 |
|
127 | 133 | # 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) |
130 | 138 | cbc = ContextBrokerClient(url=CB_URL, fiware_header=fiware_header)
|
131 | 139 | iotac = IoTAClient(url=IOTA_URL, fiware_header=fiware_header)
|
132 | 140 | iotac.post_groups(service_groups=groups)
|
|
167 | 175 | heater_entity = cbc.get_entity(entity_id=heater.entity_name,
|
168 | 176 | entity_type=heater.entity_type)
|
169 | 177 | 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)}") |
171 | 179 |
|
172 | 180 | # create a MQTTv5 client with paho-mqtt and the known groups and devices.
|
173 | 181 | mqttc = IoTAMQTTClient(protocol=mqtt.MQTTv5,
|
@@ -237,7 +245,7 @@ def on_measurement(client, obj, msg):
|
237 | 245 | """
|
238 | 246 | Callback for measurement notifications
|
239 | 247 | """
|
240 |
| - message = Message.parse_raw(msg.payload) |
| 248 | + message = Message.model_validate_json(msg.payload) |
241 | 249 | updated_zone_temperature_sensor = message.data[0]
|
242 | 250 |
|
243 | 251 | # ToDo: retrieve the value of temperature attribute
|
@@ -340,7 +348,7 @@ def on_measurement(client, obj, msg):
|
340 | 348 | mqttc.disconnect()
|
341 | 349 |
|
342 | 350 | 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)) |
344 | 352 |
|
345 | 353 | # plot results
|
346 | 354 | fig, ax = plt.subplots()
|
@@ -372,21 +380,21 @@ def on_measurement(client, obj, msg):
|
372 | 380 | f"Wrong file extension! {WRITE_DEVICES_FILEPATH.suffix}"
|
373 | 381 | WRITE_DEVICES_FILEPATH.touch(exist_ok=True)
|
374 | 382 | 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()] |
376 | 384 | json.dump(devices, f, ensure_ascii=False, indent=2)
|
377 | 385 |
|
378 | 386 | assert WRITE_GROUPS_FILEPATH.suffix == '.json', \
|
379 | 387 | f"Wrong file extension! {WRITE_GROUPS_FILEPATH.suffix}"
|
380 | 388 | WRITE_GROUPS_FILEPATH.touch(exist_ok=True)
|
381 | 389 | 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()] |
383 | 391 | json.dump(groups, f, ensure_ascii=False, indent=2)
|
384 | 392 |
|
385 | 393 | assert WRITE_SUBSCRIPTIONS_FILEPATH.suffix == '.json', \
|
386 | 394 | f"Wrong file extension! {WRITE_SUBSCRIPTIONS_FILEPATH.suffix}"
|
387 | 395 | WRITE_SUBSCRIPTIONS_FILEPATH.touch(exist_ok=True)
|
388 | 396 | 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()] |
390 | 398 | json.dump(subs, f, ensure_ascii=False, indent=2)
|
391 | 399 |
|
392 | 400 | clear_iot_agent(url=IOTA_URL, fiware_header=fiware_header)
|
|
0 commit comments