diff --git a/include/f1x/openauto/autoapp/Service/SensorService.hpp b/include/f1x/openauto/autoapp/Service/SensorService.hpp index 5dfd8e5..4343d3e 100644 --- a/include/f1x/openauto/autoapp/Service/SensorService.hpp +++ b/include/f1x/openauto/autoapp/Service/SensorService.hpp @@ -34,6 +34,8 @@ class SensorService: public aasdk::channel::sensor::ISensorServiceChannelEventHa { public: SensorService(boost::asio::io_service& ioService, aasdk::messenger::IMessenger::Pointer messenger); + bool isNight = false; + bool previous = false; void start() override; void stop() override; @@ -46,7 +48,10 @@ private: using std::enable_shared_from_this::shared_from_this; void sendDrivingStatusUnrestricted(); void sendNightData(); + bool is_file_exist(const char *filename); + void nightSensorPolling(); + boost::asio::deadline_timer timer_; boost::asio::io_service::strand strand_; aasdk::channel::sensor::SensorServiceChannel::Pointer channel_; }; diff --git a/src/autoapp/Service/SensorService.cpp b/src/autoapp/Service/SensorService.cpp index 6aa1489..f169f5e 100644 --- a/src/autoapp/Service/SensorService.cpp +++ b/src/autoapp/Service/SensorService.cpp @@ -31,8 +31,9 @@ namespace service { SensorService::SensorService(boost::asio::io_service& ioService, aasdk::messenger::IMessenger::Pointer messenger) - : strand_(ioService) - , channel_(std::make_shared(strand_, std::move(messenger))) + : strand_(ioService), + timer_(ioService), + channel_(std::make_shared(strand_, std::move(messenger))) { } @@ -122,19 +123,47 @@ void SensorService::sendDrivingStatusUnrestricted() void SensorService::sendNightData() { aasdk::proto::messages::SensorEventIndication indication; - if (!std::ifstream("/tmp/night_mode_enabled")) { - OPENAUTO_LOG(error) << "[CS] [SensorService] Mode day triggered"; - indication.add_night_mode()->set_is_night(false); - } else { + + if (SensorService::isNight) { + OPENAUTO_LOG(info) << "[SensorService] Mode night triggered"; indication.add_night_mode()->set_is_night(true); - OPENAUTO_LOG(error) << "[CS] [SensorService] Mode night triggered"; + } else { + OPENAUTO_LOG(info) << "[SensorService] Mode day triggered"; } + //if (!std::ifstream("/tmp/night_mode_enabled")) { + // OPENAUTO_LOG(error) << "[CS] [SensorService] Mode day triggered"; + // indication.add_night_mode()->set_is_night(false); + //} else { + // indication.add_night_mode()->set_is_night(true); + // OPENAUTO_LOG(error) << "[CS] [SensorService] Mode night triggered"; + //} + auto promise = aasdk::channel::SendPromise::defer(strand_); promise->then([]() {}, std::bind(&SensorService::onChannelError, this->shared_from_this(), std::placeholders::_1)); channel_->sendSensorEventIndication(indication, std::move(promise)); } +void SensorService::nightSensorPolling() +{ + strand_.dispatch([this, self = this->shared_from_this()]() { + this->isNight = is_file_exist("/tmp/night_mode_enabled"); + if (this->previous != this->isNight) { + this->previous = this->isNight; + this->sendNightData(); + } + + timer_.expires_from_now(boost::posix_time::seconds(5)); + timer_.async_wait(strand_.wrap(std::bind(&SensorService::nightSensorPolling, this->shared_from_this()))); + }); +} + +bool SensorService::is_file_exist(const char *fileName) +{ + std::ifstream ifile(fileName, std::ios::in); + return ifile.good(); +} + void SensorService::onChannelError(const aasdk::error::Error& e) { OPENAUTO_LOG(error) << "[SensorService] channel error: " << e.what();