Add adsb fadeout

This commit is contained in:
30hours 2024-02-14 12:42:23 +00:00
parent 464f740725
commit b050e2e7a2
4 changed files with 88 additions and 63 deletions

48
api/map/event/adsb.js Normal file
View file

@ -0,0 +1,48 @@
function event_adsb() {
fetch(adsb_url)
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.then(data => {
// Update aircraft points based on new data
updateAircraftPoints(data);
})
.catch(error => {
// Handle errors during fetch
console.error('Error during fetch:', error);
})
.finally(() => {
// Schedule the next fetch after a delay (e.g., 5 seconds)
setTimeout(event_adsb, 1000);
});
}
// Function to update aircraft points
function updateAircraftPoints(data) {
removeEntitiesOlderThanAndFade("adsb", 60, 0.5);
// Process aircraft data and add points
const aircraft = data.aircraft || [];
aircraft.forEach(processAircraftData);
}
// Function to process aircraft data
function processAircraftData(aircraftData) {
const icao = aircraftData.hex;
const flight = aircraftData.flight;
const lat = aircraftData.lat;
const lon = aircraftData.lon;
const alt = aircraftData.alt_baro;
const seen_pos = aircraftData.seen_pos;
// Check if the aircraft has valid position data
if (lat !== undefined && lon !== undefined && alt !== undefined && seen_pos < 10) {
addPoint(lat, lon, alt, flight, 'rgba(255, 0, 0, 0.5)', 10, "adsb", Date.now());
}
}

4
api/map/event/radar.js Normal file
View file

@ -0,0 +1,4 @@
function event_radar() {
setTimeout(event_radar, 1000);
}

View file

@ -25,6 +25,8 @@
<body> <body>
<div id="cesiumContainer"></div> <div id="cesiumContainer"></div>
<script src="lib/jquery-3.6.0.min.js"></script> <script src="lib/jquery-3.6.0.min.js"></script>
<script src="event/adsb.js"></script>
<script src="event/radar.js"></script>
<script src="main.js"></script> <script src="main.js"></script>
</body> </body>
</html> </html>

View file

@ -257,81 +257,52 @@ window.addEventListener('load', function () {
}) })
// Function to process aircraft data
function processAircraftData(aircraftData) {
const icao = aircraftData.hex;
const flight = aircraftData.flight;
const lat = aircraftData.lat;
const lon = aircraftData.lon;
const alt = aircraftData.alt_baro;
const seen_pos = aircraftData.seen_pos;
// Check if the aircraft has valid position data
if (lat !== undefined && lon !== undefined && alt !== undefined && seen_pos < 10) {
addPoint(lat, lon, alt, flight, 'rgba(255, 0, 0, 0.5)', 10, "adsb", Date.now());
}
}
function removeEntitiesOlderThan(entityType, maxAgeSeconds) { function removeEntitiesOlderThan(entityType, maxAgeSeconds) {
viewer.entities.values.forEach((entity) => {
var entities = viewer.entities.values;
for (var i = entities.length - 1; i >= 0; i--) {
var entity = entities[i];
const type = entity.properties["type"].getValue(); const type = entity.properties["type"].getValue();
const timestamp = entity.properties["timestamp"].getValue(); const timestamp = entity.properties["timestamp"].getValue();
console.log(type); if (entity.properties && entity.properties["type"] &&
console.log(timestamp); entity.properties["type"].getValue() === entityType &&
if (type === entityType & Date.now()-timestamp > maxAgeSeconds) { Date.now()-timestamp > maxAgeSeconds*1000) {
viewer.entities.remove(entity); viewer.entities.remove(entity);
} }
}); }
} }
// Function to update aircraft points function removeEntitiesOlderThanAndFade(entityType, maxAgeSeconds, baseAlpha) {
function updateAircraftPoints(data) {
// Clear existing points var entities = viewer.entities.values;
//viewer.entities.removeAll(); for (var i = entities.length - 1; i >= 0; i--) {
//removeEntitiesByType("adsb"); var entity = entities[i];
removeEntitiesOlderThan("adsb", 10); const type = entity.properties["type"].getValue();
const timestamp = entity.properties["timestamp"].getValue();
if (entity.properties && entity.properties["type"] &&
entity.properties["type"].getValue() === entityType &&
Date.now()-timestamp > maxAgeSeconds*1000) {
viewer.entities.remove(entity);
}
else {
entity.point.color = new Cesium.Color.fromAlpha(
entity.point.color.getValue(), baseAlpha*(1-(Date.now()-timestamp)/(maxAgeSeconds*1000)));
}
}
// Process aircraft data and add points
const aircraft = data.aircraft || [];
aircraft.forEach(processAircraftData);
} }
function removeEntitiesByType(entityType) { function removeEntitiesByType(entityType) {
viewer.entities.values.forEach((entity) => {
if (entity.properties["type"].getValue() === entityType) { var entities = viewer.entities.values;
viewer.entities.remove(entity); for (var i = entities.length - 1; i >= 0; i--) {
var entity = entities[i];
if (entity.properties && entity.properties["type"] &&
entity.properties["type"].getValue() === entityType) {
viewer.entities.remove(entity);
} }
}); }
}
function event_adsb() {
fetch(adsb_url)
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.then(data => {
// Update aircraft points based on new data
updateAircraftPoints(data);
})
.catch(error => {
// Handle errors during fetch
console.error('Error during fetch:', error);
})
.finally(() => {
// Schedule the next fetch after a delay (e.g., 5 seconds)
setTimeout(event_adsb, 1000);
});
}
function event_radar() {
setTimeout(event_radar, 1000);
} }
function doesEntityNameExist(name) { function doesEntityNameExist(name) {