/*
 * Solenoid.cpp
 *
 *  Created on: Aug 2, 2018
 *      Author: Andreas Schneider, Johannes Wendel, Jonas Zeunert
 */

#include "Solenoid.h"

#include "utility/config.h"

namespace flippR_driver
{
namespace output
{
namespace items
{
namespace detail
{

Solenoid::Solenoid(std::shared_ptr<DriverBoardPinController> pin_controller, const uint8_t & address, const uint8_t & pin_base, const std::string & name, const std::chrono::milliseconds & deactivation_time)
: detail::Item(std::move(name)), DriverBoardItem(pin_controller, address, pin_base), pin_controller(pin_controller), deactivation_time(deactivation_time)
{
  CLOG(INFO, OUTPUT_LOGGER) << "Created solenoid " << name << " with address " << address;
}

void Solenoid::triggerTask()
{
  this->pin_controller->activate(*this);

  std::this_thread::sleep_for(deactivation_time);

  this->pin_controller->deactivate(*this);
}

void Solenoid::trigger()
{
  CLOG(INFO, OUTPUT_LOGGER) << "Trigger Solenoid "  << name << " for " << deactivation_time.count() << "ms";
  this->trigger_task = std::async(std::launch::async, &Solenoid::triggerTask, this);
}

}
}
} /* namespace output */
}
