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