Subversion Repositories HomeAutomation

Rev

Rev 1619 | Rev 1642 | 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
 
25
#include "can/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
 
38
    this->ExportFunction("Module_SendMessage", Module::Export_SendModuleMessage);
1604 runge 39
    this->ExportFunction("Module_IsModuleAvailable", Module::Export_IsModuleAvailable);
1602 runge 40
}
41
 
42
Module::~Module()
43
{
44
    this->tracker_.reset();
45
}
46
 
47
void Module::InitializeDone()
48
{
49
    Plugin::InitializeDone();
50
 
1608 runge 51
    this->ImportFunction("Module_OnChange");
52
    this->ImportFunction("Module_OnMessage");
53
 
1614 runge 54
    can::Manager::Instance()->ConnectSlots(can::Manager::SignalOnNodeChange::slot_type(&Module::SlotOnNodeChange, this, _1, _2).track(this->tracker_),
55
                                           can::Manager::SignalOnModuleChange::slot_type(&Module::SlotOnModuleChange, this, _1, _2).track(this->tracker_),
1602 runge 56
                                           can::Manager::SignalOnModuleMessage::slot_type(&Module::SlotOnModuleMessage, this, _1, _2, _3).track(this->tracker_));
57
}
58
 
1614 runge 59
void Module::SlotOnNodeChange(unsigned int node_id, bool available)
60
{
1625 runge 61
    this->io_service_.post(boost::bind(&Module::SlotOnNodeChangeHandler, this, node_id, available));
1614 runge 62
}
63
 
1602 runge 64
void Module::SlotOnModuleChange(std::string full_id, bool available)
65
{
1625 runge 66
    this->io_service_.post(boost::bind(&Module::SlotOnModuleChangeHandler, this, full_id, available));
67
}
68
 
69
void Module::SlotOnModuleMessage(std::string full_id, std::string command, type::StringMap variables)
70
{
71
    this->io_service_.post(boost::bind(&Module::SlotOnModuleMessageHandler, this, full_id, command, variables));
72
}
73
 
74
void Module::SlotOnNodeChangeHandler(unsigned int node_id, bool available)
75
{
76
 
77
}
78
 
79
void Module::SlotOnModuleChangeHandler(std::string full_id, bool available)
80
{
81
    v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
82
 
83
    LOG.Debug(std::string(__FUNCTION__) + " called!");
84
 
1602 runge 85
    ArgumentListPointer arguments = ArgumentListPointer(new ArgumentList);
86
    arguments->push_back(v8::String::New(full_id.data()));
87
    arguments->push_back(v8::Boolean::New(available));
88
 
1608 runge 89
    this->Call(0, "Module_OnChange", arguments);
1602 runge 90
}
91
 
1625 runge 92
void Module::SlotOnModuleMessageHandler(std::string full_id, std::string command, type::StringMap variables)
1602 runge 93
{
1619 runge 94
    v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
95
 
1625 runge 96
    LOG.Debug(std::string(__FUNCTION__) + " called!");
1609 runge 97
 
1602 runge 98
    ArgumentListPointer arguments = ArgumentListPointer(new ArgumentList);
99
 
100
    arguments->push_back(v8::String::New(full_id.data()));
101
    arguments->push_back(v8::String::New(command.data()));
102
 
1619 runge 103
    v8::Local<v8::Array> vars = v8::Array::New(variables.size());
104
 
1602 runge 105
    for (type::StringMap::iterator it = variables.begin(); it != variables.end(); it++)
106
    {
1619 runge 107
        vars->Set(v8::String::New(it->first.data()), v8::String::New(it->second.data()));
1602 runge 108
    }
109
 
1619 runge 110
    arguments->push_back(vars);
111
 
1608 runge 112
    this->Call(0, "Module_OnMessage", arguments);
1602 runge 113
}
114
 
1604 runge 115
Value Module::Export_SendModuleMessage(const v8::Arguments& args)
1602 runge 116
{
1625 runge 117
    v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
1604 runge 118
 
1625 runge 119
    LOG.Debug(std::string(__FUNCTION__) + " called!");
120
 
1604 runge 121
    if (args.Length() < 3)
122
    {
123
        LOG.Error("To few arguments.");
124
    }
125
 
1602 runge 126
    type::StringMap variables;
127
 
128
    v8::String::AsciiValue full_id(args[0]);
129
    v8::String::AsciiValue command(args[1]);
130
    unsigned int length = args[2]->Uint32Value();
131
 
132
    for (unsigned int n = 3; n < length * 2 + 3; n += 2)
133
    {
134
        v8::String::AsciiValue name(args[n]);
135
        v8::String::AsciiValue value(args[n + 1]);
136
 
1607 runge 137
        //LOG.Debug(std::string(*name) + "====" + std::string(*value));
138
 
1602 runge 139
        variables[std::string(*name)] = std::string(*value);
140
    }
141
 
142
    can::Manager::Instance()->SendMessage(std::string(*full_id), std::string(*command), variables);
1604 runge 143
 
144
    return v8::Undefined();
1602 runge 145
}
1604 runge 146
 
147
Value Module::Export_IsModuleAvailable(const v8::Arguments& args)
148
{
1625 runge 149
    v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
150
 
1604 runge 151
    LOG.Debug(std::string(__FUNCTION__) + " called!");
1602 runge 152
 
1604 runge 153
    if (args.Length() < 1)
154
    {
155
        LOG.Error("To few arguments.");
156
    }
157
 
158
    v8::String::AsciiValue full_id(args[0]);
159
 
160
    return v8::Boolean::New(can::Manager::Instance()->IsModuleAvailable(std::string(*full_id)));
161
}
162
 
1602 runge 163
}; // namespace plugin
164
}; // namespace vm
165
}; // namespace atom