Subversion Repositories HomeAutomation

Rev

Rev 2083 | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
1608 runge 1
 
1647 runge 2
Console_Functions = {};
3
Console_CurrentFunction = null;
4
Console_DefaultPrompt = "\033[29;1m[atomic] \033[0m";
5
Console_LastClientId = null;
1609 runge 6
 
1647 runge 7
function Console_GetFunctionNames()
1608 runge 8
{
1647 runge 9
    return get_keys(Console_Functions);
10
}
1609 runge 11
 
1647 runge 12
// C++ callable functions
13
 
14
function Console_AutocompleteRequest(client_id, arg_index, commandline)
15
{
16
    Console_LastClientId = client_id;
2083 runge 17
 
1647 runge 18
    var result = [];
19
    var parts = [];
20
    var name = "Console_Shell";
2083 runge 21
 
1647 runge 22
    if (commandline.length > 0 && arg_index != 0)
1609 runge 23
    {
1647 runge 24
        parts = commandline.split(" ");
25
        name = parts.shift();
26
        arg_index--;
1609 runge 27
    }
1647 runge 28
 
29
    if (Console_Functions[name] && Console_Functions[name]["autocomplete"])
30
    {
31
        //Log("calling autocomplete_callback: " + name + "\n");
32
        result = Console_Functions[name]["autocomplete"](arg_index, parts);
33
    }
2083 runge 34
 
1647 runge 35
    var result_string = result.join("\n");
2083 runge 36
 
1647 runge 37
    ConsoleExport_AutoCompleteResponse(Console_LastClientId, result_string);
38
    return true;
39
}
40
 
41
function Console_PromptResponse(client_id, response)
42
{
43
    Console_LastClientId = client_id;
2083 runge 44
 
1660 runge 45
    var parts = array_remove_empty(response.split(" "));
1647 runge 46
 
2083 runge 47
  var result = true;
48
 
49
  if (Console_CurrentFunction)
50
  {
51
    var current_function = Console_CurrentFunction;
52
    Console_CurrentFunction = Console_Shell;
53
 
54
    result = current_function.apply(null, Array.prototype.slice.call(parts, 0));
55
 
56
    if (Console_CurrentFunction == Console_Shell)
57
    {
58
      Console_SetDefaultPrompt();
59
    }
60
  }
61
 
1647 runge 62
    return result;
1608 runge 63
}
1609 runge 64
 
1647 runge 65
function Console_NewConnection(client_id)
66
{
67
    Console_LastClientId = client_id;
2083 runge 68
 
1647 runge 69
    return Console_SetDefaultPrompt();
70
}
71
 
72
// User script functions
73
 
1657 runge 74
function Console_GetClientId()
75
{
76
    return Console_LastClientId;
77
}
78
 
79
function Console_LogToClient(client_id, text)
80
{
81
    if (client_id)
82
    {
83
        return ConsoleExport_LogToClient(client_id, text);
84
    }
2083 runge 85
 
1657 runge 86
    return false;
87
}
88
 
1647 runge 89
function Console_PreventDefaultPrompt()
90
{
91
    Console_CurrentFunction = null;
92
}
93
 
94
function Console_SetDefaultPrompt()
95
{
96
    return Console_PromptRequest(Console_DefaultPrompt, Console_Shell);
97
}
98
 
99
function Console_PromptRequest(prompt, callback)
100
{
101
    Console_CurrentFunction = callback;
102
    return ConsoleExport_PromptRequest(Console_LastClientId, prompt);
103
}
104
 
1644 runge 105
function Console_RegisterCommand(command, autocomplete_callback)
1609 runge 106
{
1610 runge 107
    var command_string = command.toString();
108
 
109
    var pos_end = command_string.indexOf("\n") - 1;
110
    var pos_start = command_string.indexOf(" ") + 1;
2083 runge 111
 
1610 runge 112
    var full_name = command_string.substr(pos_start, pos_end - pos_start);
2083 runge 113
 
1610 runge 114
    var pos_split = full_name.indexOf("(");
2083 runge 115
 
1610 runge 116
    var name = full_name.substr(0, pos_split);
2083 runge 117
 
1610 runge 118
    var argument_string = full_name.substr(pos_split + 1);
2083 runge 119
 
1610 runge 120
    if (argument_string.length > 0)
121
    {
122
        argument_string = argument_string.replace(/,/g, "");
123
        argument_string = "<" + argument_string.replace(/ /g, "> <") + ">";
124
    }
2083 runge 125
 
1647 runge 126
    Console_Functions[name] = { "command" : command, "autocomplete" : autocomplete_callback, "help" : argument_string };
127
    return true;
128
}
129
 
