Subversion Repositories HomeAutomation

Rev

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