Subversion Repositories HomeAutomation

Rev

Rev 1740 | 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"
1679 runge 28
#include "config.h"
1601 runge 29
 
30
namespace atom {
31
namespace vm {
32
namespace plugin {
33
 
34
logging::Logger System::LOG("vm::plugin::System");
35
 
1644 runge 36
System::System(boost::asio::io_service& io_service) : Plugin(io_service)
1601 runge 37
{
38
    this->name_ = "system";
1609 runge 39
 
1679 runge 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);
1601 runge 45
}
46
 
47
System::~System()
48
{
49
 
50
}
51
 
52
void System::InitializeDone()
53
{
54
    Plugin::InitializeDone();
55
 
1679 runge 56
    Manager::Instance()->LoadScript("interface/list.js");
57
    Manager::Instance()->LoadScript("autostart.js");
1601 runge 58
}
59
 
1647 runge 60
void System::CallOutput(unsigned int request_id, std::string output)
1601 runge 61
{
1647 runge 62
    LOG.Info(output);
1601 runge 63
}
64
 
1644 runge 65
Value System::Export_Require(const v8::Arguments& args)
1601 runge 66
{
1740 runge 67
    v8::Locker lock;
1625 runge 68
    v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
1741 runge 69
    v8::HandleScope handle_scope;
1604 runge 70
 
1648 runge 71
    //LOG.Debug(std::string(__FUNCTION__) + " called!");
1625 runge 72
 
1604 runge 73
    if (args.Length() < 1)
74
    {
1644 runge 75
        LOG.Error(std::string(__FUNCTION__) + ": To few arguments.");
1647 runge 76
        return v8::Boolean::New(false);
1604 runge 77
    }
78
 
1601 runge 79
    v8::String::AsciiValue str(args[0]);
80
 
1741 runge 81
    return handle_scope.Close(v8::Boolean::New(Manager::Instance()->LoadScript(*str)));
1601 runge 82
}
83
 
84
Value System::Export_Execute(const v8::Arguments& args)
85
{
1740 runge 86
    v8::Locker lock;
1625 runge 87
    v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
1741 runge 88
    v8::HandleScope handle_scope;
1604 runge 89
 
1648 runge 90
    //LOG.Debug(std::string(__FUNCTION__) + " called!");
1625 runge 91
 
1604 runge 92
    if (args.Length() < 1)
93
    {
1644 runge 94
        LOG.Error(std::string(__FUNCTION__) + ": To few arguments.");
1647 runge 95
        return v8::Boolean::New(false);
1604 runge 96
    }
97
 
1601 runge 98
    v8::String::AsciiValue command(args[0]);
99
 
100
    std::string output_buffer;
101
    char output[128];
102
 
103
    FILE* pipe = popen(*command, "r" );
104
 
105
    if (pipe == NULL)
106
    {
107
        LOG.Info("Failed to execute \"" + std::string(*command) + "\".");
108
        return v8::Boolean::New(false);
109
    }
110
 
111
    while (!feof(pipe))
112
    {
113
        if (fgets(output, 128, pipe) != NULL)
114
        {
115
            output_buffer += output;
116
        }
117
    }
118
 
119
    pclose(pipe);
120
 
1741 runge 121
    return handle_scope.Close(v8::String::New(output_buffer.data()));
1601 runge 122
}
1657 runge 123
 
124
Value System::Export_ToHex(const v8::Arguments& args)
125
{
1740 runge 126
    v8::Locker lock;
1657 runge 127
    v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
1741 runge 128
    v8::HandleScope handle_scope;
1601 runge 129
 
1657 runge 130
    //LOG.Debug(std::string(__FUNCTION__) + " called!");
131
 
132
    if (args.Length() < 1)
133
    {
134
        LOG.Error(std::string(__FUNCTION__) + ": To few arguments.");
1741 runge 135
        return handle_scope.Close(v8::Boolean::New(false));
1657 runge 136
    }
137
 
1741 runge 138
    return handle_scope.Close(v8::String::New(common::ToHex(args[0]->Uint32Value()).data()));
1657 runge 139
}
140
 
1679 runge 141
Value System::Export_Version(const v8::Arguments& args)
142
{
1740 runge 143
    v8::Locker lock;
1679 runge 144
    v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
1741 runge 145
    v8::HandleScope handle_scope;
146
 
147
    return handle_scope.Close(v8::String::New(VERSION));
1679 runge 148
}
149
 
150
Value System::Export_License(const v8::Arguments& args)
151
{
1740 runge 152
    v8::Locker lock;
1679 runge 153
    v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
1741 runge 154
    v8::HandleScope handle_scope;
155
 
156
    return handle_scope.Close(v8::String::New(LICENSE));
1679 runge 157
}
158
 
1601 runge 159
}; // namespace plugin
160
}; // namespace vm
161
}; // namespace atom