Subversion Repositories HomeAutomation

Rev

Rev 1897 | Rev 2079 | 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_in)
  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.     var level = level_in;
  102.    
  103.     for (var name in aliases_data)
  104.     {
  105.         if (aliases_data[name]["module_name"]=="hwPWM") {
  106.           level = level_in*39;
  107.         }
  108.         var variables = {
  109.         "Channel"   : aliases_data[name]["specific"]["Channel"],
  110.         "Speed"     : speed,
  111.         "EndValue"  : level };
  112.        
  113.         if (Module_SendMessage(aliases_data[name]["module_name"], aliases_data[name]["module_id"], "Abs_Fade", variables))
  114.         {
  115.             //Log("\033[32mCommand sent successfully to " + name + ".\033[0m\n");
  116.         }
  117.         else
  118.         {
  119.             Log("\033[31mFailed to send command to " + name + ".\033[0m\n");
  120.         }
  121.        
  122.         found = true;
  123.     }
  124.    
  125.     if (!found)
  126.     {
  127.         Log("\033[31mNo aliases by the name " + alias_name + " were applicable for this command.\033[0m\n");
  128.         return false;
  129.     }
  130.    
  131.     return true;
  132. }
  133. Console_RegisterCommand(Dimmer_AbsoluteFade, function(arg_index, args) { return Console_StandardAutocomplete(arg_index, args, Dimmer_Aliases(), Dimmer_SpeedLevels(), Dimmer_StrengthLevels()); });
  134.  
  135. function Dimmer_RelativeFade(alias_name, speed, direction, steps)
  136. {
  137.     if (arguments.length < 4)
  138.     {
  139.         Log("\033[31mNot enough parameters given.\033[0m\n");
  140.         return false;
  141.     }
  142.    
  143.     var aliases_data = Module_ResolveAlias(alias_name, Dimmer_ModuleNames);
  144.     var found = false;
  145.    
  146.     for (var name in aliases_data)
  147.     {
  148.         if (aliases_data[name]["module_name"]=="hwPWM") {
  149.           steps = steps*39;
  150.         }
  151.         var variables = {
  152.         "Channel"   : aliases_data[name]["specific"]["Channel"],
  153.         "Speed"     : speed,
  154.         "Direction" : direction,
  155.         "Steps"     : steps };
  156.        
  157.         if (Module_SendMessage(aliases_data[name]["module_name"], aliases_data[name]["module_id"], "Rel_Fade", variables))
  158.         {
  159.             //Log("\033[32mCommand sent successfully to " + name + ".\033[0m\n");
  160.         }
  161.         else
  162.         {
  163.             Log("\033[31mFailed to send command to " + name + ".\033[0m\n");
  164.         }
  165.        
  166.         found = true;
  167.     }
  168.    
  169.     if (!found)
  170.     {
  171.         Log("\033[31mNo aliases by the name " + alias_name + " were applicable for this command.\033[0m\n");
  172.         return false;
  173.     }
  174.    
  175.     return true;
  176. }
  177. Console_RegisterCommand(Dimmer_RelativeFade, function(arg_index, args) { return Console_StandardAutocomplete(arg_index, args, Dimmer_Aliases(), Dimmer_SpeedLevels(), Dimmer_Directions(), Dimmer_StepsLevels()); });
  178.  
  179. function Dimmer_Demo(alias_name, speed, steps)
  180. {
  181.     if (arguments.length < 3)
  182.     {
  183.         Log("\033[31mNot enough parameters given.\033[0m\n");
  184.         return false;
  185.     }
  186.    
  187.     var aliases_data = Module_ResolveAlias(alias_name, Dimmer_ModuleNames);
  188.     var found = false;
  189.    
  190.     for (var name in aliases_data)
  191.     {
  192.         if (aliases_data[name]["module_name"]=="hwPWM") {
  193.           steps = steps*39;
  194.         }
  195.         var variables = {
  196.         "Channel"   : aliases_data[name]["specific"]["Channel"],
  197.         "Speed"     : speed,
  198.         "Steps"     : steps };
  199.        
  200.         if (Module_SendMessage(aliases_data[name]["module_name"], aliases_data[name]["module_id"], "Demo", variables))
  201.         {
  202.             //Log("\033[32mCommand sent successfully to " + name + ".\033[0m\n");
  203.         }
  204.         else
  205.         {
  206.             Log("\033[31mFailed to send command to " + name + ".\033[0m\n");
  207.         }
  208.        
  209.         found = true;
  210.     }
  211.    
  212.     if (!found)
  213.     {
  214.         Log("\033[31mNo aliases by the name " + alias_name + " were applicable for this command.\033[0m\n");
  215.         return false;
  216.     }
  217.    
  218.     return true;
  219. }
  220. Console_RegisterCommand(Dimmer_Demo, function(arg_index, args) { return Console_StandardAutocomplete(arg_index, args, Dimmer_Aliases(), Dimmer_SpeedLevels(), Dimmer_StepsLevels()); });
  221.  
  222. function Dimmer_OnMessage(module_name, module_id, command, variables)
  223. {
  224.     if (in_array(Dimmer_ModuleNames, module_name))
  225.     {
  226.         var aliases_data = Module_LookupAliases({
  227.             "module_name" : module_name,
  228.             "module_id"   : module_id,
  229.             "group"       : false
  230.         });
  231.        
  232.         switch (command)
  233.         {
  234.             case "Netinfo":
  235.             {
  236.                 for (var alias_name in aliases_data)
  237.                 {
  238.                     if (aliases_data[alias_name]["specific"]["Channel"] != variables["Channel"])
  239.                     {
  240.                         continue;
  241.                     }
  242.                    
  243.                     var last_value = {};
  244.                     var last_value_string = Storage_GetParameter("LastValues", alias_name);
  245.  
  246.                     if (last_value_string)
  247.                     {
  248.                         last_value = eval("(" + last_value_string + ")");
  249.                     }
  250.                    
  251.                     last_value["Connection"] = { "value" : variables["Connection"], "timestamp" : get_time() };
  252.                     last_value["Frequency"] = { "value" : variables["Frequency"], "timestamp" : get_time() };
  253.                     last_value["Level"] = { "value" : variables["DimmerValue"], "timestamp" : get_time() };
  254.                    
  255.                     Storage_SetParameter("LastValues", alias_name, JSON.stringify(last_value));
  256.                 }
  257.                
  258.                 break;
  259.             }
  260.             case "Pwm":
  261.             {
  262.                 for (var alias_name in aliases_data)
  263.                 {
  264.                     if (aliases_data[alias_name]["specific"]["Channel"] != variables["Channel"])
  265.                     {
  266.                         continue;
  267.                     }
  268.                    
  269.                     var last_value = {};
  270.                     var last_value_string = Storage_GetParameter("LastValues", alias_name);
  271.  
  272.                     if (last_value_string)
  273.                     {
  274.                         last_value = eval("(" + last_value_string + ")");
  275.                     }
  276.                     var value = 0 + variables["Value"];
  277.                     value = value / 39;
  278.                     value = parseInt(value).toString();
  279.                     last_value["Level"] = { "value" : value, "timestamp" : get_time() };
  280.                    
  281.                     Storage_SetParameter("LastValues", alias_name, JSON.stringify(last_value));
  282.                 }
  283.                
  284.                 break;
  285.             }
  286.         }
  287.     }
  288. }
  289. Module_RegisterToOnMessage("all", Dimmer_OnMessage);
  290.