#include "DeviceBase.h"
#include "FrequencyTuning.h"
#include "SignalGenerator.h"
#include "TimeSwitcher.h"
#include "FreqSwitcher.h"
#include "BCodeTerminal.h"
#include "TimeReplicator.h"
#include "FreqReplicator.h"
#include "NtpServer.h"

#include <QDateTime>

DeviceBase::DeviceBase(QObject *parent) : QObject(parent)
{

}

QString DeviceBase::getDevCode()
{
    return this->devCode;
}
QString DeviceBase::getDeviceId()
{
    return this->deviceId;
}
void DeviceBase::setComName(QString comName)
{
    this->comName = comName;
}
void DeviceBase::setBaudRate(int baudRate)
{
    this->baudRate = baudRate;
}
void DeviceBase::setDevCode(QString devCode)
{
    this->devCode = devCode;
}
void DeviceBase::setDeviceId(QString deviceId)
{
    this->deviceId = deviceId;
}

DeviceBase * DeviceBase::deviceFactory(QString deviceType, QObject * parent)
{
    DeviceBase * base = 0;
    if (deviceType == "03")
    {
        base = new FrequencyTuning(parent);
    } else if (deviceType == "04")
    {
        base = new SignalGenerator(parent);
    } else if (deviceType == "05")
    {
        base = new TimeSwitcher(parent);
    } else if (deviceType == "06")
    {
        base = new FreqSwitcher(parent);
    } else if (deviceType == "07")
    {
        base = new BCodeTerminal(parent);
    } else if (deviceType == "09")
    {
        base = new TimeReplicator(parent);
    } else if (deviceType == "10")
    {
        base = new FreqReplicator(parent);
    } else if (deviceType == "11")
    {
        base = new NtpServer(parent);
    }

    return base;
}

bool DeviceBase::isSerialOpen()
{
    return this->serialUtil.isOpen();
}

void DeviceBase::initSerialPort()
{
    QStringList comNameList = this->comName.split(",");
    this->serialUtil.openSerialPort(comNameList.at(0), this->baudRate);
    if (comNameList.size() == 2)
    {
        this->serialUtilB.openSerialPort(comNameList.at(1), this->baudRate);
    }
}
