Subversion Repositories HomeAutomation

Rev

Rev 1625 | Rev 1647 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed

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