Subversion Repositories HomeAutomation

Rev

Rev 1008 | Rev 1014 | 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.myProgrammingHexAddress = 0;
  9.     this.myIsOnline = false;
  10.    
  11.     this.stopProgramming();
  12. }
  13.  
  14. CanNode.prototype.myIsOnline = null;
  15. CanNode.prototype.myHardwareId = null;
  16. CanNode.prototype.myNumberOfModules = null;
  17. CanNode.prototype.myModules = null;
  18. CanNode.prototype.myHeartbeatTime = null;
  19. CanNode.prototype.myProgrammingIsBios = null;
  20. CanNode.prototype.myProgramming = null;
  21. CanNode.prototype.myProgrammingHex = null;
  22. CanNode.prototype.myProgrammingHexAddress = null;
  23. CanNode.prototype.myProgrammingCallback = null;
  24. CanNode.prototype.myProgrammingWantAck = null;
  25.  
  26. CanNode.prototype.isOnline = function()
  27. {
  28.     return this.myIsOnline;
  29. }
  30.  
  31. CanNode.prototype.start = function()
  32. {
  33.     var canMessage = new CanNMTMessage("nmt", "Start_App");
  34.     canMessage.setData("HardwareId", this.myHardwareId);
  35.     canMessage.send();
  36. }
  37.  
  38. CanNode.prototype.reset = function()
  39. {
  40.     var canMessage = new CanNMTMessage("nmt", "Reset");
  41.     canMessage.setData("HardwareId", this.myHardwareId);
  42.     canMessage.send();
  43. }
  44.  
  45. CanNode.prototype.startApplication = function()
  46. {
  47.     var canMessage = new CanNMTMessage("nmt", "Start_App");
  48.     canMessage.setData("HardwareId", this.myHardwareId);
  49.     canMessage.send();
  50. }
  51.  
  52. CanNode.prototype.handleHeartbeat = function(numberOfModules)
  53. {
  54.     if (numberOfModules)
  55.     {
  56.         this.myNumberOfModules = numberOfModules;
  57.     }
  58.    
  59.     if (this.myNumberOfModules == null || this.myNumberOfModules > this.myModules.length)
  60.     {
  61.         var canMessage = new CanMessage("mnmt", "To_Owner", "", 0, "List");
  62.         canMessage.setData("HardwareId", this.myHardwareId);
  63.         canMessage.send();
  64.     }
  65.    
  66.     this.onlineHandler();
  67. }
  68.  
  69. CanNode.prototype.startProgramming = function(hexData, progressCallback, isBios)
  70. {
  71.     if (isBios)
  72.     {
  73.         this.myProgrammingIsBios = isBios;
  74.     }
  75.     else
  76.     {
  77.         this.myProgrammingIsBios = false;
  78.     }
  79.    
  80.     this.myProgramming = true;
  81.     this.myProgrammingHex = hexData;
  82.     this.myProgrammingCallback = progressCallback;
  83.    
  84.     this.reset();
  85. }
  86.  
  87. CanNode.prototype.handleBiosStart = function(biosVersion, hasApplication)
  88. {
  89. //print("node was reset and bios just started, ver:" + biosVersion);
  90.     if (this.myProgramming)
  91.     {
  92.         var address = this.myProgrammingHex.getAddrLower();
  93.         this.myProgrammingWantAck = address&0xffff;
  94.    
  95.         var canMessage = new CanNMTMessage("nmt", "Pgm_Start");
  96.         canMessage.setData("HardwareId", this.myHardwareId);
  97.         canMessage.setData("AddressLow", address&0xffff);
  98.         canMessage.setData("AddressHigh", (address>16)&0xffff);
  99.         canMessage.send();
  100.        
  101.         this.myProgrammingCallback(true, "START", "Started programming of node");
  102.     }
  103. }
  104.  
  105. CanNode.prototype.handleAck = function(data)
  106. {
  107. this.myProgrammingCallback(true, "DBG", "data: "+data+", myProgrammingWantAck: "+this.myProgrammingWantAck+"\n");
  108.    
  109. //dont do this right now
  110.     if (data == this.myProgrammingWantAck && false)
  111.     {
  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.myProgrammingHexAddress + ":" + 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.myProgrammingHexAddress = 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.