Subversion Repositories HomeAutomation

Rev

Rev 1593 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  1. /*
  2.  *
  3.  *  Copyright (C) 2010  Mattias Runge
  4.  *
  5.  *  This program is free software; you can redistribute it and/or modify
  6.  *  it under the terms of the GNU General Public License as published by
  7.  *  the Free Software Foundation; either version 2 of the License, or
  8.  *  (at your option) any later version.
  9.  *
  10.  *  This program is distributed in the hope that it will be useful,
  11.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  *  GNU General Public License for more details.
  14.  *
  15.  *  You should have received a copy of the GNU General Public License along
  16.  *  with this program; if not, write to the Free Software Foundation, Inc.,
  17.  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18.  *
  19.  */
  20.  
  21. #include "Timer.h"
  22.  
  23. #include <boost/date_time/posix_time/posix_time.hpp>
  24.  
  25. namespace atom {
  26. namespace timer {
  27.  
  28. Timer::Timer(boost::asio::io_service& io_service, TimerId id, unsigned int timeout, bool repeat) : instance_(io_service)
  29. {
  30.     this->id_ = id;
  31.     this->timeout_ = timeout;
  32.     this->repeat_ = repeat;
  33.     this->time_ = "";
  34. }
  35.  
  36. Timer::Timer(boost::asio::io_service& io_service, TimerId id, std::string time) : instance_(io_service)
  37. {
  38.     this->id_ = id;
  39.     this->timeout_ = 0;
  40.     this->repeat_ = false;
  41.     this->time_ = time;
  42. }
  43.  
  44. Timer::~Timer()
  45. {
  46.     this->Cancel();
  47. }
  48.  
  49. void Timer::ConnectSlots(const SignalOnTimeout::slot_type& slot_on_timeout)
  50. {
  51.     this->signal_on_timeout_.connect(slot_on_timeout);
  52. }
  53.  
  54. void Timer::Start()
  55. {
  56.     if (this->timeout_ == 0)
  57.     {
  58.         boost::posix_time::ptime now = boost::posix_time::second_clock::local_time();
  59.        
  60.         this->instance_.expires_from_now(boost::posix_time::time_from_string(this->time_) - now);
  61.     }
  62.     else
  63.     {
  64.         this->instance_.expires_from_now(boost::posix_time::millisec(this->timeout_));
  65.     }
  66.    
  67.     this->instance_.async_wait(boost::bind(&Timer::TimeoutHandler,
  68.                                            this,
  69.                                            boost::asio::placeholders::error));
  70. }
  71.  
  72. void Timer::Cancel()
  73. {
  74.     this->instance_.cancel();
  75. }
  76.  
  77. void Timer::TimeoutHandler(const boost::system::error_code& error)
  78. {
  79.     if (this->repeat_)
  80.     {
  81.         this->Start();
  82.     }
  83.    
  84.     this->signal_on_timeout_(this->id_);
  85. }
  86.  
  87. TimerId Timer::GetId()
  88. {
  89.     return this->id_;
  90. }
  91.  
  92. bool Timer::GetRepeat()
  93. {
  94.     return this->repeat_;
  95. }
  96.  
  97. }; // namespace timer
  98. }; // namespace atom
  99.