Subversion Repositories HomeAutomation

Rev

Rev 1998 | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  1.  
  2. IRIn_ModuleNames    = [ "irReceive", "rfTransceive" ];
  3. IRIn_Channels       = function() { return [ 0 ]; };
  4. IRIn_Aliases        = function() { return Module_GetAliasNames(IRIn_ModuleNames); };
  5. IRIn_AvailableIds   = function() { return Module_GetAvailableIds(IRIn_ModuleNames); };
  6. IRIn_Remotes        = function() { return get_keys(Storage_GetParameters("RemoteList")); };
  7.  
  8. IRIn_RecordRemoteName = null;
  9. IRIn_RecordAliasData = null;
  10. IRIn_RecordLast = null;
  11. IRIn_RecordClientId = null;
  12. IRIn_OnMessageFunctions = null;
  13.  
  14. for (var i=0; i<IRIn_Aliases().length; i++)
  15. {
  16.     /* Register to messages from all ir aliases */
  17.     Module_RegisterToOnMessage(IRIn_Aliases()[i], IRIn_IrMessage);
  18. }
  19. function IRIn_IrMessage(alias_name, command, variables)
  20. {
  21.     /* First check if there are any callbacks registered */
  22.     if (IRIn_OnMessageFunctions == null)
  23.     {
  24.         return false;
  25.     }
  26.     /* Also check if there are any callbacks registered to this alias */
  27.     if (!IRIn_OnMessageFunctions[alias_name])
  28.     {
  29.         return false;
  30.     }
  31.     /* Check if code and protocol maches one or more remote */
  32.     var remotes = get_keys(Storage_GetParameters("RemoteList"));
  33.     for (var i=0; i < remotes.length; i++)
  34.     {
  35.         var buttons = get_keys(Storage_GetParameters("Remote_"+remotes[i]));
  36.         for (var j=0; j < buttons.length; j++)
  37.         {
  38.             var button_string = Storage_GetParameter("Remote_"+remotes[i], buttons[j]);
  39.             var button_data = eval("(" + button_string + ")");
  40.    
  41.             if (typeof button_data["protocol"] == 'undefined' || typeof button_data["data"] == 'undefined')
  42.             {
  43.                 Log("\033[31mButton data is corrupt.\033[0m\n");
  44.                 return false;
  45.             }
  46.            
  47.             if (button_data["protocol"] == variables["Protocol"] && button_data["data"] == variables["IRdata"])
  48.             {
  49.                 //Log("Found match: "+remotes[i]+" "+buttons[j]+" "+button_data["protocol"]+" "+button_data["data"]+"");
  50.                 if (IRIn_OnMessageFunctions[alias_name])
  51.                 {
  52.                     for (var n in IRIn_OnMessageFunctions[alias_name])
  53.                     {
  54.                         //Log("Found callback, calling");
  55.                         /* Call callback with arguments remotes[i] buttons[j] and variables["Status"]*/
  56.                         IRIn_OnMessageFunctions[alias_name][n](alias_name, remotes[i], buttons[j], variables["Status"]);
  57.                     }
  58.                 }
  59.             }
  60.         }
  61.     }
  62.     return true;
  63. }
  64.  
  65. function IRIn_RegisterToMessage(alias_name, callback)
  66. {
  67.     if (IRIn_OnMessageFunctions == null)
  68.     {
  69.         IRIn_OnMessageFunctions = new Array();
  70.     }
  71.    
  72.     if (!IRIn_OnMessageFunctions[alias_name])
  73.     {
  74.         IRIn_OnMessageFunctions[alias_name] = new Array();
  75.     }
  76.    
  77.     IRIn_OnMessageFunctions[alias_name].push(callback);
  78. }
  79.  
  80. function IRIn_CodeToName(data, protocol, remote_name)
  81. {
  82.     var remote_storage_name = Storage_GetParameter("RemoteList", remote_name);
  83.  
  84.         if (!remote_storage_name)
  85.         {
  86.         Log("No such remote " + remote_name);
  87.                 return "";
  88.         }
  89.  
  90.     var buttons = Storage_GetParameters(remote_storage_name);
  91.  
  92.     for (var button_name in buttons)
  93.     {
  94.         var button_data = eval("(" + buttons[button_name]+ ")");
  95.  
  96.         Log(buttons[button_name]);
  97.  
  98.         if (button_data["data"] == (data + '') && button_data["protocol"] == protocol)
  99.         {
  100.             return button_name;
  101.         }
  102.     }
  103.  
  104.     Log("Found no matching button on remote " + remote_name);
  105.     return "";
  106. }
  107.  
  108. function IRIn_Record(alias_name, remote_name)
  109. {
  110.     if (arguments.length < 2)
  111.     {
  112.         Log("\033[31mNot enough parameters given.\033[0m\n");
  113.         return false;
  114.     }
  115.    
  116.     IRIn_RecordRemoteName = Storage_GetParameter("RemoteList", remote_name);
  117.    
  118.     if (!IRIn_RecordRemoteName)
  119.     {
  120.         IRIn_RecordRemoteName = "Remote_" + remote_name;
  121.        
  122.         Log("\033[32mNo such remote name in list, " + remote_name + ", creating...\033[0m\n");
  123.        
  124.         Storage_SetParameter("RemoteList", remote_name, IRIn_RecordRemoteName);
  125.     }
  126.    
  127.     var aliases_data = Module_ResolveAlias(alias_name, IRIn_ModuleNames);
  128.    
  129.     for (var name in aliases_data)
  130.     {
  131.         if (aliases_data[name]["group"])
  132.         {
  133.             Log("\033[31mAlias must not be an alias group.\033[0m\n");
  134.             return false;
  135.         }
  136.        
  137.         IRIn_RecordClientId = Console_GetClientId();
  138.        
  139.         IRIn_RecordAliasData = aliases_data[name];
  140.         Log("To finish press a random key on the remote and then enter dot as name.\n");
  141.        
  142.         Log("\033[29mPress a button on you remote...\033[0m\n");
  143.         Console_PreventDefaultPrompt();
  144.    
  145.         Module_RegisterToOnMessage(alias_name, IRIn_RecordOnIrMessage);
  146.         return true;
  147.     }
  148.    
  149.     Log("\033[31mNo such alias name found, " + alias_name + ".\033[0m\n");
  150.     return false;
  151.    
  152. }
  153. Console_RegisterCommand(IRIn_Record, function(arg_index, args) { return Console_StandardAutocomplete(arg_index, args, IRIn_Aliases(), IRIn_Remotes()); });
  154.  
  155. function IRIn_RecordOnResponse(response)
  156. {
  157.     if (response == ".")
  158.     {
  159.         IRIn_RecordClientId = null;
  160.         return true;
  161.     }
  162.    
  163.     Storage_SetParameter(IRIn_RecordRemoteName, response, JSON.stringify(IRIn_RecordLast));
  164.     IRIn_RecordLast = null;
  165.    
  166.     Log("\033[29mPress a button on you remote...\033[0m\n");
  167.     Console_PreventDefaultPrompt();
  168.    
  169.     return true;
  170. }
  171.  
  172. function IRIn_RecordOnIrMessage(alias_name, command, variables)
  173. {
  174.     if (variables["Status"] != "Pressed" || IRIn_RecordLast != null)
  175.     {
  176.         return;
  177.     }
  178.    
  179.     ConsoleExport_LogToClient(IRIn_RecordClientId, "\033[32mGot code " + variables["IRdata"] + " on protocol " + variables["Protocol"] + ".\033[0m\n");
  180.    
  181.     IRIn_RecordLast = { "protocol" : variables["Protocol"], "data" : variables["IRdata"] };
  182.    
  183.     Console_PromptRequest("\033[29mEnter name: \033[0m", IRIn_RecordOnResponse);
  184.    
  185.     return true;
  186. }
  187.