Subversion Repositories DevTools

Rev

Blame | Last modification | View Log | RSS feed

//---------------------------------------------------------------------------

#include <vcl.h>
#include <FileCtrl.hpp>
#pragma hdrstop

#include "UdTransfer.h"
#include "UdTransferManager.h"

namespace
{
    const char * REG_BASE_KEY       =   "\\Software\\ERG\\TxnTestManager\\UD Transfer";
    const char * REG_DURATION_TYPE  =   "DurationType";
    const char * REG_DURATION       =   "Duration";
    const char * REG_FREQUENCY_TYPE =   "FrequencyType";
    const char * REG_FREQUENCY      =   "Frequency";
    const char * REG_RECURRING      =   "Recurring";
    const char * REG_RAMP_TEST_TYPE =   "RampTestType";
    const char * REG_RAMP_GRADIENT  =   "RampGradient";
    const char * REG_SOURCE_PATH    =   "SourcePath";
    const char * REG_DESTINATION    =   "Destination";

    const char * REG_RENAME         =   "RenameSentFiles";
    const char * REG_IGNORESENT     =   "IgnoreSentFiles";
    const char * REG_MAXDEVICES     =   "MaxDeviceSimulators";
    const char * REG_DEVICEID       =   "StartingDeviceId";
    const char * REG_DEVICETYPE     =   "DeviceType";
    const char * REG_CONNECTTIMEOUT =   "DeviceConnectTimeout";

    const char * SENT_STRING        =   "sent_";

    static int MAX_DEVICES = 400;

    static Variant vInteger(1);
    static Variant vDouble(double(1));
    static Variant vString(AnsiString("1"));
}

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "AdvDirectoryEdit"
#pragma link "AdvEdBtn"
#pragma link "AdvEdit"
#pragma link "AdvPageControl"
#pragma resource "*.dfm"
TUdTransferForm *UdTransferForm;
//---------------------------------------------------------------------------
__fastcall TUdTransferForm::TUdTransferForm(TComponent* Owner)
    : TForm(Owner), m_Registry(NULL), m_manager(NULL)
{
        m_Registry = new TRegistry;
        m_Registry->RootKey = HKEY_CURRENT_USER;
        if (! m_Registry->OpenKey(REG_BASE_KEY, true) )
        {
                m_Registry->CloseKey();
                delete m_Registry;
                m_Registry = NULL;
        }
}
//---------------------------------------------------------------------------

__fastcall TUdTransferForm::~TUdTransferForm()
{
    if( m_Registry != NULL ) m_Registry->CloseKey();
    delete m_Registry;
    m_Registry = NULL;
    m_manager = NULL;
}
//---------------------------------------------------------------------------

int __fastcall TUdTransferForm::ShowForm( UdTransferManager * manager )
{
    m_manager = manager;
    InitialiseValuesFromRegistry();

    return ShowModal();
}
//---------------------------------------------------------------------------

