Subversion Repositories HomeAutomation

Rev

Rev 1950 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

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