Subversion Repositories HomeAutomation

Rev

Rev 1646 | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
1593 runge 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
 
1646 runge 23
#include <boost/date_time/posix_time/posix_time.hpp>
24
 
1593 runge 25
namespace atom {
26
namespace timer {
27
 
1768 runge 28
Timer::Timer(boost::asio::io_service& io_service, TimerId id, unsigned int timeout, bool repeat) : instance_(io_service), LOG("timer::Timer")
1593 runge 29
{
30
    this->id_ = id;
31
    this->timeout_ = timeout;
32
    this->repeat_ = repeat;
1646 runge 33
    this->time_ = "";
1593 runge 34
}
35
 
1768 runge 36
Timer::Timer(boost::asio::io_service& io_service, TimerId id, std::string time) : instance_(io_service), LOG("timer::Timer")
1646 runge 37
{
38
    this->id_ = id;
39
    this->timeout_ = 0;
40
    this->repeat_ = false;
41
    this->time_ = time;
42
}
43
 
1593 runge 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
{
1646 runge 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
    }
1593 runge 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
{
1768 runge 79
    if (error.value() != 0)
80
    {
81
        return;
82
    }
83
 
1593 runge 84
    if (this->repeat_)
85
    {
86
        this->Start();
87
    }
88
 
89
    this->signal_on_timeout_(this->id_);
90
}
91
 
92
TimerId Timer::GetId()
93
{
94
    return this->id_;
95
}
96
 
97
bool Timer::GetRepeat()
98
{
99
    return this->repeat_;
100
}
101
 
102
}; // namespace timer
103
}; // namespace atom