Subversion Repositories HomeAutomation

Rev

Rev 1013 | Rev 1022 | 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.     CanProgrammingNode = this;
  85.    
  86.     this.reset();
  87. }
  88.  
  89. CanNode.prototype.handleBiosStart = function(biosVersion, hasApplication)
  90. {
  91. //print("node was reset and bios just started, ver:" + biosVersion);
  92.     if (this.myProgramming)
  93.     {
  94.         var address = this.myProgrammingHex.getAddrLower();
  95.         this.myProgrammingWantAck = address&0xffff;
  96.    
  97.         var canMessage = new CanNMTMessage("nmt", "Pgm_Start");
  98.         canMessage.setData("HardwareId", this.myHardwareId);
  99.         canMessage.setData("AddressLow", address&0xffff);
  100.         canMessage.setData("AddressHigh", (address>16)&0xffff);
  101.         canMessage.send();
  102.        
  103.         this.myProgrammingCallback(true, "START", "Started programming of node");
  104.     }
  105. }
  106.  
  107. CanNode.prototype.handleAck = function(data)
  108. {
  109. this.myProgrammingCallback(true, "DBG", "data: "+data+", myProgrammingWantAck: "+this.myProgrammingWantAck+"\n");
  110.    
  111. //dont do this right now
  112.     if (data == this.myProgrammingWantAck && false)
  113.     {
  114.        
  115.         var type = ""; ///TODO: Fill in which type of data we have
  116.        
  117.         switch (type)
  118.         {
  119.         case "DATA":///TODO: Maybe it is called something else, maybe a number
  120.             var offset = ""; ///TODO: Fill in something here
  121.             var data = ""; ///TODO: Fill in something here
  122.        
  123.             this.myProgrammingWantAck = "";  ///TODO: Fill in something here
  124.        
  125.             var canMessage = new CanNMTMessage("nmt", "Pgm_Data");
  126.             canMessage.setData("Offset", offset);
  127.             canMessage.setData("Data", data);
  128.             canMessage.send();
  129.            
  130.             this.myProgrammingCallback(true, "PROGRESS", this.myProgrammingHexAddress + ":" + this.myProgrammingHex.length);
  131.             break;
  132.        
  133.         case "END":///TODO: Maybe it is called something else, maybe a number
  134.             this.myProgrammingWantAck = "";  ///TODO: Fill in something here
  135.        
  136.             var canMessage = new CanNMTMessage("nmt", "Pgm_End");
  137.             canMessage.send();
  138.            
  139.             this.myProgrammingCallback(true, "END", ""); ///TODO: Fill in something meaningfull text
  140.             break;
  141.        
  142.         case "COPY":///TODO: Maybe it is called something else, maybe a number
  143.             var source = ""; ///TODO: Fill in something here
  144.             var destination = ""; ///TODO: Fill in something here
  145.             var length = ""; ///TODO: Fill in something here
  146.            
  147.             this.myProgrammingWantAck = "";  ///TODO: Fill in something here
  148.        
  149.             var canMessage = new CanNMTMessage("nmt", "Pgm_Copy");
  150.             canMessage.setData("Source", source);
  151.             canMessage.setData("Destination", destination);
  152.             canMessage.setData("Length", length);
  153.             canMessage.send();
  154.            
  155.             this.myProgrammingCallback(true, "END", ""); ///TODO: Fill in something meaningfull text
  156.             break;
  157.            
  158.         default:
  159.         ///TODO: Are we done here? I have no idea :)
  160.         ///TODO: But we should call this.stopProgramming(true, "All done"); we are done, maybe restart node?
  161.         }
  162.     }
  163.     else
  164.     {
  165.         ///TODO: We got something that was not correct... we should abort
  166.         stopProgramming(false, ""); ///TODO: Fill in some kind of error text
  167.     }
  168. }
  169.  
  170. CanNode.prototype.stopProgramming = function(status, text)
  171. {
  172.     if (status && text)
  173.     {
  174.         this.myProgrammingCallback(status, "STOP", text);
  175.     }
  176.    
  177.     this.myProgrammingIsBios = false;
  178.     this.myProgramming = false;
  179.     this.myProgrammingHex = new Array();
  180.     this.myProgrammingHexAddress = 0;
  181.     this.myProgrammingCallback = function() {};
  182.     this.myProgrammingWantAck = null;
  183.    
  184.     CanProgrammingNode = null;
  185. }
  186.  
  187.  
  188.  
  189. CanNode.prototype.addService = function(sequenceNumber, service)
  190. {
  191.     this.myModules[sequenceNumber] = service;
  192.     service.setOnline();
  193. }
  194.  
  195. CanNode.prototype.onlineHandler = function()
  196. {
  197.     var date = new Date();
  198.     this.myHeartbeatTime = date.getTimestamp();
  199.  
  200.     if (!this.myIsOnline)
  201.     {
  202.         this.myIsOnline = true;
  203.  
  204.         log(uint2hex(this.myHardwareId, 32) + "> Node went online\n");
  205.        
  206.         for (var n in this.myModules)
  207.         {
  208.             this.myModules[n].setOnline();
  209.         }
  210.     }
  211. }
  212.  
  213. CanNode.prototype.checkOffline = function()
  214. {
  215.     if (this.myIsOnline)
  216.     {
  217.         var date = new Date();
  218.         if (this.myHeartbeatTime + 10 < date.getTimestamp())
  219.         {
  220.             this.setOffline();
  221.         }
  222.     }
  223. }
  224.  
  225. CanNode.prototype.setOffline = function()
  226. {
  227.     if (this.myIsOnline)
  228.     {
  229.         this.myIsOnline = false;
  230.  
  231.         log(uint2hex(this.myHardwareId, 32) + "> Node went offline\n");
  232.        
  233.         for (var n in this.myModules)
  234.         {
  235.             this.myModules[n].setOffline();
  236.         }
  237.     }
  238. }
  239.