Subversion Repositories HomeAutomation

Rev

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

  1.  
  2. loadScript("Logic/MenuItem.js");
  3.  
  4. function Display(type, name, id)
  5. {
  6.     /* We must always call the parent constructor, initialization
  7.        of variables could be done here, but initialize is a better place */
  8.     this.Service(type, name, id);
  9. }
  10.  
  11. /* We inherit from Service with this call, it must always be next
  12.    after the constructor */
  13. extend(Display, Service);
  14.  
  15. /* Declaration of instance variables, for static variables remove prototype */
  16. /* The currently displayed menuitem */
  17. Display.prototype.currentMenuItem = null;
  18.  
  19. /* format: minutes since 00:00 (480/60 = 8:00) */
  20. Display.prototype.bookFromTime = 480;
  21. Display.prototype.bookToTime = 510;
  22.  
  23. /* the shortname for this exchangeobject */
  24. Display.prototype.shortName = "";  
  25.  
  26. /* the name for this display (shown on first screen) */
  27. Display.prototype.niceName = "";   
  28.  
  29. /* The display should regulary update and store the calendar */
  30. Display.prototype.exchangeData = null;
  31.  
  32. Display.prototype.myLCDService = null;
  33. Display.prototype.myRotaryService = null;
  34. Display.prototype.mySoftPwmService = null;
  35. Display.prototype.myInterval = null;
  36. Display.prototype.exchangeCalendar = null;
  37.  
  38.  
  39. /* This function must always be declared, this is where all the startup code
  40.    should be placed. Gets called with arguments like what ids to use etc. */
  41. Display.prototype.initialize = function(initialArguments)
  42. {
  43.     /* We must always call the parent initialize */
  44.     this.Service.prototype.initialize.call(this, initialArguments);
  45.  
  46.     /* This is used for function declarations like the callbacks below */
  47.     var self = this;
  48.  
  49.     if (!this.myInitialArguments["HD44789"])
  50.     {
  51.         log(this.myName + ":" + this.myId + "> Failed to initialize, HD44789-config missing from config.\n");
  52.         return;
  53.     }
  54.    
  55.     if (!this.myInitialArguments["Rotary"])
  56.     {
  57.         log(this.myName + ":" + this.myId + "> Failed to initialize, Rotary-config missing from config.\n");
  58.         return;
  59.     }
  60.    
  61.     if (!this.myInitialArguments["softPWM"])
  62.     {
  63.         log(this.myName + ":" + this.myId + "> Failed to initialize, softPWM-config missing from config.\n");
  64.         return;
  65.     }
  66.    
  67.     if (!this.myInitialArguments["Name"])
  68.     {
  69.         log(this.myName + ":" + this.myId + "> Failed to initialize, Name-config missing from config.\n");
  70.         return;
  71.     }
  72.     this.niceName = this.myInitialArguments["Name"];
  73.  
  74.     if (!this.myInitialArguments["ShortName"])
  75.     {
  76.         log(this.myName + ":" + this.myId + "> Failed to initialize, Name-config missing from config.\n");
  77.         return;
  78.     }
  79.     this.shortName = this.myInitialArguments["ShortName"];
  80.    
  81.     /* Get the LCD service that we want from the ServiceManager, it takes type, service name, service id */
  82.     this.myLCDService = ServiceManager.getService("Can", "HD44789", this.myInitialArguments["HD44789"]["Id"]);
  83.     /* Add a callback for when the service goes online */
  84.     this.myLCDService.registerEventCallback("online", function(args) { self.lcdOnline(); });
  85.     /* If the service is already online we should call the handler here */
  86.     this.lcdOnline();
  87.     /* Add a callback for when the service goes offline */
  88.     this.myLCDService.registerEventCallback("offline", function(args) { self.lcdOffline(); });
  89.    
  90.     /* Get the Rotary service that we want from the ServiceManager, it takes type, service name, service id */
  91.     this.myRotaryService = ServiceManager.getService("Can", "Rotary", this.myInitialArguments["Rotary"]["Id"]);
  92.     /* Add a callback for when the encoder reports a new position */
  93.     this.myRotaryService.registerEventCallback("newPosition", function(args) { self.rotaryPosUpdate(args); });
  94.     /* Add a callback for when the encoder reports a new button status */
  95.     this.myRotaryService.registerEventCallback("newBtnStatus", function(args) { self.rotaryBtnUpdate(args); });
  96.     /* Add a callback for when the service goes online */
  97.     this.myRotaryService.registerEventCallback("online", function(args) { self.rotaryOnline(); });
  98.     /* If the service is already online we should call the handler here */
  99.     this.rotaryOnline();
  100.    
  101.     /* Get the softPWM service that we want from the ServiceManager, it takes type, service name, service id */
  102.     this.mySoftPwmService = ServiceManager.getService("Can", "softPWM", this.myInitialArguments["softPWM"]["Id"]);
  103.     /* Add a callback for when the service goes online */
  104.     this.mySoftPwmService.registerEventCallback("online", function(args) { self.softPwmOnline(); });
  105.     /* If the service is already online we should call the handler here */
  106.     this.softPwmOnline();
  107.    
  108.     this.exchangeCalendar = new ExchangeCalendar(function(shortname, data) { self.exchangeCalendarLookupCallback(shortname, data); });
  109. }
  110.  
  111.  
  112.  
  113. Display.prototype.exchangeCalendarLookupCallback = function(shortname, data)
  114. {
  115.     if (shortname == this.shortName)
  116.     {
  117.         //FIXME check if data contains error tag and error message, print to log and keep previous data?
  118.         this.exchangeData = data;
  119.     }
  120. }
  121.  
  122. Display.prototype.softPwmOnline = function()
  123. {
  124. }
  125.  
  126. Display.prototype.rotaryOnline = function()
  127. {
  128. }
  129.  
  130. Display.prototype.rotaryPosUpdate = function(SwitchId)
  131. {
  132.     for (var i = 0; i < myRotaryService.getSteps(SwitchId); i++)
  133.     {
  134.         if (myRotaryService.getDirection(SwitchId) == "Clockwise")
  135.         {
  136.             this.currentMenuItem.doRight();
  137.         }
  138.         else
  139.         {
  140.             this.currentMenuItem.doLeft();
  141.         }
  142.     }
  143. }
  144.  
  145. Display.prototype.rotaryBtnUpdate = function(SwitchId)
  146. {
  147.     if (myRotaryService.getButtonStatus(SwitchId) == "Released")
  148.     {
  149.         this.currentMenuItem.doPress();
  150.     }
  151. }
  152.  
  153. Display.prototype.updateDisplay = function()
  154. {
  155.     /* see if the current menuitem wants to update itself */
  156.     if (this.currentMenuItem.doUpdate)
  157.     {
  158.         this.currentMenuItem.doUpdate();
  159.     }
  160.    
  161.     /* send the data for the current menuitem to display */
  162.     for (var i = 0; i < this.currentMenuItem.displayData.length; i++)
  163.     {
  164.         this.myLCDService.printText(0, i, currentMenuItem.displayData[i]);
  165.     }
  166.    
  167. }
  168.  
  169. Display.prototype.lcdOffline = function()
  170. {
  171.     /* If we have no interval timer running do nothing */
  172.     if (this.myInterval != null)
  173.     {
  174.         /* Stop the interval */
  175.         this.myInterval.stop();
  176.     }
  177. }
  178.  
  179. Display.prototype.lcdOnline = function()
  180. {
  181.     /* If service is not online do nothing */
  182.     if (this.myLCDService.isOnline())
  183.     {
  184.         /* Clear the LCD screen */
  185.         this.myLCDService.clearScreen();
  186.         /* Set backlight to max */
  187.         this.myLCDService.setBacklight(255);
  188.  
  189.         /* If we have no interval timer running start it */
  190.         if (this.myInterval == null)
  191.         {
  192.             var self = this;
  193.        
  194.             /* Start interval timer for our printout. Arguments are the callback function and time in milliseconds */
  195.             this.myInterval = new Interval(function() { self.timerUpdate() }, 60000);
  196.         }
  197.        
  198.         this.myInterval.start();
  199.     }
  200. }
  201.  
  202. Display.prototype.timerUpdate = function()
  203. {
  204.     /* If LCD service is not online do nothing */
  205.     if (this.myLCDService.isOnline())
  206.     {
  207.         this.exchangeCalendar.lookup(this.shortName);
  208.        
  209.         /* update the info on display */
  210.         this.updateDisplay();
  211.        
  212.         //var date = new Date();
  213.         /* Get the current date time on the format YYYY-mm-dd HH.ii.ss */
  214.         //var dateAndTime = date.getDateTimeFormated();
  215.         /* Print the text to the LCD */
  216.         //this.myLCDService.printText(0, 1, dateAndTime);
  217.         /* Print out what we are doing to the console */
  218.         //log(this.myName + ":" + this.myId + "> Trying to print " + dateAndTime + " to LCD\n");
  219.     }
  220. }
  221.  
  222.