Subversion Repositories HomeAutomation

Rev

Rev 987 | 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.myTimeoutId = 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.  
  36.     /* Get the LCD service that we want from the ServiceManager, it takes type, service name, service id */
  37.     this.myTempService = ServiceManager.getService("Can", "DS18x20", this.myInitialArguments["DS18x20"]["Id"]);
  38.     /* Add a callback for when the sensor reports a new value */
  39.     this.myTempService.registerEventCallback("newValue", function(args) { self.temperatureUpdate(args); });
  40.     /* Add a callback for when the service goes online */
  41.     this.myTempService.registerEventCallback("online", function(args) { self.tempOnline(); });
  42.     /* If the service is already online we should call the handler here */
  43.     this.tempOnline();
  44.  
  45.     /* Get the LCD service that we want from the ServiceManager, it takes type, service name, service id */
  46.     this.myVoltService = ServiceManager.getService("Can", "BusVoltage", this.myInitialArguments["BusVoltage"]["Id"]);
  47.     /* Add a callback for when the sensor reports a new value */
  48.     this.myVoltService.registerEventCallback("newValue", function(args) { self.voltageUpdate(args); });
  49.     /* Add a callback for when the service goes online */
  50.     this.myVoltService.registerEventCallback("online", function(args) { self.voltOnline(); });
  51.     /* If the service is already online we should call the handler here */
  52.     this.voltOnline();
  53. }
  54.  
  55. /* This method is static */
  56. SensorPrint.timerCallback = function(type, name, id)
  57. {
  58.     /* Get the service instance and call the timer update */
  59.     var service = ServiceManager.getService(type, name, id);
  60.     service.timerUpdate();
  61. }
  62.  
  63. SensorPrint.prototype.lcdOnline = function()
  64. {
  65.     /* If service is not online do nothing */
  66.     if (this.myLCDService.isOnline())
  67.     {
  68.         /* Clear the LCD screen */
  69.         this.myLCDService.clearScreen();
  70.         /* Set backlight to max */
  71.         this.myLCDService.setBacklight(255);
  72.  
  73.         /* If we have no interval timer running start it */
  74.         if (this.myTimeoutId == null)
  75.         {
  76.             /* Start interval timer for our printout. Arguments are expression and time in seconds */
  77.             this.myTimeoutId = setInterval("SensorPrint.timerCallback('Logic', '" + this.myName + "', " + this.myId + ");", 1000);
  78.         }
  79.     }
  80. }
  81.  
  82. SensorPrint.prototype.tempOnline = function()
  83. {
  84.     /* If service is not online do nothing */
  85.     if (this.myTempService.isOnline())
  86.     {
  87.         /* Set report interval to 5 seconds */
  88.         this.myTempService.setReportInterval(5);
  89.     }
  90. }
  91.  
  92. SensorPrint.prototype.voltOnline = function()
  93. {
  94.     /* If service is not online do nothing */
  95.     if (this.myVoltService.isOnline())
  96.     {
  97.         /* Set report interval to 5 seconds */
  98.         this.myVoltService.setReportInterval(5);
  99.     }
  100. }
  101.  
  102. SensorPrint.prototype.temperatureUpdate = function(sensorId)
  103. {
  104.     /* If LCD service is not online do nothing */
  105.     if (this.myLCDService.isOnline())
  106.     {
  107.         /* Format our data to the right format */
  108.         var data = (this.myTempService.getValue(sensorId).toFixed(2).toString() + " C").pad(7, " ", 1);
  109.         /* Print the text to the LCD */
  110.         this.myLCDService.printText(0, 0, data);
  111.         /* Print out what we are doing to the console */
  112.         log(this.myName + ":" + this.myId + "> Trying to print " + data + " value to LCD\n");
  113.     }
  114. }
  115.  
  116. SensorPrint.prototype.voltageUpdate = function(sensorId)
  117. {
  118.     /* If LCD service is not online do nothing */
  119.     if (this.myLCDService.isOnline())
  120.     {
  121.         /* Format our data to the right format */
  122.         var data = (this.myVoltService.getValue(sensorId).toFixed(2).toString() + " V").pad(7, " ", 1);
  123.         /* Print the text to the LCD */
  124.         this.myLCDService.printText(8, 0, data);
  125.         /* Print out what we are doing to the console */
  126.         log(this.myName + ":" + this.myId + "> Trying to print " + data + " value to LCD\n");
  127.     }
  128. }
  129.  
  130. SensorPrint.prototype.timerUpdate = function()
  131. {
  132.     /* If LCD service is not online do nothing */
  133.     if (this.myLCDService.isOnline())
  134.     {
  135.         /* Get the current date time on the format YYYY-mm-dd HH.ii.ss */
  136.         var dateAndTime = formatDateTime();
  137.         /* Print the text to the LCD */
  138.         this.myLCDService.printText(0, 3, dateAndTime);
  139.         /* Print out what we are doing to the console */
  140.         log(this.myName + ":" + this.myId + "> Trying to print " + dateAndTime + " to LCD\n");
  141.     }
  142. }
  143.