﻿// CURRENT CURRENT CURRENT CURRENT CURRENT
if (device.action.toLowerCase() == "load")
{
    CreateProgressStep(1, "Transferring");
    CreateProgressStep(1, "Loading");
    CreateProgressStep(1, "Loading");

    CheckProgress();

    // AVX Control will automatically process the zip once it is transferred.
    if (TransferFile("avxcontrol.zip", "avxcontrol.zip", 120))
    {
        CheckProgress();

        if (WaitForState("Running", 60))
        {
            if (SetValue("/configuration/device/fingerprint", device.configid, 30))
            {
                ProgressEnd(true, "Success");
            }
            else
            {
                ProgressEnd(false, "Critical failure trying to set the fingerprint value.");
            }
        }
        else
        {
            ProgressEnd(false, "Critical failure during waiting for state running.");
        }
    }
    else
    {
        ProgressEnd(false, "Critical failure tranferring configuration");
    }
}

else if (device.action.toLowerCase() == "synctx")
{
    CreateProgressStep(1, "Transferring");
    CreateProgressStep(1, "Synchronizing");
    CreateProgressStep(1, "Synchronizing");

    CheckProgress();

    // AVX Control will automatically process the parameter_sync.json once it is transferred.
    if (TransferFile("parameter_sync.json", "parameter_sync.json", 30))
    {
        CheckProgress();

        if (WaitForState("Running", 30))
        {
            ProgressEnd(true, "Success");
        }
        else
        {
            ProgressEnd(false, "Timeout error while loading synchronization data");
        }
    }
    else
    {
        ProgressEnd(false, "Critical failure tranferring synchronization data");
    }
}

// NEXT NEXT NEXT NEXT NEXT
if (device.action.toLowerCase() == "load")
{
    CreateProgressStep(1, "Transferring");
    CreateProgressStep(1, "Loading");
    CreateProgressStep(1, "Loading");

    CheckProgress();

    // AVX Control will automatically process the zip once it is transferred.
    if (TransferFile("avxcontrol.zip", "avxcontrol.zip", 60))
    {
        CheckProgress();

        if (ExecuteCommand("exec {\"path\":\"\/\", \"command\":\"load\", \"arguments\":\"avxcontrol.zip\"}", "@exec", 60))
        {
            CheckProgress();

            if (WaitForState("Running", 60))
            {
                if (SetValue("/configuration/device/fingerprint", device.configid, 60))
                {
                    ProgressEnd(true, "Success");
                }
                else
                {
                    ProgressEnd(false, "Critical failure trying to set the fingerprint value.");
                }
            }
            else
            {
                ProgressEnd(false, "Critical failure during waiting for state running.");
            }
        }
        else
        {
            ProgressEnd(false, "Critical failure loading configuration");
        }    
    }
    else
    {
        ProgressEnd(false, "Critical failure tranferring configuration");
    }
}

else if (device.action.toLowerCase() == "synctx")
{
    CreateProgressStep(1, "Transferring");
    CreateProgressStep(1, "Synchronizing");
    CreateProgressStep(1, "Synchronizing");

    CheckProgress();

    // AVX Control will automatically process the parameter_sync.json once it is transferred.
    if (TransferFile("parameter_sync.json", "parameter_sync.json", 60))
    {
        CheckProgress();

        if (ExecuteCommand("exec {\"path\":\"\/\", \"command\":\"sync\", \"arguments\":\"parameter_sync.json\"}", "@exec", 60))
        {
            CheckProgress();

            if (WaitForState("Running", 60))
            {
                ProgressEnd(true, "Success");
            }
            else
            {
                ProgressEnd(false, "Critical failure during waiting for state running.");
            }
        }
        else
        {
            ProgressEnd(false, "Critical failure loading synchronization data");
        }    
    }
    else
    {
        ProgressEnd(false, "Critical failure tranferring synchronization data");
    }
}
