Subversion Repositories HomeAutomation

Rev

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