Subversion Repositories HomeAutomation

Rev

Rev 1741 | 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 "control/Node.h"
  27. #include "vm/Manager.h"
  28.  
  29. namespace atom {
  30. namespace vm {
  31. namespace plugin {
  32.  
  33. logging::Logger Node::LOG("vm::plugin::node");
  34.    
  35. Node::Node(boost::asio::io_service& io_service) : Plugin(io_service)
  36. {
  37.     this->name_ = "node";
  38.    
  39.     this->ExportFunction("NodeExport_ResetNode",         Node::Export_ResetNode);
  40.     this->ExportFunction("NodeExport_GetAvailableNodes", Node::Export_GetAvailableNodes);
  41.     this->ExportFunction("NodeExport_ProgramNode",       Node::Export_ProgramNode);
  42.     this->ExportFunction("NodeExport_ProgramNodeHex",    Node::Export_ProgramNodeHex);
  43.     this->ExportFunction("NodeExport_GetNodeInformation",Node::Export_GetNodeInformation);
  44. }
  45.  
  46. Node::~Node()
  47. {
  48.     this->tracker_.reset();
  49. }
  50.  
  51. void Node::InitializeDone()
  52. {
  53.     Plugin::InitializeDone();
  54.    
  55.     this->ImportFunction("Node_OnChange");
  56.    
  57.     control::Manager::Instance()->ConnectSlotNode(control::Manager::SignalOnNodeChange::slot_type(&Node::SlotOnNodeChange, this, _1, _2).track(this->tracker_));
  58. }
  59.  
  60. void Node::CallOutput(unsigned int request_id, std::string output)
  61. {
  62.     LOG.Info(output);
  63. }
  64.  
  65. void Node::SlotOnNodeChange(control::Node::Id node_id, bool available)
  66. {
  67.     this->io_service_.post(boost::bind(&Node::SlotOnNodeChangeHandler, this, node_id, available));
  68. }
  69.  
  70. void Node::SlotOnNodeChangeHandler(control::Node::Id node_id, bool available)
  71. {
  72.     v8::Locker lock;
  73.     v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
  74.     v8::HandleScope handle_scope;
  75.    
  76.     //LOG.Debug(std::string(__FUNCTION__) + " called!");
  77.    
  78.     ArgumentListPointer arguments = ArgumentListPointer(new ArgumentList);
  79.     arguments->push_back(v8::String::New(node_id.data()));
  80.     arguments->push_back(v8::Boolean::New(available));
  81.    
  82.     this->Call(0, "Node_OnChange", arguments);
  83. }
  84.  
  85. Value Node::Export_ResetNode(const v8::Arguments& args)
  86. {
  87.     v8::Locker lock;
  88.     v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
  89.     v8::HandleScope handle_scope;
  90.    
  91.     //LOG.Debug(std::string(__FUNCTION__) + " called!");
  92.    
  93.     if (args.Length() < 1)
  94.     {
  95.         LOG.Error(std::string(__FUNCTION__) + ": To few arguments.");
  96.         return handle_scope.Close(v8::Boolean::New(false));
  97.     }
  98.    
  99.     v8::String::AsciiValue node_id(args[0]);
  100.    
  101.     return handle_scope.Close(v8::Boolean::New(control::Manager::Instance()->ResetNode(*node_id)));
  102. }
  103.  
  104. Value Node::Export_GetAvailableNodes(const v8::Arguments& args)
  105. {
  106.     v8::Locker lock;
  107.     v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
  108.     v8::HandleScope handle_scope;
  109.    
  110.     //LOG.Debug(std::string(__FUNCTION__) + " called!");
  111.    
  112.     common::StringList available_nodes = control::Manager::Instance()->GetAvailableNodes();
  113.    
  114.     v8::Local<v8::Array> result = v8::Array::New(available_nodes.size());
  115.    
  116.     for (unsigned int n = 0; n < available_nodes.size(); n++)
  117.     {
  118.         result->Set(n, v8::String::New(available_nodes[n].data()));
  119.     }
  120.    
  121.     return handle_scope.Close(result);
  122. }
  123.  
  124. Value Node::Export_ProgramNode(const v8::Arguments& args)
  125. {
  126.     v8::Locker lock;
  127.     v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
  128.     v8::HandleScope handle_scope;
  129.    
  130.     //LOG.Debug(std::string(__FUNCTION__) + " called!");
  131.    
  132.     if (args.Length() < 3)
  133.     {
  134.         LOG.Error(std::string(__FUNCTION__) + ": To few arguments.");
  135.         return handle_scope.Close(v8::Boolean::New(false));
  136.     }
  137.    
  138.     v8::String::AsciiValue node_id(args[0]);
  139.     v8::String::AsciiValue filename(args[2]);
  140.    
  141.     return handle_scope.Close(v8::Boolean::New(control::Manager::Instance()->ProgramNode(*node_id, args[1]->BooleanValue(), std::string(*filename))));
  142. }
  143.  
  144. Value Node::Export_ProgramNodeHex(const v8::Arguments& args)
  145. {
  146.     v8::Locker lock;
  147.     v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
  148.     v8::HandleScope handle_scope;
  149.    
  150.     //LOG.Debug(std::string(__FUNCTION__) + " called!");
  151.    
  152.     if (args.Length() < 3)
  153.     {
  154.         LOG.Error(std::string(__FUNCTION__) + ": To few arguments.");
  155.         return handle_scope.Close(v8::Boolean::New(false));
  156.     }
  157.    
  158.     v8::String::AsciiValue node_id(args[0]);
  159.     v8::String::AsciiValue hex_data(args[2]);
  160.    
  161.     return handle_scope.Close(v8::Boolean::New(control::Manager::Instance()->ProgramNodeHex(*node_id, args[1]->BooleanValue(), std::string(*hex_data))));
  162. }
  163.  
  164. Value Node::Export_GetNodeInformation(const v8::Arguments& args)
  165. {
  166.     v8::Locker lock;
  167.     v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
  168.     v8::HandleScope handle_scope;
  169.    
  170.     //LOG.Debug(std::string(__FUNCTION__) + " called!");
  171.    
  172.     if (args.Length() < 1)
  173.     {
  174.         LOG.Error(std::string(__FUNCTION__) + ": To few arguments.");
  175.         return handle_scope.Close(v8::Boolean::New(false));
  176.     }
  177.    
  178.     v8::String::AsciiValue node_id(args[0]);
  179.    
  180.     control::Node::Information information;
  181.    
  182.     try
  183.     {
  184.         information = control::Manager::Instance()->GetNodeInformation(*node_id);
  185.     }
  186.     catch (std::runtime_error& e)
  187.     {
  188.         LOG.Error(e.what());
  189.         return handle_scope.Close(v8::Undefined());
  190.     }
  191.    
  192.     v8::Local<v8::Array> result = v8::Array::New(6);
  193.    
  194.     result->Set(v8::String::New("Id"),            args[0]);
  195.     result->Set(v8::String::New("Valid"),         v8::Boolean::New(information.valid_));
  196.     result->Set(v8::String::New("BiosVersion"),   v8::Uint32::New(information.bios_version_));
  197.     result->Set(v8::String::New("DeviceType"),    v8::String::New(information.device_type_.data()));
  198.     result->Set(v8::String::New("HasApplication"),v8::Boolean::New(information.has_application_));
  199.     result->Set(v8::String::New("LastActive"),    v8::Uint32::New(information.last_active_));
  200.    
  201.     return handle_scope.Close(result);
  202. }
  203.  
  204. }; // namespace plugin
  205. }; // namespace vm
  206. }; // namespace atom
  207.