Subversion Repositories HomeAutomation

Rev

Rev 1679 | 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 "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.    
  70.     //LOG.Debug(std::string(__FUNCTION__) + " called!");
  71.    
  72.     if (args.Length() < 1)
  73.     {
  74.         LOG.Error(std::string(__FUNCTION__) + ": To few arguments.");
  75.         return v8::Boolean::New(false);
  76.     }
  77.    
  78.     v8::String::AsciiValue str(args[0]);
  79.    
  80.     return v8::Boolean::New(Manager::Instance()->LoadScript(*str));
  81. }
  82.  
  83. Value System::Export_Execute(const v8::Arguments& args)
  84. {
  85.     v8::Locker lock;
  86.     v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
  87.    
  88.     //LOG.Debug(std::string(__FUNCTION__) + " called!");
  89.    
  90.     if (args.Length() < 1)
  91.     {
  92.         LOG.Error(std::string(__FUNCTION__) + ": To few arguments.");
  93.         return v8::Boolean::New(false);
  94.     }
  95.    
  96.     v8::String::AsciiValue command(args[0]);
  97.    
  98.     std::string output_buffer;
  99.     char output[128];
  100.    
  101.     FILE* pipe = popen(*command, "r" );
  102.    
  103.     if (pipe == NULL)
  104.     {
  105.         LOG.Info("Failed to execute \"" + std::string(*command) + "\".");
  106.         return v8::Boolean::New(false);
  107.     }
  108.    
  109.     while (!feof(pipe))
  110.     {
  111.         if (fgets(output, 128, pipe) != NULL)
  112.         {
  113.             output_buffer += output;
  114.         }
  115.     }
  116.    
  117.     pclose(pipe);
  118.    
  119.     return v8::String::New(output_buffer.data());
  120. }
  121.  
  122. Value System::Export_ToHex(const v8::Arguments& args)
  123. {
  124.     v8::Locker lock;
  125.     v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
  126.    
  127.     //LOG.Debug(std::string(__FUNCTION__) + " called!");
  128.    
  129.     if (args.Length() < 1)
  130.     {
  131.         LOG.Error(std::string(__FUNCTION__) + ": To few arguments.");
  132.         return v8::Boolean::New(false);
  133.     }
  134.    
  135.     return v8::String::New(common::ToHex(args[0]->Uint32Value()).data());
  136. }
  137.  
  138. Value System::Export_Version(const v8::Arguments& args)
  139. {
  140.     v8::Locker lock;
  141.     v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
  142.     return v8::String::New(VERSION);
  143. }
  144.  
  145. Value System::Export_License(const v8::Arguments& args)
  146. {
  147.     v8::Locker lock;
  148.     v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
  149.     return v8::String::New(LICENSE);
  150. }
  151.  
  152. }; // namespace plugin
  153. }; // namespace vm
  154. }; // namespace atom
  155.