Subversion Repositories HomeAutomation

Rev

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