Subversion Repositories HomeAutomation

Rev

Rev 1604 | Go to most recent revision | Details | 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"
26
 
27
namespace atom {
28
namespace vm {
29
namespace plugin {
30
 
31
logging::Logger Module::LOG("vm::plugin::module");
32
 
33
Module::Module()
34
{
35
    this->name_ = "module";
36
    this->tracker_ = TrackerPointer(new char);
37
 
38
    this->ImportFunction("Module_OnChange");
39
    this->ImportFunction("Module_OnMessage");
40
 
41
    this->ExportFunction("Module_SendMessage", Module::Export_SendModuleMessage);
42
}
43
 
44
Module::~Module()
45
{
46
    this->tracker_.reset();
47
}
48
 
49
void Module::InitializeDone()
50
{
51
    Plugin::InitializeDone();
52
 
53
    can::Manager::Instance()->ConnectSlots(can::Manager::SignalOnModuleChange::slot_type(&Module::SlotOnModuleChange, this, _1, _2).track(this->tracker_),
54
                                           can::Manager::SignalOnModuleMessage::slot_type(&Module::SlotOnModuleMessage, this, _1, _2, _3).track(this->tracker_));
55
}
56
 
57
void Module::SlotOnModuleChange(std::string full_id, bool available)
58
{
59
    ArgumentListPointer arguments = ArgumentListPointer(new ArgumentList);
60
    arguments->push_back(v8::String::New(full_id.data()));
61
    arguments->push_back(v8::Boolean::New(available));
62
 
63
    this->Call("Module_OnChange", arguments);
64
}
65
 
66
void Module::SlotOnModuleMessage(std::string full_id, std::string command, type::StringMap variables)
67
{
68
    ArgumentListPointer arguments = ArgumentListPointer(new ArgumentList);
69
 
70
    arguments->push_back(v8::String::New(full_id.data()));
71
    arguments->push_back(v8::String::New(command.data()));
72
    arguments->push_back(v8::Uint32::New(variables.size()));
73
 
74
    for (type::StringMap::iterator it = variables.begin(); it != variables.end(); it++)
75
    {
76
        arguments->push_back(v8::String::New(it->first.data()));
77
        arguments->push_back(v8::String::New(it->second.data()));
78
    }
79
 
80
    this->Call("Module_OnMessage", arguments);
81
}
82
 
83
Value atom::vm::plugin::Module::Export_SendModuleMessage(const v8::Arguments& args)
84
{
85
    type::StringMap variables;
86
 
87
    v8::String::AsciiValue full_id(args[0]);
88
    v8::String::AsciiValue command(args[1]);
89
    unsigned int length = args[2]->Uint32Value();
90
 
91
    for (unsigned int n = 3; n < length * 2 + 3; n += 2)
92
    {
93
        v8::String::AsciiValue name(args[n]);
94
        v8::String::AsciiValue value(args[n + 1]);
95
 
96
        variables[std::string(*name)] = std::string(*value);
97
    }
98
 
99
    can::Manager::Instance()->SendMessage(std::string(*full_id), std::string(*command), variables);
100
}
101
 
102
}; // namespace plugin
103
}; // namespace vm
104
}; // namespace atom