Subversion Repositories HomeAutomation

Rev

Rev 1740 | 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 "System.h"
  22.  
  23. #include <v8-debug.h>
  24. #include <stdio.h>
  25.  
  26. #include "vm/Manager.h"
  27. #include "common/common.h"
  28. #include "config.h"
  29.  
  30. namespace atom {
  31. namespace vm {
  32. namespace plugin {
  33.  
  34. logging::Logger System::LOG("vm::plugin::System");
  35.    
  36. System::System(boost::asio::io_service& io_service) : Plugin(io_service)
  37. {
  38.     this->name_ = "system";
  39.    
  40.     this->ExportFunction("Require",              System::Export_Require);
  41.     this->ExportFunction("Execute",              System::Export_Execute);
  42.     this->ExportFunction("ToHex",                System::Export_ToHex);
  43.     this->ExportFunction("SystemExport_Version", System::Export_Version);
  44.     this->ExportFunction("SystemExport_License", System::Export_License);
  45. }
  46.  
  47. System::~System()
  48. {
  49.  
  50. }
  51.  
  52. void System::InitializeDone()
  53. {
  54.     Plugin::InitializeDone();
  55.    
  56.     Manager::Instance()->LoadScript("interface/list.js");
  57.     Manager::Instance()->LoadScript("autostart.js");
  58. }
  59.  
  60. void System::CallOutput(unsigned int request_id, std::string output)
  61. {
  62.     LOG.Info(output);
  63. }
  64.  
  65. Value System::Export_Require(const v8::Arguments& args)
  66. {
  67.     v8::Locker lock;
  68.     v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
  69.     v8::HandleScope handle_scope;
  70.    
  71.     //LOG.Debug(std::string(__FUNCTION__) + " called!");
  72.    
  73.     if (args.Length() < 1)
  74.     {
  75.         LOG.Error(std::string(__FUNCTION__) + ": To few arguments.");
  76.         return v8::Boolean::New(false);
  77.     }
  78.    
  79.     v8::String::AsciiValue str(args[0]);
  80.    
  81.     return handle_scope.Close(v8::Boolean::New(Manager::Instance()->LoadScript(*str)));
  82. }
  83.  
  84. Value System::Export_Execute(const v8::Arguments& args)
  85. {
  86.     v8::Locker lock;
  87.     v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
  88.     v8::HandleScope handle_scope;
  89.    
  90.     //LOG.Debug(std::string(__FUNCTION__) + " called!");
  91.    
  92.     if (args.Length() < 1)
  93.     {
  94.         LOG.Error(std::string(__FUNCTION__) + ": To few arguments.");
  95.         return v8::Boolean::New(false);
  96.     }
  97.    
  98.     v8::String::AsciiValue command(args[0]);
  99.    
  100.     std::string output_buffer;
  101.     char output[128];
  102.    
  103.     FILE* pipe = popen(*command, "r" );
  104.    
  105.     if (pipe == NULL)
  106.     {
  107.         LOG.Info("Failed to execute \"" + std::string(*command) + "\".");
  108.         return v8::Boolean::New(false);
  109.     }
  110.    
  111.     while (!feof(pipe))
  112.     {
  113.         if (fgets(output, 128, pipe) != NULL)
  114.         {
  115.             output_buffer += output;
  116.         }
  117.     }
  118.    
  119.     pclose(pipe);
  120.    
  121.     return handle_scope.Close(v8::String::New(output_buffer.data()));
  122. }
  123.  
  124. Value System::Export_ToHex(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() < 1)
  133.     {
  134.         LOG.Error(std::string(__FUNCTION__) + ": To few arguments.");
  135.         return handle_scope.Close(v8::Boolean::New(false));
  136.     }
  137.    
  138.     return handle_scope.Close(v8::String::New(common::ToHex(args[0]->Uint32Value()).data()));
  139. }
  140.  
  141. Value System::Export_Version(const v8::Arguments& args)
  142. {
  143.     v8::Locker lock;
  144.     v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
  145.     v8::HandleScope handle_scope;
  146.    
  147.     return handle_scope.Close(v8::String::New(VERSION));
  148. }
  149.  
  150. Value System::Export_License(const v8::Arguments& args)
  151. {
  152.     v8::Locker lock;
  153.     v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
  154.     v8::HandleScope handle_scope;
  155.    
  156.     return handle_scope.Close(v8::String::New(LICENSE));
  157. }
  158.  
  159. }; // namespace plugin
  160. }; // namespace vm
  161. }; // namespace atom
  162.