Subversion Repositories HomeAutomation

Rev

Rev 1656 | 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.  
  29. namespace atom {
  30. namespace vm {
  31. namespace plugin {
  32.  
  33. logging::Logger System::LOG("vm::plugin::System");
  34.    
  35. System::System(boost::asio::io_service& io_service) : Plugin(io_service)
  36. {
  37.     this->name_ = "system";
  38.    
  39.     this->ExportFunction("Require",    System::Export_Require);
  40.     this->ExportFunction("Execute",    System::Export_Execute);
  41.     this->ExportFunction("ToHex",      System::Export_ToHex);
  42. }
  43.  
  44. System::~System()
  45. {
  46.  
  47. }
  48.  
  49. void System::InitializeDone()
  50. {
  51.     v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
  52.    
  53.     Plugin::InitializeDone();
  54.    
  55.     v8::Boolean::New(Manager::Instance()->LoadScript("interface/list.js"));
  56.     v8::Boolean::New(Manager::Instance()->LoadScript("autostart.js"));
  57. }
  58.  
  59. void System::CallOutput(unsigned int request_id, std::string output)
  60. {
  61.     LOG.Info(output);
  62. }
  63.  
  64. Value System::Export_Require(const v8::Arguments& args)
  65. {
  66.     v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
  67.    
  68.     //LOG.Debug(std::string(__FUNCTION__) + " called!");
  69.    
  70.     if (args.Length() < 1)
  71.     {
  72.         LOG.Error(std::string(__FUNCTION__) + ": To few arguments.");
  73.         return v8::Boolean::New(false);
  74.     }
  75.    
  76.     v8::String::AsciiValue str(args[0]);
  77.    
  78.     return v8::Boolean::New(Manager::Instance()->LoadScript(*str));
  79. }
  80.  
  81. Value System::Export_Execute(const v8::Arguments& args)
  82. {
  83.     v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
  84.    
  85.     //LOG.Debug(std::string(__FUNCTION__) + " called!");
  86.    
  87.     if (args.Length() < 1)
  88.     {
  89.         LOG.Error(std::string(__FUNCTION__) + ": To few arguments.");
  90.         return v8::Boolean::New(false);
  91.     }
  92.    
  93.     v8::String::AsciiValue command(args[0]);
  94.    
  95.     std::string output_buffer;
  96.     char output[128];
  97.    
  98.     FILE* pipe = popen(*command, "r" );
  99.    
  100.     if (pipe == NULL)
  101.     {
  102.         LOG.Info("Failed to execute \"" + std::string(*command) + "\".");
  103.         return v8::Boolean::New(false);
  104.     }
  105.    
  106.     while (!feof(pipe))
  107.     {
  108.         if (fgets(output, 128, pipe) != NULL)
  109.         {
  110.             output_buffer += output;
  111.         }
  112.     }
  113.    
  114.     pclose(pipe);
  115.    
  116.     return v8::String::New(output_buffer.data());
  117. }
  118.  
  119. Value System::Export_ToHex(const v8::Arguments& args)
  120. {
  121.     v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
  122.    
  123.     //LOG.Debug(std::string(__FUNCTION__) + " called!");
  124.    
  125.     if (args.Length() < 1)
  126.     {
  127.         LOG.Error(std::string(__FUNCTION__) + ": To few arguments.");
  128.         return v8::Boolean::New(false);
  129.     }
  130.    
  131.     return v8::String::New(common::ToHex(args[0]->Uint32Value()).data());
  132. }
  133.  
  134. }; // namespace plugin
  135. }; // namespace vm
  136. }; // namespace atom
  137.