Subversion Repositories HomeAutomation

Rev

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