Subversion Repositories HomeAutomation

Rev

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