Subversion Repositories HomeAutomation

Rev

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