Subversion Repositories HomeAutomation

Rev

Blame | Last modification | View Log | SVN | RSS feed

  1.  
  2. loadScript("Logic/MenuItem.js");
  3.  
  4. function SangDisplay(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(SangDisplay, Service);
  14.  
  15. /* Declaration of instance variables, for static variables remove prototype */
  16.  
  17. SangDisplay.prototype.myLCDService = null;
  18. SangDisplay.prototype.myRotaryService = null;
  19. SangDisplay.prototype.myTempService = null;
  20. SangDisplay.prototype.myInterval = null;
  21. SangDisplay.prototype.myIntervalAlways = null;
  22.  
  23. SangDisplay.prototype.mainMenuItem = null;
  24. SangDisplay.prototype.screenSaverMenuItem = null;
  25.  
  26.  
  27. /* The currently displayed menuitem */
  28. SangDisplay.prototype.currentMenuItem = null;
  29.  
  30. /* counter for going to screensaver */
  31. SangDisplay.prototype.screenSaverCnt = null;
  32.  
  33. /* counter for going to main screen */
  34. SangDisplay.prototype.mainScreenCnt = null;
  35.  
  36. const outsideTemperatureSensorId = 236;
  37. const insideTemperatureSensorId = 204;
  38. var outsideTemperature = 0;
  39. var insideTemperature= 0;
  40. var CurrentPower = 0;
  41. var CurrentEnergy = 0;
  42.  
  43. const mainScreenTimeout = 5;
  44. const screenSaverTimeout = 10;
  45.  
  46.  
  47. /* This function must always be declared, this is where all the startup code
  48.    should be placed. Gets called with arguments like what ids to use etc. */
  49. SangDisplay.prototype.initialize = function(initialArguments)
  50. {
  51.     /* We must always call the parent initialize */
  52.     this.Service.prototype.initialize.call(this, initialArguments);
  53.  
  54.     /* This is used for function declarations like the callbacks below */
  55.     var self = this;
  56.  
  57.     if (!this.myInitialArguments["HD44789"])
  58.     {
  59.         log(this.myName + ":" + this.myId + "> Failed to initialize, HD44789-config missing from config.\n");
  60.         return;
  61.     }
  62.    
  63.     if (!this.myInitialArguments["Rotary"])
  64.     {
  65.         log(this.myName + ":" + this.myId + "> Failed to initialize, Rotary-config missing from config.\n");
  66.         return;
  67.     }
  68. if (!this.myInitialArguments["DS18x20"])
  69.     {
  70.         log(this.myName + ":" + this.myId + "> Failed to initialize, DS18x20-config missing from config.\n");
  71.         return;
  72.     }
  73. if (!this.myInitialArguments["power"])
  74.     {
  75.         log(this.myName + ":" + this.myId + "> Failed to initialize, power-config missing from config.\n");
  76.         return;
  77.     }
  78.     /* Start interval timer for sending timestamp to network. Arguments are the callback function and time in milliseconds
  79.     used to make sure an eth-node gets its init packet (600s) */
  80.     this.myIntervalAlways = new Interval(function() { self.sendTimeStamp() }, 600000);
  81.     this.myIntervalAlways.start();
  82.  
  83.     this.myInterval = new Interval(function() { self.timerUpdate() }, 5000);
  84.     this.myInterval.start();
  85.  
  86.     this.screenSaverCnt = 0;
  87.     this.mainScreenCnt = 0;
  88.    
  89.    
  90.     /* Get the LCD service that we want from the ServiceManager, it takes type, service name, service id */
  91.     this.myLCDService = ServiceManager.getService("Can", "HD44789", this.myInitialArguments["HD44789"]["Id"]);
  92.     /* Add a callback for when the service goes online */
  93.     this.myLCDService.registerEventCallback("online", function(args) { self.lcdOnline(); });
  94.     /* If the service is already online we should call the handler here */
  95.     this.lcdOnline();
  96.     /* Add a callback for when the service goes offline */
  97.     this.myLCDService.registerEventCallback("offline", function(args) { self.lcdOffline(); });
  98.    
  99.     /* Get the Rotary service that we want from the ServiceManager, it takes type, service name, service id */
  100.     this.myRotaryService = ServiceManager.getService("Can", "Rotary", this.myInitialArguments["Rotary"]["Id"]);
  101.     /* Add a callback for when the encoder reports a new position */
  102.     this.myRotaryService.registerEventCallback("newPosition", function(args) { self.rotaryPosUpdate(args); });
  103.     /* Add a callback for when the encoder reports a new button status */
  104.     this.myRotaryService.registerEventCallback("newBtnStatus", function(args) { self.rotaryBtnUpdate(args); });
  105.     /* Add a callback for when the service goes online */
  106.     this.myRotaryService.registerEventCallback("online", function(args) { self.rotaryOnline(); });
  107.     /* If the service is already online we should call the handler here */
  108.     this.rotaryOnline();
  109.  
  110.     /* Get the LCD service that we want from the ServiceManager, it takes type, service name, service id */
  111.     //this.myTempService = ServiceManager.getService("Can", "DS18x20", this.myInitialArguments["DS18x20"]["Id"]);
  112.     /* Add a callback for when the sensor reports a new value */
  113.     //this.myTempService.registerEventCallback("newValue", function(args) { self.temperatureUpdate(args); });
  114.     /* Add a callback for when the service goes online */
  115.     //this.myTempService.registerEventCallback("online", function(args) { self.tempOnline(); });
  116.     /* If the service is already online we should call the handler here */
  117.     //this.tempOnline();
  118.  
  119. /* Get the PWM service that we want from the ServiceManager, it takes type, service name, service id */
  120.     this.myPowerService = ServiceManager.getService("Can", "power", this.myInitialArguments["power"]["Id"]);
  121.     /* Add a callback for when the service goes online */
  122.     this.myPowerService.registerEventCallback("online", function(args) { self.PowerOnline(); });
  123.     /* Add a callback for when the sensor reports a new value */
  124.     this.myPowerService.registerEventCallback("newValue", function(args) { self.powerUpdate(); });
  125.     /* If the service is already online we should call the handler here */
  126.     this.PowerOnline();
  127.    
  128.     /* create the first menu item */
  129.     this.mainMenuItem = new MenuItem(this);
  130.  
  131.  
  132.    
  133. this.mainMenuItem.displayData[0] = this.lcdCenterText("Main");
  134.  
  135.     this.mainMenuItem.doUpdate = function(args) { self.updateMainMenuItem(); };
  136.     this.currentMenuItem = this.mainMenuItem;
  137.  
  138.    
  139.     /* create the screensaver menu item */
  140.     this.screenSaverMenuItem = new MenuItem(this);
  141.     this.screenSaverMenuItem.displayData[0] = this.lcdCenterText("Saver");
  142.     this.screenSaverMenuItem.displayData[1] = this.lcdCenterText("MenuItem");
  143. this.screenSaverMenuItem.displayData[2] = this.lcdCenterText("");
  144. this.screenSaverMenuItem.displayData[3] = this.lcdCenterText("");
  145.     this.screenSaverMenuItem.doRight = function(args) { self.changeToNext(); };
  146.     this.screenSaverMenuItem.doLeft = function(args) { self.changeToPrev(); };
  147.     this.screenSaverMenuItem.setNextItem(this.mainMenuItem);
  148.     this.screenSaverMenuItem.setPrevItem(this.mainMenuItem);
  149.  
  150.     /* create the menuitem where you can choose to enter the booking sub-menu */
  151.  
  152.  
  153.     /* connect the items as a linked list */
  154.  
  155.     /* set the function that shall be executed when knob is turned */
  156.     this.mainMenuItem.doRight = function(args) { self.changeToNext(); };
  157.     this.mainMenuItem.doLeft = function(args) { self.changeToPrev(); };
  158.     this.mainMenuItem.setNextItem(this.screenSaverMenuItem);
  159.     this.mainMenuItem.setPrevItem(this.screenSaverMenuItem);
  160. }
  161.  
  162.  
  163. SangDisplay.prototype.tempOnline = function()
  164. {
  165.     /* If service is not online do nothing */
  166.     if (this.myTempService.isOnline())
  167.     {
  168.         /* Set report interval to 5 seconds */
  169.         this.myTempService.setReportInterval(5);
  170.     }
  171. }
  172.  
  173. SangDisplay.prototype.temperatureUpdate = function(sensorId)
  174. {
  175.     if (sensorId == outsideTemperatureSensorId) {
  176.         outsideTemperature = this.myTempService.getValue(sensorId).toFixed(2).toString();
  177.     }
  178.     if (sensorId == insideTemperatureSensorId) {
  179.         insideTemperature = this.myTempService.getValue(sensorId).toFixed(2).toString();
  180.     }
  181. }
  182. SangDisplay.prototype.PowerOnline = function()
  183. {
  184.     /* If service is not online do nothing */
  185.     if (this.myPowerService.isOnline())
  186.     {
  187.         log("Power is online!\n");
  188.         /* Set report interval to 2 seconds */
  189.         this.myPowerService.setReportInterval(2);
  190.     }
  191. }
  192.  
  193. SangDisplay.prototype.powerUpdate = function()
  194. {
  195.     CurrentPower = this.myPowerService.getPower();
  196.     CurrentEnergy = this.myPowerService.getEnergy();
  197.     if (this.currentMenuItem == this.mainMenuItem)
  198.     {
  199.         this.updateDisplay();
  200.     }
  201. }
  202.  
  203. SangDisplay.prototype.changeToDesc = function()
  204. {
  205.     if (this.currentMenuItem.descItem)
  206.     {
  207.         this.currentMenuItem = this.currentMenuItem.descItem;
  208.     }
  209. }
  210.  
  211. SangDisplay.prototype.changeToNext = function()
  212. {
  213.     if (this.currentMenuItem.nextItem)
  214.     {
  215.         this.currentMenuItem = this.currentMenuItem.nextItem;
  216.     }
  217. }
  218.  
  219. SangDisplay.prototype.changeToPrev = function()
  220. {
  221.     if (this.currentMenuItem.prevItem)
  222.     {
  223.         this.currentMenuItem = this.currentMenuItem.prevItem;
  224.     }
  225. }
  226.  
  227.  
  228.  
  229. SangDisplay.prototype.updateMainMenuItem = function()
  230. {
  231.     var date = new Date();
  232.     var dateAndTime = "" + date.getTimeShortFormated();
  233.     this.mainMenuItem.displayData[0] = this.lcdCenterText(dateAndTime);
  234. this.mainMenuItem.displayData[1] = "";
  235.     this.mainMenuItem.displayData[2] = this.lcdCenterText(""+getSensorValue('Vardagsrumssensor').toFixed(1).toString() + " C " + getSensorValue('Västersensor').toFixed(1).toString() + " C");
  236.     this.mainMenuItem.displayData[3] = this.lcdCenterText(""+CurrentPower.toString()+" Watt "+(CurrentEnergy/1000).toString() + "kWh");
  237. this.myLCDService.setBacklight(10);
  238. }
  239.  
  240.  
  241.  
  242. SangDisplay.prototype.replaceAumlauts = function(intext)
  243. {
  244.     intext = intext.replace(/Å/, String.fromCharCode(1));
  245.     intext = intext.replace(/å/, String.fromCharCode(0));
  246.     intext = intext.replace(/Ä/, String.fromCharCode(2));
  247.     intext = intext.replace(/ä/, String.fromCharCode(225));
  248.     intext = intext.replace(/Ö/, String.fromCharCode(3));
  249.     intext = intext.replace(/ö/, String.fromCharCode(239));
  250.     //intext = intext.replace(/:/, ";");
  251.     //intext = intext.replace(/,/, ".");
  252.     return intext;
  253. }
  254.  
  255.  
  256.  
  257. SangDisplay.prototype.rotaryOnline = function()
  258. {
  259. }
  260.  
  261. SangDisplay.prototype.rotaryPosUpdate = function(SwitchId)
  262. {
  263.     this.screenSaverCnt = 0;
  264.     this.mainScreenCnt = 0;
  265.     for (var i = 0; i < this.myRotaryService.getSteps(SwitchId); i++)
  266.     {
  267.         if (this.myRotaryService.getDirection(SwitchId) == "Clockwise")
  268.         {
  269.             if (this.currentMenuItem.doRight)
  270.             {
  271.                 this.currentMenuItem.doRight();
  272.             }
  273.         }
  274.         else
  275.         {
  276.             if (this.currentMenuItem.doLeft)
  277.             {
  278.                 this.currentMenuItem.doLeft();
  279.             }
  280.         }
  281.     }
  282.    
  283.     this.updateDisplay();
  284. }
  285.  
  286. SangDisplay.prototype.rotaryBtnUpdate = function(SwitchId)
  287. {
  288.     this.screenSaverCnt = 0;
  289.     this.mainScreenCnt = 0;
  290.     if (this.myRotaryService.getButtonStatus(SwitchId) == "Released")
  291.     {
  292.         if (this.currentMenuItem.doPress)
  293.         {
  294.             this.currentMenuItem.doPress();
  295.         }
  296.         this.updateDisplay();
  297.     }
  298. }
  299.  
  300. SangDisplay.prototype.updateDisplay = function()
  301. {
  302.     /* see if the current menuitem wants to update itself */
  303.     if (this.currentMenuItem.doUpdate)
  304.     {
  305.         this.currentMenuItem.doUpdate();
  306.     }
  307.  
  308.     /* Clear the LCD screen */
  309.     //this.myLCDService.clearScreen();
  310.    
  311.     /* send the data for the current menuitem to display */
  312.     for (var i = 0; i < this.currentMenuItem.displayData.length; i++)
  313.     {
  314.         this.myLCDService.printText(0, i, this.currentMenuItem.displayData[i]);
  315.     }
  316.    
  317. }
  318.  
  319. SangDisplay.prototype.lcdCenterText = function(text)
  320. {
  321.     returnText = text;
  322.     var displayWidth = 20;  //this.myLCDService.getWidth();
  323.     /* pad on both sides with spaces to displayWidth */
  324.     returnText = text.pad(displayWidth, " ", 2);
  325.    
  326.     return returnText;
  327. }
  328.  
  329. SangDisplay.prototype.lcdOffline = function()
  330. {
  331. }
  332.  
  333. SangDisplay.prototype.lcdOnline = function()
  334. {
  335.     /* If service is not online do nothing */
  336.     if (this.myLCDService.isOnline())
  337.     {
  338.         /* Clear the LCD screen */
  339.         this.myLCDService.clearScreen();
  340.         /* Set backlight to max */
  341.         this.myLCDService.setBacklight(0);
  342.  
  343.         this.timerUpdate();
  344.     }
  345. }
  346.  
  347.  
  348. SangDisplay.prototype.sendTimeStamp = function()
  349. {
  350.     var canMessage = new CanNMTMessage("nmt", "Time");
  351.     canMessage.send();
  352. }
  353.  
  354. SangDisplay.prototype.timerUpdate = function()
  355. {
  356.     /* If LCD service is not online do nothing */
  357.     if (this.myLCDService.isOnline())
  358.     {
  359.         this.screenSaverCnt++;
  360.         this.mainScreenCnt++;
  361.         if (this.mainScreenCnt > mainScreenTimeout/5 && this.currentMenuItem != this.screenSaverMenuItem)
  362.         {
  363.             /* Go to main screen (time) */
  364.             this.currentMenuItem = this.mainMenuItem;
  365.  
  366.             /* update the info on display */
  367.             this.updateDisplay();
  368.  
  369.             this.mainScreenCnt = 0;
  370. this.myLCDService.setBacklight(10);
  371.         }
  372.        
  373.         if (this.screenSaverCnt > screenSaverTimeout/5)
  374.         {
  375.             /* Go to screensaver menu */
  376.             this.currentMenuItem = this.screenSaverMenuItem;
  377. this.myLCDService.setBacklight(0);
  378.             /* update the info on display */
  379.             this.updateDisplay();
  380.            
  381.             this.screenSaverCnt = 0;
  382.         }
  383.  
  384.        
  385.  
  386.     }
  387. }
  388.  
  389.