Subversion Repositories HomeAutomation

Rev

Rev 969 | Rev 999 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  1.  
  2. function SensorPrint(name, id)
  3. {
  4.     /* We must always call the parent constructor, initialization
  5.        of variables could be done here, but initialize is a better place */
  6.     this.Service(name, id);
  7. }
  8.  
  9. /* We inherit from Service with this call, it must always be next
  10.    after the constructor */
  11. extend(SensorPrint, Service);
  12.  
  13. /* Declaration of instance variables, for static variables remove prototype */
  14. SensorPrint.prototype.myLCDService = null;
  15. SensorPrint.prototype.myTempService = null;
  16. SensorPrint.prototype.myVoltService = null;
  17. SensorPrint.prototype.myInterval = null;
  18.  
  19. /* This function must always be declared, this is where all the startup code
  20.    should be placed. Gets called with arguments like what ids to use etc. */
  21. SensorPrint.prototype.initialize = function(initialArguments)
  22. {
  23.     /* We must always call the parent initialize */
  24.     this.Service.prototype.initialize.call(this, initialArguments);
  25.  
  26.     /* This is used for function decalrations like the callbacks below */
  27.     var self = this;
  28.  
  29.     /* Get the LCD service that we want from the ServiceManager, it takes type, service name, service id */
  30.     this.myLCDService = ServiceManager.getService("Can", "HD44789", this.myInitialArguments["HD44789"]["Id"]);
  31.     /* Add a callback for when the service goes online */
  32.     this.myLCDService.registerEventCallback("online", function(args) { self.lcdOnline(); });
  33.     /* If the service is already online we should call the handler here */
  34.     this.lcdOnline();
  35.     /* Add a callback for when the service goes offline */
  36.     this.myLCDService.registerEventCallback("offline", function(args) { self.lcdOffline(); });
  37.  
  38.     /* Get the LCD service that we want from the ServiceManager, it takes type, service name, service id */
  39.     this.myTempService = ServiceManager.getService("Can", "DS18x20", this.myInitialArguments["DS18x20"]["Id"]);
  40.     /* Add a callback for when the sensor reports a new value */
  41.     this.myTempService.registerEventCallback("newValue", function(args) { self.temperatureUpdate(args); });
  42.     /* Add a callback for when the service goes online */
  43.     this.myTempService.registerEventCallback("online", function(args) { self.tempOnline(); });
  44.     /* If the service is already online we should call the handler here */
  45.     this.tempOnline();
  46.  
  47.     /* Get the LCD service that we want from the ServiceManager, it takes type, service name, service id */
  48.     this.myVoltService = ServiceManager.getService("Can", "BusVoltage", this.myInitialArguments["BusVoltage"]["Id"]);
  49.     /* Add a callback for when the sensor reports a new value */
  50.     this.myVoltService.registerEventCallback("newValue", function(args) { self.voltageUpdate(args); });
  51.     /* Add a callback for when the service goes online */
  52.     this.myVoltService.registerEventCallback("online", function(args) { self.voltOnline(); });
  53.     /* If the service is already online we should call the handler here */
  54.     this.voltOnline();
  55. }
  56.  
  57. SensorPrint.prototype.lcdOnline = function()
  58. {
  59.     /* If service is not online do nothing */
  60.     if (this.myLCDService.isOnline())
  61.     {
  62.         /* Clear the LCD screen */
  63.         this.myLCDService.clearScreen();
  64.         /* Set backlight to max */
  65.         this.myLCDService.setBacklight(255);
  66.  
  67.         /* If we have no interval timer running start it */
  68.         if (this.myInterval == null)
  69.         {
  70.             var self = this;
  71.        
  72.             /* Start interval timer for our printout. Arguments are the callback function and time in seconds */
  73.             this.myInterval = new Interval(function() { self.timerUpdate() }, 1000);
  74.         }
  75.        
  76.         this.myInterval.start();
  77.     }
  78. }
  79.  
  80. SensorPrint.prototype.lcdOffline = function()
  81. {
  82.     /* If we have no interval timer running do nothing */
  83.     if (this.myInterval != null)
  84.     {
  85.         /* Stop the interval */
  86.         this.myInterval.stop();
  87.     }
  88. }
  89.  
  90. SensorPrint.prototype.tempOnline = function()
  91. {
  92.     /* If service is not online do nothing */
  93.     if (this.myTempService.isOnline())
  94.     {
  95.         /* Set report interval to 5 seconds */
  96.         this.myTempService.setReportInterval(5);
  97.     }
  98. }
  99.  
  100. SensorPrint.prototype.voltOnline = function()
  101. {
  102.     /* If service is not online do nothing */
  103.     if (this.myVoltService.isOnline())
  104.     {
  105.         /* Set report interval to 5 seconds */
  106.         this.myVoltService.setReportInterval(5);
  107.     }
  108. }
  109.  
  110. SensorPrint.prototype.temperatureUpdate = function(sensorId)
  111. {
  112.     /* If LCD service is not online do nothing */
  113.     if (this.myLCDService.isOnline())
  114.     {
  115.         /* Format our data to the right format */
  116.         var data = (this.myTempService.getValue(sensorId).toFixed(2).toString() + " C").pad(7, " ", 1);
  117.         /* Print the text to the LCD */
  118.         this.myLCDService.printText(0, 0, data);
  119.         /* Print out what we are doing to the console */
  120.         log(this.myName + ":" + this.myId + "> Trying to print " + data + " value to LCD\n");
  121.     }
  122. }
  123.  
  124. SensorPrint.prototype.voltageUpdate = function(sensorId)
  125. {
  126.     /* If LCD service is not online do nothing */
  127.     if (this.myLCDService.isOnline())
  128.     {
  129.         /* Format our data to the right format */
  130.         var data = (this.myVoltService.getValue(sensorId).toFixed(2).toString() + " V").pad(7, " ", 1);
  131.         /* Print the text to the LCD */
  132.         this.myLCDService.printText(8, 0, data);
  133.         /* Print out what we are doing to the console */
  134.         log(this.myName + ":" + this.myId + "> Trying to print " + data + " value to LCD\n");
  135.     }
  136. }
  137.  
  138. SensorPrint.prototype.timerUpdate = function()
  139. {
  140.     /* If LCD service is not online do nothing */
  141.     if (this.myLCDService.isOnline())
  142.     {
  143.         var date = new Date();
  144.         /* Get the current date time on the format YYYY-mm-dd HH.ii.ss */
  145.         var dateAndTime = date.getDateTimeFormated();
  146.         /* Print the text to the LCD */
  147.         this.myLCDService.printText(0, 3, dateAndTime);
  148.         /* Print out what we are doing to the console */
  149.         log(this.myName + ":" + this.myId + "> Trying to print " + dateAndTime + " to LCD\n");
  150.     }
  151. }
  152.