Subversion Repositories HomeAutomation

Rev

Rev 1593 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 1593 Rev 1646
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
 
-
 
23
#include <boost/date_time/posix_time/posix_time.hpp>
22
 
24
 
23
namespace atom {
25
namespace atom {
24
namespace timer {
26
namespace timer {
25
 
27
 
26
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)
27
{
29
{
28
    this->id_ = id;
30
    this->id_ = id;
29
    this->timeout_ = timeout;
31
    this->timeout_ = timeout;
30
    this->repeat_ = repeat;
32
    this->repeat_ = repeat;
-
 
33
    this->time_ = "";
31
}
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
}
32
 
43
 
33
Timer::~Timer()
44
Timer::~Timer()
34
{
45
{
35
    this->Cancel();
46
    this->Cancel();
36
}
47
}
37
 
48
 
38
void Timer::ConnectSlots(const SignalOnTimeout::slot_type& slot_on_timeout)
49
void Timer::ConnectSlots(const SignalOnTimeout::slot_type& slot_on_timeout)
39
{
50
{
40
    this->signal_on_timeout_.connect(slot_on_timeout);
51
    this->signal_on_timeout_.connect(slot_on_timeout);
41
}
52
}
42
 
53
 
43
void Timer::Start()
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
44
{
63
    {
45
    this->instance_.expires_from_now(boost::posix_time::millisec(this->timeout_));
64
        this->instance_.expires_from_now(boost::posix_time::millisec(this->timeout_));
-
 
65
    }
46
   
66
   
47
    this->instance_.async_wait(boost::bind(&Timer::TimeoutHandler,
67
    this->instance_.async_wait(boost::bind(&Timer::TimeoutHandler,
48
                                           this,
68
                                           this,
49
                                           boost::asio::placeholders::error));
69
                                           boost::asio::placeholders::error));
50
}
70
}
51
 
71
 
52
void Timer::Cancel()
72
void Timer::Cancel()
53
{
73
{
54
    this->instance_.cancel();
74
    this->instance_.cancel();
55
}
75
}
56
 
76
 
57
void Timer::TimeoutHandler(const boost::system::error_code& error)
77
void Timer::TimeoutHandler(const boost::system::error_code& error)
58
{
78
{
59
    if (this->repeat_)
79
    if (this->repeat_)
60
    {
80
    {
61
        this->Start();
81
        this->Start();
62
    }
82
    }
63
   
83
   
64
    this->signal_on_timeout_(this->id_);
84
    this->signal_on_timeout_(this->id_);
65
}
85
}
66
 
86
 
67
TimerId Timer::GetId()
87
TimerId Timer::GetId()
68
{
88
{
69
    return this->id_;
89
    return this->id_;
70
}
90
}
71
 
91
 
72
bool Timer::GetRepeat()
92
bool Timer::GetRepeat()
73
{
93
{
74
    return this->repeat_;
94
    return this->repeat_;
75
}
95
}
76
 
96
 
77
}; // namespace timer
97
}; // namespace timer
78
}; // namespace atom
98
}; // namespace atom
79
 
99