<!-- defines a rules to snooze and stop an active alarm on a doubletap or turndown event -->
<script xmlns="http://www.nokia.com/ns/cep/script/1.0/" xmlns:cep="http://www.nokia.com/ns/cep/1.0/">

    <!-- active sensor channels when alarm expires -->
    <if>
        <and>
            <!-- must have sensor interaction active... -->
            <equals>
                <contextRef source='Sensor' type='Setting.Active'/>
                <string>On</string>
            </equals>
            <!-- ...and an active alarm -->
            <contextUpdated>
                <contextRef source='AlarmUI' type='State'/>
            </contextUpdated>
            <notEqual>
                <contextRef source='AlarmUI' type='State'/>
                <string>Inactive</string>
            </notEqual>
        </and>
        <actions>
            <!-- activate doubletap channel -->
            <publishContext>
                <contextRef source='Sensor' type='Event' value='DoubleTap.Activate'/>
            </publishContext>

            <!-- activate orientation channel -->
            <publishContext>
                <contextRef source='Sensor' type='Event' value='Orientation.Activate'/>
            </publishContext>
        </actions>
    </if>

    <!-- deactivate sensor channels when alarm is dismissed -->
    <elseIf>
        <and>
            <!-- must have sensor interaction active... -->
            <equals>
                <contextRef source='Sensor' type='Setting.Active'/>
                <string>On</string>
            </equals>
            <!-- ...and an alarm that was just deactivated -->
            <contextUpdated>
                <contextRef source='AlarmUI' type='State'/>
            </contextUpdated>
            <equals>
                <contextRef source='AlarmUI' type='State'/>
                <string>Inactive</string>
            </equals>
        </and>
        <actions>
            <!-- deactivate doubletap channel -->
            <publishContext>
                <contextRef source='Sensor' type='Event' value='DoubleTap.Deactivate'/>
            </publishContext>

            <!-- deactivate orientation channel -->
            <publishContext>
                <contextRef source='Sensor' type='Event' value='Orientation.Deactivate'/>
            </publishContext>
        </actions>
    </elseIf>

    <!-- snooze active alarm on a doubletap event -->
    <elseIf>
        <and>
            <!-- must have sensor interaction active -->
            <equals>
                <contextRef source='Sensor' type='Setting.Active'/>
                <string>On</string>
            </equals>
            <!-- check that doubletap interaction is set... -->
            <equals>
                <contextRef source='Sensor' type='Setting.TappingInteraction.SilenceAlarm'/>
                <string>On</string>
            </equals>
            <!-- ...and alarm is active... -->
            <notEqual>
                <contextRef source='AlarmUI' type='State'/>
                <string>Inactive</string>
            </notEqual>
            <!-- ...and we receive a new doubletap event from sensors -->
            <contextUpdated>
                <contextRef source='Sensor' type='Event.DoubleTap'/>
            </contextUpdated>
        </and>
        <actions>
            <!-- snooze the alarm -->
            <AlarmUI.Snooze dummy='dummy'/>

            <!-- request vibra feedback -->
            <Vibra Mode='Start' Delay='0' Repeats='2' RepeatInterval='200' Duration='50' Intensity='50'/>
        </actions>
    </elseIf>

    <!-- snooze an active alarm on a turndown event -->
    <elseIf>
        <and>
            <!-- must have sensor interaction active -->
            <equals>
                <contextRef source='Sensor' type='Setting.Active'/>
                <string>On</string>
            </equals>
            <!-- check that turning interaction is set and... -->
            <equals>
                <contextRef source='Sensor' type='Setting.TurningInteraction.SilenceAlarm'/>
                <string>On</string>
            </equals>
            <!-- ...and alarm is active... -->
            <notEqual>
                <contextRef source='AlarmUI' type='State'/>
                <string>Inactive</string>
            </notEqual>
            <!-- ...and we receive a new orientation event from sensors... -->
            <contextUpdated>
                <contextRef source='Sensor' type='Event.Orientation'/>
            </contextUpdated>
            <!-- ...and orientation event value must be 'display downwards' -->
            <equals>
                <contextRef source='Sensor' type='Event.Orientation'/>
                <string>DisplayDownwards</string>
            </equals>
            <notEqual>
                <contextRef source='Sensor' type='Event.Orientation.Previous'/>
                <string>Undefined</string>
            </notEqual>
        </and>
        <actions>
            <!-- snooze the alarm -->
            <AlarmUI.Snooze dummy='dummy'/>

            <!-- request vibra feedback -->
            <Vibra Mode='Start' Delay='0' Repeats='2' RepeatInterval='200' Duration='50' Intensity='50'/>
        </actions>
    </elseIf>

    <!-- stop an active alarm on a 'Stop' command -->
    <elseIf>
        <and>
            <!-- alarm is active... -->
            <notEqual>
                <contextRef source='AlarmUI' type='State'/>
                <string>Inactive</string>
            </notEqual>
            <!-- ...and we got a new stop command -->
            <contextUpdated>
                <contextRef source='AlarmUI' type='Command'/>
            </contextUpdated>
            <equals>
                <contextRef source='AlarmUI' type='Command'/>
                <string>Stop</string>
            </equals>
        </and>
        <actions>
            <!-- stop the alarm -->
            <AlarmUI.Stop dummy='dummy'/>

            <!-- request vibra feedback -->
            <Vibra Mode='Start' Delay='0' Repeats='2' RepeatInterval='200' Duration='50' Intensity='50'/>
        </actions>
    </elseIf>

    <!-- snooze an active alarm on a 'Snooze' command -->
    <elseIf>
        <and>
            <!-- alarm is active... -->
            <notEqual>
                <contextRef source='AlarmUI' type='State'/>
                <string>Inactive</string>
            </notEqual>
            <!-- ...and we got a new stop command -->
            <contextUpdated>
                <contextRef source='AlarmUI' type='Command'/>
            </contextUpdated>
            <equals>
                <contextRef source='AlarmUI' type='Command'/>
                <string>Snooze</string>
            </equals>
        </and>
        <actions>
            <!-- snooze the alarm -->
            <AlarmUI.Snooze dummy='dummy'/>

            <!-- request vibra feedback -->
            <Vibra Mode='Start' Delay='0' Repeats='2' RepeatInterval='200' Duration='50' Intensity='50'/>
        </actions>
    </elseIf>

</script>
