Subversion Repositories HomeAutomation

Rev

Blame | Last modification | View Log | SVN | RSS feed

  1. function bedroomDisplay(aliasDisplay, aliasPower, aliasOutsideTemp, aliasStek)
  2. {
  3.     /* We must always call the parent constructor, initialization
  4.        of variables could be done here, but initialize is a better place */
  5.     var self = this;
  6.    
  7.     /* Store the alias of the hardware that is used */
  8.     this.myDisplay = aliasDisplay;
  9.     this.myPower = aliasPower;
  10.     this.myStek=aliasStek;
  11.     this.myOutsideTemp = aliasOutsideTemp;
  12.  
  13.     Log("\033[33mBedroom display controller created.\033[0m");
  14.  
  15.     /* Register callbacks for information on the hardware online/offline status */
  16.     Module_RegisterToOnChange(this.myDisplay, function(alias_name, available) { self.displayOnline(alias_name, available) });
  17.     Module_RegisterToOnChange(this.myPower, function(alias_name, available) { self.powerOnline(alias_name, available) });
  18.     Module_RegisterToOnChange(this.myOutsideTemp, function(alias_name, available) { self.outsideTempOnline(alias_name, available) });
  19.  
  20.     /* Start a timer which will call a function every 5 seconds */
  21.     this.myUpdater = Timer_SetTimer(function(timer) {self.updateDisplay(timer)}, 10000, true);
  22.  
  23.     /* Reset counter for displayitem */
  24.     this.displayItemCounter=0;
  25. }
  26.  
  27. bedroomDisplay.prototype.myDisplay = null;
  28. bedroomDisplay.prototype.myDisplayIsOnline = false;
  29. bedroomDisplay.prototype.myPower = null;
  30. bedroomDisplay.prototype.myStek = null;
  31. bedroomDisplay.prototype.myPowerIsOnline = false;
  32. bedroomDisplay.prototype.myOutsideTemp = null;
  33. bedroomDisplay.prototype.myOutsideTempIsOnline = false;
  34. bedroomDisplay.prototype.myUpdater = null;
  35. bedroomDisplay.prototype.displayItemCounter = null;
  36. bedroomDisplay.prototype.lastTimeHour = 0;
  37. bedroomDisplay.prototype.lastTimeMinute = 0;
  38.  
  39. /* When the online status of the display changes */
  40. bedroomDisplay.prototype.displayOnline = function(alias_name, available)
  41. {
  42.     this.myDisplayIsOnline = available;
  43.     if (available)
  44.     {
  45.         //Log("\033[33m--Display online.\033[0m");
  46.         /* Clear the LCD screen */
  47.         //Display_Clear(alias_name);
  48.         /* Set backlight to max */
  49.         //Display_SetBacklight(alias_name, this.defaultBacklight);
  50.        
  51.         /* Send to display */
  52.         this.updateDisplay();
  53.     } else {
  54.         //Log("\033[33m--Display offline.\033[0m");
  55.     }
  56. }
  57.  
  58. /* When the online status of the power meter changes */
  59. bedroomDisplay.prototype.powerOnline = function(alias_name, available)
  60. {
  61.     this.myPowerIsOnline = available;
  62.     if (this.myPowerIsOnline)
  63.     {
  64.         //Log("\033[33m--Power meter online.\033[0m");
  65.     } else {
  66.         //Log("\033[33m--Power meter offline.\033[0m");
  67.     }
  68. }
  69.  
  70. /* When the online status of the outside temperature sensor changes */
  71. bedroomDisplay.prototype.outsideTempOnline = function(alias_name, available)
  72. {
  73.     this.myOutsideTempIsOnline = available;
  74.     if (this.myOutsideTempIsOnline)
  75.     {
  76.         //Log("\033[33m--Outside temperature sensor online.\033[0m");
  77.     } else {
  78.         //Log("\033[33m--Outside temperature sensor offline.\033[0m");
  79.     }
  80. }
  81.  
  82. bedroomDisplay.prototype.displayTime = function()
  83. {
  84.     var shorttime = new Date().getTimeShortFormated();
  85.     /* Remove leading zero if any */
  86.     var data = parseInt(shorttime.substr(0,2));
  87.     if (data != this.lastTimeHour) {
  88.       //Log("\033[33m--New Time.\033[0m");
  89.       this.lastTimeHour = data;
  90.       if (data < 7 || data >= 22) {
  91.         GDisplay_SetBacklight(this.myDisplay, 2);
  92.         //Log("\033[33m--Setting backlight at 5.\033[0m");
  93.       } else if (data < 9 || data >= 21) {
  94.         GDisplay_SetBacklight(this.myDisplay, 20);
  95.         //Log("\033[33m--Setting backlight at 20.\033[0m");
  96.       } else {
  97.         GDisplay_SetBacklight(this.myDisplay, 128);
  98.         //Log("\033[33m--Setting backlight at 128.\033[0m");
  99.       }
  100.     }
  101.     data = parseInt(shorttime.substr(3,2));
  102.     if (data != this.lastTimeMinute) {
  103.       this.lastTimeMinute = data;
  104.       if (shorttime.substr(0,1) == "0")
  105.       {
  106.           shorttime = shorttime.substr(1,4);
  107.       }
  108.       this.displayPrintRightAdj(shorttime,0);
  109.     }
  110. }
  111.  
  112. bedroomDisplay.prototype.displayElectricPower = function()
  113. {
  114.     if (this.myPowerIsOnline)
  115.     {
  116.         var last_value_string = Storage_GetParameter("LastValues", this.myPower);
  117.         var last_value = eval("(" + last_value_string + ")");
  118.    
  119.         var powerusage = last_value["Power32"]["value"];
  120.         var timestamp = last_value["Power32"]["timestamp"];
  121.         if (timestamp+120 > get_time())
  122.         {
  123.             //Log("\033[33m--"+timestamp+" "+get_time()+".\033[0m");
  124.             powerusage = Math.round(powerusage/10)*10;
  125.             var unit = "W";
  126.             if (powerusage > 9999)
  127.             {
  128.                 powerusage = Math.round(powerusage/1000);
  129.                 unit = "kW";
  130.             }
  131.             else if (powerusage > 999)
  132.             {
  133.                 powerusage = Math.round(powerusage/100)/10;
  134.                 unit = "kW";
  135.             }
  136.             this.displayPrintRightAdj(powerusage+unit,1);
  137.         }
  138.         else
  139.         {
  140.             //Log("\033[33m--Data to old, electricpower, timestamp: "+timestamp+" current time: "+get_time()+".\033[0m");
  141.             /* Show time */
  142.             this.displayPrintRightAdj("  -  ",1);
  143.         }
  144.     }
  145.     else
  146.     {
  147.         /* Show time */
  148.         this.displayPrintRightAdj("  -  ",1);
  149.     }
  150. }
  151.  
  152.  
  153. bedroomDisplay.prototype.displayOutsideTemperature = function()
  154. {
  155.     if (this.myOutsideTempIsOnline)
  156.     {
  157.         var last_value_string = Storage_GetParameter("LastValues", this.myOutsideTemp);
  158.         var last_value = eval("(" + last_value_string + ")");
  159.    
  160.         var temperature = last_value["Temperature_Celsius"]["value"];
  161.         var timestamp = last_value["Temperature_Celsius"]["timestamp"];
  162.         if (timestamp+120 > get_time())
  163.         {
  164.             temperature = Math.round(temperature);
  165.             var unit = "¤C";
  166.             this.displayPrintRightAdj(temperature+unit,1);
  167.         }
  168.         else
  169.         {
  170.             //Log("\033[33m--Data to old, temperature, timestamp: "+timestamp+" current time: "+get_time()+".\033[0m");
  171.             /* Show time */
  172.             this.displayPrintRightAdj("  -  ",1);
  173.         }
  174.     }
  175.     else
  176.     {
  177.         /* Show time */
  178.         this.displayPrintRightAdj("  -  ",1);
  179.     }
  180. }
  181.  
  182. bedroomDisplay.prototype.displayPrintRightAdj = function(text, row)
  183. {
  184.     /* Find adjustment in x axis, 5 chars fits in the display */
  185.     text = text.pad(5, " ", 0);
  186.     GDisplay_Print(this.myDisplay, 0, row, "Standard", "Standard", text);
  187. }
  188.  
  189. /* When the timer executes */
  190. bedroomDisplay.prototype.updateDisplay = function(timer)
  191. {
  192.     /* If display service is not online do nothing */
  193.     if (this.myDisplayIsOnline)
  194.     {
  195.         this.displayTime();
  196.       //Log("\033[33m--Update.\033[0m");
  197.  
  198.         var last_value_string = Storage_GetParameter("LastValues", this.myStek);
  199.         var last_value = eval("(" + last_value_string + ")");
  200.    
  201.         var temperature = last_value["Temperature_Celsius"]["value"];
  202.         var timestamp = last_value["Temperature_Celsius"]["timestamp"];
  203.         if (timestamp+30 > get_time() && temperature > 28)
  204.         {
  205.             temperature = Math.round(temperature);
  206.             var unit = "¤C";
  207.             this.displayPrintRightAdj(temperature+unit,1);
  208.         }
  209.         else
  210.         {
  211.         switch (this.displayItemCounter)
  212.         {
  213.         case 0:
  214.             /* Show outdoor temperature */
  215.             this.displayOutsideTemperature();
  216.             break;
  217.         case 1:
  218.             /* Show electrical power usage */
  219.             this.displayElectricPower();
  220.             break;
  221.         }
  222.         }
  223.         /* Clear position indicator (the LED on the right side of the display) */
  224.         GDisplay_DrawLine(this.myDisplay, 31, 0, 31, 7, "Inverted");
  225.         /* Set position indicator */
  226.         GDisplay_DrawLine(this.myDisplay, 31, this.displayItemCounter, 31, this.displayItemCounter, "Standard");
  227.        
  228.         this.displayItemCounter++;
  229.         if (this.displayItemCounter == 2)
  230.         {
  231.             this.displayItemCounter = 0;
  232.         }
  233.     } else {
  234.     //  Log("\033[33m--Update failed.\033[0m");
  235.      
  236.     }
  237. }
  238.  
  239.