Subversion Repositories HomeAutomation

Rev

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

  1.  
  2. Require
  3.  
  4. Console_Functions = {};
  5. Console_CurrentFunction = null;
  6. Console_DefaultPrompt = "\033[29;1m[atomic] \033[0m";
  7. Console_LastClientId = null;
  8.  
  9. function Console_GetFunctionNames()
  10. {
  11.     return get_keys(Console_Functions);
  12. }
  13.  
  14. // C++ callable functions
  15.  
  16. function Console_AutocompleteRequest(client_id, arg_index, commandline)
  17. {
  18.     Console_LastClientId = client_id;
  19.    
  20.     var result = [];
  21.     var parts = [];
  22.     var name = "Console_Shell";
  23.    
  24.     if (commandline.length > 0 && arg_index != 0)
  25.     {
  26.         parts = commandline.split(" ");
  27.         name = parts.shift();
  28.         arg_index--;
  29.     }
  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.     }
  36.    
  37.     var result_string = result.join("\n");
  38.    
  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.    
  47.     var parts = array_remove_empty(response.split(" "));
  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)
  55.     {
  56.         Console_SetDefaultPrompt();
  57.     }
  58.    
  59.     return result;
  60. }
  61.  
  62. function Console_NewConnection(client_id)
  63. {
  64.     Console_LastClientId = client_id;
  65.    
  66.     return Console_SetDefaultPrompt();
  67. }
  68.  
  69. // User script functions
  70.  
  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.  
  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.  
  102. function Console_RegisterCommand(command, autocomplete_callback)
  103. {
  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.    
  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)
  133.     {
  134.         return arguments[arg_index + 2];
  135.     }
  136.    
  137.     return [];
  138. }
  139.  
  140. // Default call function
  141.  
  142. function Console_Shell()
  143. {
  144.     var args = to_array(arguments);
  145.     var name = args.shift();
  146.    
  147.     if (Console_Functions[name] && Console_Functions[name]["command"])
  148.     {
  149.         return Console_Functions[name]["command"].apply(null, Array.prototype.slice.call(args, 0));
  150.     }
  151.     else
  152.     {
  153.         Log("\033[31mNo such command found, " + name + ".\033[0m\n");
  154.     }
  155.    
  156.     return false;
  157. }
  158. Console_RegisterCommand(Console_Shell, function(arg_index, args) { return Console_StandardAutocomplete(arg_index, args, Console_GetFunctionNames()); });
  159.  
  160. // Help functions
  161.  
  162. function help_complete(arg_index, args)
  163. {
  164.     var result = [];
  165.    
  166.     if (arg_index == 1) // command name
  167.     {
  168.         for (var name in Console_Functions)
  169.         {
  170.             result.push(name);
  171.         }
  172.     }
  173.    
  174.     return result;
  175. }
  176.  
  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.  
  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);
  217.  
  218. function quit()
  219. {
  220.     Log("Goodbye!\n");
  221.    
  222.     ConsoleExport_DisconnectClient(Console_LastClientId);
  223.     Console_LastClientId = null;
  224.     return false;
  225. }
  226. Console_RegisterCommand(quit);
  227.