Subversion Repositories HomeAutomation

Rev

Rev 971 | Rev 1001 | 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.myDTMFService = null;
  16. SensorPrint.prototype.myTempService = null;
  17. SensorPrint.prototype.myVoltService = null;
  18. SensorPrint.prototype.myInterval = null;
  19. SensorPrint.prototype.myOnlinePhonebook = null;
  20.  
  21. /* This function must always be declared, this is where all the startup code
  22.    should be placed. Gets called with arguments like what ids to use etc. */
  23. SensorPrint.prototype.initialize = function(initialArguments)
  24. {
  25.     /* We must always call the parent initialize */
  26.     this.Service.prototype.initialize.call(this, initialArguments);
  27.  
  28.     /* This is used for function decalrations like the callbacks below */
  29.     var self = this;
  30.  
  31.     /* Get the LCD service that we want from the ServiceManager, it takes type, service name, service id */
  32.     this.myLCDService = ServiceManager.getService("Can", "HD44789", this.myInitialArguments["HD44789"]["Id"]);
  33.     /* Add a callback for when the service goes online */
  34.     this.myLCDService.registerEventCallback("online", function(args) { self.lcdOnline(); });
  35.     /* If the service is already online we should call the handler here */
  36.     this.lcdOnline();
  37.     /* Add a callback for when the service goes offline */
  38.     this.myLCDService.registerEventCallback("offline", function(args) { self.lcdOffline(); });
  39.  
  40.     /* Get the LCD service that we want from the ServiceManager, it takes type, service name, service id */
  41.     this.myTempService = ServiceManager.getService("Can", "DS18x20", this.myInitialArguments["DS18x20"]["Id"]);
  42.     /* Add a callback for when the sensor reports a new value */
  43.     this.myTempService.registerEventCallback("newValue", function(args) { self.temperatureUpdate(args); });
  44.     /* Add a callback for when the service goes online */
  45.     this.myTempService.registerEventCallback("online", function(args) { self.tempOnline(); });
  46.     /* If the service is already online we should call the handler here */
  47.     this.tempOnline();
  48.  
  49.     /* Get the LCD service that we want from the ServiceManager, it takes type, service name, service id */
  50.     this.myVoltService = ServiceManager.getService("Can", "BusVoltage", this.myInitialArguments["BusVoltage"]["Id"]);
  51.     /* Add a callback for when the sensor reports a new value */
  52.     this.myVoltService.registerEventCallback("newValue", function(args) { self.voltageUpdate(args); });
  53.     /* Add a callback for when the service goes online */
  54.     this.myVoltService.registerEventCallback("online", function(args) { self.voltOnline(); });
  55.     /* If the service is already online we should call the handler here */
  56.     this.voltOnline();
  57.    
  58.    
  59.     this.myDTMFService = ServiceManager.getService("Can", "SimpleDTMF", this.myInitialArguments["SimpleDTMF"]["Id"]);
  60.     this.myDTMFService.registerEventCallback("newPhonenumber", function(args) { self.dtmfUpdate(args); });
  61.     //this.myDTMFService.registerEventCallback("online", function(args) { self.dtmfOnline(); });
  62.     this.dtmfOnline();
  63.  
  64.     this.myOnlinePhonebook = new OnlinePhonebook(function(phonenumber, persons) { self.phonebookLookupCallback(phonenumber, persons); });
  65. }
  66.  
  67. SensorPrint.prototype.dtmfOnline = function()
  68. {
  69. }
  70.  
  71. SensorPrint.prototype.dtmfUpdate = function(args)
  72. {
  73.     this.myOnlinePhonebook.lookup(this.myDTMFService.getLastPhonenumber());
  74. }
  75.  
  76. SensorPrint.prototype.phonebookLookupCallback = function(phonenumber, persons)
  77. {
  78.     if (this.myLCDService.isOnline())
  79.     {
  80.         var personString = "";
  81.    
  82.         for (var n = 0; n < persons.length; n++)
  83.         {
  84.             this.myLCDService.printText(0, n+1, persons[n].pad(18, ' ', 1));
  85.             /* Print out what we are doing to the console */
  86.             log(this.myName + ":" + this.myId + "> Trying to print " + persons[n] + " value to LCD\n");
  87.         }
  88.        
  89.         for (var c = n+1; c < 4; c++)
  90.         {
  91.             var str = "                  ";
  92.             this.myLCDService.printText(0, c, str);
  93.         }
  94.     }
  95. }
  96.  
  97. SensorPrint.prototype.lcdOnline = function()
  98. {
  99.     /* If service is not online do nothing */
  100.     if (this.myLCDService.isOnline())
  101.     {
  102.         /* Clear the LCD screen */
  103.         this.myLCDService.clearScreen();
  104.         /* Set backlight to max */
  105.         this.myLCDService.setBacklight(255);
  106.  
  107.         /* If we have no interval timer running start it */
  108.         if (this.myInterval == null)
  109.         {
  110.             var self = this;
  111.        
  112.             /* Start interval timer for our printout. Arguments are the callback function and time in seconds */
  113.             this.myInterval = new Interval(function() { self.timerUpdate() }, 1000);
  114.         }
  115.        
  116.         this.myInterval.start();
  117.     }
  118. }
  119.  
  120. SensorPrint.prototype.lcdOffline = function()
  121. {
  122.     /* If we have no interval timer running do nothing */
  123.     if (this.myInterval != null)
  124.     {
  125.         /* Stop the interval */
  126.         this.myInterval.stop();
  127.     }
  128. }
  129.  
  130. SensorPrint.prototype.tempOnline = function()
  131. {
  132.     /* If service is not online do nothing */
  133.     if (this.myTempService.isOnline())
  134.     {
  135.         /* Set report interval to 5 seconds */
  136.         this.myTempService.setReportInterval(5);
  137.     }
  138. }
  139.  
  140. SensorPrint.prototype.voltOnline = function()
  141. {
  142.     /* If service is not online do nothing */
  143.     if (this.myVoltService.isOnline())
  144.     {
  145.         /* Set report interval to 5 seconds */
  146.         this.myVoltService.setReportInterval(5);
  147.     }
  148. }
  149.  
  150. SensorPrint.prototype.temperatureUpdate = function(sensorId)
  151. {
  152.     /* If LCD service is not online do nothing */
  153.     if (this.myLCDService.isOnline())
  154.     {
  155.         /* Format our data to the right format */
  156.         var data = (this.myTempService.getValue(sensorId).toFixed(2).toString() + " C").pad(7, " ", 1);
  157.         /* Print the text to the LCD */
  158.         this.myLCDService.printText(0, 0, data);
  159.         /* Print out what we are doing to the console */
  160.         log(this.myName + ":" + this.myId + "> Trying to print " + data + " value to LCD\n");
  161.     }
  162. }
  163.  
  164. SensorPrint.prototype.voltageUpdate = function(sensorId)
  165. {
  166.     /* If LCD service is not online do nothing */
  167.     if (this.myLCDService.isOnline())
  168.     {
  169.         /* Format our data to the right format */
  170.         var data = (this.myVoltService.getValue(sensorId).toFixed(2).toString() + " V").pad(7, " ", 1);
  171.         /* Print the text to the LCD */
  172.         this.myLCDService.printText(8, 0, data);
  173.         /* Print out what we are doing to the console */
  174.         log(this.myName + ":" + this.myId + "> Trying to print " + data + " value to LCD\n");
  175.     }
  176. }
  177.  
  178. SensorPrint.prototype.timerUpdate = function()
  179. {
  180.     /* If LCD service is not online do nothing */
  181.     if (this.myLCDService.isOnline())
  182.     {
  183.         var date = new Date();
  184.         /* Get the current date time on the format YYYY-mm-dd HH.ii.ss */
  185.         var dateAndTime = date.getDateTimeFormated();
  186.         /* Print the text to the LCD */
  187.         this.myLCDService.printText(0, 3, dateAndTime);
  188.         /* Print out what we are doing to the console */
  189.         log(this.myName + ":" + this.myId + "> Trying to print " + dateAndTime + " to LCD\n");
  190.     }
  191. }
  192.