Subversion Repositories HomeAutomation

Rev

Rev 1952 | Blame | Last modification | View Log | SVN | RSS feed

  1.  
  2. Mbb_Clients = {};
  3.  
  4. Mbb_Susbscribers = [];
  5.  
  6. function Mbb_SubscribeToDeviceStatus(callback)
  7. {
  8.   Mbb_Susbscribers.push(callback);
  9. }
  10.  
  11. function Mbb_Connected(client_id)
  12. {
  13.   Log(client_id + " connected!");
  14.  
  15.   Mbb_Clients[client_id] = {};
  16. }
  17.  
  18. function Mbb_Disconnected(client_id)
  19. {
  20.   Log(client_id + " disconnected!");
  21.  
  22.   for (var n = 0; n < Mbb_Susbscribers.length; n++)
  23.   {
  24.     Mbb_Susbscribers[n]("disconnected", Mbb_Clients[client_id]);
  25.   }
  26.  
  27.   delete Mbb_Clients[client_id];
  28. }
  29.  
  30. function Mbb_ReceivedData(client_id, data)
  31. {
  32.   Log(data);
  33.  
  34.   var json_data = eval("(" + data + ")");
  35.  
  36.   //Log(JSON.stringify(json_data));
  37.  
  38.   if (json_data.method == "MBB.OnPosition")
  39.   {
  40.     if (Mbb_Clients[client_id])
  41.     {
  42.       Mbb_Clients[client_id]["Position"] = json_data.params;
  43.      
  44.       //Log("In params: " + JSON.stringify(json_data.params));
  45.      
  46.       for (var n = 0; n < Mbb_Susbscribers.length; n++)
  47.       {
  48.         Mbb_Susbscribers[n]("position", Mbb_Clients[client_id]);
  49.       }
  50.      
  51.      
  52.      
  53.       //Log("Out nmea:" + JSON.stringify(_Mbb_NmeaToPosition(json_data.params.NmeaRmc, json_data.params.NmeaGga)));
  54.     }
  55.     else
  56.     {
  57.       Log("Got data from unknown tracker!");
  58.     }
  59.   }
  60.   else if (json_data.method == "MBB.OnConnected")
  61.   {
  62.     Log("Client " + client_id + " has IMEI " + json_data.params.Imei);
  63.    
  64.     Mbb_Clients[client_id]["Id"] = client_id;
  65.     Mbb_Clients[client_id]["Info"] = json_data.params;
  66.     Mbb_Clients[client_id]["Position"] = false;
  67.    
  68.     for (var n = 0; n < Mbb_Susbscribers.length; n++)
  69.     {
  70.       Mbb_Susbscribers[n]("connected", Mbb_Clients[client_id]);
  71.     }
  72.   }
  73.   else
  74.   {
  75.     Log("Got unknown data \"" + data + "\" from client " + client_id);
  76.   }
  77. }
  78.  
  79. function Mbb_SendData(client_id, json_data)
  80. {
  81.   return MbbExport_SendData(client_id, JSON.Stringify(json_data));
  82. }
  83.  
  84.  
  85. function _Mbb_NmeaToPosition(rmc, gga)
  86. {
  87.   /*
  88.       GGA          Global Positioning System Fix Data
  89. *     123519       Fix taken at 12:35:19 UTC
  90. *     4807.038,N   Latitude 48 deg 07.038' N
  91. *     01131.000,E  Longitude 11 deg 31.000' E
  92.   1            Fix quality: 0 = invalid
  93.                             1 = GPS fix (SPS)
  94.                             2 = DGPS fix
  95.                             3 = PPS fix
  96.                             4 = Real Time Kinematic
  97.                             5 = Float RTK
  98.                             6 = estimated (dead reckoning) (2.3 feature)
  99.                             7 = Manual input mode
  100.                             8 = Simulation mode
  101. *     08           Number of satellites being tracked
  102. *     0.9          Horizontal dilution of position
  103. *     545.4,M      Altitude, Meters, above mean sea level
  104. *     46.9,M       Height of geoid (mean sea level) above WGS84
  105.                   ellipsoid
  106.   (empty field) time in seconds since last DGPS update
  107.   (empty field) DGPS station ID number
  108.   *47          the checksum data, always begins with *
  109.  
  110.  
  111.   $GPRMC,123519,A,4807.038,N,01131.000,E,022.4,084.4,230394,003.1,W*6A
  112.  
  113.       RMC          Recommended Minimum sentence C
  114. *     123519       Fix taken at 12:35:19 UTC
  115. *     A            Status A=active or V=Void.
  116. *     4807.038,N   Latitude 48 deg 07.038' N
  117. *     01131.000,E  Longitude 11 deg 31.000' E
  118. *     022.4        Speed over the ground in knots
  119. *     084.4        Track angle in degrees True
  120. *     230394       Date - 23rd of March 1994
  121. *     003.1,W      Magnetic Variation
  122.   *6A          The checksum data, always begins with *
  123. */
  124.  
  125.  
  126.   var nmea_rmc_parts = rmc.split(",");
  127.  
  128.   if (nmea_rmc_parts[2] == "A")
  129.   {
  130.     var nmea_gga_parts = gga.split(",");
  131.     var position = {};
  132.  
  133.     position["Datetime"]            = "20" + nmea_rmc_parts[9].substr(4, 2) + "-" + nmea_rmc_parts[9].substr(2, 2) + "-" + nmea_rmc_parts[9].substr(0, 2);
  134.     position["Datetime"]            += " " + nmea_rmc_parts[1].substr(0, 2) + ":" + nmea_rmc_parts[1].substr(2, 2) + ":" + nmea_rmc_parts[1].substr(4, 2) + " UTC";
  135.  
  136.    
  137.     /* Calculate decimal latitude */
  138.     var degrees = parseFloat(nmea_rmc_parts[3].substr(0, 2));
  139.     var decimal = parseFloat(nmea_rmc_parts[3].substr(2));
  140.    
  141.     position["Latitude"] = degrees + (decimal / 60.0);
  142.    
  143.     if (nmea_rmc_parts[4] == "S")
  144.     {
  145.       position["Latitude"] = -position["Latitude"];
  146.     }
  147.  
  148.     /* Calculate decimal longitude */
  149.     var degrees = parseFloat(nmea_rmc_parts[5].substr(0, 3));
  150.     var decimal = parseFloat(nmea_rmc_parts[5].substr(3));
  151.    
  152.     position["Longitude"] = degrees + (decimal / 60.0);
  153.    
  154.     if (nmea_rmc_parts[6] == "W")
  155.     {
  156.       position["Longitude"] = -position["Longitude"];
  157.     }
  158.    
  159.     position["Hdop"] = nmea_gga_parts[8];
  160.    
  161.     position["Satellites"] = nmea_gga_parts[7];
  162.    
  163.     position["Altitude"] = nmea_gga_parts[9]; // Meters
  164.     position["HeightOfGeoid"] = nmea_gga_parts[11] // Meters
  165.    
  166.     position["Speed"] = nmea_rmc_parts[7]; // Knots
  167.     position["Angle"] = nmea_rmc_parts[8];
  168.  
  169.    
  170.     return position;
  171.   }
  172.  
  173.   return false;
  174. }
  175.