Subversion Repositories HomeAutomation

Rev

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

  1.  
  2. Node_ResetNodeId = null;
  3. Node_ResetClientId = null;
  4.  
  5. Node_ProgramNodeId = null;
  6. Node_ProgramClientId = null;
  7.  
  8. Node_WaitClientId = null;
  9.  
  10. Node_Native_Reset = false;
  11. Node_Native_Program = false;
  12. //Node_Native_WaitNodeId = null;
  13.  
  14. Node_HexData = "";
  15. Node_HexNumLines = 0;
  16.  
  17. function Node_OnChange(node_id, available)
  18. {
  19. Log("Node_OnChange "+Node_ResetNodeId+" "+Node_ProgramNodeId+" "+node_id+" "+available+"\n");
  20.     if (node_id == Node_ResetNodeId && available)
  21.     {
  22. Log("ResetNodeId "+Node_Native_Reset+"\n");
  23.         Console_LogToClient(Node_ResetClientId, "\033[32m" + Node_ResetNodeId + " started okay.\033[0m\n");
  24.  
  25.         Console_NewConnection(Node_ResetClientId); // This is a ugly hack for a bad design for multiusers
  26.         Node_ResetNodeId = null;
  27.         Node_ResetClientId = null;
  28.        
  29.         Node_Native_Reset = true;
  30.     }
  31.  
  32.     if (node_id == Node_ProgramNodeId && available)
  33.     {
  34. Log("ProgramNodeId "+Node_Native_Program+"\n");
  35.         Console_LogToClient(Node_ProgramClientId, "\033[32m" + Node_ProgramNodeId + " started okay.\033[0m\n");
  36.  
  37.         Console_NewConnection(Node_ProgramClientId); // This is a ugly hack for a bad design for multiusers
  38.         Node_ProgramNodeId = null;
  39.         Node_ProgramClientId = null;
  40.        
  41.         Node_Native_Program = true;
  42.     }
  43.  
  44.     if (Node_WaitClientId != null && available)
  45.     {
  46.         var result = NodeExport_GetNodeInformation(node_id);
  47.  
  48.         if (!result)
  49.         {
  50.             Console_LogToClient(Node_WaitClientId, "\033[31mNo node with id " + node_id + " found.\033[0m\n");
  51.             return false;
  52.         }
  53.  
  54.         var date = new Date(result["LastActive"] * 1000);
  55.  
  56.         Console_LogToClient(Node_WaitClientId, "Id: " + result["Id"] + "\n");
  57.         Console_LogToClient(Node_WaitClientId, "Valid: " + result["Valid"] + "\n");
  58.         Console_LogToClient(Node_WaitClientId, "Bios Version: " + result["BiosVersion"] + "\n");
  59.         Console_LogToClient(Node_WaitClientId, "Device Type: " + result["DeviceType"] + "\n");
  60.         Console_LogToClient(Node_WaitClientId, "Has Application: " + result["HasApplication"] + "\n");
  61.         Console_LogToClient(Node_WaitClientId, "Last Active: " + date.toString() + "\n");
  62.  
  63.         Console_NewConnection(Node_WaitClientId); // This is a ugly hack for a bad design for multiusers
  64.         Node_WaitClientId = null;
  65.     }
  66. }
  67.  
  68. function Node_GetAvailableIds()
  69. {
  70.     var result_list = [];
  71.     var available_nodes = NodeExport_GetAvailableNodes();
  72.  
  73.     for (var n in available_nodes)
  74.     {
  75.         var id_parts = available_nodes[n].split(",", 2);
  76.  
  77.         result_list.push(id_parts[0]);
  78.     }
  79.  
  80.     return result_list;
  81. }
  82.  
  83. function Node_ListAvailable()
  84. {
  85.     var available_nodes = NodeExport_GetAvailableNodes();
  86.  
  87.     var lines = [["Id", "Modules"]];
  88.  
  89.     for (var n in available_nodes)
  90.     {
  91.         var parts = available_nodes[n].split(",");
  92.         var id = parts.shift();
  93.         var modules = "<none>";
  94.  
  95.         if (parts.length > 0)
  96.         {
  97.             modules = parts.join(", ");
  98.         }
  99.  
  100.         lines.push([id, modules]);
  101.     }
  102.  
  103.     var table_lines = create_table(lines);
  104.  
  105.     Log("\033[29;1m" + table_lines[0] + "\033[0m\n");
  106.  
  107.     for (var n = 1; n < table_lines.length; n++)
  108.     {
  109.         Log(table_lines[n] + "\n");
  110.     }
  111.  
  112.     return true;
  113. }
  114. Console_RegisterCommand(Node_ListAvailable);
  115.  
  116. function Node_ResetPollNative()
  117. {
  118.     return Node_Native_Reset;
  119. }
  120.  
  121. function Node_ResetNative(node_id)
  122. {
  123.     if (arguments.length < 1)
  124.     {
  125.         return "\033[31mError: Not enough parameters given.\033[0m";
  126.     }
  127.  
  128.     Node_Native_Reset = false;
  129.     if (!NodeExport_ResetNode(node_id))
  130.     {
  131.         return "\033[31mError: Failed to send reset, this can happen if node only contains bios and it was connected before atom was started/restarted.\033[0m";
  132.     }
  133.  
  134.     Node_ResetClientId = Console_GetClientId();
  135.     Node_ResetNodeId = node_id;
  136.  
  137.     return "Reset command sent successfully";
  138. }
  139.  
  140. function Node_Reset(node_id)
  141. {
  142.     if (arguments.length < 1)
  143.     {
  144.         Log("\033[31mNot enough parameters given.\033[0m\n");
  145.         return false;
  146.     }
  147.  
  148.     Log("Sending reset to " + node_id + "...\n");
  149.  
  150.     if (!NodeExport_ResetNode(node_id))
  151.     {
  152.         Log("\033[31mFailed to send reset, this can happen if node only contains bios and it was connected before atom was started/restarted.\033[0m\n");
  153.         return false;
  154.     }
  155.  
  156.     Node_ResetClientId = Console_GetClientId();
  157.     Node_ResetNodeId = node_id;
  158.     Console_PreventDefaultPrompt();
  159.  
  160.     return true;
  161. }
  162. Console_RegisterCommand(Node_Reset, function(arg_index, args) { return Console_StandardAutocomplete(arg_index, args, Node_GetAvailableIds()); });
  163.  
  164. function Node_ProgramPollNative()
  165. {
  166.     return Node_Native_Program;
  167. }
  168.  
  169. function Node_AppendHexNative(hexrow)
  170. {
  171.     if (arguments.length < 1)
  172.     {
  173.         return "\033[31mError: Not enough parameters given.\033[0m";
  174.     }
  175.  
  176.     Node_HexData = Node_HexData + hexrow + "\n";
  177.     Node_HexNumLines += 1;
  178.  
  179.     return true;
  180. }
  181.  
  182. function Node_ProgramNative(node_id, bios, hexnumlines)
  183. {
  184.     if (arguments.length < 3)
  185.     {
  186.         return "\033[31mError: Not enough parameters given.\033[0m";
  187.     }
  188.     if (Node_HexNumLines != hexnumlines)
  189.     {
  190.         return "\033[31mError: Number of hex lines does not match received (" + Node_HexNumLines + " received, should be " + hexnumlines + ").\033[0m";
  191.     }
  192.    
  193.     Node_Native_Program = false;
  194.     if (!NodeExport_ProgramNodeHex(node_id, bios > 0, Node_HexData))
  195.     {
  196.         return "\033[31mError: Failed to start programming, this can happen if node only contains bios and it was connected before atom was started/restarted.\033[0m";
  197.     }
  198.  
  199.     Node_ProgramClientId = Console_GetClientId();
  200.     Node_ProgramNodeId = node_id;
  201.     Node_HexData = "";
  202.     Node_HexNumLines = 0;
  203.  
  204.     return "Program command sent successfully";
  205. }
  206.  
  207. function Node_Program(node_id, bios, filename)
  208. {
  209.     if (arguments.length < 3)
  210.     {
  211.         Log("\033[31mNot enough parameters given.\033[0m\n");
  212.         return false;
  213.     }
  214.  
  215.     Log("Initiating programming of " + node_id + "...\n");
  216.  
  217.     if (!NodeExport_ProgramNode(node_id, bios > 0, filename))
  218.     {
  219.         Log("\033[31mFailed to start programming, this can happen if node only contains bios and it was connected before atom was started/restarted.\033[0m\n");
  220.         return false;
  221.     }
  222.  
  223.     Node_ProgramClientId = Console_GetClientId();
  224.     Node_ProgramNodeId = node_id;
  225.     Console_PreventDefaultPrompt();
  226.  
  227.     return true;
  228. }
  229. Console_RegisterCommand(Node_Program, function(arg_index, args) { return Console_StandardAutocomplete(arg_index, args, Node_GetAvailableIds(), [0, 1]); });
  230.  
  231. function Node_ProgramHex(node_id, bios, hexnumlines)
  232. {
  233.     if (arguments.length < 2)
  234.     {
  235.         Log("\033[31mNot enough parameters given.\033[0m\n");
  236.         return false;
  237.     }
  238.     if (Node_HexNumLines != hexnumlines)
  239.     {
  240.         Log("\033[31mNumber of hex lines does not match received (" + Node_HexNumLines + " received, should be " + hexnumlines + ").\033[0m\n");
  241.         return false;
  242.     }
  243.  
  244.     Log("Initiating programming of " + node_id + "...\n");
  245.  
  246.     if (!NodeExport_ProgramNodeHex(node_id, bios > 0, Node_HexData))
  247.     {
  248.         Log("\033[31mFailed to start programming, this can happen if node only contains bios and it was connected before atom was started/restarted.\033[0m\n");
  249.         return false;
  250.     }
  251.  
  252.     Node_ProgramClientId = Console_GetClientId();
  253.     Node_ProgramNodeId = node_id;
  254.     Console_PreventDefaultPrompt();
  255.  
  256.     Node_HexData = "";
  257.     Node_HexNumLines = 0;
  258.  
  259.  
  260.     return true;
  261. }
  262. Console_RegisterCommand(Node_ProgramHex, function(arg_index, args) { return Console_StandardAutocomplete(arg_index, args, Node_GetAvailableIds(), [0, 1]); });
  263.  
  264. function Node_ClearHex()
  265. {
  266. //    Log("Clearing hexdata...\n");
  267.  
  268.     Node_HexData = "";
  269.     Node_HexNumLines = 0;
  270.  
  271.     return true;
  272. }
  273. Console_RegisterCommand(Node_ClearHex);
  274.  
  275. function Node_AppendHex(hexrow)
  276. {
  277.     if (arguments.length < 1)
  278.     {
  279.         Log("\033[31mNot enough parameters given.\033[0m\n");
  280.         return false;
  281.     }
  282.  
  283. //    Log("Appending hexrow...\n");
  284.  
  285.     Node_HexData = Node_HexData + hexrow + "\n";
  286.     Node_HexNumLines += 1;
  287.  
  288.     return true;
  289. }
  290. Console_RegisterCommand(Node_AppendHex);
  291.  
  292. function Node_GetInformation(node_id)
  293. {
  294.     if (arguments.length < 1)
  295.     {
  296.         Log("\033[31mNot enough parameters given.\033[0m\n");
  297.         return false;
  298.     }
  299.  
  300.     var result = NodeExport_GetNodeInformation(node_id);
  301.  
  302.     if (!result)
  303.     {
  304.         Log("\033[31mNo node with id " + node_id + " found.\033[0m\n");
  305.         return false;
  306.     }
  307.  
  308.     var date = new Date(result["LastActive"] * 1000);
  309.  
  310.     Log("\033[96mId: \033[0;1m" + result["Id"] + "\033[0m\n");
  311.     Log("\033[96mValid: \033[0;1m" + result["Valid"] + "\033[0m\n");
  312.     Log("\033[96mBios Version: \033[0;1m" + result["BiosVersion"] + "\033[0m\n");
  313.     Log("\033[96mDevice Type: \033[0;1m" + result["DeviceType"] + "\033[0m\n");
  314.     Log("\033[96mHas Application: \033[0;1m" + result["HasApplication"] + "\033[0m\n");
  315.     Log("\033[96mLast Active: \033[0;1m" + date.toString() + "\033[0m\n");
  316.  
  317.     return true;
  318. }
  319. Console_RegisterCommand(Node_GetInformation, function(arg_index, args) { return Console_StandardAutocomplete(arg_index, args, Node_GetAvailableIds()); });
  320.  
  321. function Node_WaitForInformation()
  322. {
  323.     Node_WaitClientId = Console_GetClientId();
  324.     Console_PreventDefaultPrompt();
  325.  
  326.     return true;
  327. }
  328. Console_RegisterCommand(Node_WaitForInformation);
  329.