This implementation provides automatic synchronization between the main energy tracking system and the IoT mock service using MQTT events.
When devices are created, updated, or deleted in the main system:
- The device operation is performed in the database
- An MQTT event is published to
energy/events/devices/{device_id}/{event_type} - Event types:
device.created,device.updated,device.deleted
The IoT mock service automatically:
- Listens for device events on MQTT
- Creates corresponding mock devices with ID format:
mock-real-{real_device_id} - Starts generating realistic data for new devices
- Updates or removes mock devices when real devices change
energy/events/devices/{device_id}/device.created
energy/events/devices/{device_id}/device.updated
energy/events/devices/{device_id}/device.deleted
{
"event_type": "device.created",
"device_id": "uuid-of-real-device",
"timestamp": "2025-08-10T12:00:00Z",
"device_data": {
"id": "uuid-of-real-device",
"name": "HVAC Unit 1",
"type": "hvac",
"location": "Building A - Floor 2",
"status": "offline",
"base_power": null,
"base_voltage": null,
...
},
"metadata": {
"source": "data-ingestion-service"
}
}MQTT_BROKER=mosquitto
MQTT_PORT=1883
MQTT_USERNAME=iot_user
MQTT_PASSWORD=iot123MQTT_BROKER_HOST=mosquitto
MQTT_BROKER_PORT=1883
MQTT_USERNAME=iot_user
MQTT_PASSWORD=iot123The system automatically maps real device types to appropriate mock device configurations:
| Real Device Type | Mock Base Power | Mock Base Voltage |
|---|---|---|
| hvac | 25.0 kW | 240.0 V |
| lighting | 8.5 kW | 240.0 V |
| server | 45.0 kW | 240.0 V |
| industrial | 120.0 kW | 480.0 V |
| appliance | 15.0 kW | 240.0 V |
| sensor | 2.0 kW | 12.0 V |
| meter | 5.0 kW | 240.0 V |
| gateway | 10.0 kW | 240.0 V |
| controller | 12.0 kW | 240.0 V |
- Automatic Synchronization: No manual intervention needed
- Real-time: Mock devices created immediately when real devices are added
- Consistent: Mock device IDs follow predictable pattern
- Reliable: Uses MQTT with QoS 1 for guaranteed delivery
- Scalable: Event-driven architecture handles multiple services
- Start the MQTT broker (Mosquitto)
- Start the Data Ingestion Service
- Start the IoT Mock Service
- Add a device through the frontend or API
- Mock device automatically appears and starts generating data
Check logs for synchronization status:
- Data Ingestion Service: "Published device event: device.created for device {id}"
- IoT Mock Service: "Created mock device mock-real-{id} for real device: {id}"