Subversion Repositories HomeAutomation

Rev

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"
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());
1604 runge 69
 
1648 runge 70
    //LOG.Debug(std::string(__FUNCTION__) + " called!");
1625 runge 71
 
1604 runge 72
    if (args.Length() < 1)
73
    {
1644 runge 74
        LOG.Error(std::string(__FUNCTION__) + ": To few arguments.");
1647 runge 75
        return v8::Boolean::New(false);
1604 runge 76
    }
77
 
1601 runge 78
    v8::String::AsciiValue str(args[0]);
79
 
1608 runge 80
    return v8::Boolean::New(Manager::Instance()->LoadScript(*str));
1601 runge 81
}
82
 
83
Value System::Export_Execute(const v8::Arguments& args)
84
{
1740 runge 85
    v8::Locker lock;
1625 runge 86
    v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
1604 runge 87
 
1648 runge 88
    //LOG.Debug(std::string(__FUNCTION__) + " called!");
1625 runge 89
 
1604 runge 90
    if (args.Length() < 1)
91
    {
1644 runge 92
        LOG.Error(std::string(__FUNCTION__) + ": To few arguments.");
1647 runge 93
        return v8::Boolean::New(false);
1604 runge 94
    }
95
 
1601 runge 96
    v8::String::AsciiValue command(args[0]);
97
 
98
    std::string output_buffer;
99
    char output[128];
100
 
101
    FILE* pipe = popen(*command, "r" );
102
 
103
    if (pipe == NULL)
104
    {
105
        LOG.Info("Failed to execute \"" + std::string(*command) + "\".");
106
        return v8::Boolean::New(false);
107
    }
108
 
109
    while (!feof(pipe))
110
    {
111
        if (fgets(output, 128, pipe) != NULL)
112
        {
113
            output_buffer += output;
114
        }
115
    }
116
 
117
    pclose(pipe);
118
 
119
    return v8::String::New(output_buffer.data());
120
}
1657 runge 121
 
122
Value System::Export_ToHex(const v8::Arguments& args)
123
{
1740 runge 124
    v8::Locker lock;
1657 runge 125
    v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
1601 runge 126
 
1657 runge 127
    //LOG.Debug(std::string(__FUNCTION__) + " called!");
128
 
129
    if (args.Length() < 1)
130
    {
131
        LOG.Error(std::string(__FUNCTION__) + ": To few arguments.");
132
        return v8::Boolean::New(false);
133
    }
134
 
135
    return v8::String::New(common::ToHex(args[0]->Uint32Value()).data());
136
}
137
 
1679 runge 138
Value System::Export_Version(const v8::Arguments& args)
139
{
1740 runge 140
    v8::Locker lock;
1679 runge 141
    v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
142
    return v8::String::New(VERSION);
143
}
144
 
145
Value System::Export_License(const v8::Arguments& args)
146
{
1740 runge 147
    v8::Locker lock;
1679 runge 148
    v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
149
    return v8::String::New(LICENSE);
150
}
151
 
1601 runge 152
}; // namespace plugin
153
}; // namespace vm
154
}; // namespace atom