Subversion Repositories HomeAutomation

Rev

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

Rev Author Line No. Line
1602 runge 1
/*
2081 runge 2
 *
1602 runge 3
 *  Copyright (C) 2010  Mattias Runge
2081 runge 4
 *
1602 runge 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.
2081 runge 9
 *
1602 runge 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.
2081 runge 14
 *
1602 runge 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.
2081 runge 18
 *
1602 runge 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");
2081 runge 33
 
1625 runge 34
Module::Module(boost::asio::io_service& io_service) : Plugin(io_service)
1602 runge 35
{
36
    this->name_ = "module";
2081 runge 37
 
1644 runge 38
    this->ExportFunction("ModuleExport_SendMessage",         Module::Export_SendModuleMessage);
39
    this->ExportFunction("ModuleExport_GetAvailableModules", Module::Export_GetAvailableModules);
1602 runge 40
}
41
 
42
Module::~Module()
43
{
44
    this->tracker_.reset();
45
}
46
 
47
void Module::InitializeDone()
48
{
49
    Plugin::InitializeDone();
2081 runge 50
 
1608 runge 51
    this->ImportFunction("Module_OnChange");
52
    this->ImportFunction("Module_OnMessage");
2081 runge 53
 
1657 runge 54
    control::Manager::Instance()->ConnectSlotModule(control::Manager::SignalOnModuleChange::slot_type(&Module::SlotOnModuleChange, this, _1, _2).track(this->tracker_));
55
    control::Manager::Instance()->ConnectSlotModule(control::Manager::SignalOnModuleMessage::slot_type(&Module::SlotOnModuleMessage, this, _1, _2, _3).track(this->tracker_));
1602 runge 56
}
57
 
1647 runge 58
void Module::CallOutput(unsigned int request_id, std::string output)
59
{
60
    LOG.Info(output);
61
}
62
 
1602 runge 63
void Module::SlotOnModuleChange(std::string full_id, bool available)
64
{
1625 runge 65
    this->io_service_.post(boost::bind(&Module::SlotOnModuleChangeHandler, this, full_id, available));
66
}
67
 
1642 runge 68
void Module::SlotOnModuleMessage(std::string full_id, std::string command, common::StringMap variables)
1625 runge 69
{
70
    this->io_service_.post(boost::bind(&Module::SlotOnModuleMessageHandler, this, full_id, command, variables));
71
}
72
 
73
void Module::SlotOnModuleChangeHandler(std::string full_id, bool available)
74
{
1740 runge 75
    v8::Locker lock;
1625 runge 76
    v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
1741 runge 77
    v8::HandleScope handle_scope;
2081 runge 78
 
1642 runge 79
    //LOG.Debug(std::string(__FUNCTION__) + " called!");
2081 runge 80
 
1602 runge 81
    ArgumentListPointer arguments = ArgumentListPointer(new ArgumentList);
82
    arguments->push_back(v8::String::New(full_id.data()));
83
    arguments->push_back(v8::Boolean::New(available));
2081 runge 84
 
1608 runge 85
    this->Call(0, "Module_OnChange", arguments);
1602 runge 86
}
87
 
1642 runge 88
void Module::SlotOnModuleMessageHandler(std::string full_id, std::string command, common::StringMap variables)
1602 runge 89
{
1740 runge 90
    v8::Locker lock;
1619 runge 91
    v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
1741 runge 92
    v8::HandleScope handle_scope;
2081 runge 93
 
94
    LOG.Debug(std::string(__FUNCTION__) + " called!");
95
 
1602 runge 96
    ArgumentListPointer arguments = ArgumentListPointer(new ArgumentList);
2081 runge 97
 
1602 runge 98
    arguments->push_back(v8::String::New(full_id.data()));
99
    arguments->push_back(v8::String::New(command.data()));
2081 runge 100
    LOG.Debug("Fullid:" + full_id);
101
    LOG.Debug("command:" + command);
102
 
1619 runge 103
    v8::Local<v8::Array> vars = v8::Array::New(variables.size());
2081 runge 104
 
1642 runge 105
    for (common::StringMap::iterator it = variables.begin(); it != variables.end(); it++)
1602 runge 106
    {
1619 runge 107
        vars->Set(v8::String::New(it->first.data()), v8::String::New(it->second.data()));
2081 runge 108
        LOG.Debug(it->first + "=" + it->second);
1602 runge 109
    }
2081 runge 110
 
1619 runge 111
    arguments->push_back(vars);
2081 runge 112
 
1608 runge 113
    this->Call(0, "Module_OnMessage", arguments);
1602 runge 114
}
115
 
1604 runge 116
Value Module::Export_SendModuleMessage(const v8::Arguments& args)
1602 runge 117
{
1740 runge 118
    v8::Locker lock;
1625 runge 119
    v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
1741 runge 120
    v8::HandleScope handle_scope;
2081 runge 121
 
1642 runge 122
    //LOG.Debug(std::string(__FUNCTION__) + " called!");
2081 runge 123
 
1604 runge 124
    if (args.Length() < 3)
125
    {
1647 runge 126
        LOG.Error(std::string(__FUNCTION__) + ": To few arguments.");
1741 runge 127
        return handle_scope.Close(v8::Boolean::New(false));
1604 runge 128
    }
2081 runge 129
 
1642 runge 130
    common::StringMap variables;
2081 runge 131
 
1602 runge 132
    v8::String::AsciiValue full_id(args[0]);
133
    v8::String::AsciiValue command(args[1]);
134
    unsigned int length = args[2]->Uint32Value();
2081 runge 135
 
1602 runge 136
    for (unsigned int n = 3; n < length * 2 + 3; n += 2)
137
    {
138
        v8::String::AsciiValue name(args[n]);
139
        v8::String::AsciiValue value(args[n + 1]);
2081 runge 140
 
1607 runge 141
        //LOG.Debug(std::string(*name) + "====" + std::string(*value));
2081 runge 142
 
1602 runge 143
        variables[std::string(*name)] = std::string(*value);
144
    }
2081 runge 145
 
1642 runge 146
    control::Manager::Instance()->SendMessage(std::string(*full_id), std::string(*command), variables);
2081 runge 147
 
1741 runge 148
    return handle_scope.Close(v8::Boolean::New(true));
1602 runge 149
}
1604 runge 150
 
1644 runge 151
Value Module::Export_GetAvailableModules(const v8::Arguments& args)
1604 runge 152
{
1740 runge 153
    v8::Locker lock;
1625 runge 154
    v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
1741 runge 155
    v8::HandleScope handle_scope;
2081 runge 156
 
1642 runge 157
    //LOG.Debug(std::string(__FUNCTION__) + " called!");
2081 runge 158
 
1644 runge 159
    common::StringList available_modules = control::Manager::Instance()->GetAvailableModules();
2081 runge 160
 
1644 runge 161
    v8::Local<v8::Array> result = v8::Array::New(available_modules.size());
2081 runge 162
 
1644 runge 163
    for (unsigned int n = 0; n < available_modules.size(); n++)
164
    {
165
        result->Set(n, v8::String::New(available_modules[n].data()));
166
    }
2081 runge 167
 
1741 runge 168
    return handle_scope.Close(result);
1604 runge 169
}
1642 runge 170
 
1602 runge 171
}; // namespace plugin
172
}; // namespace vm
173
}; // namespace atom