Subversion Repositories HomeAutomation

Rev

Rev 2162 | Rev 2170 | 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_GetProgramProgress(node_id)
  216. {
  217.     if (arguments.length < 1)
  218.     {
  219.         return "\033[31mError: Not enough parameters given.\033[0m";
  220.     }
  221.    
  222.     //if (!NodeExport_ProgramNodeHex(node_id, bios > 0, Node_HexData))
  223.     //{
  224.     //    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";
  225.     //}
  226.  
  227.     return NodeExport_GetProgramProgress(node_id);
  228. }
  229.  
  230. function Node_ProgramNative(node_id, bios, hexnumlines)
  231. {
  232.     if (arguments.length < 3)
  233.     {
  234.         return "\033[31mError: Not enough parameters given.\033[0m";
  235.     }
  236.     if (Node_HexNumLines != hexnumlines)
  237.     {
  238.         return "\033[31mError: Number of hex lines does not match received (" + Node_HexNumLines + " received, should be " + hexnumlines + ").\033[0m";
  239.     }
  240.    
  241.     Node_Native_Program = false;
  242.     if (!NodeExport_ProgramNodeHex(node_id, bios > 0, Node_HexData))
  243.     {
  244.         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";
  245.     }
  246.  
  247.     Node_ProgramClientId = Console_GetClientId();
  248.     Node_ProgramNodeId = node_id;
  249.     Node_HexData = "";
  250.     Node_HexNumLines = 0;
  251.  
  252.     return "Program command sent successfully";
  253. }
  254.  
  255. function Node_Program(node_id, bios, filename)
  256. {
  257.     if (arguments.length < 3)
  258.     {
  259.         Log("\033[31mNot enough parameters given.\033[0m\n");
  260.         return false;
  261.     }
  262.  
  263.     Log("Initiating programming of " + node_id + "...\n");
  264.  
  265.     if (!NodeExport_ProgramNode(node_id, bios > 0, filename))
  266.     {
  267.         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");
  268.         return false;
  269.     }
  270.  
  271.     Node_ProgramClientId = Console_GetClientId();
  272.     Node_ProgramNodeId = node_id;
  273.     Console_PreventDefaultPrompt();
  274.  
  275.     return true;
  276. }
  277. Console_RegisterCommand(Node_Program, function(arg_index, args) { return Console_StandardAutocomplete(arg_index, args, Node_GetAvailableIds(), [0, 1]); });
  278.  
  279. function Node_ProgramHex(node_id, bios, hexnumlines)
  280. {
  281.     if (arguments.length < 2)
  282.     {
  283.         Log("\033[31mNot enough parameters given.\033[0m\n");
  284.         return false;
  285.     }
  286.     if (Node_HexNumLines != hexnumlines)
  287.     {
  288.         Log("\033[31mNumber of hex lines does not match received (" + Node_HexNumLines + " received, should be " + hexnumlines + ").\033[0m\n");
  289.         return false;
  290.     }
  291.  
  292.     Log("Initiating programming of " + node_id + "...\n");
  293.  
  294.     if (!NodeExport_ProgramNodeHex(node_id, bios > 0, Node_HexData))
  295.     {
  296.         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");
  297.         return false;
  298.     }
  299.  
  300.     Node_ProgramClientId = Console_GetClientId();
  301.     Node_ProgramNodeId = node_id;
  302.     Console_PreventDefaultPrompt();
  303.  
  304.     Node_HexData = "";
  305.     Node_HexNumLines = 0;
  306.  
  307.  
  308.     return true;
  309. }
  310. Console_RegisterCommand(Node_ProgramHex, function(arg_index, args) { return Console_StandardAutocomplete(arg_index, args, Node_GetAvailableIds(), [0, 1]); });
  311.  
  312. function Node_ClearHex()
  313. {
  314. //    Log("Clearing hexdata...\n");
  315.  
  316.     Node_HexData = "";
  317.     Node_HexNumLines = 0;
  318.  
  319.     return true;
  320. }
  321. Console_RegisterCommand(Node_ClearHex);
  322.  
  323. function Node_AppendHex(hexrow)
  324. {
  325.     if (arguments.length < 1)
  326.     {
  327.         Log("\033[31mNot enough parameters given.\033[0m\n");
  328.         return false;
  329.     }
  330.  
  331. //    Log("Appending hexrow...\n");
  332.  
  333.     Node_HexData = Node_HexData + hexrow + "\n";
  334.     Node_HexNumLines += 1;
  335.  
  336.     return true;
  337. }
  338. Console_RegisterCommand(Node_AppendHex);
  339.  
  340. function Node_GetInformation(node_id)
  341. {
  342.     if (arguments.length < 1)
  343.     {
  344.         Log("\033[31mNot enough parameters given.\033[0m\n");
  345.         return false;
  346.     }
  347.  
  348.     var result = NodeExport_GetNodeInformation(node_id);
  349.  
  350.     if (!result)
  351.     {
  352.         Log("\033[31mNo node with id " + node_id + " found.\033[0m\n");
  353.         return false;
  354.     }
  355.  
  356.     var date = new Date(result["LastActive"] * 1000);
  357.  
  358.     Log("\033[96mId: \033[0;1m" + result["Id"] + "\033[0m\n");
  359.     Log("\033[96mValid: \033[0;1m" + result["Valid"] + "\033[0m\n");
  360.     Log("\033[96mBios Version: \033[0;1m" + result["BiosVersion"] + "\033[0m\n");
  361.     Log("\033[96mDevice Type: \033[0;1m" + result["DeviceType"] + "\033[0m\n");
  362.     Log("\033[96mHas Application: \033[0;1m" + result["HasApplication"] + "\033[0m\n");
  363.     Log("\033[96mLast Active: \033[0;1m" + date.toString() + "\033[0m\n");
  364.  
  365.     return true;
  366. }
  367. Console_RegisterCommand(Node_GetInformation, function(arg_index, args) { return Console_StandardAutocomplete(arg_index, args, Node_GetAvailableIds()); });
  368.  
  369. function Node_WaitForInformation()
  370. {
  371.     Node_WaitClientId = Console_GetClientId();
  372.     Console_PreventDefaultPrompt();
  373.  
  374.     return true;
  375. }
  376. Console_RegisterCommand(Node_WaitForInformation);
  377.