void __fastcall TUdTransferForm::InitialiseValuesFromRegistry()
{
    int duration = 0;
    int durationType = 1;
    int frequency = 0;
    int frequencyType = 1;
    int rampTestType = 0;
    double rampGradient = 1;
    bool recurringUpload = false;
    AnsiString sourcePath = "";
    AnsiString destination = "";
    bool renameSent = false;
    bool ignoreSent = false;
    int maxDevices = 100;
    int deviceId = 1;
    AnsiString deviceType = "SIM";
    int connectTimeOut = 5; 

    if( m_Registry != NULL )
    {
        try {
            // Previous transfer settings

            if( m_Registry->ValueExists(REG_RECURRING) )
            {
                recurringUpload = m_Registry->ReadBool(REG_RECURRING);
            }
            if( m_Registry->ValueExists(REG_DURATION_TYPE) &&
                m_Registry->ValueExists(REG_DURATION) )
            {
                duration        = m_Registry->ReadInteger(REG_DURATION);
                durationType    = m_Registry->ReadInteger(REG_DURATION_TYPE);
            }
            if( m_Registry->ValueExists(REG_FREQUENCY_TYPE) &&
                m_Registry->ValueExists(REG_FREQUENCY) )
            {
                frequency       = m_Registry->ReadInteger(REG_FREQUENCY);
                frequencyType   = m_Registry->ReadInteger(REG_FREQUENCY_TYPE);
            }
            if( m_Registry->ValueExists(REG_RAMP_TEST_TYPE) )
            {
                rampTestType    = m_Registry->ReadInteger(REG_RAMP_TEST_TYPE);
            }
            if( m_Registry->ValueExists(REG_SOURCE_PATH) )
            {
                sourcePath      = m_Registry->ReadString(REG_SOURCE_PATH);
            }
            if( m_Registry->ValueExists(REG_DESTINATION) )
            {
                destination     = m_Registry->ReadString(REG_DESTINATION);
            }
            if( m_Registry->ValueExists(REG_RAMP_GRADIENT) )
            {
                rampGradient    = m_Registry->ReadFloat(REG_RAMP_GRADIENT);
            }

            // General settings

            if( m_Registry->ValueExists(REG_RENAME) )
            {
                renameSent      = m_Registry->ReadBool(REG_RENAME);
            }
            if( m_Registry->ValueExists(REG_IGNORESENT) )
            {
                ignoreSent      = m_Registry->ReadBool(REG_IGNORESENT);
            }
            if( m_Registry->ValueExists(REG_MAXDEVICES) )
            {
                maxDevices      = m_Registry->ReadInteger(REG_MAXDEVICES);
            }
            if( m_Registry->ValueExists(REG_DEVICEID) )
            {
                deviceId        = m_Registry->ReadInteger(REG_DEVICEID);
            }
            if( m_Registry->ValueExists(REG_DEVICETYPE) )
            {
                deviceType      = m_Registry->ReadString(REG_DEVICETYPE);
            }
            if( m_Registry->ValueExists(REG_CONNECTTIMEOUT) )
            {
                connectTimeOut  = m_Registry->ReadInteger(REG_CONNECTTIMEOUT);
            }
        }
        catch( EReadError & e ) { } // Ignore
        catch( ERegistryException & e ) { }
    }

    // Set default or loaded values

    cbRecurring->Checked        = recurringUpload;
    edUploadDuration->Text      = IntToStr(duration);
    coDurationType->ItemIndex   = durationType;
    edUploadFrequency->Text     = IntToStr(frequency);
    coFrequencyType->ItemIndex  = frequencyType;
    coRampTestType->ItemIndex   = rampTestType;
    edGradient->Text            = FloatToStr(rampGradient);
    edSourcePath->Text          = sourcePath;
    edDestination->Text         = destination;

    dtScheduleDate->Date        = TDateTime::CurrentDate();
    dtScheduleTime->Time        = TDateTime::CurrentTime();

    cbRename->Checked           = renameSent;
    cbIgnoreSent->Checked       = ignoreSent;
    coMaxDevices->Text          = maxDevices;
    edStartingDeviceId->Text    = IntToHex( deviceId, 8 );    
    edDeviceType->Text          = deviceType.SubString(0, 6);
    edDeviceTimeout->Text       = IntToStr(connectTimeOut);

    coRampTestTypeChange( NULL );
    rbStartNowClick( NULL );
    SetDefaultBatchSize();
}
//---------------------------------------------------------------------------

void __fastcall TUdTransferForm::coRampTestTypeChange(TObject *Sender)
{
    bool rampEnabled = false;

    lblRatioDescription->Hide();
    lblGradient->Hide();
    edGradient->Hide();

    switch( coRampTestType->ItemIndex )
    {
    case RampTestByDemand:
        lblGradient->Show();
        edGradient->Show();
        rampEnabled = true;
        break;

    case RampTestByRatio:
        lblRatioDescription->Show();
        rampEnabled = true;
        break;
    };

    rbStartNow->Enabled     = rampEnabled;
    rbSchedule->Enabled     = rampEnabled;
    dtScheduleDate->Enabled = rampEnabled && rbSchedule->Checked;
    dtScheduleTime->Enabled = rampEnabled && rbSchedule->Checked;
}
//---------------------------------------------------------------------------

