Subversion Repositories HomeAutomation

Rev

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

  1.  
  2. Require("plugin/module.js");
  3.  
  4. Dimmer_ModuleNames    = [ "Dimmer230", "hwPWM" ];
  5. Dimmer_SpeedLevels    = function() { return [ 50, 135, 200, 255 ]; };
  6. Dimmer_StrengthLevels = function() { return [ 0, 50, 100, 150, 200, 255 ]; };
  7. Dimmer_StepsLevels    = function() { return [ 0, 10, 20, 30, 40, 50, 60, 70, 80, 90 ]; };
  8. Dimmer_Directions     = function() { return [ "Increase", "Decrease" ]; };
  9. Dimmer_Channels       = function() { return [ 0 ]; };
  10. Dimmer_Aliases        = function() { return Module_GetAliasNames(Dimmer_ModuleNames); };
  11. Dimmer_AvailableIds   = function() { return Module_GetAvailableIds(Dimmer_ModuleNames); };
  12.  
  13. function Dimmer_StartFade(alias_name, speed, direction)
  14. {
  15.     if (arguments.length < 3)
  16.     {  
  17.         Log("\033[31mNot enough parameters given.\033[0m\n");
  18.         return false;
  19.     }
  20.    
  21.     var aliases_data = Module_ResolveAlias(alias_name, Dimmer_ModuleNames);
  22.     var found = false;
  23.    
  24.     for (var name in aliases_data)
  25.     {
  26.         var variables = {
  27.             "Channel"   : aliases_data[name]["specific"]["Channel"],
  28.             "Speed"     : speed,
  29.             "Direction" : direction };
  30.    
  31.         if (Module_SendMessage(aliases_data[name]["module_name"], aliases_data[name]["module_id"], "Start_Fade", variables))
  32.         {
  33.             //Log("\033[32mCommand sent successfully to " + name + ".\033[0m\n");
  34.         }
  35.         else
  36.         {
  37.             Log("\033[31mFailed to send command to " + name + ".\033[0m\n");
  38.         }
  39.        
  40.         found = true;
  41.     }
  42.    
  43.     if (!found)
  44.     {
  45.         Log("\033[31mNo aliases by the name " + alias_name + " were applicable for this command.\033[0m\n");
  46.         return false;
  47.     }
  48.    
  49.     return true;
  50. }
  51. Console_RegisterCommand(Dimmer_StartFade, function(arg_index, args) { return Console_StandardAutocomplete(arg_index, args, Dimmer_Aliases(), Dimmer_SpeedLevels(), Dimmer_Directions()); });
  52.  
  53. function Dimmer_StopFade(alias_name)
  54. {
  55.     if (arguments.length < 1)
  56.     {
  57.         Log("\033[31mNot enough parameters given.\033[0m\n");
  58.         return false;
  59.     }
  60.    
  61.     var aliases_data = Module_ResolveAlias(alias_name, Dimmer_ModuleNames);
  62.     var found = false;
  63.    
  64.     for (var name in aliases_data)
  65.     {
  66.         var variables = {
  67.         "Channel" : aliases_data[name]["specific"]["Channel"] };
  68.        
  69.         if (Module_SendMessage(aliases_data[name]["module_name"], aliases_data[name]["module_id"], "Stop_Fade", variables))
  70.         {
  71.             //Log("\033[32mCommand sent successfully to " + name + ".\033[0m\n");
  72.         }
  73.         else
  74.         {
  75.             Log("\033[31mFailed to send command to " + name + ".\033[0m\n");
  76.         }
  77.        
  78.         found = true;
  79.     }
  80.    
  81.     if (!found)
  82.     {
  83.         Log("\033[31mNo aliases by the name " + alias_name + " were applicable for this command.\033[0m\n");
  84.         return false;
  85.     }
  86.    
  87.     return true;
  88. }
  89. Console_RegisterCommand(Dimmer_StopFade, function(arg_index, args) { return Console_StandardAutocomplete(arg_index, args, Dimmer_Aliases()); });
  90.  
  91. function Dimmer_AbsoluteFade(alias_name, speed, level)
  92. {
  93.     if (arguments.length < 3)
  94.     {
  95.         Log("\033[31mNot enough parameters given.\033[0m\n");
  96.         return false;
  97.     }
  98.    
  99.     var aliases_data = Module_ResolveAlias(alias_name, Dimmer_ModuleNames);
  100.     var found = false;
  101.    
  102.     for (var name in aliases_data)
  103.     {
  104.         if (aliases_data[name]["module_name"]=="hwPWM") {
  105.           level = level*39;
  106.         }
  107.         var variables = {
  108.         "Channel"   : aliases_data[name]["specific"]["Channel"],
  109.         "Speed"     : speed,
  110.         "EndValue"  : level };
  111.        
  112.         if (Module_SendMessage(aliases_data[name]["module_name"], aliases_data[name]["module_id"], "Abs_Fade", variables))
  113.         {
  114.             //Log("\033[32mCommand sent successfully to " + name + ".\033[0m\n");
  115.         }
  116.         else
  117.         {
  118.             Log("\033[31mFailed to send command to " + name + ".\033[0m\n");
  119.         }
  120.        
  121.         found = true;
  122.     }
  123.    
  124.     if (!found)
  125.     {
  126.         Log("\033[31mNo aliases by the name " + alias_name + " were applicable for this command.\033[0m\n");
  127.         return false;
  128.     }
  129.    
  130.     return true;
  131. }
  132. Console_RegisterCommand(Dimmer_AbsoluteFade, function(arg_index, args) { return Console_StandardAutocomplete(arg_index, args, Dimmer_Aliases(), Dimmer_SpeedLevels(), Dimmer_StrengthLevels()); });
  133.  
  134. function Dimmer_RelativeFade(alias_name, speed, direction, steps)
  135. {
  136.     if (arguments.length < 4)
  137.     {
  138.         Log("\033[31mNot enough parameters given.\033[0m\n");
  139.         return false;
  140.     }
  141.    
  142.     var aliases_data = Module_ResolveAlias(alias_name, Dimmer_ModuleNames);
  143.     var found = false;
  144.    
  145.     for (var name in aliases_data)
  146.     {
  147.         if (aliases_data[name]["module_name"]=="hwPWM") {
  148.           steps = steps*39;
  149.         }
  150.         var variables = {
  151.         "Channel"   : aliases_data[name]["specific"]["Channel"],
  152.         "Speed"     : speed,
  153.         "Direction" : direction,
  154.         "Steps"     : steps };
  155.        
  156.         if (Module_SendMessage(aliases_data[name]["module_name"], aliases_data[name]["module_id"], "Rel_Fade", variables))
  157.         {
  158.             //Log("\033[32mCommand sent successfully to " + name + ".\033[0m\n");
  159.         }
  160.         else
  161.         {
  162.             Log("\033[31mFailed to send command to " + name + ".\033[0m\n");
  163.         }
  164.        
  165.         found = true;
  166.     }
  167.    
  168.     if (!found)
  169.     {
  170.         Log("\033[31mNo aliases by the name " + alias_name + " were applicable for this command.\033[0m\n");
  171.         return false;
  172.     }
  173.    
  174.     return true;
  175. }
  176. Console_RegisterCommand(Dimmer_RelativeFade, function(arg_index, args) { return Console_StandardAutocomplete(arg_index, args, Dimmer_Aliases(), Dimmer_SpeedLevels(), Dimmer_Directions(), Dimmer_StepsLevels()); });
  177.  
  178. function Dimmer_Demo(alias_name, speed, steps)
  179. {
  180.     if (arguments.length < 3)
  181.     {
  182.         Log("\033[31mNot enough parameters given.\033[0m\n");
  183.         return false;
  184.     }
  185.    
  186.     var aliases_data = Module_ResolveAlias(alias_name, Dimmer_ModuleNames);
  187.     var found = false;
  188.    
  189.     for (var name in aliases_data)
  190.     {
  191.         if (aliases_data[name]["module_name"]=="hwPWM") {
  192.           steps = steps*39;
  193.         }
  194.         var variables = {
  195.         "Channel"   : aliases_data[name]["specific"]["Channel"],
  196.         "Speed"     : speed,
  197.         "Steps"     : steps };
  198.        
  199.         if (Module_SendMessage(aliases_data[name]["module_name"], aliases_data[name]["module_id"], "Demo", variables))
  200.         {
  201.             //Log("\033[32mCommand sent successfully to " + name + ".\033[0m\n");
  202.         }
  203.         else
  204.         {
  205.             Log("\033[31mFailed to send command to " + name + ".\033[0m\n");
  206.         }
  207.        
  208.         found = true;
  209.     }
  210.    
  211.     if (!found)
  212.     {
  213.         Log("\033[31mNo aliases by the name " + alias_name + " were applicable for this command.\033[0m\n");
  214.         return false;
  215.     }
  216.    
  217.     return true;
  218. }
  219. Console_RegisterCommand(Dimmer_Demo, function(arg_index, args) { return Console_StandardAutocomplete(arg_index, args, Dimmer_Aliases(), Dimmer_SpeedLevels(), Dimmer_StepsLevels()); });
  220.  
  221. function Dimmer_OnMessage(module_name, module_id, command, variables)
  222. {
  223.     if (in_array(Dimmer_ModuleNames, module_name))
  224.     {
  225.         var aliases_data = Module_LookupAliases({
  226.             "module_name" : module_name,
  227.             "module_id"   : module_id,
  228.             "group"       : false
  229.         });
  230.        
  231.         switch (command)
  232.         {
  233.             case "Netinfo":
  234.             {
  235.                 for (var alias_name in aliases_data)
  236.                 {
  237.                     if (aliases_data[alias_name]["specific"]["Channel"] != variables["Channel"])
  238.                     {
  239.                         continue;
  240.                     }
  241.                    
  242.                     var last_value = {};
  243.                     var last_value_string = Storage_GetParameter("LastValues", alias_name);
  244.  
  245.                     if (last_value_string)
  246.                     {
  247.                         last_value = eval("(" + last_value_string + ")");
  248.                     }
  249.                    
  250.                     last_value["Connection"] = { "value" : variables["Connection"], "timestamp" : get_time() };
  251.                     last_value["Frequency"] = { "value" : variables["Frequency"], "timestamp" : get_time() };
  252.                     last_value["Level"] = { "value" : variables["DimmerValue"], "timestamp" : get_time() };
  253.                    
  254.                     Storage_SetParameter("LastValues", alias_name, JSON.stringify(last_value));
  255.                 }
  256.                
  257.                 break;
  258.             }
  259.             case "Pwm":
  260.             {
  261.                 for (var alias_name in aliases_data)
  262.                 {
  263.                     if (aliases_data[alias_name]["specific"]["Channel"] != variables["Channel"])
  264.                     {
  265.                         continue;
  266.                     }
  267.                    
  268.                     var last_value = {};
  269.                     var last_value_string = Storage_GetParameter("LastValues", alias_name);
  270.  
  271.                     if (last_value_string)
  272.                     {
  273.                         last_value = eval("(" + last_value_string + ")");
  274.                     }
  275.                     var value = 0 + variables["Value"];
  276.                     value = value / 39;
  277.                     value = parseInt(value).toString();
  278.                     last_value["Level"] = { "value" : value, "timestamp" : get_time() };
  279.                    
  280.                     Storage_SetParameter("LastValues", alias_name, JSON.stringify(last_value));
  281.                 }
  282.                
  283.                 break;
  284.             }
  285.         }
  286.     }
  287. }
  288. Module_RegisterToOnMessage("all", Dimmer_OnMessage);
  289.