Subversion Repositories HomeAutomation

Rev

Rev 1244 | Rev 1246 | 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. //log("callback: "+shortname+" \n");
  439.     if (shortname == this.shortName)
  440.     {
  441.         //FIXME check if data contains error tag and error message, print to log and keep previous data?
  442.         if (data.error)
  443.         {
  444.             log("Display: got this error from exchange.php: " + data.error + "\n");
  445.         }
  446.         else
  447.         {
  448.             this.exchangeData = data;
  449.             this.createCalendarMenu();
  450.            
  451.             this.setLEDtoCalendar();
  452.         }
  453.     }
  454. }
  455.  
  456. /* this function parses calendar data from exchange-script and creates a menu */
  457. Display.prototype.createCalendarMenu = function()
  458. {
  459.     /* This is used for function declarations like the callbacks below */
  460.     var self = this;
  461.  
  462.     this.exchangeCalendarLastMenuItem = null;
  463.     this.exchangeCalendarFirstMenuItem = null;
  464.    
  465.     if (this.exchangeData)
  466.     {
  467.         var lastMenuItem;
  468.         for (var i = 0; i < this.exchangeData.meetings.length; i++)
  469.         {
  470.             /* create the menuitem for a calendar meeting */
  471.             var menu = new MenuItem(this);
  472.             menu.displayData[0] = this.lcdCenterText(this.replaceAumlauts(this.exchangeData.meetings[i].subject));
  473.             menu.displayData[1] = this.lcdCenterText(this.exchangeData.meetings[i].start.replace(":",".") + " - "
  474.                                         + this.exchangeData.meetings[i].end.replace(":","."));
  475.             menu.doRight = function(args) { self.changeToNext(); };
  476.             menu.doLeft = function(args) { self.changeToPrev(); };
  477.            
  478.             if (i == 0)
  479.             {
  480.                 this.exchangeCalendarFirstMenuItem = menu;
  481.             }
  482.             else
  483.             {
  484.                 menu.setPrevItem(lastMenuItem);
  485.                 lastMenuItem.setNextItem(menu);
  486.             }
  487.            
  488.             lastMenuItem = menu;
  489.         }
  490.         if (lastMenuItem)
  491.         {
  492.             this.exchangeCalendarLastMenuItem = lastMenuItem;
  493.             this.exchangeCalendarLastMenuItem.setNextItem(this.bookMenuItem);
  494.             this.exchangeCalendarFirstMenuItem.setPrevItem(this.statusMenuItem);
  495.        
  496.         }
  497.         /* to link in the newly created menuitems with exchangeinformation the display must be showing
  498.            either the statusmenu or the bookingmenu */
  499.         if (this.currentMenuItem == this.statusMenuItem || this.currentMenuItem == this.bookMenuItem || this.currentMenuItem == this.bookResultMenuItem)
  500.         {
  501.             /* if there is one or more exchangemenuitems */
  502.             if (this.exchangeCalendarFirstMenuItem && this.exchangeCalendarLastMenuItem)
  503.             {
  504.                 this.statusMenuItem.setNextItem(this.exchangeCalendarFirstMenuItem);
  505.                 this.bookMenuItem.setPrevItem(this.exchangeCalendarLastMenuItem);
  506.             }
  507.             /* if not then link statusmenu to bookingmenu */
  508.             else
  509.             {
  510.                 this.statusMenuItem.setNextItem(this.bookMenuItem);
  511.                 this.bookMenuItem.setPrevItem(this.statusMenuItem);
  512.             }
  513.         }
  514.     }
  515. }
  516.  
  517. Display.prototype.replaceAumlauts = function(intext)
  518. {
  519.     intext = intext.replace(/Ã…/, String.fromCharCode(1));
  520.     intext = intext.replace(/Ã¥/, String.fromCharCode(0));
  521.     intext = intext.replace(/Ä/, String.fromCharCode(2));
  522.     intext = intext.replace(/ä/, String.fromCharCode(225));
  523.     intext = intext.replace(/Ö/, String.fromCharCode(3));
  524.     intext = intext.replace(/ö/, String.fromCharCode(239));
  525.     //intext = intext.replace(/:/, ";");
  526.     //intext = intext.replace(/,/, ".");
  527.     return intext;
  528. }
  529.  
  530. Display.prototype.softPwmOnline = function()
  531. {
  532. }
  533.  
  534. Display.prototype.rotaryOnline = function()
  535. {
  536. }
  537.  
  538. Display.prototype.rotaryPosUpdate = function(SwitchId)
  539. {
  540.     this.screenSaverCnt = 0;
  541.     this.mainScreenCnt = 0;
  542.     for (var i = 0; i < this.myRotaryService.getSteps(SwitchId); i++)
  543.     {
  544.         if (this.myRotaryService.getDirection(SwitchId) == "Clockwise")
  545.         {
  546.             if (this.currentMenuItem.doRight)
  547.             {
  548.                 this.currentMenuItem.doRight();
  549.             }
  550.         }
  551.         else
  552.         {
  553.             if (this.currentMenuItem.doLeft)
  554.             {
  555.                 this.currentMenuItem.doLeft();
  556.             }
  557.         }
  558.     }
  559.    
  560.     this.updateDisplay();
  561. }
  562.  
  563. Display.prototype.rotaryBtnUpdate = function(SwitchId)
  564. {
  565.     this.screenSaverCnt = 0;
  566.     this.mainScreenCnt = 0;
  567.     if (this.myRotaryService.getButtonStatus(SwitchId) == "Released")
  568.     {
  569.         if (this.currentMenuItem.doPress)
  570.         {
  571.             this.currentMenuItem.doPress();
  572.         }
  573.         this.updateDisplay();
  574.     }
  575. }
  576.  
  577. Display.prototype.updateDisplay = function()
  578. {
  579.     /* see if the current menuitem wants to update itself */
  580.     if (this.currentMenuItem.doUpdate)
  581.     {
  582.         this.currentMenuItem.doUpdate();
  583.     }
  584.  
  585.     /* Clear the LCD screen */
  586.     //this.myLCDService.clearScreen();
  587.    
  588.     /* send the data for the current menuitem to display */
  589.     for (var i = 0; i < this.currentMenuItem.displayData.length; i++)
  590.     {
  591.         this.myLCDService.printText(0, i, this.currentMenuItem.displayData[i]);
  592.     }
  593.    
  594. }
  595.  
  596. Display.prototype.lcdCenterText = function(text)
  597. {
  598.     returnText = text;
  599.     var displayWidth = 20;  //this.myLCDService.getWidth();
  600.     /* pad on both sides with spaces to displayWidth */
  601.     returnText = text.pad(displayWidth, " ", 2);
  602.    
  603.     return returnText;
  604. }
  605.  
  606. Display.prototype.lcdOffline = function()
  607. {
  608.     /* If we have no interval timer running do nothing */
  609.     if (this.myInterval != null)
  610.     {
  611.         /* Stop the interval */
  612.         this.myInterval.stop();
  613.     }
  614. }
  615.  
  616. Display.prototype.lcdOnline = function()
  617. {
  618.     /* If service is not online do nothing */
  619.     if (this.myLCDService.isOnline())
  620.     {
  621.         /* Clear the LCD screen */
  622.         this.myLCDService.clearScreen();
  623.         /* Set backlight to max */
  624.         this.myLCDService.setBacklight(255);
  625.  
  626.         /* If we have no interval timer running start it */
  627.         if (this.myInterval == null)
  628.         {
  629.             var self = this;
  630.        
  631.             /* Start interval timer for our printout. Arguments are the callback function and time in milliseconds */
  632.             this.myInterval = new Interval(function() { self.timerUpdate() }, 5000);
  633.         }
  634.        
  635.         this.myInterval.start();
  636.        
  637.         this.timerUpdate();
  638.     }
  639. }
  640.  
  641. Display.prototype.setLEDtoCalendar = function()
  642. {
  643.     if (this.exchangeData)
  644.     {
  645.         var now = new Date();
  646.         //for (var i = 0; i < this.exchangeData.meetings.length; i++)
  647.         if (this.exchangeData.meetings.length > 0)
  648.         {
  649.             /* check if current time is before start of first meeting
  650.             first meeting is always the next meeting or current meeting */
  651.             var startDate = new Date(this.exchangeData.meetings[0].start);
  652.             if (now < startDate)
  653.             {
  654.                 this.setLEDgreen();
  655.             }
  656.             else
  657.             {
  658.                 this.setLEDred();
  659.             }
  660.  
  661.             //var endDate = new Date(this.exchangeData.meetings[i].end);
  662.             //now.getHours()
  663.             //this.exchangeData.meetings[i].start
  664.             //this.exchangeData.meetings[i].end
  665.             //var startTimeSplit = this.exchangeData.meetings[i].start.split(":");
  666.             //var endTimeSplit = this.exchangeData.meetings[i].end.split(":");
  667.         }
  668. }
  669.  
  670. Display.prototype.setLEDred = function()
  671. {
  672.     this.setLED(255, 0, 0);
  673. }
  674.  
  675. Display.prototype.setLEDgreen = function()
  676. {
  677.     this.setLED(0, 255, 0);
  678. }
  679.  
  680. Display.prototype.setLED = function(red, green, blue)
  681. {
  682.     if (this.mySoftPwmService.isOnline())
  683.     {
  684.         this.mySoftPwmService.setPWMValue(255-(red&0xff), 1);
  685.         this.mySoftPwmService.setPWMValue(255-(green&0xff), 2);
  686.         this.mySoftPwmService.setPWMValue(255-(blue&0xff), 0);
  687.     }
  688. }
  689.  
  690. Display.prototype.sendTimeStamp = function()
  691. {
  692.     var canMessage = new CanNMTMessage("nmt", "Time");
  693.     canMessage.send();
  694. }
  695.  
  696. Display.prototype.timerUpdate = function()
  697. {
  698.     /* If LCD service is not online do nothing */
  699.     if (this.myLCDService.isOnline())
  700.     {
  701. //this.setLEDgreen();
  702.         this.screenSaverCnt++;
  703.         this.mainScreenCnt++;
  704.         this.exchangeUpdateCnt++;
  705.  
  706.         if (this.mainScreenCnt > mainScreenTimeout/5)
  707.         {
  708. //this.setLEDred();
  709.             /* Go to main screen (time) */
  710.             this.currentMenuItem = this.statusMenuItem;
  711.  
  712.             /* update the info on display */
  713.             this.updateDisplay();
  714.  
  715.             this.mainScreenCnt = 0;
  716.         }
  717.        
  718.         if (this.screenSaverCnt > screenSaverTimeout/5)
  719.         {
  720.            
  721.             this.screenSaverCnt = 0;
  722.         }
  723.  
  724.         if (this.exchangeUpdateCnt > exchangeUpdateTimeout/5)
  725.         {
  726.             /* update the info on display */
  727.             this.updateDisplay();
  728.            
  729.             /* do an exchange lookup */
  730.         //  this.exchangeCalendar.lookup(this.shortName);
  731.            
  732.             this.exchangeUpdateCnt = 0;
  733.         }
  734.        
  735.     }
  736.    
  737.     /*if (this.mySoftPwmService.isOnline())
  738.     {
  739.         this.mySoftPwmService.setPWMValue(0, 0);
  740.         this.mySoftPwmService.setPWMValue(11, 1);
  741.         this.mySoftPwmService.setPWMValue(11, 2);
  742.     }*/
  743. }
  744.  
  745.