// $Header: https://oberon.oapd.inaf.it/shins/trunk/shins/src/tiptilt_ctrl/bcu/Interface/Sharknir/Bcu/ThreadedDiagnosticsCollector.cpp 1166 2019-01-25 09:09:58Z depa $
#include "tiptilt_ctrl/bcu/Interface/Sharknir/Bcu/ThreadedDiagnosticsCollector.h"

#include <Nice/Log.h>


namespace Sharknir { namespace Bcu {

SlaveDiagnosticsCollectorThread::SlaveDiagnosticsCollectorThread(
        DiagnosticsDispatcherFactoryPtr factory)
{
    factory_= factory;
    isActive_= false;
}

void SlaveDiagnosticsCollectorThread::run()
{
    while (isActive_)
    {
        try
        {
            if (0 == dispatcher_)
            {
                dispatcher_= factory_->build();
            }
            dispatcher_->dispatchOnce();
        }
        catch (std::exception& e)
        {
            W_LOG(e.what())
            dispatcher_.reset();
        }
    }
    N_LOG("BCU diagnostics collector slave thread for " <<
            (dispatcher_ ? dispatcher_->name() : "NULL") << " is terminating.")
}

void SlaveDiagnosticsCollectorThread::activate()
{
    isActive_= true;
}

void SlaveDiagnosticsCollectorThread::deactivate()
{
    isActive_= false;
}

// ThreadedDiagnosticsCollector::ThreadedDiagnosticsCollector(
//         const DiagnosticsDispatcherFactoryPtr& slopeBcuDiagDispatcherFactory,
//         const DiagnosticsDispatcherFactoryPtr& asmBcuDiagDispatcherFactory) :
//     slopeBcuDiagDispatcherFactory_(slopeBcuDiagDispatcherFactory),
//     asmBcuDiagDispatcherFactory_(asmBcuDiagDispatcherFactory)
// {
// }


/**
 * @brief      Constructs the object. Removed ASM dispatcher, not applicable to SHARK-NIR
 *
 * @param[in]  slopeBcuDiagDispatcherFactory  The slope bcu diagram dispatcher factory
 */
ThreadedDiagnosticsCollector::ThreadedDiagnosticsCollector(
        const DiagnosticsDispatcherFactoryPtr& slopeBcuDiagDispatcherFactory):
    slopeBcuDiagDispatcherFactory_(slopeBcuDiagDispatcherFactory)
{
}

void ThreadedDiagnosticsCollector::start()
{
    N_LOG("Starting BCU diagnostics collector.")

    slopeBcuDiagThread_= new SlaveDiagnosticsCollectorThread(
            slopeBcuDiagDispatcherFactory_);
    slopeBcuDiagThread_->activate();
    slopeBcuDiagCtrl_= slopeBcuDiagThread_->start();

    // asmBcuDiagThread_= new SlaveDiagnosticsCollectorThread(
    //         asmBcuDiagDispatcherFactory_);
    // asmBcuDiagThread_->activate();
    // asmBcuDiagCtrl_= asmBcuDiagThread_->start();

    N_LOG("BCU diagnostics dispatcher collector is started.")
}

void ThreadedDiagnosticsCollector::terminate()
{
    N_LOG("Terminating BCU diagnostics collector.")
    slopeBcuDiagThread_->deactivate();
    // asmBcuDiagThread_->deactivate();
}

void ThreadedDiagnosticsCollector::waitForTermination()
{
    slopeBcuDiagCtrl_.join();
    slopeBcuDiagThread_= 0;
    // asmBcuDiagCtrl_.join();
    // asmBcuDiagThread_= 0;
}

bool ThreadedDiagnosticsCollector::isAlive()
{
    // return (slopeBcuDiagThread_ != 0 && slopeBcuDiagThread_->isAlive()) &&
    //         (asmBcuDiagThread_ != 0 && asmBcuDiagThread_->isAlive());
    return (slopeBcuDiagThread_ != 0 && slopeBcuDiagThread_->isAlive());
}

}
}
