#include "Sprinkler.h"


CSprinkler::CSprinkler(CSprinklerGui *GUI)
{
    mNetworkInterface = new CSprinklerMasterCtrlInterface(this);
    mGui = GUI;

    CSprinklerDevice *Sprinkler = new CSprinklerDevice();
    mSprinklers.append(Sprinkler);
}

CSprinkler::~CSprinkler()
{
    for(int i = 0; i < mSprinklers.size(); i++)
    {
        delete mSprinklers[i];
    }
    mSprinklers.clear();
}

CSprinklerDevice* CSprinkler::FindSprinkler(int DeviceAddress)
{
    for(int i = 0; i < mSprinklers.size(); i++)
    {
        if(mSprinklers.at(i)->mDeviceAddress == DeviceAddress)
        {
            return mSprinklers[i];
        }
    }
    return 0;
}

int CSprinkler::SetSprinklerState(int DeviceAddress, unsigned char State)
{
    CSprinklerDevice *Dev = FindSprinkler(DeviceAddress);
    if(Dev != 0)
    {
        Dev->SetSprinklerState((SprinklerState)State);
    }
    else
    {
        return 0;
    }

    return 1;
}

int CSprinkler::SetSprinklerFlow(int DeviceAddress, unsigned short Flow)
{
    CSprinklerDevice *Dev = FindSprinkler(DeviceAddress);
    if(Dev != 0)
    {
        Dev->SetFlow(Flow);
    }
    else
    {
        return 0;
    }

    return 1;
}
