Subversion Repositories HomeAutomation

Rev

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