Subversion Repositories HomeAutomation

Rev

Rev 1657 | Rev 1740 | 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
{
1625 runge 67
    v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
1604 runge 68
 
1648 runge 69
    //LOG.Debug(std::string(__FUNCTION__) + " called!");
1625 runge 70
 
1604 runge 71
    if (args.Length() < 1)
72
    {
1644 runge 73
        LOG.Error(std::string(__FUNCTION__) + ": To few arguments.");
1647 runge 74
        return v8::Boolean::New(false);
1604 runge 75
    }
76
 
1601 runge 77
    v8::String::AsciiValue str(args[0]);
78
 
1608 runge 79
    return v8::Boolean::New(Manager::Instance()->LoadScript(*str));
1601 runge 80
}
81
 
82
Value System::Export_Execute(const v8::Arguments& args)
83
{
1625 runge 84
    v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
1604 runge 85
 
1648 runge 86
    //LOG.Debug(std::string(__FUNCTION__) + " called!");
1625 runge 87
 
1604 runge 88
    if (args.Length() < 1)
89
    {
1644 runge 90
        LOG.Error(std::string(__FUNCTION__) + ": To few arguments.");
1647 runge 91
        return v8::Boolean::New(false);
1604 runge 92
    }
93
 
1601 runge 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
}
1657 runge 119
 
120
Value System::Export_ToHex(const v8::Arguments& args)
121
{
122
    v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
1601 runge 123
 
1657 runge 124
    //LOG.Debug(std::string(__FUNCTION__) + " called!");
125
 
126
    if (args.Length() < 1)
127
    {
128
        LOG.Error(std::string(__FUNCTION__) + ": To few arguments.");
129
        return v8::Boolean::New(false);
130
    }
131
 
132
    return v8::String::New(common::ToHex(args[0]->Uint32Value()).data());
133
}
134
 
1679 runge 135
Value System::Export_Version(const v8::Arguments& args)
136
{
137
    v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
138
    return v8::String::New(VERSION);
139
}
140
 
141
Value System::Export_License(const v8::Arguments& args)
142
{
143
    v8::Context::Scope context_scope(vm::Manager::Instance()->GetContext());
144
    return v8::String::New(LICENSE);
145
}
146
 
1601 runge 147
}; // namespace plugin
148
}; // namespace vm
149
}; // namespace atom