<?xml version="1.0" encoding="UTF-8"?>
<Mission xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns="Tethys"
           xmlns:Units="Tethys/Units"
           xsi:schemaLocation="Tethys http://okeanids.mbari.org/tethys/Xml/Tethys.xsd
                               Tethys/Units http://okeanids.mbari.org/tethys/Xml/Units.xsd"
       Id="TestAckMessage">

    <Description>
        Mission to test DAT message acknowledgement.

        Missing standard envelopes -- lab use only.

        This script can take two roles, Sender==True and Sender==False. In the Sender==True role,
        the script sends "_.relievedOfDuty" to an acoustic recipient and waits for the ACK. In
        the Sender==False role, the script monitors for "_.relievedOfDuty" and logs when it is
        recieved.
        
        You must hard-code in the recipient modem address in the SendData element.
    </Description>

    <DefineArg Name="MissionTimeout"><Description>
        Maximum length of mission
    </Description><Units:minute/><Value>30</Value></DefineArg>

    <DefineArg Name="Sender"><Description>
        Whether this vehicle should send the original message or simply acknowledge it.
    </Description><Units:bool/><Value>1</Value></DefineArg>

<!-- Mission args -->
    <DefineOutput Name="AckReceived"><Description>
        Mission variable (don't change). An ACK flag with a timestamp used to detect
        when new ACK signals have arrived.
    </Description><False/></DefineOutput>

    <DefineOutput Name="RelievedOfDuty"><Description>
        Mission variable (don't change). Whether the relief vehicle has showed up
        and communicated its intent to relieve this sampling vehicle.
    </Description><Units:count/><Value>0</Value></DefineOutput>

    <DefineOutput Name="TrueVar"><Description>
        A True boolean, defined as an arg because you can't directly place values in 
        a call to SendData.
    </Description><True/></DefineOutput>

<!-- Monitor for the relievedOfDuty message (only relevant to Sender==False) -->
    <Aggregate Id="RespondToAcousticGoHomeMsg">

        <When>
            <Elapsed><CustomUri Uri="_.relievedOfDuty"/></Elapsed>
            <Lt><Elapsed><Arg Name="RelievedOfDuty"/></Elapsed></Lt>
        </When>

        <Assign><Sequence/><Arg Name="RelievedOfDuty"/><CustomUri Uri="_.relievedOfDuty"/></Assign>
        <Syslog Severity="Important">Recieved relievedOfDuty transmission from relief vehicle.</Syslog>

    </Aggregate>

    <Aggregate Id="RespondToACKMsg">

<!-- Activate on the rising edge of DAT.msgAcknowledge, if Sender==True -->
        <When>
            <Arg Name="Sender"/><Eq><True/></Eq><And>
                <Elapsed><Sensor:DAT.msgAcknowledged/></Elapsed><Lt><Elapsed><Arg Name="AckReceived"/></Elapsed></Lt>
                <And><Sensor:DAT.msgAcknowledged/><Eq><True/></Eq></And>
            </And>
        </When>

        <Syslog Severity="Important">Recieved ACK. Hooray!</Syslog>
        <Assign><Sequence/><Arg Name="AckReceived"/><Sensor:DAT.msgAcknowledged/></Assign>

    </Aggregate>


    <Aggregate Id="SendAcousticMsg">

<!-- Activate sequentially if Sender==True -->
        <Sequence/>

        <Break>
            <Arg Name="Sender"/><Eq><False/></Eq>
        </Break>

        <SendData Service="Direct" Destination="modem:10:_.relievedOfDuty">
            <Arg Name="TrueVar"/>
        </SendData>
        <Syslog Severity="Important">Sent _.relievedOfDuty Message.</Syslog>

    </Aggregate>

    <Aggregate Id="ActivateCamera">

        <When>
            <Called/>
        </When>

        <Syslog Severity="Important">Lights, Camera on at range: <Estimation:TrackAcousticContact.range_to_contact/><Units:meter/>.</Syslog>

        <ReadData Strategy="MinError">
            <Sensor:PowerOnly.samplePowerOnly/>
        </ReadData>
        
        <ReadData Strategy="MinError">
            <Sensor:NanoDVR.sampleNanoDVR/>
        </ReadData>

        <Guidance:Wait>
            <Sequence/>
            <Setting><Guidance:Wait.duration/><Units:second/><Value>0.5</Value></Setting>
        </Guidance:Wait>

    </Aggregate>

    <Call RefId="ActivateCamera"/>
    
    <Guidance:Wait>
        <Sequence/>
        <Setting><Guidance:Wait.duration/><Arg Name="MissionTimeout"/></Setting>
    </Guidance:Wait>

</Mission>
