Subversion Repositories HomeAutomation

Rev

Rev 986 | 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.     this.stopProgramming();
  11. }
  12.  
  13. CanNode.prototype.myIsOnline = null;
  14. CanNode.prototype.myHardwareId = null;
  15. CanNode.prototype.myNumberOfModules = null;
  16. CanNode.prototype.myModules = null;
  17. CanNode.prototype.myHeartbeatTime = null;
  18. CanNode.prototype.myProgrammingIsBios = null;
  19. CanNode.prototype.myProgramming = null;
  20. CanNode.prototype.myProgrammingHex = null;
  21. CanNode.prototype.myProgrammingHexIndex = null;
  22. CanNode.prototype.myProgrammingCallback = null;
  23. CanNode.prototype.myProgrammingWantAck = null;
  24.  
  25. CanNode.prototype.isOnline = function()
  26. {
  27.     return this.myIsOnline;
  28. }
  29.  
  30. CanNode.prototype.start = function()
  31. {
  32.     var canMessage = new CanNMTMessage("nmt", "Start_App");
  33.     canMessage.setData("HardwareId", this.myHardwareId);
  34.     canMessage.send();
  35. }
  36.  
  37. CanNode.prototype.reset = function()
  38. {
  39.     var canMessage = new CanNMTMessage("nmt", "Reset");
  40.     canMessage.setData("HardwareId", this.myHardwareId);
  41.     canMessage.send();
  42. }
  43.  
  44. CanNode.prototype.startApplication = function()
  45. {
  46.     var canMessage = new CanNMTMessage("nmt", "Start_App");
  47.     canMessage.setData("HardwareId", this.myHardwareId);
  48.     canMessage.send();
  49. }
  50.  
  51. CanNode.prototype.handleHeartbeat = function(numberOfModules)
  52. {
  53.     if (numberOfModules)
  54.     {
  55.         this.myNumberOfModules = numberOfModules;
  56.     }
  57.    
  58.     if (this.myNumberOfModules == null || this.myNumberOfModules > this.myModules.length)
  59.     {
  60.         var canMessage = new CanMessage("mnmt", "To_Owner", "", 0, "List");
  61.         canMessage.setData("HardwareId", this.myHardwareId);
  62.         canMessage.send();
  63.     }
  64.    
  65.     this.onlineHandler();
  66. }
  67.  
  68. CanNode.prototype.startProgramming = function(hexData, progressCallback, isBios)
  69. {
  70.     if (isBios)
  71.     {
  72.         this.myProgrammingIsBios = bios;
  73.     }
  74.     else
  75.     {
  76.         this.myProgrammingIsBios = false;
  77.     }
  78.    
  79.     this.myProgramming = true;
  80.     this.myProgrammingHex = hexData;
  81.     this.myProgrammingCallback = progressCallback;
  82.    
  83.     this.reset();
  84. }
  85.  
  86. CanNode.prototype.handleBiosStart = function(biosVersion, hasApplication)
  87. {
  88.     if (this.myProgramming)
  89.     {
  90.         var hexLine = this.myProgrammingHex[this.myProgrammingHexIndex];
  91.         this.myProgrammingHexIndex++;
  92.    
  93.         var address = ""; ///TODO: Fill in something here
  94.    
  95.         this.myProgrammingWantAck = "";  ///TODO: Fill in the 16 lower bits of address
  96.    
  97.         var canMessage = new CanNMTMessage("nmt", "Pgm_Start");
  98.         canMessage.setData("HardwareId", this.myHardwareId);
  99.         canMessage.setData("Address", address);
  100.         canMessage.send();
  101.        
  102.         this.myProgrammingCallback(true, "START", "Started programming of node");
  103.     }
  104. }
  105.  
  106. CanNode.prototype.handleAck = function(data)
  107. {
  108.     if (data == this.myProgrammingWantAck)
  109.     {
  110.         var hexLine = this.myProgrammingHex[this.myProgrammingHexIndex];
  111.         this.myProgrammingHexIndex++;
  112.        
  113.         var type = ""; ///TODO: Fill in which type of data we have
  114.        
  115.         switch (type)
  116.         {
  117.         case "DATA":///TODO: Maybe it is called something else, maybe a number
  118.             var offset = ""; ///TODO: Fill in something here
  119.             var data = ""; ///TODO: Fill in something here
  120.        
  121.             this.myProgrammingWantAck = "";  ///TODO: Fill in something here
  122.        
  123.             var canMessage = new CanNMTMessage("nmt", "Pgm_Data");
  124.             canMessage.setData("Offset", offset);
  125.             canMessage.setData("Data", data);
  126.             canMessage.send();
  127.            
  128.             this.myProgrammingCallback(true, "PROGRESS", this.myProgrammingHexIndex + ":" + this.myProgrammingHex.length);
  129.             break;
  130.        
  131.         case "END":///TODO: Maybe it is called something else, maybe a number
  132.             this.myProgrammingWantAck = "";  ///TODO: Fill in something here
  133.        
  134.             var canMessage = new CanNMTMessage("nmt", "Pgm_End");
  135.             canMessage.send();
  136.            
  137.             this.myProgrammingCallback(true, "END", ""); ///TODO: Fill in something meaningfull text
  138.             break;
  139.        
  140.         case "COPY":///TODO: Maybe it is called something else, maybe a number
  141.             var source = ""; ///TODO: Fill in something here
  142.             var destination = ""; ///TODO: Fill in something here
  143.             var length = ""; ///TODO: Fill in something here
  144.            
  145.             this.myProgrammingWantAck = "";  ///TODO: Fill in something here
  146.        
  147.             var canMessage = new CanNMTMessage("nmt", "Pgm_Copy");
  148.             canMessage.setData("Source", source);
  149.             canMessage.setData("Destination", destination);
  150.             canMessage.setData("Length", length);
  151.             canMessage.send();
  152.            
  153.             this.myProgrammingCallback(true, "END", ""); ///TODO: Fill in something meaningfull text
  154.             break;
  155.            
  156.         default:
  157.         ///TODO: Are we done here? I have no idea :)
  158.         ///TODO: But we should call this.stopProgramming(true, "All done"); we are done, maybe restart node?
  159.         }
  160.     }
  161.     else
  162.     {
  163.         ///TODO: We got something that was not correct... we should abort
  164.         stopProgramming(false, ""); ///TODO: Fill in some kind of error text
  165.     }
  166. }
  167.  
  168. CanNode.prototype.stopProgramming = function(status, text)
  169. {
  170.     if (status && text)
  171.     {
  172.         this.myProgrammingCallback(status, "STOP", text);
  173.     }
  174.    
  175.     this.myProgrammingIsBios = false;
  176.     this.myProgramming = false;
  177.     this.myProgrammingHex = new Array();
  178.     this.myProgrammingHexIndex = 0;
  179.     this.myProgrammingCallback = function() {};
  180.     this.myProgrammingWantAck = null;
  181. }
  182.  
  183.  
  184.  
  185. CanNode.prototype.addService = function(sequenceNumber, service)
  186. {
  187.     this.myModules[sequenceNumber] = service;
  188.     service.setOnline();
  189. }
  190.  
  191. CanNode.prototype.onlineHandler = function()
  192. {
  193.     var date = new Date();
  194.     this.myHeartbeatTime = date.getTimestamp();
  195.  
  196.     if (!this.myIsOnline)
  197.     {
  198.         this.myIsOnline = true;
  199.  
  200.         log(uint2hex(this.myHardwareId, 32) + "> Node went online\n");
  201.        
  202.         for (var n in this.myModules)
  203.         {
  204.             this.myModules[n].setOnline();
  205.         }
  206.     }
  207. }
  208.  
  209. CanNode.prototype.checkOffline = function()
  210. {
  211.     if (this.myIsOnline)
  212.     {
  213.         var date = new Date();
  214.         if (this.myHeartbeatTime + 10 < date.getTimestamp())
  215.         {
  216.             this.setOffline();
  217.         }
  218.     }
  219. }
  220.  
  221. CanNode.prototype.setOffline = function()
  222. {
  223.     if (this.myIsOnline)
  224.     {
  225.         this.myIsOnline = false;
  226.  
  227.         log(uint2hex(this.myHardwareId, 32) + "> Node went offline\n");
  228.        
  229.         for (var n in this.myModules)
  230.         {
  231.             this.myModules[n].setOffline();
  232.         }
  233.     }
  234. }
  235.