Subversion Repositories HomeAutomation

Rev

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

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