Sample 0121 - Using Event Windows
Introduction
This sample demonstrates how to set up an execution plan that makes use of an event window to analyze the sensor reading of a smart home environment with multiple sensors in every room. This sample uses the Event Simulator for inputs and the Logger Publisher for logging the outputs in the DAS console.
The execution plan used in this sample is as follows.
The following query defines the sensor input stream.
define stream SensorStream (sensorType string, value float, roomNo int, deviceID string);
The following query defines an event window namedÂ
SensorWindow
to collect the events that arrive and to emit them every 5 seconds.define window SensorWindow (sensorType string, value float, roomNo int, deviceID string) timeBatch(5 seconds);
The following query inserts the events that are passed to theÂ
SensorStream
stream to theÂsensorWindow
window.from SensorStream insert into SensorWindow;
The following query finds the maximum reading of each sensor in each room for each 5 second interval, and inserts them into theÂ
MaxSensorReadingPerRoomStream
stream.from SensorWindow select sensorType, max(value) as maxValue, roomNo group by sensorType, roomNo insert into MaxSensorReadingPerRoomStream;
The following query finds the average reading of each sensor in the smart home for each 5 second interval and inserts them into the
OverallAverageSensorReadingStream
stream.from SensorWindow select sensorType, avg(value) as avgValue group by sensorType insert into OverallAverageSensorReadingStream;
Prerequisites
Set up the prerequisites required for all samples.
Building the sample
Start the WSO2 DAS server with the sample configuration numbered 0121. For instructions, see Starting sample CEP configurations.
This sample configuration does the following.
- Creates the following event streams.
SensorStream:1.0.0
MaxSensorReadingPerRoomStream:1.0.0
OverallAverageSensorReadingStream:1.0.0
OverallMaxSensorReadingStream:1.0.0
- Creates an execution plan namedÂ
SmartHome
. - Creates the following event publishers.
maxSensorReadingPerRoomStreamPublisher
overallAverageSensorReadingStreamPublisher
overallMaxSensorReadingStreamPublisher
Executing the sample
Follow the steps below to execute the sample.
- Access the WSO2 DAS Management Console using the following URL, and log in using your credentials.
https://<DAS_HOST>:<DAS_PORT>/carbon/Â
- In the Tools tab, click Event Stimulator to open the Event Stream Simulator page. A file named
events.csv
containing sample data is displayed in the Send multiple events section as follows. Click Play to start sending events to the DAS from this file.The events triggered in the WSO2 CLI are logged in the CLI as shown below. The following events are printed for every 5 seconds.MaxSensorReadingPerRoom
The maximum reading of each sensor in each room with a unique ID.OverallMaxSensorReading
The maximum reading of every sensor in the smart home with a unique ID.ÂOverallAverageSensorReading
The average reading of each sensor in the smart home with a unique ID.