Subversion Repositories HomeAutomation

Rev

Rev 1646 | Rev 1651 | 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
 
1647 runge 64
void Timer::CallOutput(unsigned int request_id, std::string output)
65
{
66
    LOG.Info(output);
67
}
68
 
1604 runge 69
void Timer::SlotOnTimeout(timer::TimerId timer_id, bool repeat)
70
{
1625 runge 71
    this->io_service_.post(boost::bind(&Timer::SlotOnTimeoutHandler, this, timer_id, repeat));
72
}
73
 
74
void Timer::SlotOnTimeoutHandler(timer::TimerId timer_id, bool repeat)
75
{
76
    v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
77
 
1642 runge 78
    //LOG.Debug(std::string(__FUNCTION__) + " called!");
1625 runge 79
 
1604 runge 80
    if (!repeat)
81
    {
82
        Timers::iterator it = Timer::timers_.find(timer_id);
83
 
84
        if (it != Timer::timers_.end())
85
        {
86
            Timer::timers_.erase(it);
87
        }
88
    }
89
 
1601 runge 90
    ArgumentListPointer arguments = ArgumentListPointer(new ArgumentList);
91
    arguments->push_back(v8::Integer::New(timer_id));
92
    arguments->push_back(v8::Boolean::New(repeat));
93
 
1608 runge 94
    this->Call( timer_id, "Timer_OnTimeout",arguments);
1601 runge 95
}
96
 
1646 runge 97
Value Timer::Export_SetAlarm(const v8::Arguments& args)
1601 runge 98
{
1625 runge 99
    v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
100
 
1642 runge 101
    //LOG.Debug(std::string(__FUNCTION__) + " called!");
1604 runge 102
 
1646 runge 103
    if (args.Length() < 1)
104
    {
105
        LOG.Error(std::string(__FUNCTION__) + ": To few arguments.");
1647 runge 106
        return v8::Boolean::New(false);
1646 runge 107
    }
108
 
109
    v8::String::AsciiValue time(args[0]);
110
 
111
    timer::TimerId timer_id = timer::Manager::Instance()->SetAlarm(*time);
112
 
113
    Timer::timers_.insert(timer_id);
114
 
115
    return v8::Integer::New(timer_id);
116
}
117
 
118
Value Timer::Export_SetTimer(const v8::Arguments& args)
119
{
120
    v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
121
 
122
    //LOG.Debug(std::string(__FUNCTION__) + " called!");
123
 
1604 runge 124
    if (args.Length() < 2)
125
    {
1644 runge 126
        LOG.Error(std::string(__FUNCTION__) + ": To few arguments.");
1647 runge 127
        return v8::Boolean::New(false);
1604 runge 128
    }
129
 
1646 runge 130
    timer::TimerId timer_id = timer::Manager::Instance()->SetTimer(args[0]->Uint32Value(), args[1]->BooleanValue());
1604 runge 131
 
132
    Timer::timers_.insert(timer_id);
133
 
134
    return v8::Integer::New(timer_id);
1601 runge 135
}
136
 
1646 runge 137
Value Timer::Export_Cancel(const v8::Arguments& args)
1601 runge 138
{
1625 runge 139
    v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
140
 
1642 runge 141
    //LOG.Debug(std::string(__FUNCTION__) + " called!");
1604 runge 142
 
143
    if (args.Length() < 1)
144
    {
1644 runge 145
        LOG.Error(std::string(__FUNCTION__) + ": To few arguments.");
1647 runge 146
        return v8::Boolean::New(false);
1604 runge 147
    }
148
 
149
    Timers::iterator it = Timer::timers_.find(args[0]->IsUint32());
150
 
151
    if (it != Timer::timers_.end())
152
    {
153
        Timer::timers_.erase(it);
154
    }
155
 
1601 runge 156
    timer::Manager::Instance()->Cancel(args[0]->IsUint32());
157
    return v8::Undefined();
158
}
159
 
1625 runge 160
Value Timer::Export_Sleep(const v8::Arguments& args)
161
{
162
    v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
163
 
1642 runge 164
    //LOG.Debug(std::string(__FUNCTION__) + " called!");
1625 runge 165
 
166
    if (args.Length() < 1)
167
    {
1644 runge 168
        LOG.Error(std::string(__FUNCTION__) + ": To few arguments.");
1647 runge 169
        return v8::Boolean::New(false);
1625 runge 170
    }
171
 
172
    boost::this_thread::sleep(boost::posix_time::milliseconds(args[0]->Uint32Value()));
173
 
174
    return v8::Undefined();
175
}
176
 
1601 runge 177
}; // namespace plugin
178
}; // namespace vm
1625 runge 179
}; // namespace atom