Subversion Repositories HomeAutomation

Rev

Rev 1246 | Rev 1248 | 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.  
  17. /* format: dat objects, just use the time */
  18. Display.prototype.bookFromTime = null;
  19. Display.prototype.bookToTime = null;
  20.  
  21. /* the shortname for this exchangeobject */
  22. Display.prototype.shortName = null;
  23.  
  24. /* the name for this display (shown on first screen) */
  25. Display.prototype.niceName = null;
  26.  
  27. /* The display should regulary update and store the calendar */
  28. Display.prototype.exchangeData = null;
  29.  
  30. Display.prototype.myLCDService = null;
  31. Display.prototype.myRotaryService = null;
  32. Display.prototype.mySoftPwmService = null;
  33. Display.prototype.myInterval = null;
  34. Display.prototype.myIntervalAlways = null;
  35.  
  36. /*  */
  37. Display.prototype.exchangeCalendar = null;
  38. /*  */
  39. Display.prototype.exchangeCalendarFirstMenuItem = null;
  40. Display.prototype.exchangeCalendarLastMenuItem = null;
  41. Display.prototype.statusMenuItem = null;
  42. Display.prototype.bookMenuItem = null;
  43. Display.prototype.bookResultMenuItem = null;
  44.  
  45.  
  46. /* The currently displayed menuitem */
  47. Display.prototype.currentMenuItem = null;
  48.  
  49. /* counter for calling exchange update */
  50. Display.prototype.exchangeUpdateCnt = null;
  51.  
  52. /* counter for going to screensaver */
  53. Display.prototype.screenSaverCnt = null;
  54.  
  55. /* counter for going to main screen */
  56. Display.prototype.mainScreenCnt = null;
  57.  
  58. const exchangeUpdateTimeout = 50;
  59. const mainScreenTimeout = 40;
  60. const screenSaverTimeout = 120;
  61.  
  62. const bookingIncrease = 15;
  63.  
  64. /* This function must always be declared, this is where all the startup code
  65.    should be placed. Gets called with arguments like what ids to use etc. */
  66. Display.prototype.initialize = function(initialArguments)
  67. {
  68.     /* We must always call the parent initialize */
  69.     this.Service.prototype.initialize.call(this, initialArguments);
  70.  
  71.     /* This is used for function declarations like the callbacks below */
  72.     var self = this;
  73.  
  74.     if (!this.myInitialArguments["HD44789"])
  75.     {
  76.         log(this.myName + ":" + this.myId + "> Failed to initialize, HD44789-config missing from config.\n");
  77.         return;
  78.     }
  79.    
  80.     if (!this.myInitialArguments["Rotary"])
  81.     {
  82.         log(this.myName + ":" + this.myId + "> Failed to initialize, Rotary-config missing from config.\n");
  83.         return;
  84.     }
  85.    
  86.     if (!this.myInitialArguments["softPWM"])
  87.     {
  88.         log(this.myName + ":" + this.myId + "> Failed to initialize, softPWM-config missing from config.\n");
  89.         return;
  90.     }
  91.    
  92.     if (!this.myInitialArguments["Name"])
  93.     {
  94.         log(this.myName + ":" + this.myId + "> Failed to initialize, Name-config missing from config.\n");
  95.         return;
  96.     }
  97.     this.niceName = this.myInitialArguments["Name"];
  98.  
  99.     if (!this.myInitialArguments["ShortName"])
  100.     {
  101.         log(this.myName + ":" + this.myId + "> Failed to initialize, Name-config missing from config.\n");
  102.         return;
  103.     }
  104.     this.shortName = this.myInitialArguments["ShortName"];
  105.  
  106.     /* Start interval timer for sending timestamp to network. Arguments are the callback function and time in milliseconds
  107.     used to make sure an eth-node gets its init packet */
  108.     this.myIntervalAlways = new Interval(function() { self.sendTimeStamp() }, 10000);
  109.     this.myIntervalAlways.start();
  110.    
  111.     this.screenSaverCnt = 0;
  112.     this.mainScreenCnt = 0;
  113.     this.exchangeUpdateCnt = exchangeUpdateTimeout-10;
  114.    
  115.     /* Get the LCD service that we want from the ServiceManager, it takes type, service name, service id */
  116.     this.myLCDService = ServiceManager.getService("Can", "HD44789", this.myInitialArguments["HD44789"]["Id"]);
  117.     /* Add a callback for when the service goes online */
  118.     this.myLCDService.registerEventCallback("online", function(args) { self.lcdOnline(); });
  119.     /* If the service is already online we should call the handler here */
  120.     this.lcdOnline();
  121.     /* Add a callback for when the service goes offline */
  122.     this.myLCDService.registerEventCallback("offline", function(args) { self.lcdOffline(); });
  123.    
  124.     /* Get the Rotary service that we want from the ServiceManager, it takes type, service name, service id */
  125.     this.myRotaryService = ServiceManager.getService("Can", "Rotary", this.myInitialArguments["Rotary"]["Id"]);
  126.     /* Add a callback for when the encoder reports a new position */
  127.     this.myRotaryService.registerEventCallback("newPosition", function(args) { self.rotaryPosUpdate(args); });
  128.     /* Add a callback for when the encoder reports a new button status */
  129.     this.myRotaryService.registerEventCallback("newBtnStatus", function(args) { self.rotaryBtnUpdate(args); });
  130.     /* Add a callback for when the service goes online */
  131.     this.myRotaryService.registerEventCallback("online", function(args) { self.rotaryOnline(); });
  132.     /* If the service is already online we should call the handler here */
  133.     this.rotaryOnline();
  134.    
  135.     /* Get the softPWM service that we want from the ServiceManager, it takes type, service name, service id */
  136.     this.mySoftPwmService = ServiceManager.getService("Can", "softPWM", this.myInitialArguments["softPWM"]["Id"]);
  137.     /* Add a callback for when the service goes online */
  138.     this.mySoftPwmService.registerEventCallback("online", function(args) { self.softPwmOnline(); });
  139.     /* If the service is already online we should call the handler here */
  140.     this.softPwmOnline();
  141.    
  142.     this.exchangeCalendar = new ExchangeCalendar(function(shortname, data) { self.exchangeCalendarLookupCallback(shortname, data); });
  143.     this.exchangeCalendarBook = new ExchangeCalendar(function(shortname, data) { self.exchangeCalendarBookCallback(shortname, data); });
  144.    
  145.     /* create the first menu item */
  146.     this.statusMenuItem = new MenuItem(this);
  147.     this.statusMenuItem.displayData[0] = this.lcdCenterText(this.niceName);
  148.     this.statusMenuItem.doUpdate = function(args) { self.updateStatusMenuItem(); };
  149.     this.currentMenuItem = this.statusMenuItem;
  150.    
  151.     /* create the menuitem where you can choose to enter the booking sub-menu */
  152.     this.bookMenuItem = new MenuItem(this);
  153.     this.bookMenuItem.displayData[0] = this.lcdCenterText("Book room");
  154.     this.bookMenuItem.displayData[1] = this.lcdCenterText("(Push button)");
  155.     this.bookMenuItem.doUpdate = function(args) { self.updateBookMenuItem(); };
  156.  
  157.     /* connect the items as a linked list */
  158.     this.statusMenuItem.setNextItem(this.bookMenuItem);
  159.     this.statusMenuItem.setPrevItem(this.bookMenuItem);
  160.     /* set the function that shall be executed when knob is turned */
  161.     this.statusMenuItem.doRight = function(args) { self.changeToNext(); };
  162.     this.statusMenuItem.doLeft = function(args) { self.changeToPrev(); };
  163.    
  164.     /* connect the items as a linked list */
  165.     this.bookMenuItem.setNextItem(this.statusMenuItem);
  166.     this.bookMenuItem.setPrevItem(this.statusMenuItem);
  167.     /* set the function that shall be executed when knob is turned */
  168.     this.bookMenuItem.doRight = function(args) { self.changeToNext(); };
  169.     this.bookMenuItem.doLeft = function(args) { self.changeToPrev(); };
  170.    
  171.     /* create the menuitem where you can choose the to-time */
  172.     var menuChooseTo = new MenuItem(this);
  173.     menuChooseTo.displayData[1] = this.lcdCenterText(" Ok     Cancel ");
  174.     menuChooseTo.doUpdate = function(args) { self.chooseBookTimeTo(); };
  175.    
  176.     this.bookMenuItem.doPress = function(args) { self.prepareBookingMenu(); };
  177.     this.bookMenuItem.setDescItem(menuChooseTo);
  178.    
  179.     /* create the menuitem where you can choose to book the room with OK */
  180.     var menuChooseOk = new MenuItem(this);
  181.     menuChooseOk.displayData[1] = this.lcdCenterText("<Ok>    Cancel ");
  182.     menuChooseOk.doUpdate = function(args) { self.setBookTime(); };
  183.  
  184.     /* create the menuitem where you can choose to cancel the booking */
  185.     var menuChooseCancel = new MenuItem(this);
  186.     menuChooseCancel.displayData[1] = this.lcdCenterText(" Ok    <Cancel>");
  187.     menuChooseCancel.doUpdate = function(args) { self.setBookTime(); };
  188.  
  189.     /* create the menuitem where you can choose the from-time */
  190.     var menuChooseFrom = new MenuItem(this);
  191.     menuChooseFrom.displayData[1] = this.lcdCenterText(" Ok     Cancel ");
  192.     menuChooseFrom.doUpdate = function(args) { self.chooseBookTimeFrom(); };
  193.  
  194.     /* create the menuitem where you see the result of a booking */
  195.     this.bookResultMenuItem = new MenuItem(this);
  196.     this.bookResultMenuItem.displayData[0] = this.lcdCenterText("Booking...");
  197.     this.bookResultMenuItem.displayData[1] = this.lcdCenterText("(Push to return)");
  198.     this.bookResultMenuItem.setDescItem(this.bookMenuItem);
  199.     this.bookResultMenuItem.doPress = function(args) { self.changeToDesc(); };
  200.  
  201.     /* connect the items as a linked list */
  202.     menuChooseTo.setNextItem(menuChooseOk);
  203.     menuChooseTo.setPrevItem(menuChooseFrom);
  204.     /* set the function that shall be executed when knob is turned */
  205.     menuChooseTo.doRight = function(args) { self.changeToNext(); };
  206.     menuChooseTo.doLeft = function(args) { self.changeToPrev(); };
  207.     menuChooseTo.doPress = function(args) { self.changeToDesc(); };
  208.  
  209.     /* connect the items as a linked list */
  210.     menuChooseOk.setNextItem(menuChooseCancel);
  211.     menuChooseOk.setPrevItem(menuChooseTo);
  212.     menuChooseOk.setDescItem(this.bookResultMenuItem);
  213.     /* set the function that shall be executed when knob is turned */
  214.     menuChooseOk.doRight = function(args) { self.changeToNext(); };
  215.     menuChooseOk.doLeft = function(args) { self.changeToPrev(); };
  216.     menuChooseOk.doPress = function(args) { self.doBooking(); };
  217.  
  218.     /* connect the items as a linked list */
  219.     menuChooseCancel.setNextItem(menuChooseFrom);
  220.     menuChooseCancel.setPrevItem(menuChooseOk);
  221.     menuChooseCancel.setDescItem(this.bookMenuItem);
  222.     /* set the function that shall be executed when knob is turned */
  223.     menuChooseCancel.doRight = function(args) { self.changeToNext(); };
  224.     menuChooseCancel.doLeft = function(args) { self.changeToPrev(); };
  225.     menuChooseCancel.doPress = function(args) { self.changeToDesc(); };
  226.  
  227.     /* connect the items as a linked list */
  228.     menuChooseFrom.setNextItem(menuChooseTo);
  229.     menuChooseFrom.setPrevItem(menuChooseCancel);
  230.     /* set the function that shall be executed when knob is turned */
  231.     menuChooseFrom.doRight = function(args) { self.changeToNext(); };
  232.     menuChooseFrom.doLeft = function(args) { self.changeToPrev(); };
  233.     menuChooseFrom.doPress = function(args) { self.changeToDesc(); };
  234.  
  235.     /* create the menuitem where you can modify the to-time */
  236.     var menuSetTo = new MenuItem(this);
  237.     menuSetTo.doUpdate = function(args) { self.setBookTimeTo(); };
  238.     menuSetTo.displayData[0] = this.lcdCenterText("Set end time");
  239.     menuSetTo.doRight = function(args) { self.incBookTimeTo(); };
  240.     menuSetTo.doLeft = function(args) { self.decBookTimeTo(); };
  241.     menuSetTo.doPress = function(args) { self.changeToDesc(); };
  242.     menuSetTo.setDescItem(menuChooseTo);
  243.     menuChooseTo.setDescItem(menuSetTo);
  244.  
  245.     /* create the menuitem where you can modify the from-time */
  246.     var menuSetFrom = new MenuItem(this);
  247.     menuSetFrom.doUpdate = function(args) { self.setBookTimeFrom(); };
  248.     menuSetFrom.displayData[0] = this.lcdCenterText("Set start time");
  249.     menuSetFrom.doRight = function(args) { self.incBookTimeFrom(); };
  250.     menuSetFrom.doLeft = function(args) { self.decBookTimeFrom(); };
  251.     menuSetFrom.doPress = function(args) { self.changeToDesc(); };
  252.     menuSetFrom.setDescItem(menuChooseFrom);
  253.     menuChooseFrom.setDescItem(menuSetFrom);
  254.  
  255. }
  256.  
  257. Display.prototype.exchangeCalendarBookCallback = function(shortname, data)
  258. {
  259.     if (shortname == this.shortName)
  260.     {
  261.         //FIXME check if data contains error tag and error message, print to log and keep previous data?
  262.         if (data.result)
  263.         {
  264.             if (data.error)
  265.             {
  266.                 this.bookResultMenuItem.displayData[0] = this.lcdCenterText("Booking failed");
  267.                 log("Display: got this error from exchange server: " + data.error + "\n");
  268.             }
  269.             else
  270.             {
  271.                 this.bookResultMenuItem.displayData[0] = this.lcdCenterText("Booking succeeded");
  272.                 log("Display: Successfully booked room\n");
  273.                
  274.                 /* do an exchange lookup */
  275.                 this.exchangeCalendar.lookup(this.shortName);
  276.             }
  277.            
  278.         }
  279.         else if (data.error)
  280.         {
  281.             this.bookResultMenuItem.displayData[0] = this.lcdCenterText("Booking failed");
  282.             log("Display: got this error from exchange.php: " + data.error + "\n");
  283.         }
  284.         this.updateDisplay();
  285.     }
  286. }
  287.  
  288. Display.prototype.doBooking = function()
  289. {
  290.     this.bookResultMenuItem.displayData[0] = this.lcdCenterText("Booking...");
  291.     if (this.currentMenuItem.descItem)
  292.     {
  293.         this.currentMenuItem = this.currentMenuItem.descItem;
  294.     }
  295.     //this.changeToDesc();
  296.  
  297.     var bookFrom = this.bookFromTime.getTimeShortFormated();
  298.     var bookTo = this.bookToTime.getTimeShortFormated();
  299.     this.exchangeCalendarBook.book(this.shortName, bookFrom, bookTo);
  300.    
  301.     //bookFromTime bookToTime
  302. }
  303.  
  304. Display.prototype.prepareBookingMenu = function()
  305. {
  306.     this.bookFromTime = new Date();
  307.     this.bookToTime = new Date();
  308.     //this.bookFromTime.setHours(8);
  309.     //this.bookToTime.setHours(8);
  310.     if (this.bookFromTime.getMinutes() < 30)
  311.     {
  312.         this.bookFromTime.setMinutes(0);
  313.         this.bookToTime.setMinutes(30);
  314.     }
  315.     else
  316.     {
  317.         this.bookFromTime.setMinutes(30);
  318.         this.bookToTime.setHours(this.bookToTime.getHours()+1);
  319.         this.bookToTime.setMinutes(0);
  320.     }
  321.  
  322.     this.currentMenuItem = this.currentMenuItem.descItem;
  323. }
  324.  
  325. Display.prototype.changeToDesc = function()
  326. {
  327.     if (this.currentMenuItem.descItem)
  328.     {
  329.         this.currentMenuItem = this.currentMenuItem.descItem;
  330.     }
  331. }
  332.  
  333. Display.prototype.changeToNext = function()
  334. {
  335.     if (this.currentMenuItem.nextItem)
  336.     {
  337.         this.currentMenuItem = this.currentMenuItem.nextItem;
  338.     }
  339. }
  340.  
  341. Display.prototype.changeToPrev = function()
  342. {
  343.     if (this.currentMenuItem.prevItem)
  344.     {
  345.         this.currentMenuItem = this.currentMenuItem.prevItem;
  346.     }
  347. }
  348.  
  349. Display.prototype.incBookTimeTo = function()
  350. {
  351.     //FIXME: constraints?
  352.     if (this.bookToTime.getHours() < 23)
  353.     {
  354.         this.bookToTime.setMinutes(this.bookToTime.getMinutes() + bookingIncrease);
  355.     }
  356. }
  357.  
  358. Display.prototype.decBookTimeTo = function()
  359. {
  360.     //FIXME: constraints?
  361.     var now = new Date();
  362.     if (this.bookToTime.getHours() >= now.getHours())
  363.     {
  364.         this.bookToTime.setMinutes(this.bookToTime.getMinutes() - bookingIncrease);
  365.     }
  366. }
  367.  
  368. Display.prototype.incBookTimeFrom = function()
  369. {
  370.     //FIXME: constraints?
  371.     if (this.bookFromTime.getHours() < 23)
  372.     {
  373.         this.bookFromTime.setMinutes(this.bookFromTime.getMinutes() + bookingIncrease);
  374.     }
  375. }
  376.  
  377. Display.prototype.decBookTimeFrom = function()
  378. {
  379.     //FIXME: constraints?
  380.     var now = new Date();
  381.     if (this.bookFromTime.getHours() >= now.getHours())
  382.     {
  383.         this.bookFromTime.setMinutes(this.bookFromTime.getMinutes() - bookingIncrease);
  384.     }
  385. }
  386.  
  387. Display.prototype.setBookTimeFrom = function()
  388. {
  389.     this.currentMenuItem.displayData[1] = "      <"+this.bookFromTime.getTimeShortFormated()+">       ";
  390. }
  391.  
  392. Display.prototype.setBookTimeTo = function()
  393. {
  394.     this.currentMenuItem.displayData[1] = "      <"+this.bookToTime.getTimeShortFormated()+">       ";
  395. }
  396.  
  397. Display.prototype.chooseBookTimeTo = function()
  398. {
  399.     this.currentMenuItem.displayData[0] = "  "+this.bookFromTime.getTimeShortFormated()+"  - <"
  400.                                 +this.bookToTime.getTimeShortFormated()+">  ";
  401. }
  402.  
  403. Display.prototype.chooseBookTimeFrom = function()
  404. {
  405.     this.currentMenuItem.displayData[0] = " <"+this.bookFromTime.getTimeShortFormated()+"> -  "
  406.                                 +this.bookToTime.getTimeShortFormated()+"   ";
  407. }
  408.  
  409. Display.prototype.setBookTime = function()
  410. {
  411.     this.currentMenuItem.displayData[0] = "  "+this.bookFromTime.getTimeShortFormated()+"  -  "
  412.                                 +this.bookToTime.getTimeShortFormated()+"   ";
  413. }
  414.  
  415. Display.prototype.updateStatusMenuItem = function()
  416. {
  417.     var date = new Date();
  418.     var dateAndTime = "" + date.getTimeShortFormated();
  419.     this.statusMenuItem.displayData[1] = this.lcdCenterText(dateAndTime);
  420. }
  421.  
  422. Display.prototype.updateBookMenuItem = function()
  423. {
  424.     if (this.exchangeCalendarFirstMenuItem && this.exchangeCalendarLastMenuItem)
  425.     {
  426.         this.statusMenuItem.setNextItem(this.exchangeCalendarFirstMenuItem);
  427.         this.bookMenuItem.setPrevItem(this.exchangeCalendarLastMenuItem);
  428.     }
  429.     else
  430.     {
  431.         this.statusMenuItem.setNextItem(this.bookMenuItem);
  432.         this.bookMenuItem.setPrevItem(this.statusMenuItem);
  433.     }
  434. }
  435.  
  436. Display.prototype.exchangeCalendarLookupCallback = function(shortname, data)
  437. {
  438.     if (shortname == this.shortName)
  439.     {
  440.         //FIXME check if data contains error tag and error message, print to log and keep previous data?
  441.         if (data.error)
  442.         {
  443.             log("Display: got this error from exchange.php: " + data.error + "\n");
  444.         }
  445.         else
  446.         {
  447.             this.exchangeData = data;
  448.             this.createCalendarMenu();
  449.            
  450.             this.setLEDtoCalendar();
  451.         }
  452.     }
  453. }
  454.  
  455. /* this function parses calendar data from exchange-script and creates a menu */
  456. Display.prototype.createCalendarMenu = function()
  457. {
  458.     /* This is used for function declarations like the callbacks below */
  459.     var self = this;
  460.  
  461.     this.exchangeCalendarLastMenuItem = null;
  462.     this.exchangeCalendarFirstMenuItem = null;
  463.    
  464.     if (this.exchangeData)
  465.     {
  466.         var lastMenuItem;
  467.         for (var i = 0; i < this.exchangeData.meetings.length; i++)
  468.         {
  469.             /* create the menuitem for a calendar meeting */
  470.             var menu = new MenuItem(this);
  471.             menu.displayData[0] = this.lcdCenterText(this.replaceAumlauts(this.exchangeData.meetings[i].subject));
  472.             menu.displayData[1] = this.lcdCenterText(this.exchangeData.meetings[i].start.replace(":",".") + " - "
  473.                                         + this.exchangeData.meetings[i].end.replace(":","."));
  474.             menu.doRight = function(args) { self.changeToNext(); };
  475.             menu.doLeft = function(args) { self.changeToPrev(); };
  476.            
  477.             if (i == 0)
  478.             {
  479.                 this.exchangeCalendarFirstMenuItem = menu;
  480.             }
  481.             else
  482.             {
  483.                 menu.setPrevItem(lastMenuItem);
  484.                 lastMenuItem.setNextItem(menu);
  485.             }
  486.            
  487.             lastMenuItem = menu;
  488.         }
  489.         if (lastMenuItem)
  490.         {
  491.             this.exchangeCalendarLastMenuItem = lastMenuItem;
  492.             this.exchangeCalendarLastMenuItem.setNextItem(this.bookMenuItem);
  493.             this.exchangeCalendarFirstMenuItem.setPrevItem(this.statusMenuItem);
  494.        
  495.         }
  496.         /* to link in the newly created menuitems with exchangeinformation the display must be showing
  497.            either the statusmenu or the bookingmenu */
  498.         if (this.currentMenuItem == this.statusMenuItem || this.currentMenuItem == this.bookMenuItem || this.currentMenuItem == this.bookResultMenuItem)
  499.         {
  500.             /* if there is one or more exchangemenuitems */
  501.             if (this.exchangeCalendarFirstMenuItem && this.exchangeCalendarLastMenuItem)
  502.             {
  503.                 this.statusMenuItem.setNextItem(this.exchangeCalendarFirstMenuItem);
  504.                 this.bookMenuItem.setPrevItem(this.exchangeCalendarLastMenuItem);
  505.             }
  506.             /* if not then link statusmenu to bookingmenu */
  507.             else
  508.             {
  509.                 this.statusMenuItem.setNextItem(this.bookMenuItem);
  510.                 this.bookMenuItem.setPrevItem(this.statusMenuItem);
  511.             }
  512.         }
  513.     }
  514. }
  515.  
  516. Display.prototype.replaceAumlauts = function(intext)
  517. {
  518.     intext = intext.replace(/Ã…/, String.fromCharCode(1));
  519.     intext = intext.replace(/Ã¥/, String.fromCharCode(0));
  520.     intext = intext.replace(/Ä/, String.fromCharCode(2));
  521.     intext = intext.replace(/ä/, String.fromCharCode(225));
  522.     intext = intext.replace(/Ö/, String.fromCharCode(3));
  523.     intext = intext.replace(/ö/, String.fromCharCode(239));
  524.     //intext = intext.replace(/:/, ";");
  525.     //intext = intext.replace(/,/, ".");
  526.     return intext;
  527. }
  528.  
  529. Display.prototype.softPwmOnline = function()
  530. {
  531. }
  532.  
  533. Display.prototype.rotaryOnline = function()
  534. {
  535. }
  536.  
  537. Display.prototype.rotaryPosUpdate = function(SwitchId)
  538. {
  539.     this.screenSaverCnt = 0;
  540.     this.mainScreenCnt = 0;
  541.     for (var i = 0; i < this.myRotaryService.getSteps(SwitchId); i++)
  542.     {
  543.         if (this.myRotaryService.getDirection(SwitchId) == "Clockwise")
  544.         {
  545.             if (this.currentMenuItem.doRight)
  546.             {
  547.                 this.currentMenuItem.doRight();
  548.             }
  549.         }
  550.         else
  551.         {
  552.             if (this.currentMenuItem.doLeft)
  553.             {
  554.                 this.currentMenuItem.doLeft();
  555.             }
  556.         }
  557.     }
  558.    
  559.     this.updateDisplay();
  560. }
  561.  
  562. Display.prototype.rotaryBtnUpdate = function(SwitchId)
  563. {
  564.     this.screenSaverCnt = 0;
  565.     this.mainScreenCnt = 0;
  566.     if (this.myRotaryService.getButtonStatus(SwitchId) == "Released")
  567.     {
  568.         if (this.currentMenuItem.doPress)
  569.         {
  570.             this.currentMenuItem.doPress();
  571.         }
  572.         this.updateDisplay();
  573.     }
  574. }
  575.  
  576. Display.prototype.updateDisplay = function()
  577. {
  578.     /* see if the current menuitem wants to update itself */
  579.     if (this.currentMenuItem.doUpdate)
  580.     {
  581.         this.currentMenuItem.doUpdate();
  582.     }
  583.  
  584.     /* Clear the LCD screen */
  585.     //this.myLCDService.clearScreen();
  586.    
  587.     /* send the data for the current menuitem to display */
  588.     for (var i = 0; i < this.currentMenuItem.displayData.length; i++)
  589.     {
  590.         this.myLCDService.printText(0, i, this.currentMenuItem.displayData[i]);
  591.     }
  592.    
  593. }
  594.  
  595. Display.prototype.lcdCenterText = function(text)
  596. {
  597.     returnText = text;
  598.     var displayWidth = 20;  //this.myLCDService.getWidth();
  599.     /* pad on both sides with spaces to displayWidth */
  600.     returnText = text.pad(displayWidth, " ", 2);
  601.    
  602.     return returnText;
  603. }
  604.  
  605. Display.prototype.lcdOffline = function()
  606. {
  607.     /* If we have no interval timer running do nothing */
  608.     if (this.myInterval != null)
  609.     {
  610.         /* Stop the interval */
  611.         this.myInterval.stop();
  612.     }
  613. }
  614.  
  615. Display.prototype.lcdOnline = function()
  616. {
  617.     /* If service is not online do nothing */
  618.     if (this.myLCDService.isOnline())
  619.     {
  620.         /* Clear the LCD screen */
  621.         this.myLCDService.clearScreen();
  622.         /* Set backlight to max */
  623.         this.myLCDService.setBacklight(255);
  624.  
  625.         /* If we have no interval timer running start it */
  626.         if (this.myInterval == null)
  627.         {
  628.             var self = this;
  629.        
  630.             /* Start interval timer for our printout. Arguments are the callback function and time in milliseconds */
  631.             this.myInterval = new Interval(function() { self.timerUpdate() }, 5000);
  632.         }
  633.        
  634.         this.myInterval.start();
  635.        
  636.         this.timerUpdate();
  637.     }
  638. }
  639. Display.prototype.setLEDtoCalendar = function()
  640. {
  641.     if (this.exchangeData)
  642.     {
  643.         var now = new Date();
  644.         //for (var i = 0; i < this.exchangeData.meetings.length; i++)
  645.         if (this.exchangeData.meetings.length > 0)
  646.         {
  647.             /* check if current time is before start of first meeting
  648.             first meeting is always the next meeting or current meeting */
  649.             //var startDate = new Date.parse(this.exchangeData.meetings[0].start);
  650.             var startTimeSplit = this.exchangeData.meetings[0].start.split(":");
  651.             if (now.getHours() < startTimeSplit[0] || now.getHours() == startTimeSplit[0] && now.getMinutes() < startTimeSplit[1])
  652.             {
  653.                 this.setLEDgreen();
  654.             }
  655.             else
  656.             {
  657.                 this.setLEDred();
  658.             }
  659.         }
  660.         else
  661.         {
  662.             this.setLEDgreen();
  663.         }
  664.     }
  665. }
  666.  
  667. Display.prototype.setLEDred = function()
  668. {
  669.     this.setLED(255, 0, 0);
  670. }
  671.  
  672. Display.prototype.setLEDgreen = function()
  673. {
  674.     this.setLED(0, 255, 0);
  675. }
  676.  
  677. Display.prototype.setLED = function(red, green, blue)
  678. {
  679. log("trying to set leds to red " +red+" green "+green+" blue "+blue+"\n");
  680.     if (this.mySoftPwmService.isOnline())
  681.     {
  682.         this.mySoftPwmService.setPWMValue(255-(red&0xff), 1);
  683.         this.mySoftPwmService.setPWMValue(255-(green&0xff), 2);
  684.         this.mySoftPwmService.setPWMValue(255-(blue&0xff), 0);
  685.     }
  686. }
  687.  
  688. Display.prototype.sendTimeStamp = function()
  689. {
  690.     var canMessage = new CanNMTMessage("nmt", "Time");
  691.     canMessage.send();
  692. }
  693.  
  694. Display.prototype.timerUpdate = function()
  695. {
  696.     /* If LCD service is not online do nothing */
  697.     if (this.myLCDService.isOnline())
  698.     {
  699.         this.screenSaverCnt++;
  700.         this.mainScreenCnt++;
  701.         this.exchangeUpdateCnt++;
  702.  
  703.         if (this.mainScreenCnt > mainScreenTimeout/5)
  704.         {
  705.             /* Go to main screen (time) */
  706.             this.currentMenuItem = this.statusMenuItem;
  707.  
  708.             /* update the info on display */
  709.             this.updateDisplay();
  710.  
  711.             this.mainScreenCnt = 0;
  712.         }
  713.        
  714.         if (this.screenSaverCnt > screenSaverTimeout/5)
  715.         {
  716.            
  717.             this.screenSaverCnt = 0;
  718.         }
  719.  
  720.         if (this.exchangeUpdateCnt > exchangeUpdateTimeout/5)
  721.         {
  722.             /* update the info on display */
  723.             this.updateDisplay();
  724.            
  725.             /* do an exchange lookup */
  726.             this.exchangeCalendar.lookup(this.shortName);
  727.            
  728.             this.exchangeUpdateCnt = 0;
  729.         }
  730.     }
  731. }
  732.  
  733.