Subversion Repositories HomeAutomation

Rev

Rev 1619 | Rev 1642 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  1.  
  2. var modules = new Array();
  3.  
  4. function RequireModule(name)
  5. {
  6.     GetModule(name);
  7. }
  8.  
  9. function GetModule(name)
  10. {
  11.     if (typeof modules[name] == 'undefined')
  12.     {
  13.         LoadScript("module/" + name + ".js");
  14.        
  15.         if (eval("typeof " + name + " != 'function'"))
  16.         {
  17.             Log("Failed to load " + name + " module.");
  18.             return;
  19.         }
  20.        
  21.         modules[name] = eval("(new " + name + "('" + name + "'))");
  22.     }
  23.    
  24.     return modules[name];
  25. }
  26.  
  27. function Module_OnChange(full_id, available)
  28. {
  29.     var parts = full_id.split(":", 2);
  30.    
  31.     if (global_legacy)
  32.     {
  33.         legacyOnState(full_id, parts[1], parts[0], available);
  34.         return;
  35.     }
  36.    
  37.     var module = GetModule(parts[0]);
  38.    
  39.     if (module)
  40.     {
  41.         module.SetStatus(parts[1], available);
  42.     }
  43. }
  44.  
  45. function Module_OnMessage()
  46. {
  47.     var full_id = arguments[0];
  48.     var command = arguments[1];
  49.     var variables = arguments[2];
  50.    
  51.     var parts = full_id.split(":", 2);
  52.    
  53.     if (global_legacy)
  54.     {
  55.         legacyOnMessage(full_id, parts[1], parts[0], command, variables);
  56.         return;
  57.     }
  58.    
  59.     var module = GetModule(parts[0]);
  60.    
  61.     if (module)
  62.     {
  63.         module.ReceiveMessage(parts[1], command, variables);
  64.     }
  65.     else
  66.     {
  67.         //Log("No one to receive this message, ignoring...");
  68.     }
  69. }
  70.  
  71. function SendModuleMessage(full_id, command, variables)
  72. {
  73.     var args = new Array();
  74.    
  75.     args[args.length] = full_id;
  76.     args[args.length] = command;
  77.     args[args.length] = variables.length;
  78.    
  79.     for (var i = 0; i < variables.length; i++)
  80.     {
  81.         var obj = variables[i];
  82.        
  83.         for (var key in obj)
  84.         {
  85.             args[args.length] = key;
  86.             args[args.length] = obj[key];
  87.         }
  88.     }
  89.    
  90.     Module_SendMessage.apply(null, Array.prototype.slice.call(args, 0));
  91. }
  92.  
  93.  
  94. function Module(name)
  95. {
  96.     this.name_ = name;
  97.     this.ids_ = new Array();
  98.    
  99.     this.EventBase();
  100. }
  101.  
  102. Extend(Module, EventBase);
  103.  
  104. Module.prototype.name_;
  105. Module.prototype.ids_;
  106.  
  107. Module.prototype.SetStatus = function(id, available)
  108. {
  109.     if (typeof this.ids_[id] == 'undefined')
  110.     {
  111.         this.ids_[id] = available;
  112.         this.EventBase.prototype.Trigger.call(this, "onStatusChange", id, available);
  113.     }
  114.     else if (this.ids[id] != available)
  115.     {
  116.         this.EventBase.prototype.Trigger.call(this, "onStatusChange", id, available);
  117.     }
  118. }
  119.  
  120. Module.prototype.ReceiveMessage = function(id, command, variables)
  121. {
  122.     //Log("received " + command);
  123. }
  124.  
  125. Module.prototype.GetAvailableIds = function()
  126. {
  127.     var ids = new Array();
  128.    
  129.     for (var id in this.ids_)
  130.     {
  131.         if (this.ids_[id] && typeof this.ids_[id] != 'function')
  132.         {
  133.             ids[ids.length] = id;
  134.         }
  135.     }
  136.    
  137.     return ids;
  138. }
  139.  
  140. Module.prototype.GetAvailableNames = function()
  141. {
  142.     var result = new Array();
  143.     var available_ids = this.GetAvailableIds();
  144.    
  145.     var module_aliases = getaliases();
  146.    
  147.     for (var alias in module_aliases)
  148.     {
  149.         if (module_aliases[alias]["is_group"])
  150.         {
  151.             for (var n = 0; n < module_aliases[alias]["aliases"].length; n++)
  152.             {
  153.                 var group_alias = module_aliases[alias]["aliases"][n];
  154.  
  155.                 if (module_aliases[group_alias]["module_name"] == this.name_ && ArrayContains(available_ids, module_aliases[group_alias]["module_id"]))
  156.                 {
  157.                     result.push(alias);
  158.                     break;
  159.                 }
  160.             }
  161.         }
  162.         else
  163.         {
  164.             if (module_aliases[alias]["module_name"] == this.name_ && ArrayContains(available_ids, module_aliases[alias]["module_id"]))
  165.             {
  166.                 result.push(alias);
  167.             }
  168.         }
  169.     }
  170.    
  171.     return result;
  172. }
  173.  
  174. Module.prototype.IsAvailable = function(id)
  175. {
  176.     if (typeof this.ids_[id] == 'undefined' || this.ids_[id] == false)
  177.     {
  178.         return false;
  179.     }
  180.    
  181.     return true;
  182. }
  183.  
  184. Module.prototype.SendMessage = function(id, command, variables)
  185. {
  186.     if (this.IsAvailable(id))
  187.     {
  188.         SendModuleMessage(this.name_ + ":" + id, command, variables);
  189.     }
  190. }
  191.  
  192.  
  193. // Console Commands
  194.  
  195. function modulelist()
  196. {
  197.     var result = "";
  198.    
  199.     for (var name in modules)
  200.     {
  201.         result += name + ":  " + modules[name].GetAvailableIds().toString() + "\n";
  202.     }
  203.    
  204.     return result;
  205. }
  206.  
  207. RegisterConsoleCommand(modulelist);
  208.  
  209.