


.png)

//Monitor temperature sensors and automatically send alerts when readings exceed 80°C
DEFINE ACTION TemperatureMonitor
ON TOPIC "sensors/+/temperature" DO
SET "sensor_id" WITH TOPIC POSITION 2
SET "temp" WITH (GET JSON "value" IN PAYLOAD AS DOUBLE) IF {temp} > 80 THEN
PUBLISH TOPIC "alerts/high-temp/" + {sensor_id}
WITH "Warning: " + {temp} + "°C"
//Only allow users with the "operator" role to send commands to machines.
DEFINE RULE AllowOperatorControl WITH PRIORITY 1
FOR PUBLISH TO TOPIC "machines/+/command"
IF USER HAS ROLE "operator" THEN
ALLOW
ELSE
DENY
//Structure raw device data into clean JSON with automatic timestamps whenever temperature updates.
DEFINE MODEL DeviceStatus WITH TOPIC "devices/+/status"
ADD STRING "device_id" WITH TOPIC POSITION 2
ADD DOUBLE "temperature" WITH TOPIC "devices/+/temp" AS TRIGGER
ADD STRING "timestamp" WITH TIMESTAMP "UTC"
//Stream all sensor data directly to a PostgreSQL database for historical analysis.
DEFINE ROUTE SensorDB WITH TYPE POSTGRESQL
ADD CONNECTION_CONFIG
WITH HOST "db.local"
WITH DATABASE "iot_data"
ADD MAPPING sensors
WITH SOURCE_TOPIC "sensors/#"
WITH TABLE "sensor_readings"