130
function Console_StandardAutocomplete()
131
{
132
    var arg_index = arguments[0];
133
    var args = arguments[1];
2083 runge 134
 
1647 runge 135
    if (0 <= arg_index && arg_index < arguments.length - 2)
1609 runge 136
    {
1647 runge 137
        return arguments[arg_index + 2];
1609 runge 138
    }
2083 runge 139
 
1647 runge 140
    return [];
1609 runge 141
}
1610 runge 142
 
1647 runge 143
// Default call function
144
 
145
function Console_Shell()
1614 runge 146
{
1647 runge 147
    var args = to_array(arguments);
148
    var name = args.shift();
2083 runge 149
 
1647 runge 150
    if (Console_Functions[name] && Console_Functions[name]["command"])
1614 runge 151
    {
1647 runge 152
        return Console_Functions[name]["command"].apply(null, Array.prototype.slice.call(args, 0));
1614 runge 153
    }
1657 runge 154
    else
155
    {
156
        Log("\033[31mNo such command found, " + name + ".\033[0m\n");
157
    }
2083 runge 158
 
1647 runge 159
    return false;
1614 runge 160
}
1647 runge 161
Console_RegisterCommand(Console_Shell, function(arg_index, args) { return Console_StandardAutocomplete(arg_index, args, Console_GetFunctionNames()); });
1614 runge 162
 
1647 runge 163
// Help functions
1644 runge 164
 
1647 runge 165
function help_complete(arg_index, args)
1610 runge 166
{
1647 runge 167
    var result = [];
2083 runge 168
 
1610 runge 169
    if (arg_index == 1) // command name
170
    {
1647 runge 171
        for (var name in Console_Functions)
1610 runge 172
        {
1647 runge 173
            result.push(name);
1610 runge 174
        }
175
    }
2083 runge 176
 
1610 runge 177
    return result;
178
}
179
 
1673 runge 180
function help(command_name)
181
{
182
    if (!command_name)
183
    {
184
        command_name = "help";
185
    }
2083 runge 186
 
1673 runge 187
    if (Console_Functions[command_name] && Console_Functions[command_name]["help"])
188
    {
189
        var result = "";
2083 runge 190
 
1673 runge 191
        if (Console_Functions[command_name] && Console_Functions[command_name]["autocomplete"])
192
        {
193
            result += "Argument autocompletion enabled for " + command_name + "\n";
194
        }
2083 runge 195
 
1673 runge 196
        result += "Syntax: " + command_name + " " + Console_Functions[command_name]["help"] + "\n";
2083 runge 197
 
1673 runge 198
        Log(result);
199
        return true;
200
    }
2083 runge 201
 
1673 runge 202
    Log("\033[31mNo help for " + command_name + " found.\033[0m\n");
203
    return false;
204
}
205
Console_RegisterCommand(help, function(arg_index, args) { return Console_StandardAutocomplete(arg_index, args, Console_GetFunctionNames()); });
206
 
1679 runge 207
function version()
208
{
209
    Log(SystemExport_Version() + "\n");
210
    return true;
211
}
212
Console_RegisterCommand(version);
1673 runge 213
 
1679 runge 214
function license()
215
{
216
    Log(SystemExport_License() + "\n");
217
    return true;
218
}
219
Console_RegisterCommand(license);
220
 
1664 runge 221
function quit()
1610 runge 222
{
1664 runge 223
    Log("Goodbye!\n");
2083 runge 224
 
1664 runge 225
    ConsoleExport_DisconnectClient(Console_LastClientId);
226
    Console_LastClientId = null;
1647 runge 227
    return false;
1610 runge 228
}
1664 runge 229
Console_RegisterCommand(quit);
2103 runge 230
 
231
function timestampRaw()
232
{
233
  var result = { timestamp: (new Date()).getTime() };
234
 
235
  Log(JSON.stringify(result));
236
  return true;
237
}
238
Console_RegisterCommand(timestampRaw);