Subversion Repositories HomeAutomation

Rev

Rev 1887 | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
1657 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 "Node.h"
22
 
23
#include <boost/lexical_cast.hpp>
24
 
25
#include "control/Manager.h"
1665 runge 26
#include "control/Node.h"
1657 runge 27
#include "vm/Manager.h"
28
 
29
namespace atom {
30
namespace vm {
31
namespace plugin {
32
 
33
logging::Logger Node::LOG("vm::plugin::node");
34
 
35
Node::Node(boost::asio::io_service& io_service) : Plugin(io_service)
36
{
37
    this->name_ = "node";
38
 
39
    this->ExportFunction("NodeExport_ResetNode",         Node::Export_ResetNode);
40
    this->ExportFunction("NodeExport_GetAvailableNodes", Node::Export_GetAvailableNodes);
2164 arune 41
    this->ExportFunction("NodeExport_GetProgramProgress",Node::Export_GetProgramProgress);
1657 runge 42
    this->ExportFunction("NodeExport_ProgramNode",       Node::Export_ProgramNode);
1887 arune 43
    this->ExportFunction("NodeExport_ProgramNodeHex",    Node::Export_ProgramNodeHex);
1665 runge 44
    this->ExportFunction("NodeExport_GetNodeInformation",Node::Export_GetNodeInformation);
1657 runge 45
}
46
 
47
Node::~Node()
48
{
49
    this->tracker_.reset();
50
}
51
 
52
void Node::InitializeDone()
53
{
54
    Plugin::InitializeDone();
55
 
56
    this->ImportFunction("Node_OnChange");
57
 
58
    control::Manager::Instance()->ConnectSlotNode(control::Manager::SignalOnNodeChange::slot_type(&Node::SlotOnNodeChange, this, _1, _2).track(this->tracker_));
59
}
60
 
61
void Node::CallOutput(unsigned int request_id, std::string output)
62
{
63
    LOG.Info(output);
64
}
65
 
66
void Node::SlotOnNodeChange(control::Node::Id node_id, bool available)
67
{
68
    this->io_service_.post(boost::bind(&Node::SlotOnNodeChangeHandler, this, node_id, available));
69
}
70
 
71
void Node::SlotOnNodeChangeHandler(control::Node::Id node_id, bool available)
72
{
1740 runge 73
    v8::Locker lock;
1657 runge 74
    v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
1741 runge 75
    v8::HandleScope handle_scope;
1657 runge 76
 
77
    //LOG.Debug(std::string(__FUNCTION__) + " called!");
78
 
79
    ArgumentListPointer arguments = ArgumentListPointer(new ArgumentList);
80
    arguments->push_back(v8::String::New(node_id.data()));
81
    arguments->push_back(v8::Boolean::New(available));
82
 
83
    this->Call(0, "Node_OnChange", arguments);
84
}
85
 
86
Value Node::Export_ResetNode(const v8::Arguments& args)
87
{
1740 runge 88
    v8::Locker lock;
1657 runge 89
    v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
1741 runge 90
    v8::HandleScope handle_scope;
1657 runge 91
 
92
    //LOG.Debug(std::string(__FUNCTION__) + " called!");
93
 
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));
1657 runge 98
    }
99
 
100
    v8::String::AsciiValue node_id(args[0]);
101
 
1741 runge 102
    return handle_scope.Close(v8::Boolean::New(control::Manager::Instance()->ResetNode(*node_id)));
1657 runge 103
}
104
 
105
Value Node::Export_GetAvailableNodes(const v8::Arguments& args)
106
{
1740 runge 107
    v8::Locker lock;
1657 runge 108
    v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
1741 runge 109
    v8::HandleScope handle_scope;
1657 runge 110
 
111
    //LOG.Debug(std::string(__FUNCTION__) + " called!");
112
 
113
    common::StringList available_nodes = control::Manager::Instance()->GetAvailableNodes();
114
 
115
    v8::Local<v8::Array> result = v8::Array::New(available_nodes.size());
116
 
117
    for (unsigned int n = 0; n < available_nodes.size(); n++)
118
    {
119
        result->Set(n, v8::String::New(available_nodes[n].data()));
120
    }
121
 
1741 runge 122
    return handle_scope.Close(result);
1657 runge 123
}
124
 
