Subversion Repositories HomeAutomation

Rev

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

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