/* $Header: https://svn.mpia.de/gulli/TwiceAsNice/branch/unstable/ln/laos-drot/test/LaosDrotIntegrationTest_derotate_noRT.cc 14191 2017-03-01 19:21:03Z mathar $
*/

#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE drotIntegrationTest

#include "Fixture.h"
#include <boost/test/unit_test.hpp>
#include <Basda/Mocca/PropertyName.h>
#include <Nice/Time/DateDummy.h>

volatile static int s_niceEnvironment = startSingletons("track_ctrl-svr.cfg");

struct DrotIntegrationFixture : public Fixture
{
        std::string m_moduleNode;

        DrotIntegrationFixture()
        {
            // LOG_LEVEL(Nice::LERROR);
//            LOG_LEVEL(Nice::LWARNING);
//            LOG_LEVEL(Nice::LINFO);
           LOG_LEVEL(Nice::LUSER9);
//            LOG_LEVEL(Nice::LDEBUG);
            U1_LOG("connecting to service ...")
            std::string serviceName = s_app->config("TRACK_CTRL.DROT.SVC")->getAtNode<std::map<std::string, std::string> >("LOAD")["NAME"];
            m_drot = Basda::Basdard::service<Sharknir::DrotServiceWorkerInterface>(serviceName);
            I_LOG("change state to ONLINE")
            m_drot->changeState( Basda::ONLINE );
            I_LOG("wait for state ONLINE")
            m_drot->waitForState(Basda::ONLINE, Nice::Time::seconds(10));
            I_LOG("service is ONLINE")

//            s_app->config("SERVICE_NAME.DEV.1")->setAtNode<double>(Basda::Mocca::MoCon::cfgDatasheetTimePerCycle(), Basda::Constant::TIME_PER_CYCLE);
        }

        virtual ~DrotIntegrationFixture()
        {
            // is empty because the open procedure fails with an exception
        }
};

BOOST_FIXTURE_TEST_SUITE( DrotIntegrationTest_derotate_noRT, DrotIntegrationFixture )

BOOST_AUTO_TEST_CASE(checkSimpleTrajectory)
{
    // derotation starts at -1000 steps with a LBT trajectory with 60 polynomials
    // and a sampling rate of 1 second
    // check the config file lbcs.pangDummy-svc.cfg for these parameters!!
    std::string unit = "STEPS";
    U1_LOG("checkSimpleTrajectory begin");
    int startPosition = -1000;

    Nice::DateDummyPtr niceDateDummy = Nice::DateDummy::init();

    double timelapse = 4;
    niceDateDummy->setTimelapse(timelapse);
    m_worker->setTimelapse(timelapse);
    U1_LOG("checkSimpleTrajectory - 1");
    BOOST_CHECK_NO_THROW(m_drot->moveToHome());
    U1_LOG("checkSimpleTrajectory - 2");
    BOOST_CHECK_NO_THROW(m_drot->moveAbsolute(startPosition, unit));
    U1_LOG("checkSimpleTrajectory - 3");
    BOOST_CHECK_EQUAL(m_drot->getPosition(), startPosition);

    U1_LOG("checkSimpleTrajectory - 4");
    Nice::Date startDate = Nice::Date::now() + Nice::Time::seconds(40);
    BOOST_CHECK_NO_THROW(m_drot->startDerotation(startDate, startPosition, unit));
    U1_LOG("checkSimpleTrajectory - 5");
    double position = m_worker->getDeviceEncoderPosition(unit);
    BOOST_CHECK_EQUAL(position, -1000);
}

BOOST_AUTO_TEST_SUITE_END();
