Subversion Repositories HomeAutomation

Rev

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

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