Try day/night code from manvirrr

This commit is contained in:
hawkeyexp 2018-12-05 15:38:26 +01:00
parent 14662664ed
commit cd36c39037
2 changed files with 41 additions and 7 deletions

View File

@ -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<SensorService>::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_;
};

View File

@ -31,8 +31,9 @@ namespace service
{
SensorService::SensorService(boost::asio::io_service& ioService, aasdk::messenger::IMessenger::Pointer messenger)
: strand_(ioService)
, channel_(std::make_shared<aasdk::channel::sensor::SensorServiceChannel>(strand_, std::move(messenger)))
: strand_(ioService),
timer_(ioService),
channel_(std::make_shared<aasdk::channel::sensor::SensorServiceChannel>(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();