Subversion Repositories HomeAutomation

Rev

Rev 1644 | Rev 1647 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
1601 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
 
1604 runge 23
#include <v8-debug.h>
1601 runge 24
#include <stdio.h>
25
 
26
#include <boost/concept_check.hpp>
27
#include <boost/lexical_cast.hpp>
28
 
29
#include "vm/Manager.h"
30
#include "timer/Manager.h"
31
 
32
namespace atom {
33
namespace vm {
34
namespace plugin {
35
 
36
logging::Logger Timer::LOG("vm::plugin::Timer");
37
 
1604 runge 38
Timer::Timers Timer::timers_;
39
 
1625 runge 40
Timer::Timer(boost::asio::io_service& io_service) : Plugin(io_service)
1601 runge 41
{
42
    this->name_ = "timer";
43
 
1646 runge 44
    this->ExportFunction("TimerExport_SetTimer",      Timer::Export_SetTimer);
45
    this->ExportFunction("TimerExport_SetAlarm",      Timer::Export_SetAlarm);
46
    this->ExportFunction("TimerExport_Cancel",        Timer::Export_Cancel);
47
    this->ExportFunction("TimerExport_Sleep",         Timer::Export_Sleep);
1601 runge 48
}
49
 
50
Timer::~Timer()
51
{
52
 
53
}
54
 
1604 runge 55
void Timer::InitializeDone()
1601 runge 56
{
1608 runge 57
    Plugin::InitializeDone();
1604 runge 58
 
1608 runge 59
    this->ImportFunction("Timer_OnTimeout");
60
 
1604 runge 61
    timer::Manager::Instance()->ConnectSlots(timer::Manager::SignalOnTimeout::slot_type(&Timer::SlotOnTimeout, this, _1, _2).track(this->tracker_));
62
}
63
 
64
void Timer::SlotOnTimeout(timer::TimerId timer_id, bool repeat)
65
{
1625 runge 66
    this->io_service_.post(boost::bind(&Timer::SlotOnTimeoutHandler, this, timer_id, repeat));
67
}
68
 
69
void Timer::SlotOnTimeoutHandler(timer::TimerId timer_id, bool repeat)
70
{
71
    v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
72
 
1642 runge 73
    //LOG.Debug(std::string(__FUNCTION__) + " called!");
1625 runge 74
 
1604 runge 75
    if (!repeat)
76
    {
77
        Timers::iterator it = Timer::timers_.find(timer_id);
78
 
79
        if (it != Timer::timers_.end())
80
        {
81
            Timer::timers_.erase(it);
82
        }
83
    }
84
 
1601 runge 85
    ArgumentListPointer arguments = ArgumentListPointer(new ArgumentList);
86
    arguments->push_back(v8::Integer::New(timer_id));
87
    arguments->push_back(v8::Boolean::New(repeat));
88
 
1608 runge 89
    this->Call( timer_id, "Timer_OnTimeout",arguments);
1601 runge 90
}
91
 
1646 runge 92
Value Timer::Export_SetAlarm(const v8::Arguments& args)
1601 runge 93
{
1625 runge 94
    v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
95
 
1642 runge 96
    //LOG.Debug(std::string(__FUNCTION__) + " called!");
1604 runge 97
 
1646 runge 98
    if (args.Length() < 1)
99
    {
100
        LOG.Error(std::string(__FUNCTION__) + ": To few arguments.");
101
    }
102
 
103
    v8::String::AsciiValue time(args[0]);
104
 
105
    timer::TimerId timer_id = timer::Manager::Instance()->SetAlarm(*time);
106
 
107
    Timer::timers_.insert(timer_id);
108
 
109
    return v8::Integer::New(timer_id);
110
}
111
 
112
Value Timer::Export_SetTimer(const v8::Arguments& args)
113
{
114
    v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
115
 
116
    //LOG.Debug(std::string(__FUNCTION__) + " called!");
117
 
1604 runge 118
    if (args.Length() < 2)
119
    {
1644 runge 120
        LOG.Error(std::string(__FUNCTION__) + ": To few arguments.");
1604 runge 121
    }
122
 
1646 runge 123
    timer::TimerId timer_id = timer::Manager::Instance()->SetTimer(args[0]->Uint32Value(), args[1]->BooleanValue());
1604 runge 124
 
125
    Timer::timers_.insert(timer_id);
126
 
127
    return v8::Integer::New(timer_id);
1601 runge 128
}
129
 
1646 runge 130
Value Timer::Export_Cancel(const v8::Arguments& args)
1601 runge 131
{
1625 runge 132
    v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
133
 
1642 runge 134
    //LOG.Debug(std::string(__FUNCTION__) + " called!");
1604 runge 135
 
136
    if (args.Length() < 1)
137
    {
1644 runge 138
        LOG.Error(std::string(__FUNCTION__) + ": To few arguments.");
1604 runge 139
    }
140
 
141
    Timers::iterator it = Timer::timers_.find(args[0]->IsUint32());
142
 
143
    if (it != Timer::timers_.end())
144
    {
145
        Timer::timers_.erase(it);
146
    }
147
 
1601 runge 148
    timer::Manager::Instance()->Cancel(args[0]->IsUint32());
149
    return v8::Undefined();
150
}
151
 
1625 runge 152
Value Timer::Export_Sleep(const v8::Arguments& args)
153
{
154
    v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
155
 
1642 runge 156
    //LOG.Debug(std::string(__FUNCTION__) + " called!");
1625 runge 157
 
158
    if (args.Length() < 1)
159
    {
1644 runge 160
        LOG.Error(std::string(__FUNCTION__) + ": To few arguments.");
1625 runge 161
    }
162
 
163
    boost::this_thread::sleep(boost::posix_time::milliseconds(args[0]->Uint32Value()));
164
 
165
    return v8::Undefined();
166
}
167
 
1601 runge 168
}; // namespace plugin
169
}; // namespace vm
1625 runge 170
}; // namespace atom