Subversion Repositories HomeAutomation

Rev

Details | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
1947 runge 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
{
1949 runge 32
  Log(data);
33
 
1947 runge 34
  var json_data = eval("(" + data + ")");
35
 
36
  //Log(JSON.stringify(json_data));
37
 
1956 runge 38
  if (json_data.method == "MBB.OnPosition")
1947 runge 39
  {
1952 runge 40
    if (Mbb_Clients[client_id])
1947 runge 41
    {
1956 runge 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++)
1947 runge 47
      {
1956 runge 48
        Mbb_Susbscribers[n]("position", Mbb_Clients[client_id]);
1947 runge 49
      }
1956 runge 50
 
51
 
52
 
53
      //Log("Out nmea:" + JSON.stringify(_Mbb_NmeaToPosition(json_data.params.NmeaRmc, json_data.params.NmeaGga)));
1947 runge 54
    }
1952 runge 55
    else
56
    {
57
      Log("Got data from unknown tracker!");
58
    }
1947 runge 59
  }
1952 runge 60
  else if (json_data.method == "MBB.OnConnected")
1947 runge 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;
1949 runge 66
    Mbb_Clients[client_id]["Position"] = false;
1947 runge 67
 
68
    for (var n = 0; n < Mbb_Susbscribers.length; n++)
69
    {
70
      Mbb_Susbscribers[n]("connected", Mbb_Clients[client_id]);
71
    }
72
  }
1952 runge 73
  else
1947 runge 74
  {
1952 runge 75
    Log("Got unknown data \"" + data + "\" from client " + client_id);
76
  }
1947 runge 77
}
78
 
79
function Mbb_SendData(client_id, json_data)
80
{
81
  return MbbExport_SendData(client_id, JSON.Stringify(json_data));
82
}
1949 runge 83
 
1956 runge 84
 
85
function _Mbb_NmeaToPosition(rmc, gga)
1949 runge 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 *
1956 runge 109
 
110
 
1949 runge 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
*/
1956 runge 124
 
125
 
126
  var nmea_rmc_parts = rmc.split(",");
127
 
1949 runge 128
  if (nmea_rmc_parts[2] == "A")
129
  {
1956 runge 130
    var nmea_gga_parts = gga.split(",");
1949 runge 131
    var position = {};
1956 runge 132
 
1949 runge 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";
1956 runge 135
 
136
 
1950 runge 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));
1956 runge 140
 
1950 runge 141
    position["Latitude"] = degrees + (decimal / 60.0);
1956 runge 142
 
1950 runge 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));
1956 runge 151
 
1950 runge 152
    position["Longitude"] = degrees + (decimal / 60.0);
1956 runge 153
 
1950 runge 154
    if (nmea_rmc_parts[6] == "W")
155
    {
156
      position["Longitude"] = -position["Longitude"];
157
    }
1956 runge 158
 
1950 runge 159
    position["Hdop"] = nmea_gga_parts[8];
1956 runge 160
 
1950 runge 161
    position["Satellites"] = nmea_gga_parts[7];
1956 runge 162
 
1950 runge 163
    position["Altitude"] = nmea_gga_parts[9]; // Meters
164
    position["HeightOfGeoid"] = nmea_gga_parts[11] // Meters
1956 runge 165
 
1950 runge 166
    position["Speed"] = nmea_rmc_parts[7]; // Knots
167
    position["Angle"] = nmea_rmc_parts[8];
1956 runge 168
 
169
 
1949 runge 170
    return position;
171
  }
1956 runge 172
 
1949 runge 173
  return false;
174
}
1956 runge 175