2015-09-28 19:20:43 +00:00
|
|
|
angular.module('appControllers').controller('TrafficCtrl', TrafficCtrl); // get the main module contollers set
|
2015-10-01 21:19:28 +00:00
|
|
|
TrafficCtrl.$inject = ['$rootScope', '$scope', '$state', '$http', '$interval']; // Inject my dependencies
|
2015-09-28 19:20:43 +00:00
|
|
|
|
|
|
|
// create our controller function with all necessary logic
|
2015-10-01 21:19:28 +00:00
|
|
|
function TrafficCtrl($rootScope, $scope, $state, $http, $interval) {
|
2015-09-28 19:20:43 +00:00
|
|
|
|
2015-10-01 21:19:28 +00:00
|
|
|
$scope.$parent.helppage = 'plates/traffic-help.html';
|
2015-10-04 17:50:51 +00:00
|
|
|
$scope.data_list = [];
|
2015-09-28 19:20:43 +00:00
|
|
|
|
2016-02-16 04:41:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2015-10-01 21:19:28 +00:00
|
|
|
function utcTimeString(epoc) {
|
|
|
|
var time = "";
|
|
|
|
var val;
|
|
|
|
var d = new Date(epoc);
|
|
|
|
val = d.getUTCHours();
|
|
|
|
time += (val < 10 ? "0" + val : "" + val);
|
|
|
|
val = d.getUTCMinutes();
|
|
|
|
time += ":" + (val < 10 ? "0" + val : "" + val);
|
|
|
|
val = d.getUTCSeconds();
|
|
|
|
time += ":" + (val < 10 ? "0" + val : "" + val);
|
|
|
|
time += "Z";
|
|
|
|
return time;
|
|
|
|
}
|
|
|
|
|
|
|
|
function dmsString(val) {
|
|
|
|
return [0 | val,
|
2015-10-27 14:55:18 +00:00
|
|
|
'° ',
|
2015-10-01 21:19:28 +00:00
|
|
|
0 | (val < 0 ? val = -val : val) % 1 * 60,
|
|
|
|
"' ",
|
|
|
|
0 | val * 60 % 1 * 60,
|
|
|
|
'"'].join('');
|
|
|
|
}
|
|
|
|
|
|
|
|
function setAircraft(obj, new_traffic) {
|
|
|
|
new_traffic.icao_int = obj.Icao_addr;
|
|
|
|
new_traffic.icao = obj.Icao_addr.toString(16).toUpperCase();
|
|
|
|
new_traffic.tail = obj.Tail;
|
|
|
|
new_traffic.lat = dmsString(obj.Lat);
|
|
|
|
new_traffic.lon = dmsString(obj.Lng);
|
2015-12-31 01:01:36 +00:00
|
|
|
var n = Math.round(obj.Alt / 25) * 25;
|
2015-10-01 21:19:28 +00:00
|
|
|
new_traffic.alt = n.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
|
2015-12-31 01:01:36 +00:00
|
|
|
var s = Math.round(obj.Speed / 5) * 5;
|
|
|
|
if (obj.Speed_valid) {
|
|
|
|
new_traffic.speed = s.toString();
|
2016-02-16 04:41:13 +00:00
|
|
|
new_traffic.heading = Math.round(obj.Track / 5) * 5;
|
2015-12-31 01:01:36 +00:00
|
|
|
} else {
|
|
|
|
new_traffic.speed = "---";
|
2016-02-16 04:41:13 +00:00
|
|
|
new_traffic.heading = "---";
|
2015-12-31 01:01:36 +00:00
|
|
|
}
|
2015-10-01 21:19:28 +00:00
|
|
|
new_traffic.vspeed = Math.round(obj.Vvel / 100) * 100
|
2016-02-18 02:12:50 +00:00
|
|
|
var timestamp = Date.parse(obj.Timestamp);
|
|
|
|
new_traffic.time = utcTimeString(timestamp);
|
2016-02-18 04:31:05 +00:00
|
|
|
new_traffic.age = (Math.round(obj.Age * 10)/10).toFixed(1);
|
2015-10-04 17:50:51 +00:00
|
|
|
new_traffic.src = obj.Last_source; // 1=ES, 2=UAT
|
2015-10-01 21:19:28 +00:00
|
|
|
// return new_aircraft;
|
|
|
|
}
|
2016-02-18 04:31:05 +00:00
|
|
|
|
2015-10-01 21:19:28 +00:00
|
|
|
|
|
|
|
function connect($scope) {
|
|
|
|
if (($scope === undefined) || ($scope === null))
|
|
|
|
return; // we are getting called once after clicking away from the status page
|
|
|
|
|
|
|
|
if (($scope.socket === undefined) || ($scope.socket === null)) {
|
2015-10-14 21:35:41 +00:00
|
|
|
socket = new WebSocket(URL_TRAFFIC_WS);
|
2015-10-01 21:19:28 +00:00
|
|
|
$scope.socket = socket; // store socket in scope for enter/exit usage
|
|
|
|
}
|
|
|
|
|
2016-01-29 19:41:30 +00:00
|
|
|
$scope.ConnectState = "Disconnected";
|
2015-10-01 21:19:28 +00:00
|
|
|
|
|
|
|
socket.onopen = function (msg) {
|
2015-10-04 17:50:51 +00:00
|
|
|
// $scope.ConnectStyle = "label-success";
|
2016-01-29 19:41:30 +00:00
|
|
|
$scope.ConnectState = "Connected";
|
2015-10-01 21:19:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
socket.onclose = function (msg) {
|
2015-10-04 17:50:51 +00:00
|
|
|
// $scope.ConnectStyle = "label-danger";
|
2016-01-29 19:41:30 +00:00
|
|
|
$scope.ConnectState = "Disconnected";
|
2015-10-04 17:50:51 +00:00
|
|
|
$scope.$apply();
|
2015-10-01 21:19:28 +00:00
|
|
|
setTimeout(connect, 1000);
|
|
|
|
};
|
|
|
|
|
|
|
|
socket.onerror = function (msg) {
|
2015-10-04 17:50:51 +00:00
|
|
|
// $scope.ConnectStyle = "label-danger";
|
2015-10-01 21:19:28 +00:00
|
|
|
$scope.ConnectState = "Problem";
|
2015-10-04 17:50:51 +00:00
|
|
|
$scope.$apply();
|
2015-10-01 21:19:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
socket.onmessage = function (msg) {
|
2016-02-17 07:18:04 +00:00
|
|
|
|
|
|
|
|
2015-10-01 21:19:28 +00:00
|
|
|
console.log('Received traffic update.')
|
2016-02-17 07:18:04 +00:00
|
|
|
|
2015-10-04 17:50:51 +00:00
|
|
|
var message = JSON.parse(msg.data);
|
|
|
|
$scope.raw_data = angular.toJson(msg.data, true);
|
|
|
|
|
|
|
|
if (message.Position_valid) {
|
2015-10-01 21:19:28 +00:00
|
|
|
// we need to use an array so AngularJS can perform sorting; it also means we need to loop to find an aircraft in the traffic set
|
|
|
|
var found = false;
|
2015-10-04 17:50:51 +00:00
|
|
|
for (var i = 0, len = $scope.data_list.length; i < len; i++) {
|
|
|
|
if ($scope.data_list[i].icao_int === message.Icao_addr) {
|
|
|
|
setAircraft(message, $scope.data_list[i]);
|
2015-10-01 21:19:28 +00:00
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!found) {
|
|
|
|
var new_traffic = {};
|
2015-10-04 17:50:51 +00:00
|
|
|
setAircraft(message, new_traffic);
|
|
|
|
$scope.data_list.unshift(new_traffic); // add to start of array
|
2015-10-01 21:19:28 +00:00
|
|
|
}
|
|
|
|
$scope.$apply();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-02-17 07:18:04 +00:00
|
|
|
var getClock = $interval(function () {
|
|
|
|
$http.get(URL_STATUS_GET).
|
|
|
|
then(function (response) {
|
|
|
|
globalStatus = angular.fromJson(response.data);
|
|
|
|
|
|
|
|
var tempClock = new Date(Date.parse(globalStatus.Clock));
|
|
|
|
var clockString = tempClock.toUTCString();
|
|
|
|
$scope.Clock = clockString;
|
|
|
|
|
|
|
|
var tempUptimeClock = new Date(Date.parse(globalStatus.UptimeClock));
|
|
|
|
var uptimeClockString = tempUptimeClock.toUTCString();
|
|
|
|
$scope.UptimeClock = uptimeClockString;
|
|
|
|
|
|
|
|
var tempLocalClock = new Date;
|
|
|
|
$scope.LocalClock = tempLocalClock.toUTCString();
|
|
|
|
$scope.SecondsFast = (tempClock-tempLocalClock)/1000;
|
|
|
|
|
|
|
|
}, function (response) {
|
|
|
|
// nop
|
|
|
|
});
|
|
|
|
}, 500, 0, false);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-02-18 04:31:05 +00:00
|
|
|
// perform cleanup every 10 seconds
|
2015-10-01 21:19:28 +00:00
|
|
|
var clearStaleTraffic = $interval(function () {
|
2016-02-18 06:10:21 +00:00
|
|
|
// remove stale aircraft = anything more than 59 seconds without an update
|
2015-10-01 21:19:28 +00:00
|
|
|
var dirty = false;
|
2016-02-18 06:10:21 +00:00
|
|
|
var cutoff =59;
|
2015-10-01 21:19:28 +00:00
|
|
|
|
2015-10-04 17:50:51 +00:00
|
|
|
for (var i = len = $scope.data_list.length; i > 0; i--) {
|
2016-02-18 06:10:21 +00:00
|
|
|
if ($scope.data_list[i - 1].age >= cutoff) {
|
2015-10-04 17:50:51 +00:00
|
|
|
$scope.data_list.splice(i - 1, 1);
|
2015-10-01 21:19:28 +00:00
|
|
|
dirty = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (dirty) {
|
2015-10-04 17:50:51 +00:00
|
|
|
$scope.raw_data = "";
|
2015-10-01 21:19:28 +00:00
|
|
|
$scope.$apply();
|
|
|
|
}
|
2016-02-18 04:31:05 +00:00
|
|
|
}, (1000 * 10), 0, false);
|
2015-10-01 21:19:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
$state.get('traffic').onEnter = function () {
|
|
|
|
// everything gets handled correctly by the controller
|
|
|
|
};
|
|
|
|
|
|
|
|
$state.get('traffic').onExit = function () {
|
|
|
|
// disconnect from the socket
|
|
|
|
if (($scope.socket !== undefined) && ($scope.socket !== null)) {
|
|
|
|
$scope.socket.close();
|
|
|
|
$scope.socket = null;
|
|
|
|
}
|
|
|
|
// stop stale traffic cleanup
|
|
|
|
$interval.cancel(clearStaleTraffic);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Traffic Controller tasks
|
|
|
|
connect($scope); // connect - opens a socket and listens for messages
|
2015-09-28 19:20:43 +00:00
|
|
|
};
|