Time for an experiment. Let’s look at a way to detect that a sensor stopped streaming data. For the example I will use the same code as in the last test.
The idea is to monitor StreamIn and when it stops, insert a message into a new output stream. The output stream can then be connected to an output adapter and send the message "out there". So, here is the output stream named sAlarm, with three fields:
- Entity – name of the device which stopped streaming data;
- Msg – a message, like "Stream stopped";
- AlmTime – time of detection.
-- Alarm stream CREATE OUTPUT STREAM sAlarm SCHEMA ( Entity STRING, Msg STRING, AlmTime STRING );
The AlmTime is formatted as a STRING, because I intend to send this message later via an email or similar adapter.
The detection technique is straight from the Coral8 CCL Cookbook; let’s see how it works.
-- Detect no data from StreamIn INSERT INTO sAlarm SELECT E1.Entity ,'Stream stopped' ,TO_STRING(GETTIMESTAMP(E1)) FROM StreamIn AS E1, StreamIn AS E2 MATCHING [2 SECONDS: E1, !E2] ON E1.Entity = E2.Entity ;
The MATCHING clause needs at least two streams (think tables), hence StreamIn is used twice with different aliases: E1 and E2. It is waiting for an event in E1, followed by a non-event from E2, within a time-window of two seconds. The GETTIMESTAMP(E1)
function returns the timestamp of the last row from the stream, so the AlmTime is actually time of the last record received.
As in the previous example, I have sent ten numbers (0-9) and then the stream stopped. Notice that AlmTime matches the time-stamp of the last StreamIn entry.
To be continued.