Skip to content

Commit 1000ce2

Browse files
SPLICE it! Order of position no longer matters
Out of order positions, will be spliced in. Path will update properly. Graph will also update (check TODO). Altitude profile will not work correctly. Wrose case is when the packets are in reverse order. (check TODO) TODO: - add logic to manipualte the null entries when splicing - make the altitude profile work in all cases
1 parent 5f8ec9a commit 1000ce2

File tree

1 file changed

+85
-34
lines changed

1 file changed

+85
-34
lines changed

js/tracker.js

Lines changed: 85 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,6 +1080,7 @@ function addPosition(position) {
10801080
subhorizon_circle: subhorizon_circle,
10811081
num_positions: 0,
10821082
positions: [],
1083+
positions_ts: [],
10831084
path_length: 0,
10841085
curr_position: position,
10851086
line: [],
@@ -1176,16 +1177,16 @@ function addPosition(position) {
11761177

11771178
if(vehicle.vehicle_type == "balloon") {
11781179
var new_latlng = new google.maps.LatLng(position.gps_lat, position.gps_lon);
1179-
var dt = (convert_time(position.gps_time) - convert_time(vehicle.curr_position.gps_time)) / 1000; // convert to seconds
1180+
var new_ts = convert_time(position.gps_time);
1181+
var curr_ts = convert_time(vehicle.curr_position.gps_time);
1182+
var dt = (new_ts - curr_ts) / 1000; // convert to seconds
11801183

1181-
if(dt == 0) {
1184+
if(dt == 0 && vehicle.num_positions) {
11821185
if (("," + vehicle.curr_position.callsign + ",").indexOf("," + position.callsign + ",") === -1) {
11831186
vehicle.curr_position.callsign += "," + position.callsign;
11841187
}
1185-
1186-
vehicle.updated = true;
11871188
}
1188-
else if(dt > 0) {
1189+
else if(dt >= 0) {
11891190
if(vehicle.num_positions > 0) {
11901191
// calculate vertical rate
11911192
var rate = (position.gps_alt - vehicle.curr_position.gps_alt) / dt;
@@ -1199,8 +1200,8 @@ function addPosition(position) {
11991200

12001201
// record altitude values for the drowing a mini profile
12011202
// only record altitude values in 2minute interval
1202-
if(!embed.lastestonly && convert_time(vehicle.curr_position.gps_time) - vehicle.time_last_alt >= 120000) { // 120s = 2minutes
1203-
vehicle.time_last_alt = convert_time(vehicle.curr_position.gps_time);
1203+
if(!embed.lastestonly && curr_ts - vehicle.time_last_alt >= 120000) { // 120s = 2minutes
1204+
vehicle.time_last_alt = curr_ts;
12041205
var alt = parseInt(vehicle.curr_position.gps_alt);
12051206

12061207
if(alt > vehicle.alt_max) vehicle.alt_max = alt; // larged value in the set is required for encoding later
@@ -1209,25 +1210,57 @@ function addPosition(position) {
12091210
}
12101211

12111212
// add the new position
1212-
if(!vehicle.curr_position
1213-
|| vehicle.curr_position.gps_lat != position.gps_lat
1214-
|| vehicle.curr_position.gps_lon != position.gps_lon) {
1215-
// add the new position
1216-
if(embed.latestonly) {
1217-
vehicle.num_positions= 1;
1218-
} else {
1219-
vehicle.positions.push(new_latlng);
1220-
vehicle.num_positions++;
1213+
if(embed.latestonly) {
1214+
vehicle.num_positions= 1;
1215+
} else {
1216+
vehicle.positions.push(new_latlng);
1217+
vehicle.positions_ts.push(new_ts);
1218+
vehicle.num_positions++;
1219+
}
1220+
1221+
// increment length
1222+
var poslen = vehicle.num_positions;
1223+
if(poslen > 1) vehicle.path_length += google.maps.geometry.spherical.computeDistanceBetween(vehicle.positions[poslen-2], vehicle.positions[poslen-1]);
1224+
1225+
vehicle.curr_position = position;
1226+
graphAddPosition(vehicle_index, vehicle.curr_position);
1227+
1228+
}
1229+
else if(vehicle.positions_ts.indexOf(new_ts) == -1) { // backlog packets, need to splice them into the array
1230+
// TODO:
1231+
// 1. altitude profile, will bug out if packets are in reverse order for example, fix or redo it
1232+
// 2. check TODO on graphAddPosition()
1233+
1234+
// find out the index at which we should insert the new point
1235+
var xref = vehicle.positions_ts;
1236+
var idx = -1, len = xref.length;
1237+
while(++idx < len) {
1238+
if(xref[idx] > new_ts) {
1239+
break;
12211240
}
1241+
}
12221242

1223-
vehicle.curr_position = position;
1224-
graphAddLastPosition(vehicle_index);
1225-
vehicle.updated = true;
1243+
// recalculate the distance in the section where we insert
1244+
if(idx == 0) {
1245+
vehicle.path_length += google.maps.geometry.spherical.computeDistanceBetween(vehicle.positions[0], new_latlng);
1246+
} else {
1247+
// subtracked the distance between the two points where we gonna insert the new one
1248+
vehicle.path_length -= google.maps.geometry.spherical.computeDistanceBetween(vehicle.positions[idx-1], vehicle.positions[idx]);
12261249

1227-
var poslen = vehicle.num_positions;
1228-
if(poslen > 1) vehicle.path_length += google.maps.geometry.spherical.computeDistanceBetween(vehicle.positions[poslen-2], vehicle.positions[poslen-1]);
1250+
// calculate the distance with the new point in place
1251+
vehicle.path_length += google.maps.geometry.spherical.computeDistanceBetween(vehicle.positions[idx-1], new_latlng);
1252+
vehicle.path_length += google.maps.geometry.spherical.computeDistanceBetween(vehicle.positions[idx], new_latlng);
12291253
}
1254+
1255+
// insert the new position into our arrays
1256+
vehicle.positions.splice(idx, 0, new_latlng);
1257+
vehicle.positions_ts.splice(idx, 0, new_ts);
1258+
vehicle.num_positions++;
1259+
1260+
graphAddPosition(vehicle_index, position);
12301261
}
1262+
1263+
vehicle.updated = true;
12311264
} else { // if car
12321265
vehicle.updated = true;
12331266
vehicle.curr_position = position;
@@ -1266,15 +1299,32 @@ function updateGraph(idx, reset_selection) {
12661299
vehicles[idx].graph_data_updated = false;
12671300
}
12681301

1269-
function graphAddLastPosition(idx) {
1302+
function graphAddPosition(idx, new_data) {
12701303
if(!plot) return;
12711304

12721305
vehicles[idx].graph_data_updated = true;
1306+
12731307
var data = vehicles[idx].graph_data;
1274-
var new_data = vehicles[idx].curr_position;
12751308
var ts = convert_time(new_data.gps_time);
1309+
var splice_idx = 0;
1310+
var splice = false;
1311+
1312+
if(data.length && data[0].data.length) {
1313+
if(data[0].data[data[0].data.length - 1][0] > ts) splice = true;
1314+
}
12761315

1277-
if(vehicles[idx].graph_data.length) {
1316+
if(splice) {
1317+
// adjust splice_idx to account for null entries
1318+
var xref = data[0].data;
1319+
var i = xref.length - 1;
1320+
for(; i >= 0; i--) {
1321+
if(ts > xref[i][0]) break;
1322+
}
1323+
splice_idx = i+1;
1324+
1325+
//TODO: correct/adjust null entries
1326+
}
1327+
else if(vehicles[idx].graph_data.length) {
12781328
var ts_last_idx = data[0].data.length - 1;
12791329
var ts_last = data[0].data[ts_last_idx][0];
12801330

@@ -1308,7 +1358,12 @@ function graphAddLastPosition(idx) {
13081358
}
13091359

13101360
// push latest altitude
1311-
data[0].data.push([ts, parseInt(new_data.gps_alt)]);
1361+
if(splice) {
1362+
data[0].data.splice(splice_idx, 0, [ts, parseInt(new_data.gps_alt)]);
1363+
} else {
1364+
data[0].data.push([ts, parseInt(new_data.gps_alt)]);
1365+
}
1366+
13121367
if(parseInt(new_data.gps_alt) < 0) delete vehicles[idx].graph_yaxes[i].min;
13131368
i++;
13141369

@@ -1334,7 +1389,11 @@ function graphAddLastPosition(idx) {
13341389

13351390
if(k != data[i].key) return;
13361391

1337-
data[i].data.push([ts, parseFloat(v)]);
1392+
if(splice) {
1393+
data[i].data.splice(splice_idx,0, [ts, parseFloat(v)]);
1394+
} else {
1395+
data[i].data.push([ts, parseFloat(v)]);
1396+
}
13381397
if(parseFloat(v) < 0) delete vehicles[idx].graph_yaxes[i].min;
13391398

13401399
i++;
@@ -1598,7 +1657,6 @@ function update(response) {
15981657
lastPPointer: lastPositions.positions.position,
15991658
idx: 0,
16001659
max: response.positions.position.length,
1601-
last_vehicle: null,
16021660
step: function(ctx) {
16031661
var draw_idx = -1;
16041662

@@ -1612,15 +1670,8 @@ function update(response) {
16121670
if(row.position_id > position_id) { position_id = row.position_id; }
16131671

16141672
if (!row.picture) {
1615-
if(ctx.last_vehicle == null) ctx.last_vehicle = row.vehicle;
1616-
16171673
addPosition(row);
16181674
got_positions = true;
1619-
1620-
if(ctx.last_vehicle != row.vehicle) {
1621-
draw_idx = vehicle_names.indexOf(ctx.last_vehicle);
1622-
ctx.last_vehicle = row.vehicle;
1623-
}
16241675
}
16251676
}
16261677

0 commit comments

Comments
 (0)