void __fastcall TUdTransferForm::btnOkClick(TObject *Sender)
{
    if( ValidateInputs() )
    {
        // Store current values in the registry

        m_Registry->WriteBool(REG_RECURRING, cbRecurring->Checked);
        m_Registry->WriteInteger(REG_DURATION, edUploadDuration->Text.ToInt());
        m_Registry->WriteInteger(REG_DURATION_TYPE, coDurationType->ItemIndex);
        m_Registry->WriteInteger(REG_FREQUENCY, edUploadFrequency->Text.ToInt());
        m_Registry->WriteInteger(REG_FREQUENCY_TYPE, coFrequencyType->ItemIndex);
        m_Registry->WriteInteger(REG_RAMP_TEST_TYPE, coRampTestType->ItemIndex);
        m_Registry->WriteString(REG_SOURCE_PATH, edSourcePath->Text);
        m_Registry->WriteString(REG_DESTINATION, edDestination->Text);
        if( coRampTestType->ItemIndex == RampTestByDemand )
        {
            m_Registry->WriteFloat(REG_RAMP_GRADIENT, edGradient->Text.ToDouble());
        }

        // Store settings values. No need to validate the majority of these; if they are
        // invalid we will fall back to default values

        try {
            m_Registry->WriteBool(REG_RENAME, cbRename->Checked);
            m_Registry->WriteBool(REG_IGNORESENT, cbIgnoreSent->Checked);
            m_Registry->WriteInteger(REG_MAXDEVICES, coMaxDevices->Text.ToIntDef(100));
            m_Registry->WriteInteger(REG_DEVICEID, StrToInt(AnsiString("$" + edStartingDeviceId->Text)));
            m_Registry->WriteString(REG_DEVICETYPE, edDeviceType->Text);
            m_Registry->WriteInteger(REG_CONNECTTIMEOUT, edDeviceTimeout->Text.ToIntDef(5));
        }
        catch( EConvertError & e ) { }

        // Add the task to the manager

        UdTransferTask task;
        task.batchSize = edBatchSize->Text.ToInt();
        task.duration = edUploadDuration->Text.ToInt();
        task.frequency = edUploadFrequency->Text.ToInt();
        task.recurring = cbRecurring->Checked;
        task.sourcePath = edSourcePath->Text;
        task.destination = edDestination->Text;
        task.rampType = (RampTestType)coRampTestType->ItemIndex;
        task.gradient = 0;
        if( task.rampType == RampTestByDemand )
        {
            task.gradient = edGradient->Text.ToDouble();
        }
        
        switch( coDurationType->ItemIndex )
        {
        case 1: task.duration *= 60;        break;
        case 2: task.duration *= 60 * 60;   break;
        }
        switch( coFrequencyType->ItemIndex )
        {
        case 1: task.frequency *= 60;       break;
        case 2: task.frequency *= 60 * 60;  break;
        }

        TDateTime rampTime = TDateTime::CurrentDateTime();
        if( rbSchedule->Checked )
        {
            rampTime = dtScheduleDate->DateTime + dtScheduleTime->Time;
        }
        task.rampTime = rampTime;

        if( m_manager != NULL )
        {
            m_manager->AddNewTask( task );
        }

        ModalResult = mrOk;
    }
}
//---------------------------------------------------------------------------

void __fastcall TUdTransferForm::btnDefaultClick(TObject *Sender)
{
    SetDefaultBatchSize();
    cbRecurring->Checked = false;
    coRampTestType->ItemIndex = RampTestDisabled;
    edGradient->Text = "1.0";

    rbStartNowClick(NULL);
    coRampTestTypeChange(NULL);
}
//---------------------------------------------------------------------------

void __fastcall TUdTransferForm::SetDefaultBatchSize()
{
    // Get the number of .devud files in the source path and
    // set this as the default batch size.

    if( edSourcePath->Text.IsEmpty() )
    {
        edBatchSize->Text = "0";
        return;
    }

    int count = 0;
    TSearchRec sr;

    if( FindFirst( edSourcePath->Text + "\\*.devud", faReadOnly, sr ) == 0 )
    {
        do
        {
            if( (sr.Attr & faDirectory) == 0 )
            {
                if( !(cbIgnoreSent->Checked && (sr.Name.Pos(SENT_STRING) == 1)) )
                {
                    ++count;
                }
            }
        } while( FindNext(sr) == 0 );
        FindClose(sr);
    }

    edBatchSize->Text = IntToStr(count);
}
//---------------------------------------------------------------------------

bool __fastcall TUdTransferForm::ValidateInputs()
{
    // All required values must have entries

    bool valid = ValidateEditControl( edSourcePath, "Source Path", vString );
    if( valid ) valid = ValidateEditControl( edDestination, "Destination", vString );
    if( valid ) valid = ValidateEditControl( edBatchSize, "Batch Size", vInteger );
    if( valid ) valid = ValidateEditControl( edUploadDuration, "Upload Duration", vInteger );
    if( valid ) valid = ValidateEditControl( edUploadFrequency, "Upload Frequency", vInteger );
    if( coRampTestType->ItemIndex == RampTestByDemand )
    {
        if( valid ) valid = ValidateEditControl( edGradient, "Ramp Gradient", vDouble );
    }

    // Scheduled ramp time must be in the future

    if( valid && rbSchedule->Checked )
    {
        TDateTime time = dtScheduleDate->DateTime + dtScheduleTime->Time;
        if( time < TDateTime::CurrentDateTime() )
        {
            AnsiString error =
                "The scheduled ramp time has already past. Use 'Start Now!' if you wish to start the ramp test immediately.";
            MessageDlg(error, mtError, TMsgDlgButtons() << mbOK, 0);
            valid = false;
        }
    }

    // Source path must be valid and contain some files to transfer

    if( valid && !DirectoryExists(edSourcePath->Text) )
    {
        AnsiString error =
            "The source path '" + edSourcePath->Text + "' does not exist.";
        MessageDlg(error, mtError, TMsgDlgButtons() << mbOK, 0);
        valid = false;
        edSourcePath->SetFocus();
    }

    if( valid && (edBatchSize->Text.ToInt() <= 0) )
    {
        AnsiString error =
            "The specified source path does not contain any UD files to transfer, or the transfer batch\n";
        error += "size has been set to zero. Do you want to continue anyway?";

        if( MessageDlg(error, mtWarning, TMsgDlgButtons() << mbNo << mbYes, 0) == mrNo )
        {
            valid = false;
            edBatchSize->SetFocus();
        }
    }

    // Duration must be greater than zero for ramp up by ratio calculation

    if( (coRampTestType->ItemIndex == RampTestByRatio) &&
        (edUploadDuration->Text.ToInt() == 0) )
    {
        AnsiString error =
            "An upload duration must be specified for Ramp Up by Ratio tests.";
        MessageDlg(error, mtError, TMsgDlgButtons() << mbOK, 0);
        valid = false;
        edUploadDuration->SetFocus();
    }

    // User input max device simulators must be less than the absolute maximum

    if( coMaxDevices->Text.ToIntDef(100) > MAX_DEVICES )
    {
        AnsiString error =
            "The maximum defined device simulators exceeds the known safe value.\n";
        error += "Please select a value between 0 and " + IntToStr(MAX_DEVICES) + ".";
        MessageDlg(error, mtError, TMsgDlgButtons() << mbOK, 0 );

        PageControl->ActivePageIndex = 1;
        coMaxDevices->SetFocus();
        valid = false;
    }

    return valid;
}
//---------------------------------------------------------------------------

bool __fastcall TUdTransferForm::ValidateEditControl( TCustomEdit * ctrl, const AnsiString & name,
                                                      const Variant & dataType )
{
    AnsiString details = "";

    bool valid = !ctrl->Text.IsEmpty();
    if( valid  )
    {
        // Ensure the control has a value of the specified data type

        if( dataType.VType == varInteger )
        {
            try {
                ctrl->Text.ToInt();
            }
            catch( EConvertError & e )
            {
                details = e.Message;
                valid = false;
            }
        }
        else if( dataType.VType == varDouble )
        {
            try {
                ctrl->Text.ToDouble();
            }
            catch( EConvertError & e )
            {
                details = e.Message;
                valid = false;
            }
        }
    }

    if( !valid )
    {
        AnsiString error = "A valid entry has not been provided for " + name + ".";
        if( !details.IsEmpty() ) error += "\n" + details + ".";
        
        MessageDlg(error, mtError, TMsgDlgButtons() << mbOK, 0);

        PageControl->ActivePageIndex = 0;
        ctrl->SetFocus();
    }

    return valid;
}

//---------------------------------------------------------------------------

void __fastcall TUdTransferForm::rbStartNowClick(TObject *Sender)
{
    rbStartNow->Checked = true;
    rbSchedule->Checked = false;

    dtScheduleDate->Enabled = false;
    dtScheduleTime->Enabled = false;
}
//---------------------------------------------------------------------------

void __fastcall TUdTransferForm::rbScheduleClick(TObject *Sender)
{
    rbSchedule->Checked = true;
    rbStartNow->Checked = false;

    dtScheduleDate->Enabled = true;
    dtScheduleTime->Enabled = true;
}
//---------------------------------------------------------------------------

void __fastcall TUdTransferForm::edSourcePathChange(TObject *Sender)
{
    SetDefaultBatchSize();
}
//---------------------------------------------------------------------------

void __fastcall TUdTransferForm::cbIgnoreSentClick(TObject *Sender)
{
    // Reload batch size and this may impact on it
    SetDefaultBatchSize();
}
//---------------------------------------------------------------------------