Subversion Repositories HomeAutomation

Rev

Rev 1647 | Rev 1740 | Go to most recent revision | 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.     this->ExportFunction("ModuleExport_ProgramNode",         Module::Export_ProgramNode);
  41. }
  42.  
  43. Module::~Module()
  44. {
  45.     this->tracker_.reset();
  46. }
  47.  
  48. void Module::InitializeDone()
  49. {
  50.     Plugin::InitializeDone();
  51.    
  52.     this->ImportFunction("Module_OnChange");
  53.     this->ImportFunction("Module_OnMessage");
  54.    
  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_));
  58. }
  59.  
  60. void Module::CallOutput(unsigned int request_id, std::string output)
  61. {
  62.     LOG.Info(output);
  63. }
  64.  
  65. void Module::SlotOnNodeChange(unsigned int node_id, bool available)
  66. {
  67.     this->io_service_.post(boost::bind(&Module::SlotOnNodeChangeHandler, this, node_id, available));
  68. }
  69.  
  70. void Module::SlotOnModuleChange(std::string full_id, bool available)
  71. {
  72.     this->io_service_.post(boost::bind(&Module::SlotOnModuleChangeHandler, this, full_id, available));
  73. }
  74.  
  75. void Module::SlotOnModuleMessage(std::string full_id, std::string command, common::StringMap variables)
  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.    
  89.     //LOG.Debug(std::string(__FUNCTION__) + " called!");
  90.    
  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.    
  95.     this->Call(0, "Module_OnChange", arguments);
  96. }
  97.  
  98. void Module::SlotOnModuleMessageHandler(std::string full_id, std::string command, common::StringMap variables)
  99. {
  100.     v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
  101.    
  102.     //LOG.Debug(std::string(__FUNCTION__) + " called!");
  103.    
  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.    
  109.     v8::Local<v8::Array> vars = v8::Array::New(variables.size());
  110.    
  111.     for (common::StringMap::iterator it = variables.begin(); it != variables.end(); it++)
  112.     {
  113.         vars->Set(v8::String::New(it->first.data()), v8::String::New(it->second.data()));
  114.     }
  115.    
  116.     arguments->push_back(vars);
  117.    
  118.     this->Call(0, "Module_OnMessage", arguments);
  119. }
  120.  
  121. Value Module::Export_SendModuleMessage(const v8::Arguments& args)
  122. {
  123.     v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
  124.    
  125.     //LOG.Debug(std::string(__FUNCTION__) + " called!");
  126.    
  127.     if (args.Length() < 3)
  128.     {
  129.         LOG.Error(std::string(__FUNCTION__) + ": To few arguments.");
  130.         return v8::Boolean::New(false);
  131.     }
  132.    
  133.     common::StringMap variables;
  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.        
  144.         //LOG.Debug(std::string(*name) + "====" + std::string(*value));
  145.        
  146.         variables[std::string(*name)] = std::string(*value);
  147.     }
  148.    
  149.     control::Manager::Instance()->SendMessage(std::string(*full_id), std::string(*command), variables);
  150.    
  151.     return v8::Boolean::New(true);
  152. }
  153.  
  154. Value Module::Export_GetAvailableModules(const v8::Arguments& args)
  155. {
  156.     v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
  157.    
  158.     //LOG.Debug(std::string(__FUNCTION__) + " called!");
  159.    
  160.     common::StringList available_modules = control::Manager::Instance()->GetAvailableModules();
  161.    
  162.     v8::Local<v8::Array> result = v8::Array::New(available_modules.size());
  163.    
  164.     for (unsigned int n = 0; n < available_modules.size(); n++)
  165.     {
  166.         result->Set(n, v8::String::New(available_modules[n].data()));
  167.     }
  168.    
  169.     return result;
  170. }
  171.  
  172. Value Module::Export_ProgramNode(const v8::Arguments& args)
  173. {
  174.     v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
  175.    
  176.     //LOG.Debug(std::string(__FUNCTION__) + " called!");
  177.    
  178.     if (args.Length() < 3)
  179.     {
  180.         LOG.Error(std::string(__FUNCTION__) + ": To few arguments.");
  181.         return v8::Boolean::New(false);
  182.     }
  183.    
  184.     v8::String::AsciiValue filename(args[2]);
  185.    
  186.     return v8::Boolean::New(control::Manager::Instance()->ProgramNode(args[0]->Uint32Value(), args[1]->BooleanValue(), std::string(*filename)));
  187. }
  188.    
  189. }; // namespace plugin
  190. }; // namespace vm
  191. }; // namespace atom
  192.