Subversion Repositories HomeAutomation

Rev

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

  1.  
  2. function CanNode(hardwareId, numberOfModules)
  3. {
  4.     this.myHardwareId = hardwareId;
  5.     this.myNumberOfModules = numberOfModules;
  6.     this.myModules = new Array();
  7.     this.myHeartbeatTime = 0;
  8.     this.myIsOnline = false;
  9. }
  10.  
  11. CanNode.prototype.myIsOnline = null;
  12. CanNode.prototype.myHardwareId = null;
  13. CanNode.prototype.myNumberOfModules = null;
  14. CanNode.prototype.myModules = null;
  15. CanNode.prototype.myHeartbeatTime = null;
  16.  
  17. CanNode.prototype.isOnline = function()
  18. {
  19.     return this.myIsOnline;
  20. }
  21.  
  22. CanNode.prototype.reset = function()
  23. {
  24.     var canMessage = new CanNMTMessage("nmt", "Reset");
  25.     canMessage.setData("HardwareId", this.myHardwareId);
  26.     sendNMTMessage(canMessage);
  27. }
  28.  
  29. CanNode.prototype.startApplication = function()
  30. {
  31.     var canMessage = new CanNMTMessage("nmt", "Start_App");
  32.     canMessage.setData("HardwareId", this.myHardwareId);
  33.     sendNMTMessage(canMessage);
  34. }
  35.  
  36. CanNode.prototype.handleHeartbeat = function(numberOfModules)
  37. {
  38.     this.myNumberOfModules = numberOfModules;
  39.  
  40.     if (this.myNumberOfModules > this.myModules.length)
  41.     {
  42.         var canMessage = new CanMessage("mnmt", "To_Owner", "", 0, "List");
  43.         canMessage.setData("HardwareId", this.myHardwareId);
  44.         sendMessage(canMessage);
  45.     }
  46.    
  47.     this.onlineHandler();
  48. }
  49.  
  50. CanNode.prototype.addService = function(sequenceNumber, service)
  51. {
  52.     this.myModules[sequenceNumber] = service;
  53.     service.setOnline();
  54. }
  55.  
  56. CanNode.prototype.onlineHandler = function()
  57. {
  58.     var date = new Date();
  59.     this.myHeartbeatTime = date.getTimestamp();
  60.  
  61.     if (!this.myIsOnline)
  62.     {
  63.         this.myIsOnline = true;
  64.  
  65.         log(uint2hex(this.myHardwareId, 32) + "> Node went online\n");
  66.        
  67.         for (var n in this.myModules)
  68.         {
  69.             this.myModules[n].setOnline();
  70.         }
  71.     }
  72. }
  73.  
  74. CanNode.prototype.checkOffline = function()
  75. {
  76.     if (this.myIsOnline)
  77.     {
  78.         var date = new Date();
  79.         if (this.myHeartbeatTime + 10 < date.getTimestamp())
  80.         {
  81.             this.setOffline();
  82.         }
  83.     }
  84. }
  85.  
  86. CanNode.prototype.setOffline = function()
  87. {
  88.     if (this.myIsOnline)
  89.     {
  90.         this.myIsOnline = false;
  91.  
  92.         log(uint2hex(this.myHardwareId, 32) + "> Node went offline\n");
  93.        
  94.         for (var n in this.myModules)
  95.         {
  96.             this.myModules[n].setOffline();
  97.         }
  98.     }
  99. }
  100.