#include "oneitem.h"
#include "JobModule.h"
#include "RunnableModule.h"
#include "LibraryLoader.h"
#include <QtCore/QDebug>
#include <typeinfo>
#include <type_traits>

oneitem::oneitem()
{
    m_pDataConsumer = nullptr;
    m_pModule = nullptr;
}

void oneitem::OnData(std::string name,QVariant val)
{
    // 这是从里往外传的数据(设备控制命令)
    //源头是模块,我倒一步手，传递给maincontrol里，通过maincontrol写redis
    if( m_pDataConsumer != nullptr )
    {
        m_pDataConsumer->OnData(name,val);
    }
}

void oneitem::OnSubData(std::string name,std::string val)
{
    // 从td读出来的测量数据，通过maincontrol给到这里
    // 通过这里在给到模块里
    if( m_pModule != nullptr )
    {
        m_pModule->OnSubData(name,val);
    }
}

void oneitem::regConsumer(DataConsumer* pDC)
{
    m_pDataConsumer = pDC;
}

void oneitem::createmodule(const ModuleInfo& mi)
{
    std::string assemblyName = mi.AssemblyName;
    std::string className = mi.ClassName;

    BaseModule* pModule = LibraryLoader::load<BaseModule>(assemblyName);
    if( pModule == nullptr )
    {
        qCritical() << __FILE__ << __LINE__ << " load " << assemblyName.c_str() << " failed.";
        return;
    }

    m_pModule = pModule;
    m_pModule->Setup(mi);
    m_pModule->regConsumer(this);
    if( m_pModule->isInheritedFrom("RunnableModule"))
    {
        RunnableModule* pRunable = dynamic_cast<RunnableModule*>(m_pModule);
        pRunable->Run();
    }
    if( m_pModule->isInheritedFrom("JobModule"))
    {
        JobModule* pJob = dynamic_cast<JobModule*>(m_pModule);
        pJob->Process(QObject());
    }
}
