Subversion Repositories HomeAutomation

Rev

Rev 1673 | Rev 1681 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed

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