2164 arune 125
Value Node::Export_GetProgramProgress(const v8::Arguments& args)
126
{
127
    v8::Locker lock;
128
    v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
129
    v8::HandleScope handle_scope;
130
 
131
    //LOG.Debug(std::string(__FUNCTION__) + " called!");
132
 
133
    if (args.Length() < 1)
134
    {
135
        LOG.Error(std::string(__FUNCTION__) + ": To few arguments.");
136
        return handle_scope.Close(v8::Uint32::New(255));
137
    }
138
 
139
    v8::String::AsciiValue node_id(args[0]);
140
 
141
    return handle_scope.Close(v8::Uint32::New(control::Manager::Instance()->GetProgramProgress(*node_id)));
142
 
143
    //return handle_scope.Close(v8::Boolean::New(control::Manager::Instance()->ProgramNode(*node_id, args[1]->BooleanValue(), std::string(*filename))));
144
}
145
 
1657 runge 146
Value Node::Export_ProgramNode(const v8::Arguments& args)
147
{
1740 runge 148
    v8::Locker lock;
1657 runge 149
    v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
1741 runge 150
    v8::HandleScope handle_scope;
1657 runge 151
 
152
    //LOG.Debug(std::string(__FUNCTION__) + " called!");
153
 
154
    if (args.Length() < 3)
155
    {
156
        LOG.Error(std::string(__FUNCTION__) + ": To few arguments.");
1741 runge 157
        return handle_scope.Close(v8::Boolean::New(false));
1657 runge 158
    }
159
 
160
    v8::String::AsciiValue node_id(args[0]);
161
    v8::String::AsciiValue filename(args[2]);
162
 
1741 runge 163
    return handle_scope.Close(v8::Boolean::New(control::Manager::Instance()->ProgramNode(*node_id, args[1]->BooleanValue(), std::string(*filename))));
1657 runge 164
}
1665 runge 165
 
1887 arune 166
Value Node::Export_ProgramNodeHex(const v8::Arguments& args)
167
{
168
    v8::Locker lock;
169
    v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
170
    v8::HandleScope handle_scope;
171
 
172
    //LOG.Debug(std::string(__FUNCTION__) + " called!");
173
 
174
    if (args.Length() < 3)
175
    {
176
        LOG.Error(std::string(__FUNCTION__) + ": To few arguments.");
177
        return handle_scope.Close(v8::Boolean::New(false));
178
    }
179
 
180
    v8::String::AsciiValue node_id(args[0]);
181
    v8::String::AsciiValue hex_data(args[2]);
182
 
183
    return handle_scope.Close(v8::Boolean::New(control::Manager::Instance()->ProgramNodeHex(*node_id, args[1]->BooleanValue(), std::string(*hex_data))));
184
}
185
 
1665 runge 186
Value Node::Export_GetNodeInformation(const v8::Arguments& args)
187
{
1740 runge 188
    v8::Locker lock;
1665 runge 189
    v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
1741 runge 190
    v8::HandleScope handle_scope;
1657 runge 191
 
1665 runge 192
    //LOG.Debug(std::string(__FUNCTION__) + " called!");
193
 
194
    if (args.Length() < 1)
195
    {
196
        LOG.Error(std::string(__FUNCTION__) + ": To few arguments.");
1741 runge 197
        return handle_scope.Close(v8::Boolean::New(false));
1665 runge 198
    }
199
 
200
    v8::String::AsciiValue node_id(args[0]);
201
 
202
    control::Node::Information information;
203
 
204
    try
205
    {
206
        information = control::Manager::Instance()->GetNodeInformation(*node_id);
207
    }
208
    catch (std::runtime_error& e)
209
    {
210
        LOG.Error(e.what());
1741 runge 211
        return handle_scope.Close(v8::Undefined());
1665 runge 212
    }
213
 
214
    v8::Local<v8::Array> result = v8::Array::New(6);
215
 
216
    result->Set(v8::String::New("Id"),            args[0]);
217
    result->Set(v8::String::New("Valid"),         v8::Boolean::New(information.valid_));
218
    result->Set(v8::String::New("BiosVersion"),   v8::Uint32::New(information.bios_version_));
219
    result->Set(v8::String::New("DeviceType"),    v8::String::New(information.device_type_.data()));
220
    result->Set(v8::String::New("HasApplication"),v8::Boolean::New(information.has_application_));
221
    result->Set(v8::String::New("LastActive"),    v8::Uint32::New(information.last_active_));
222
 
1741 runge 223
    return handle_scope.Close(result);
1665 runge 224
}
225
 
1657 runge 226
}; // namespace plugin
227
}; // namespace vm
228
}; // namespace atom