Subversion Repositories HomeAutomation

Rev

Rev 1646 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

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