Subversion Repositories HomeAutomation

Rev

Rev 1603 | Rev 1609 | 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.  
  28. namespace atom {
  29. namespace vm {
  30. namespace plugin {
  31.  
  32. logging::Logger System::LOG("vm::plugin::System");
  33.    
  34. System::System()
  35. {
  36.     this->name_ = "system";
  37.                  
  38.     this->ImportFunction("Start");
  39.    
  40.     this->ExportFunction("Log",        System::Export_Log);
  41.     this->ExportFunction("LoadScript", System::Export_LoadScript);
  42.     this->ExportFunction("Execute",    System::Export_Execute);
  43. }
  44.  
  45. System::~System()
  46. {
  47.  
  48. }
  49.  
  50. void System::InitializeDone()
  51. {
  52.     Plugin::InitializeDone();
  53.    
  54.     this->Call("Start", ArgumentListPointer(new ArgumentList));
  55. }
  56.  
  57. Value System::Export_Log(const v8::Arguments& args)
  58. {
  59.     if (args.Length() < 1)
  60.     {
  61.         LOG.Error("To few arguments to log.");
  62.     }
  63.    
  64.     v8::String::AsciiValue str(args[0]);
  65.    
  66.     LOG.Info(*str);
  67.    
  68.     return v8::Undefined();
  69. }
  70.  
  71. Value System::Export_LoadScript(const v8::Arguments& args)
  72. {
  73.     LOG.Debug(std::string(__FUNCTION__) + " called!");
  74.    
  75.     if (args.Length() < 1)
  76.     {
  77.         LOG.Error("To few arguments.");
  78.     }
  79.    
  80.     v8::String::AsciiValue str(args[0]);
  81.    
  82.     return v8::Boolean::New(Manager::Instance()->LoadScriptHandler(*str));
  83. }
  84.  
  85. Value System::Export_Execute(const v8::Arguments& args)
  86. {
  87.     LOG.Debug(std::string(__FUNCTION__) + " called!");
  88.    
  89.     if (args.Length() < 1)
  90.     {
  91.         LOG.Error("To few arguments.");
  92.     }
  93.    
  94.     v8::String::AsciiValue command(args[0]);
  95.    
  96.     std::string output_buffer;
  97.     char output[128];
  98.    
  99.     FILE* pipe = popen(*command, "r" );
  100.    
  101.     if (pipe == NULL)
  102.     {
  103.         LOG.Info("Failed to execute \"" + std::string(*command) + "\".");
  104.         return v8::Boolean::New(false);
  105.     }
  106.    
  107.     while (!feof(pipe))
  108.     {
  109.         if (fgets(output, 128, pipe) != NULL)
  110.         {
  111.             output_buffer += output;
  112.         }
  113.     }
  114.    
  115.     pclose(pipe);
  116.    
  117.     return v8::String::New(output_buffer.data());
  118. }
  119.    
  120. }; // namespace plugin
  121. }; // namespace vm
  122. }; // namespace atom
  123.