// -*- C++ -*-
/*!
 * @file  RHCtrl.cpp
 * @brief Generalization Control of Reference Hardware
 * @date $Date$
 *
 * $Id$
 */

#include "RHCtrl.h"

// Module specification
// <rtc-template block="module_spec">
static const char* rhctrl_spec[] =
  {
    "implementation_id", "RHCtrl",
    "type_name",         "RHCtrl",
    "description",       "Generalization Control of Reference Hardware",
    "version",           "1.0.0",
    "vendor",            "RTC-Center ",
    "category",          "Controller",
    "activity_type",     "PERIODIC",
    "kind",              "DataFlowComponent",
    "max_instance",      "1",
    "language",          "C++",
    "lang_type",         "compile",
    ""
  };
// </rtc-template>

/*!
 * @brief constructor
 * @param manager Maneger Object
 */
RHCtrl::RHCtrl(RTC::Manager* manager)
    // <rtc-template block="initializer">
  : RTC::DataFlowComponentBase(manager),
    m_RefHardCtrlProvPort("RefHardCtrlProv"),
    m_ControlTerminalConsPort("ControlTerminalCons"),
    m_BatteryCtrlProvPort("BatteryCtrlProv")

    // </rtc-template>
{
}

/*!
 * @brief destructor
 */
RHCtrl::~RHCtrl()
{
}



RTC::ReturnCode_t RHCtrl::onInitialize()
{
  // Registration: InPort/OutPort/Service
  // <rtc-template block="registration">
  // Set InPort buffers
  
  // Set OutPort buffer
  
  // Set service provider to Ports
  m_RefHardCtrlProvPort.registerProvider("RefHardCtrl", "RefHardCtrl", m_RefHardCtrl);
  m_BatteryCtrlProvPort.registerProvider("BatteryAlert", "BatteryAlert", m_BatteryCtrl);
  
  // Set service consumers to Ports
  m_ControlTerminalConsPort.registerConsumer("StateChangeFromRH", "StateChangeFromRH", m_ControlTerminal);
  
  // Set CORBA Service Ports
  addPort(m_RefHardCtrlProvPort);
  addPort(m_ControlTerminalConsPort);
  addPort(m_BatteryCtrlProvPort);
 
  // -- The port is passed to the Consumer -- //
  m_RefHardCtrl.setConsumers(&m_ControlTerminal);

  // </rtc-template>

  return RTC::RTC_OK;
}

/*
RTC::ReturnCode_t RHCtrl::onFinalize()
{
  return RTC::RTC_OK;
}
*/

/*
RTC::ReturnCode_t RHCtrl::onStartup(RTC::UniqueId ec_id)
{
  return RTC::RTC_OK;
}
*/

/*
RTC::ReturnCode_t RHCtrl::onShutdown(RTC::UniqueId ec_id)
{
  return RTC::RTC_OK;
}
*/

RTC::ReturnCode_t RHCtrl::onActivated(RTC::UniqueId ec_id)
{
  iamhungry               = 0;
  old_iamhungry           = 0;
  alert_cnt               = 0;
  m_BatteryCtrl.iamhungry = 0;
  m_RefHardCtrl.iamhungry = 0;

  return RTC::RTC_OK;
}

/*
RTC::ReturnCode_t RHCtrl::onDeactivated(RTC::UniqueId ec_id)
{
  return RTC::RTC_OK;
}
*/

RTC::ReturnCode_t RHCtrl::onExecute(RTC::UniqueId ec_id)
{
  int ret;

  iamhungry              =m_BatteryCtrl.iamhungry;
  m_RefHardCtrl.iamhungry=m_BatteryCtrl.iamhungry;

  if( iamhungry == 1 ){
    if(alert_cnt==0){
      char cmd[64];
      sprintf( cmd, "python ../scripts/RHBatteryAlert.py");
      ret = system(cmd);
      alert_cnt++;
    } else if ( alert_cnt >= 20000 ){
      alert_cnt=0;
    } else {
      alert_cnt++;
    }
  }
  return RTC::RTC_OK;
}

/*
RTC::ReturnCode_t RHCtrl::onAborting(RTC::UniqueId ec_id)
{
  return RTC::RTC_OK;
}
*/

/*
RTC::ReturnCode_t RHCtrl::onError(RTC::UniqueId ec_id)
{
  return RTC::RTC_OK;
}
*/

/*
RTC::ReturnCode_t RHCtrl::onReset(RTC::UniqueId ec_id)
{
  return RTC::RTC_OK;
}
*/

/*
RTC::ReturnCode_t RHCtrl::onStateUpdate(RTC::UniqueId ec_id)
{
  return RTC::RTC_OK;
}
*/

/*
RTC::ReturnCode_t RHCtrl::onRateChanged(RTC::UniqueId ec_id)
{
  return RTC::RTC_OK;
}
*/



extern "C"
{
 
  void RHCtrlInit(RTC::Manager* manager)
  {
    coil::Properties profile(rhctrl_spec);
    manager->registerFactory(profile,
                             RTC::Create<RHCtrl>,
                             RTC::Delete<RHCtrl>);
  }
  
};


