Subversion Repositories HomeAutomation

Rev

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

  1.  
  2. CanNodes = new Array();
  3. CanServices = new Array();
  4. CanOfflineTimer = new Interval(offlineCheck, 10000);
  5. CanProgrammingNode = null;
  6.  
  7. function handleNMTMessage(className, commandName, data)
  8. {
  9.     var canMessage = new CanNMTMessage(className, commandName, data);
  10.  
  11.     switch (canMessage.getCommandName())
  12.     {
  13.     case "Reset":
  14.     // Someone sent a reset command... should we do something?
  15.     break;
  16.    
  17.     case "Bios_Start":
  18.     var node = CanNodes[canMessage.getData("HardwareId")];
  19.    
  20.     if (!node)
  21.     {
  22.         log("A can node with hardware id " + uint2hex(canMessage.getData("HardwareId"), 32) + " was not found, starting.\n");
  23.         node = new CanNode(canMessage.getData("HardwareId"), null);
  24.         CanNodes[canMessage.getData("HardwareId")] = node;
  25.     }
  26.    
  27.     node.handleBiosStart(canMessage.getData("BiosVersion"), canMessage.getData("HasApplication"));
  28.     break;
  29.    
  30.     case "Pgm_Start":
  31.     // Someone sent a programming start command... should we do something?
  32.     break;
  33.    
  34.     case "Pgm_Data":
  35.     // Someone sent a programming data command... should we do something?
  36.     break;
  37.    
  38.     case "Pgm_End":
  39.     // Someone sent a programming end command... should we do something?
  40.     break;
  41.    
  42.     case "Pgm_Copy":
  43.     // Someone sent a programming copy command... should we do something?
  44.     break;
  45.    
  46.     case "Pgm_Ack":
  47.     if (CanProgrammingNode)
  48.     {
  49.         CanProgrammingNode.handleAck(canMessage.getData("Data"));
  50.     }
  51.     //else
  52.     //{
  53.         // A node sent pgm ack but we are not in the progress of programming, probably to someone else
  54.     //}
  55.     break;
  56.    
  57.     case "Pgm_Nack":
  58.     // I do not now what this is for...
  59.     break;
  60.    
  61.     case "Start_App":
  62.     // Someone sent a app start command... should we do something?
  63.     break;
  64.    
  65.     case "App_Start":
  66.     var node = CanNodes[canMessage.getData("HardwareId")];
  67.    
  68.     if (!node)
  69.     {
  70.         log("A can node with hardware id " + uint2hex(canMessage.getData("HardwareId"), 32) + " was not found, starting.\n");
  71.         node = new CanNode(canMessage.getData("HardwareId"), null);
  72.         CanNodes[canMessage.getData("HardwareId")] = node;
  73.     }
  74.    
  75.     node.handleHeartbeat();
  76.     break;
  77.    
  78.     case "Heartbeat":
  79.     var node = CanNodes[canMessage.getData("HardwareId")];
  80.    
  81.     if (!node)
  82.     {
  83.         log("A can node with hardware id " + uint2hex(canMessage.getData("HardwareId"), 32) + " was not found, starting.\n");
  84.         node = new CanNode(canMessage.getData("HardwareId"), canMessage.getData("NumberOfModules"));
  85.         CanNodes[canMessage.getData("HardwareId")] = node;
  86.     }
  87.    
  88.     node.handleHeartbeat(canMessage.getData("NumberOfModules"));
  89.     break;
  90.     }
  91. }
  92.  
  93. // This is used to send messages to the can network
  94. function sendNMTMessage(canMessage)
  95. {
  96.     sendCanNMTMessage(  canMessage.getClassName(),
  97.                 canMessage.getCommandName(),
  98.                 canMessage.getDataString());
  99. }
  100.  
  101. // This is called when a message has been received from the can network
  102. function handleMessage(className, directionFlag, moduleName, moduleId, commandName, data)
  103. {
  104.     var canMessage = new CanMessage(className, directionFlag, moduleName, moduleId, commandName, data);
  105.  
  106.     var fullId = canMessage.getModuleName() + canMessage.getModuleId();
  107.  
  108.     if (canMessage.getCommandName() == "List" && canMessage.getDirectionFlag() == "From_Owner")
  109.     {
  110.         if (!CanServices[fullId])
  111.         {
  112.             var service = ServiceManager.getService("Can", canMessage.getModuleName(), canMessage.getModuleId())
  113.  
  114.             if (service != null)
  115.             {
  116.                 var node = CanNodes[canMessage.getData("HardwareId")];
  117.  
  118.                 if (node)
  119.                 {
  120.                     service.setNode(node);
  121.                     node.addService(canMessage.getData("SequenceNumber"), service);
  122.                     CanNodes[canMessage.getData("HardwareId")] = node;
  123.                 }
  124.  
  125.                 CanServices[fullId] = service;
  126.             }
  127.             else
  128.             {
  129.                 log("No such service found: " + canMessage.getModuleName() + "\n");
  130.             }
  131.         }
  132.     }
  133.     else
  134.     {
  135.         if (CanServices[fullId])
  136.         {
  137.             // Tell the service to handle the message, a module service must implement canMessageHandler or derive from CanService-class
  138.             CanServices[fullId].canMessageHandler(canMessage);
  139.         }
  140.         else if (canMessage.getModuleName() == "Debug")
  141.         {
  142.             var service = ServiceManager.getService("Can", canMessage.getModuleName(), canMessage.getModuleId())
  143.             if (service != null)
  144.             {
  145.                 CanServices[fullId] = service;
  146.                 CanServices[fullId].canMessageHandler(canMessage);
  147.             }
  148.         }
  149.     }
  150. }
  151.  
  152. // This is used to send messages to the can network
  153. function sendMessage(canMessage)
  154. {
  155.     sendCanMessage( canMessage.getClassName(),
  156.             canMessage.getDirectionFlag(),
  157.             canMessage.getModuleName(),
  158.             canMessage.getModuleId(),
  159.             canMessage.getCommandName(),
  160.             canMessage.getDataString());
  161. }
  162.  
  163. // This will be called at reqular intervals
  164. function offlineCheck()
  165. {
  166.     for (var n in CanNodes)
  167.     {
  168.         CanNodes[n].checkOffline();
  169.     }
  170. }
  171.  
  172. function setAllOffline()
  173. {
  174.     for (var n in CanNodes)
  175.     {
  176.         CanNodes[n].setOffline();
  177.     }
  178. }
  179.