Subversion Repositories HomeAutomation

Rev

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