Vibration Alerts example

In this example we use svc_expr to generate a vibration alert if the variable Vibration exceeds 100 at least 3 times in any 10-minute fixed timeslot.

Figure: Vibration Alerts - Mix overview


Tip: Get the code from the SPARKL public repository.
Table 1. Vibration Alerts - Markup
Example Description
<folder name="MonitorVibration">   
  <field name="vibration"    
    type="integer"/>  
  <field name="RESET"/>  
  <field name="VIBRATION_ALERT"/>   
  <service name="Sequencer"    
    provision="sequencer">
  <service name="Expr"    
    provision="expr">    
    <prop name="expr.state"      
      HiCount="NewHiCount"/>
    <prop name="expr.init"
      HiCount="0"/>    
    <prop name="expr.src"      
      content-type="text/x-erlang"><![CDATA[
TooHigh = 100.    
    ]]></prop>  
  </service>   

  <mix name="Mix">     

    <folder name="Sensor">     
      ...   
    </folder>     

    <folder name="Timeslot">      
      ...    
    </folder>     
    
    <folder name="Alert">      
      ...    
    </folder>   
      
  </mix>
</folder>
In this mix we have the following:
  • Three fields sent between the operations
  • The Sequencer service that implements the notify operations
  • The Expr service that's specified as a client or service for multiple operations in this mix
  • Three folders containing operations, each with a notify that can start a new transaction

The Expr service has a state variable HiCount which can be updated by binding a value to NewHiCount in a request or consume operation.

The state variable is initialised to zero.

It also has a static variable TooHigh which is set to 100.
Note: Do not manually type the <![CDATA...]]> delimiters. The Editor automatically renders text content as XML CDATA sections.
<folder name="Sensor">
  
  <service name="SensorReader">
    ...
  </service>

  <notify name="SensorReading"        
    service="Sequencer"        
    clients="SensorReader"
    fields="vibration"/>       
    
  <consume name="RecordVibration"        
    service="Expr"        
    fields="vibration">        
    <prop name="expr.bind.in"          
      Vibration="vibration"/>        
    <prop name="expr.src"          
      content-type="text/x-erlang"><![CDATA[
case Vibration > TooHigh of  
  true ->    
    NewHiCount = HiCount + 1;   

  false ->    
    ok
end.        
    ]]></prop>      
  </consume>    
</folder>
Sensor Reading Transaction

The SensorReader service can be any service that is capable of translating sensor readings into field values, such as a phidget service.

Each time the SensorReading notify operation fires, the vibration field is consumed by the RecordVibration operation. This operation has a property that binds the vibration field to the Vibration variable.

In the expressions list, this variable is compared to the static variable TooHigh.

If the expression evaluates to true, the HiCount state variable is incremented by setting the value of NewHiCount.

This means that the HiCount variable gets bigger each time there is too much vibration.

<folder name="Timeslot">      
      <notify name="Tick"        
        clients="Expr"        
        service="Sequencer"        
        fields="RESET">        
        <prop name="expr.auto"          
          interval="10m"/>      
      </notify>       

      <consume name="Reset"        
        service="Expr"        
        fields="RESET">        
        <prop name="expr.src"          
          content-type="text/x-erlang"><![CDATA[
NewHiCount = 0.        
        ]]></prop>      
      </consume>    
    </folder>
Timeslot Reset Transaction

Every 10 minutes, the Timeslot notify operation is triggered by the Expr service. This operation has no expr.src property so it always fires.

The Reset consume operation simply sets the HiCount value back to zero, by setting the NewHiCount variable.

<folder name="Alert">     

  <notify name="VibrationAlert"      
    service="Sequencer"      
    clients="Expr"      
    fields="VIBRATION_ALERT">      
    <prop name="expr.state"        
      HiCount="OldHiCount"/>      
    <prop name="expr.src"        
      content-type="text/x-erlang"><![CDATA[
HiCount =:= 3.      
     ]]></prop>    
  </notify>  
</folder>
Vibration Alert Transaction

The VibrationAlert notify operation is triggered on a state change of the HiCount state variable.

The last (and only) expression in the expr.src property returns true if the HiCount value equals 3.

Since this can only occur once in any timeslot, either zero or one vibration alert notify operation is fired in each 10 minute slot.