Subversion Repositories HomeAutomation

Rev

Rev 1172 | Rev 1182 | 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(SensorPrint, 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.  
  40. /* This function must always be declared, this is where all the startup code
  41.    should be placed. Gets called with arguments like what ids to use etc. */
  42. Display.prototype.initialize = function(initialArguments)
  43. {
  44.     /* We must always call the parent initialize */
  45.     this.Service.prototype.initialize.call(this, initialArguments);
  46.  
  47.     /* This is used for function declarations like the callbacks below */
  48.     var self = this;
  49.  
  50.     if (!this.myInitialArguments["HD44789"])
  51.     {
  52.         log(this.myName + ":" + this.myId + "> Failed to initialize, HD44789-config missing from config.\n");
  53.         return;
  54.     }
  55.    
  56.     if (!this.myInitialArguments["Rotary"])
  57.     {
  58.         log(this.myName + ":" + this.myId + "> Failed to initialize, Rotary-config missing from config.\n");
  59.         return;
  60.     }
  61.    
  62.     if (!this.myInitialArguments["softPWM"])
  63.     {
  64.         log(this.myName + ":" + this.myId + "> Failed to initialize, softPWM-config missing from config.\n");
  65.         return;
  66.     }
  67.    
  68.     if (!this.myInitialArguments["Name"])
  69.     {
  70.         log(this.myName + ":" + this.myId + "> Failed to initialize, Name-config missing from config.\n");
  71.         return;
  72.     }
  73.     this.niceName = this.myInitialArguments["Name"];
  74.  
  75.     if (!this.myInitialArguments["ShortName"])
  76.     {
  77.         log(this.myName + ":" + this.myId + "> Failed to initialize, Name-config missing from config.\n");
  78.         return;
  79.     }
  80.     this.shortName = this.myInitialArguments["ShortName"];
  81.    
  82.     /* Get the LCD service that we want from the ServiceManager, it takes type, service name, service id */
  83.     this.myLCDService = ServiceManager.getService("Can", "HD44789", this.myInitialArguments["HD44789"]["Id"]);
  84.     /* Add a callback for when the service goes online */
  85.     this.myLCDService.registerEventCallback("online", function(args) { self.lcdOnline(); });
  86.     /* If the service is already online we should call the handler here */
  87.     this.lcdOnline();
  88.     /* Add a callback for when the service goes offline */
  89.     this.myLCDService.registerEventCallback("offline", function(args) { self.lcdOffline(); });
  90.    
  91.     /* Get the Rotary service that we want from the ServiceManager, it takes type, service name, service id */
  92.     this.myRotaryService = ServiceManager.getService("Can", "Rotary", this.myInitialArguments["Rotary"]["Id"]);
  93.     /* Add a callback for when the encoder reports a new position */
  94.     this.myRotaryService.registerEventCallback("newPosition", function(args) { self.rotaryPosUpdate(args); });
  95.     /* Add a callback for when the encoder reports a new button status */
  96.     this.myRotaryService.registerEventCallback("newBtnStatus", function(args) { self.rotaryBtnUpdate(args); });
  97.     /* Add a callback for when the service goes online */
  98.     this.myRotaryService.registerEventCallback("online", function(args) { self.rotaryOnline(); });
  99.     /* If the service is already online we should call the handler here */
  100.     this.rotaryOnline();
  101.    
  102.     /* Get the softPWM service that we want from the ServiceManager, it takes type, service name, service id */
  103.     this.mySoftPwmService = ServiceManager.getService("Can", "softPWM", this.myInitialArguments["softPWM"]["Id"]);
  104.     /* Add a callback for when the service goes online */
  105.     this.mySoftPwmService.registerEventCallback("online", function(args) { self.softPwmOnline(); });
  106.     /* If the service is already online we should call the handler here */
  107.     this.softPwmOnline();
  108.    
  109.     this.exchangeCalendar = new ExchangeCalendar(function(shortname, data) { self.exchangeCalendarLookupCallback(shortname, data); });
  110. }
  111.  
  112.  
  113.  
  114. Display.prototype.exchangeCalendarLookupCallback = function(shortname, data)
  115. {
  116.     if (shortname == this.shortName)
  117.     {
  118.         //FIXME check if data contains error tag and error message, print to log and keep previous data?
  119.         this.exchangeData = data;
  120.     }
  121. }
  122.  
  123. Display.prototype.softPwmOnline = function()
  124. {
  125. }
  126.  
  127. Display.prototype.rotaryOnline = function()
  128. {
  129. }
  130.  
  131. Display.prototype.rotaryPosUpdate = function(SwitchId)
  132. {
  133. }
  134.  
  135. Display.prototype.rotaryBtnUpdate = function(SwitchId)
  136. {
  137.     if (myRotaryService.getButtonStatus(SwitchId) == "Released")
  138.     {
  139.         this.currentMenuItem.doPress();
  140.     }
  141. }
  142.  
  143.  
  144. Display.prototype.lcdOnline = function()
  145. {
  146.     /* If service is not online do nothing */
  147.     if (this.myLCDService.isOnline())
  148.     {
  149.         /* Clear the LCD screen */
  150.         this.myLCDService.clearScreen();
  151.         /* Set backlight to max */
  152.         this.myLCDService.setBacklight(255);
  153.  
  154.         /* If we have no interval timer running start it */
  155.         if (this.myInterval == null)
  156.         {
  157.             var self = this;
  158.        
  159.             /* Start interval timer for our printout. Arguments are the callback function and time in milliseconds */
  160.             this.myInterval = new Interval(function() { self.timerUpdate() }, 60000);
  161.         }
  162.        
  163.         this.myInterval.start();
  164.     }
  165. }
  166.  
  167. Display.prototype.timerUpdate = function()
  168. {
  169.     /* If LCD service is not online do nothing */
  170.     if (this.myLCDService.isOnline())
  171.     {
  172.         this.exchangeCalendar.lookup(this.shortName);
  173.        
  174.         /* see if the current menuitem wants to update itself */
  175.         if (this.currentMenuItem.doUpdate)
  176.         {
  177.             this.currentMenuItem.doUpdate();
  178.         }
  179.        
  180.         /* send the data for the current menuitem to display */
  181.         for (var i = 0; i < this.currentMenuItem.displayData.length; i++)
  182.         {
  183.             this.myLCDService.printText(0, i, currentMenuItem.displayData[i]);
  184.         }
  185.        
  186.         //var date = new Date();
  187.         /* Get the current date time on the format YYYY-mm-dd HH.ii.ss */
  188.         //var dateAndTime = date.getDateTimeFormated();
  189.         /* Print the text to the LCD */
  190.         //this.myLCDService.printText(0, 1, dateAndTime);
  191.         /* Print out what we are doing to the console */
  192.         //log(this.myName + ":" + this.myId + "> Trying to print " + dateAndTime + " to LCD\n");
  193.     }
  194. }
  195.  
  196.