Subversion Repositories HomeAutomation

Rev

Rev 1665 | 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 "Node.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 Node::LOG("vm::plugin::node");
  33.    
  34. Node::Node(boost::asio::io_service& io_service) : Plugin(io_service)
  35. {
  36.     this->name_ = "node";
  37.    
  38.     this->ExportFunction("NodeExport_ResetNode",         Node::Export_ResetNode);
  39.     this->ExportFunction("NodeExport_GetAvailableNodes", Node::Export_GetAvailableNodes);
  40.     this->ExportFunction("NodeExport_ProgramNode",       Node::Export_ProgramNode);
  41. }
  42.  
  43. Node::~Node()
  44. {
  45.     this->tracker_.reset();
  46. }
  47.  
  48. void Node::InitializeDone()
  49. {
  50.     Plugin::InitializeDone();
  51.    
  52.     this->ImportFunction("Node_OnChange");
  53.    
  54.     control::Manager::Instance()->ConnectSlotNode(control::Manager::SignalOnNodeChange::slot_type(&Node::SlotOnNodeChange, this, _1, _2).track(this->tracker_));
  55. }
  56.  
  57. void Node::CallOutput(unsigned int request_id, std::string output)
  58. {
  59.     LOG.Info(output);
  60. }
  61.  
  62. void Node::SlotOnNodeChange(control::Node::Id node_id, bool available)
  63. {
  64.     this->io_service_.post(boost::bind(&Node::SlotOnNodeChangeHandler, this, node_id, available));
  65. }
  66.  
  67. void Node::SlotOnNodeChangeHandler(control::Node::Id node_id, bool available)
  68. {
  69.     v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
  70.    
  71.     //LOG.Debug(std::string(__FUNCTION__) + " called!");
  72.    
  73.     ArgumentListPointer arguments = ArgumentListPointer(new ArgumentList);
  74.     arguments->push_back(v8::String::New(node_id.data()));
  75.     arguments->push_back(v8::Boolean::New(available));
  76.    
  77.     this->Call(0, "Node_OnChange", arguments);
  78. }
  79.  
  80. Value Node::Export_ResetNode(const v8::Arguments& args)
  81. {
  82.     v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
  83.    
  84.     //LOG.Debug(std::string(__FUNCTION__) + " called!");
  85.    
  86.     if (args.Length() < 1)
  87.     {
  88.         LOG.Error(std::string(__FUNCTION__) + ": To few arguments.");
  89.         return v8::Boolean::New(false);
  90.     }
  91.    
  92.     v8::String::AsciiValue node_id(args[0]);
  93.    
  94.     return v8::Boolean::New(control::Manager::Instance()->ResetNode(*node_id));
  95. }
  96.  
  97. Value Node::Export_GetAvailableNodes(const v8::Arguments& args)
  98. {
  99.     v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
  100.    
  101.     //LOG.Debug(std::string(__FUNCTION__) + " called!");
  102.    
  103.     common::StringList available_nodes = control::Manager::Instance()->GetAvailableNodes();
  104.    
  105.     v8::Local<v8::Array> result = v8::Array::New(available_nodes.size());
  106.    
  107.     for (unsigned int n = 0; n < available_nodes.size(); n++)
  108.     {
  109.         LOG.Debug(boost::lexical_cast<std::string>(n) + ": " + available_nodes[n]);
  110.         result->Set(n, v8::String::New(available_nodes[n].data()));
  111.     }
  112.    
  113.     return result;
  114. }
  115.  
  116. Value Node::Export_ProgramNode(const v8::Arguments& args)
  117. {
  118.     v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
  119.    
  120.     //LOG.Debug(std::string(__FUNCTION__) + " called!");
  121.    
  122.     if (args.Length() < 3)
  123.     {
  124.         LOG.Error(std::string(__FUNCTION__) + ": To few arguments.");
  125.         return v8::Boolean::New(false);
  126.     }
  127.    
  128.     v8::String::AsciiValue node_id(args[0]);
  129.     v8::String::AsciiValue filename(args[2]);
  130.    
  131.     return v8::Boolean::New(control::Manager::Instance()->ProgramNode(*node_id, args[1]->BooleanValue(), std::string(*filename)));
  132. }
  133.    
  134. }; // namespace plugin
  135. }; // namespace vm
  136. }; // namespace atom
  137.