Subversion Repositories HomeAutomation

Rev

Rev 1001 | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

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