/*
 * Detector.cpp
 *
 *  Created on: Apr 5, 2018
 *      Author: Andreas Schneider, Johannes Wendel, Jonas Zeunert
 */

#include "Detector.h"

#include <iostream>

#include "utility/config.h"

namespace flippR_driver
{
namespace input
{
namespace detail
{

Detector::Detector(std::unique_ptr<InputPinController> input_pin_controller, std::vector<std::shared_ptr<DistributingEvent>> events) :
  input_pin_controller(std::move(input_pin_controller)), events(std::move(events)), is_running(true)
{
  this->detect_thread = std::thread(&Detector::detect, this);

  CLOG(DEBUG, INPUT_LOGGER) << "Created Detector";
  CLOG(INFO, INPUT_LOGGER) << "Detector thread running. Occuring input-events should get detected now!";
}

Detector::~Detector()
{
  this->is_running = false;

  this->detect_thread.join();
}

void Detector::detect() const
{
  while(this->is_running)
  {
    check_inputs();
  }
}

void Detector::check_inputs() const
{
  for(auto &event : events)
  {
    input_pin_controller->read_data(event->address) ? event->active() : event->inactive();
  }
}

}
}
}
