Subversion Repositories HomeAutomation

Rev

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

Rev Author Line No. Line
1602 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 "Module.h"
22
 
23
#include <boost/lexical_cast.hpp>
24
 
1642 runge 25
#include "control/Manager.h"
1619 runge 26
#include "vm/Manager.h"
1602 runge 27
 
28
namespace atom {
29
namespace vm {
30
namespace plugin {
31
 
32
logging::Logger Module::LOG("vm::plugin::module");
33
 
1625 runge 34
Module::Module(boost::asio::io_service& io_service) : Plugin(io_service)
1602 runge 35
{
36
    this->name_ = "module";
37
 
1644 runge 38
    this->ExportFunction("ModuleExport_SendMessage",         Module::Export_SendModuleMessage);
39
    this->ExportFunction("ModuleExport_GetAvailableModules", Module::Export_GetAvailableModules);
40
    this->ExportFunction("ModuleExport_ProgramNode",         Module::Export_ProgramNode);
1602 runge 41
}
42
 
43
Module::~Module()
44
{
45
    this->tracker_.reset();
46
}
47
 
48
void Module::InitializeDone()
49
{
50
    Plugin::InitializeDone();
51
 
1608 runge 52
    this->ImportFunction("Module_OnChange");
53
    this->ImportFunction("Module_OnMessage");
54
 
1642 runge 55
    control::Manager::Instance()->ConnectSlots(control::Manager::SignalOnNodeChange::slot_type(&Module::SlotOnNodeChange, this, _1, _2).track(this->tracker_),
56
                                               control::Manager::SignalOnModuleChange::slot_type(&Module::SlotOnModuleChange, this, _1, _2).track(this->tracker_),
57
                                               control::Manager::SignalOnModuleMessage::slot_type(&Module::SlotOnModuleMessage, this, _1, _2, _3).track(this->tracker_));
1602 runge 58
}
59
 
1647 runge 60
void Module::CallOutput(unsigned int request_id, std::string output)
61
{
62
    LOG.Info(output);
63
}
64
 
1614 runge 65
void Module::SlotOnNodeChange(unsigned int node_id, bool available)
66
{
1625 runge 67
    this->io_service_.post(boost::bind(&Module::SlotOnNodeChangeHandler, this, node_id, available));
1614 runge 68
}
69
 
1602 runge 70
void Module::SlotOnModuleChange(std::string full_id, bool available)
71
{
1625 runge 72
    this->io_service_.post(boost::bind(&Module::SlotOnModuleChangeHandler, this, full_id, available));
73
}
74
 
1642 runge 75
void Module::SlotOnModuleMessage(std::string full_id, std::string command, common::StringMap variables)
1625 runge 76
{
77
    this->io_service_.post(boost::bind(&Module::SlotOnModuleMessageHandler, this, full_id, command, variables));
78
}
79
 
80
void Module::SlotOnNodeChangeHandler(unsigned int node_id, bool available)
81
{
82
 
83
}
84
 
85
void Module::SlotOnModuleChangeHandler(std::string full_id, bool available)
86
{
87
    v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
88
 
1642 runge 89
    //LOG.Debug(std::string(__FUNCTION__) + " called!");
1625 runge 90
 
1602 runge 91
    ArgumentListPointer arguments = ArgumentListPointer(new ArgumentList);
92
    arguments->push_back(v8::String::New(full_id.data()));
93
    arguments->push_back(v8::Boolean::New(available));
94
 
1608 runge 95
    this->Call(0, "Module_OnChange", arguments);
1602 runge 96
}
97
 
1642 runge 98
void Module::SlotOnModuleMessageHandler(std::string full_id, std::string command, common::StringMap variables)
1602 runge 99
{
1619 runge 100
    v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
101
 
1642 runge 102
    //LOG.Debug(std::string(__FUNCTION__) + " called!");
1609 runge 103
 
1602 runge 104
    ArgumentListPointer arguments = ArgumentListPointer(new ArgumentList);
105
 
106
    arguments->push_back(v8::String::New(full_id.data()));
107
    arguments->push_back(v8::String::New(command.data()));
108
 
1619 runge 109
    v8::Local<v8::Array> vars = v8::Array::New(variables.size());
110
 
1642 runge 111
    for (common::StringMap::iterator it = variables.begin(); it != variables.end(); it++)
1602 runge 112
    {
1619 runge 113
        vars->Set(v8::String::New(it->first.data()), v8::String::New(it->second.data()));
1602 runge 114
    }
115
 
1619 runge 116
    arguments->push_back(vars);
117
 
1608 runge 118
    this->Call(0, "Module_OnMessage", arguments);
1602 runge 119
}
120
 
1604 runge 121
Value Module::Export_SendModuleMessage(const v8::Arguments& args)
1602 runge 122
{
1625 runge 123
    v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
1604 runge 124
 
1642 runge 125
    //LOG.Debug(std::string(__FUNCTION__) + " called!");
1625 runge 126
 
1604 runge 127
    if (args.Length() < 3)
128
    {
1647 runge 129
        LOG.Error(std::string(__FUNCTION__) + ": To few arguments.");
130
        return v8::Boolean::New(false);
1604 runge 131
    }
132
 
1642 runge 133
    common::StringMap variables;
1602 runge 134
 
135
    v8::String::AsciiValue full_id(args[0]);
136
    v8::String::AsciiValue command(args[1]);
137
    unsigned int length = args[2]->Uint32Value();
138
 
139
    for (unsigned int n = 3; n < length * 2 + 3; n += 2)
140
    {
141
        v8::String::AsciiValue name(args[n]);
142
        v8::String::AsciiValue value(args[n + 1]);
143
 
1607 runge 144
        //LOG.Debug(std::string(*name) + "====" + std::string(*value));
145
 
1602 runge 146
        variables[std::string(*name)] = std::string(*value);
147
    }
148
 
1642 runge 149
    control::Manager::Instance()->SendMessage(std::string(*full_id), std::string(*command), variables);
1604 runge 150
 
1644 runge 151
    return v8::Boolean::New(true);
1602 runge 152
}
1604 runge 153
 
1644 runge 154
Value Module::Export_GetAvailableModules(const v8::Arguments& args)
1604 runge 155
{
1625 runge 156
    v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
157
 
1642 runge 158
    //LOG.Debug(std::string(__FUNCTION__) + " called!");
1602 runge 159
 
1604 runge 160
    if (args.Length() < 1)
161
    {
1644 runge 162
        LOG.Error(std::string(__FUNCTION__) + ": To few arguments.");
1647 runge 163
        return v8::Boolean::New(false);
1604 runge 164
    }
165
 
1644 runge 166
    common::StringList available_modules = control::Manager::Instance()->GetAvailableModules();
1604 runge 167
 
1644 runge 168
    v8::Local<v8::Array> result = v8::Array::New(available_modules.size());
169
 
170
    for (unsigned int n = 0; n < available_modules.size(); n++)
171
    {
172
        result->Set(n, v8::String::New(available_modules[n].data()));
173
    }
174
 
175
    return result;
1604 runge 176
}
1642 runge 177
 
178
Value Module::Export_ProgramNode(const v8::Arguments& args)
179
{
180
    v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
1604 runge 181
 
1642 runge 182
    //LOG.Debug(std::string(__FUNCTION__) + " called!");
183
 
184
    if (args.Length() < 3)
185
    {
1644 runge 186
        LOG.Error(std::string(__FUNCTION__) + ": To few arguments.");
1647 runge 187
        return v8::Boolean::New(false);
1642 runge 188
    }
189
 
190
    v8::String::AsciiValue filename(args[2]);
191
 
192
    return v8::Boolean::New(control::Manager::Instance()->ProgramNode(args[0]->Uint32Value(), args[1]->BooleanValue(), std::string(*filename)));
193
}
194
 
1602 runge 195
}; // namespace plugin
196
}; // namespace vm
197
}; // namespace atom