mirror of
https://github.com/30hours/3lips.git
synced 2024-11-08 12:25:42 +00:00
Add adsb fadeout
This commit is contained in:
parent
464f740725
commit
b050e2e7a2
4 changed files with 88 additions and 63 deletions
48
api/map/event/adsb.js
Normal file
48
api/map/event/adsb.js
Normal 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
4
api/map/event/radar.js
Normal file
|
@ -0,0 +1,4 @@
|
|||
function event_radar() {
|
||||
|
||||
setTimeout(event_radar, 1000);
|
||||
}
|
|
@ -25,6 +25,8 @@
|
|||
<body>
|
||||
<div id="cesiumContainer"></div>
|
||||
<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>
|
||||
</body>
|
||||
</html>
|
||||
|
|
|
@ -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) {
|
||||
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 timestamp = entity.properties["timestamp"].getValue();
|
||||
console.log(type);
|
||||
console.log(timestamp);
|
||||
if (type === entityType & Date.now()-timestamp > maxAgeSeconds) {
|
||||
if (entity.properties && entity.properties["type"] &&
|
||||
entity.properties["type"].getValue() === entityType &&
|
||||
Date.now()-timestamp > maxAgeSeconds*1000) {
|
||||
viewer.entities.remove(entity);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Function to update aircraft points
|
||||
function updateAircraftPoints(data) {
|
||||
// Clear existing points
|
||||
//viewer.entities.removeAll();
|
||||
//removeEntitiesByType("adsb");
|
||||
removeEntitiesOlderThan("adsb", 10);
|
||||
function removeEntitiesOlderThanAndFade(entityType, maxAgeSeconds, baseAlpha) {
|
||||
|
||||
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 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) {
|
||||
viewer.entities.values.forEach((entity) => {
|
||||
if (entity.properties["type"].getValue() === entityType) {
|
||||
viewer.entities.remove(entity);
|
||||
|
||||
var entities = viewer.entities.values;
|
||||
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) {
|
||||
|
|
Loading…
Reference in a new issue