Commit 6f5696d6 authored by Stephen D's avatar Stephen D
Browse files

Added web workers to prevent browser freezing

parent 839fd52a
......@@ -45,7 +45,7 @@
<td><b>Start time</b></td>
<td><b>Max AoAs</b></td>
<td><b>End time</b></td>
<td><b>Total time of contact<b></td>
<td><b>Total time of contact</b></td>
</tr>
<tr ng-repeat="pass in passes">
<td>{{getDate(pass.time_start)}}</td>
......
importScripts('satellite.min.js');
self.addEventListener('message', function(e) {
//The data returned from calcSats isn't fit for displaying,
//as it is arranged into groups by satallite. Below fixes that
//Make new array equal to all the old array.passes
var passes = calcSatellites(e.data.coords, e.data.satellites).map(function(a) {
//Add satellite names to each pass
a.passes.map(function(b) {
b.name = a.name;
return b;
});
return a.passes;
});
passes = flatten(passes);
//Sort by time_start
passes.sort(function(a,b) {
return a.time_start - b.time_start;
});
self.postMessage(passes);
}, false);
function flatten(arr) {
return arr.reduce(function (flat, toFlatten) {
return flat.concat(Array.isArray(toFlatten) ? flatten(toFlatten) : toFlatten);
}, []);
}
function calcSatellites(ground_coords, satellites) {
//Convert coords to radians
var ground_coords_rad = [];
for(var i = 0; i < ground_coords.length; i++) { //Coords
var radian_coords = {
latitude: ground_coords[i].lat * Math.PI / 180,
longitude: ground_coords[i].lng * Math.PI / 180,
height: ground_coords[i].height};
ground_coords_rad.push(radian_coords);
}
var viableSats = [];
var currentTimestamp = new Date();
for(var i = 0; i < satellites.length; i++) { //Satellites
if(satellites[i].satrec.error === 0) {
var passes = [];
var time_start = 0;
var pointStats = [];
for(var secondsFromNow = 0; secondsFromNow < 604800; secondsFromNow += 30) { //Up to a week in the future, in 30 second increments
var timeToProgressTo = new Date(currentTimestamp.getTime() + (secondsFromNow * 1000));
//console.log(satellites[i].satrec.error);
var viableTime = true;
for(var j = 0; j < ground_coords_rad.length; j++) { //Coordinates
var g_coords = ground_coords_rad[j];
var posAndVel = satellite.propagate(satellites[i].satrec, timeToProgressTo);
if(posAndVel.position != false && posAndVel.position != null) {
var lookAngles = satellite.ecfToLookAngles(g_coords, satellite.eciToEcf(posAndVel.position, satellite.gstimeFromDate(timeToProgressTo)));
var elevationDeg = lookAngles.elevation * 180 / Math.PI;
//Define pointStats[j]
if(!pointStats[j]) pointStats[j] = {maxElv: 0, timeMaxElv: 0};
if(elevationDeg > pointStats[j].maxElv && elevationDeg > ground_coords[j].angle) { //Has to be during a pass
pointStats[j].maxElv = elevationDeg;
pointStats[j].timeMaxElv = timeToProgressTo;
}
if(elevationDeg < ground_coords[j].angle) {
viableTime = false;
if(time_start != 0) {
//Push pass to array and reset for next pass
passes.push({
time_start: time_start,
pointStats: pointStats,
time_end: timeToProgressTo
});
time_start = 0;
pointStats = [];
}
break;
}
}
}
if(viableTime) {
if(time_start == 0) time_start = timeToProgressTo;
}
}
viableSats.push({
name: satellites[i].name,
passes: passes
});
}
}
return viableSats;
}
......@@ -67,27 +67,14 @@ app.controller('satTrack-ctrl', function($scope, NgMap) {
};
});
//The data returned from calcSats isn't fit for displaying,
//as it is arranged into groups by satallite. Below fixes that
//Make new array equal to all the old array.passes
var passes = calcSatellites(coords, satellites).map(function(a) {
//Add satellite names to each pass
a.passes.map(function(b) {
b.name = a.name;
return b;
});
return a.passes;
});
passes = flatten(passes);
var worker = new Worker('js/calc.js');
worker.addEventListener('message', function(e) {
//Sort by time_start
passes.sort(function(a,b){
return a.time_start - b.time_start;
});
$scope.passes = passes;
$scope.isGettingPasses = false;
$scope.$apply();
$scope.passes = e.data;
$scope.isGettingPasses = false;
$scope.$apply();
}, false);
worker.postMessage({coords: coords, satellites: satellites});
}
}
......@@ -113,12 +100,6 @@ app.controller('satTrack-ctrl', function($scope, NgMap) {
}
});
function flatten(arr) {
return arr.reduce(function (flat, toFlatten) {
return flat.concat(Array.isArray(toFlatten) ? flatten(toFlatten) : toFlatten);
}, []);
}
function loadSatelliteData(callback) {
$.getJSON("names.json", function(json) {
var idsAndNames = [];
......@@ -142,76 +123,3 @@ function loadSatelliteData(callback) {
});
});
}
function calcSatellites(ground_coords, satellites) {
//Convert coords to radians
var ground_coords_rad = [];
for(var i = 0; i < ground_coords.length; i++) { //Coords
var radian_coords = {
latitude: ground_coords[i].lat * Math.PI / 180,
longitude: ground_coords[i].lng * Math.PI / 180,
height: ground_coords[i].height};
ground_coords_rad.push(radian_coords);
}
var viableSats = [];
var currentTimestamp = new Date();
for(var i = 0; i < satellites.length; i++) { //Satellites
if(satellites[i].satrec.error === 0) {
var passes = [];
var time_start = 0;
var pointStats = [];
for(var secondsFromNow = 0; secondsFromNow < 604800; secondsFromNow += 30) { //Up to a week in the future, in 30 second increments
var timeToProgressTo = new Date(currentTimestamp.getTime() + (secondsFromNow * 1000));
//console.log(satellites[i].satrec.error);
var viableTime = true;
for(var j = 0; j < ground_coords_rad.length; j++) { //Coordinates
var g_coords = ground_coords_rad[j];
var posAndVel = satellite.propagate(satellites[i].satrec, timeToProgressTo);
if(posAndVel.position != false && posAndVel.position != null) {
var lookAngles = satellite.ecfToLookAngles(g_coords, satellite.eciToEcf(posAndVel.position, satellite.gstimeFromDate(timeToProgressTo)));
var elevationDeg = lookAngles.elevation * 180 / Math.PI;
//Define pointStats[j]
if(!pointStats[j]) pointStats[j] = {maxElv: 0, timeMaxElv: 0};
if(elevationDeg > pointStats[j].maxElv && elevationDeg > ground_coords[j].angle) { //Has to be during a pass
pointStats[j].maxElv = elevationDeg;
pointStats[j].timeMaxElv = timeToProgressTo;
}
if(elevationDeg < ground_coords[j].angle) {
viableTime = false;
if(time_start != 0) {
//Push pass to array and reset for next pass
passes.push({
time_start: time_start,
pointStats: pointStats,
time_end: timeToProgressTo
});
time_start = 0;
pointStats = [];
}
break;
}
}
}
if(viableTime) {
if(time_start == 0) time_start = timeToProgressTo;
}
}
viableSats.push({
name: satellites[i].name,
passes: passes
});
}
}
return viableSats;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment