Subversion Repositories HomeAutomation

Rev

Rev 1673 | Rev 1681 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

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