Subversion Repositories HomeAutomation

Rev

Rev 1182 | Rev 1190 | 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.     /* create the first menu item */
  112.     this.currentMenuItem = new MenuItem(this);
  113.     this.currentMenuItem.displayData[0] = "       " + this.niceName;
  114.     this.currentMenuItem.doUpdate = this.setTime;
  115.    
  116.     /* create a test item */
  117.     var meny = new MenuItem(this);
  118.     meny.displayData[0] = "Hej  ";
  119.     meny.displayData[1] = "   ";
  120.    
  121.     /* connect the items as a linked list */
  122.     this.currentMenuItem.setNextItem(meny);
  123.     this.currentMenuItem.setPrevItem(meny);
  124.     /* set the function that shall be executed when knob is turned */
  125.     this.currentMenuItem.doRight = this.changeToNext;
  126.     this.currentMenuItem.doLeft = this.changeToPrev;
  127.    
  128.     /* connect the items as a linked list */
  129.     meny.setNextItem(this.currentMenuItem);
  130.     meny.setPrevItem(this.currentMenuItem);
  131.     /* set the function that shall be executed when knob is turned */
  132.     meny.doRight = this.changeToNext;
  133.     meny.doLeft = this.changeToPrev;
  134.    
  135.    
  136. }
  137.  
  138. Display.prototype.changeToNext = function()
  139. {
  140.     if (this.nextItem)
  141.     {
  142.         this.parentDisplay.currentMenuItem = this.nextItem;
  143.     }
  144.    
  145. }
  146.  
  147. Display.prototype.changeToPrev = function()
  148. {
  149.     if (this.prevItem)
  150.     {
  151.         this.parentDisplay.currentMenuItem = this.prevItem;
  152.     }
  153. }
  154.  
  155. Display.prototype.setTime = function()
  156. {
  157.     var date = new Date();
  158.     /* Get the current date time on the format YYYY-mm-dd HH.ii.ss */
  159.     var dateAndTime = "" + date.getHours().toString() + "." + date.getMinutes().toString().pad(2, "0", 0);
  160.     this.displayData[1] = "       " + dateAndTime;
  161.     //this.currentMenuItem.displayData[1] = "       " + dateAndTime;
  162. }
  163.  
  164.  
  165. Display.prototype.exchangeCalendarLookupCallback = function(shortname, data)
  166. {
  167.     if (shortname == this.shortName)
  168.     {
  169.         //FIXME check if data contains error tag and error message, print to log and keep previous data?
  170.         this.exchangeData = data;
  171.     }
  172. }
  173.  
  174. Display.prototype.softPwmOnline = function()
  175. {
  176. }
  177.  
  178. Display.prototype.rotaryOnline = function()
  179. {
  180. }
  181.  
  182. Display.prototype.rotaryPosUpdate = function(SwitchId)
  183. {
  184.     for (var i = 0; i < this.myRotaryService.getSteps(SwitchId); i++)
  185.     {
  186.         if (this.myRotaryService.getDirection(SwitchId) == "Clockwise")
  187.         {
  188.             if (this.currentMenuItem.doRight)
  189.             {
  190.                 this.currentMenuItem.doRight();
  191.             }
  192.         }
  193.         else
  194.         {
  195.             if (this.currentMenuItem.doLeft)
  196.             {
  197.                 this.currentMenuItem.doLeft();
  198.             }
  199.         }
  200.     }
  201.    
  202.     this.updateDisplay();
  203. }
  204.  
  205. Display.prototype.rotaryBtnUpdate = function(SwitchId)
  206. {
  207.     if (this.myRotaryService.getButtonStatus(SwitchId) == "Released")
  208.     {
  209.         if (this.currentMenuItem.doPress)
  210.         {
  211.             this.currentMenuItem.doPress();
  212.         }
  213.     }
  214.     this.updateDisplay();
  215. }
  216.  
  217. Display.prototype.updateDisplay = function()
  218. {
  219.     /* see if the current menuitem wants to update itself */
  220.     if (this.currentMenuItem.doUpdate)
  221.     {
  222.         this.currentMenuItem.doUpdate();
  223.     }
  224.  
  225.     /* Clear the LCD screen */
  226.     this.myLCDService.clearScreen();
  227.    
  228.     /* send the data for the current menuitem to display */
  229.     for (var i = 0; i < this.currentMenuItem.displayData.length; i++)
  230.     {
  231.         this.myLCDService.printText(0, i, this.currentMenuItem.displayData[i]);
  232.     }
  233.    
  234. }
  235.  
  236. Display.prototype.lcdOffline = function()
  237. {
  238.     /* If we have no interval timer running do nothing */
  239.     if (this.myInterval != null)
  240.     {
  241.         /* Stop the interval */
  242.         this.myInterval.stop();
  243.     }
  244. }
  245.  
  246. Display.prototype.lcdOnline = function()
  247. {
  248.     /* If service is not online do nothing */
  249.     if (this.myLCDService.isOnline())
  250.     {
  251.         /* Clear the LCD screen */
  252.         this.myLCDService.clearScreen();
  253.         /* Set backlight to max */
  254.         this.myLCDService.setBacklight(255);
  255.  
  256.         /* If we have no interval timer running start it */
  257.         if (this.myInterval == null)
  258.         {
  259.             var self = this;
  260.        
  261.             /* Start interval timer for our printout. Arguments are the callback function and time in milliseconds */
  262.             this.myInterval = new Interval(function() { self.timerUpdate() }, 60000);
  263.         }
  264.        
  265.         this.myInterval.start();
  266.        
  267.         this.updateDisplay();
  268.     }
  269. }
  270.  
  271. Display.prototype.timerUpdate = function()
  272. {
  273.     /* If LCD service is not online do nothing */
  274.     if (this.myLCDService.isOnline())
  275.     {
  276.         //this.exchangeCalendar.lookup(this.shortName);
  277.        
  278.         /* update the info on display */
  279.         this.updateDisplay();
  280.        
  281.         //var date = new Date();
  282.         /* Get the current date time on the format YYYY-mm-dd HH.ii.ss */
  283.         //var dateAndTime = date.getDateTimeFormated();
  284.         /* Print the text to the LCD */
  285.         //this.myLCDService.printText(0, 1, dateAndTime);
  286.         /* Print out what we are doing to the console */
  287.         //log(this.myName + ":" + this.myId + "> Trying to print " + dateAndTime + " to LCD\n");
  288.     }
  289. }
  290.  
  291.