Subversion Repositories HomeAutomation

Rev

Rev 1656 | Rev 1679 | 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"
1657 runge 27
#include "common/common.h"
1601 runge 28
 
29
namespace atom {
30
namespace vm {
31
namespace plugin {
32
 
33
logging::Logger System::LOG("vm::plugin::System");
34
 
1644 runge 35
System::System(boost::asio::io_service& io_service) : Plugin(io_service)
1601 runge 36
{
37
    this->name_ = "system";
1609 runge 38
 
1644 runge 39
    this->ExportFunction("Require",    System::Export_Require);
1601 runge 40
    this->ExportFunction("Execute",    System::Export_Execute);
1657 runge 41
    this->ExportFunction("ToHex",      System::Export_ToHex);
1601 runge 42
}
43
 
44
System::~System()
45
{
46
 
47
}
48
 
49
void System::InitializeDone()
50
{
1625 runge 51
    v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
52
 
1601 runge 53
    Plugin::InitializeDone();
54
 
1656 runge 55
    v8::Boolean::New(Manager::Instance()->LoadScript("interface/list.js"));
56
    v8::Boolean::New(Manager::Instance()->LoadScript("autostart.js"));
1601 runge 57
}
58
 
1647 runge 59
void System::CallOutput(unsigned int request_id, std::string output)
1601 runge 60
{
1647 runge 61
    LOG.Info(output);
1601 runge 62
}
63
 
1644 runge 64
Value System::Export_Require(const v8::Arguments& args)
1601 runge 65
{
1625 runge 66
    v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
1604 runge 67
 
1648 runge 68
    //LOG.Debug(std::string(__FUNCTION__) + " called!");
1625 runge 69
 
1604 runge 70
    if (args.Length() < 1)
71
    {
1644 runge 72
        LOG.Error(std::string(__FUNCTION__) + ": To few arguments.");
1647 runge 73
        return v8::Boolean::New(false);
1604 runge 74
    }
75
 
1601 runge 76
    v8::String::AsciiValue str(args[0]);
77
 
1608 runge 78
    return v8::Boolean::New(Manager::Instance()->LoadScript(*str));
1601 runge 79
}
80
 
81
Value System::Export_Execute(const v8::Arguments& args)
82
{
1625 runge 83
    v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
1604 runge 84
 
1648 runge 85
    //LOG.Debug(std::string(__FUNCTION__) + " called!");
1625 runge 86
 
1604 runge 87
    if (args.Length() < 1)
88
    {
1644 runge 89
        LOG.Error(std::string(__FUNCTION__) + ": To few arguments.");
1647 runge 90
        return v8::Boolean::New(false);
1604 runge 91
    }
92
 
1601 runge 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
}
1657 runge 118
 
119
Value System::Export_ToHex(const v8::Arguments& args)
120
{
121
    v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
1601 runge 122
 
1657 runge 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
 
1601 runge 134
}; // namespace plugin
135
}; // namespace vm
136
}; // namespace atom