Hello,
I am trying to fetch the flying hours from a the private plane of a German politician to calculate his average CO2 emissions. I am not a professional coder and think I didn't understand the APIs functionality properly. Maybe you can help?
Here is what I did. In the :
import axios from 'axios';
const BASE_URL = '';
// Replace YOUR_USERNAME and YOUR_PASSWORD with your actual OpenSky Network credentials
const USERNAME = "jeangleur";
const PASSWORD = encodeURIComponent("CorrectivIsSuperCool123!");
const auth = {
username: USERNAME,
password: PASSWORD
};
export const fetchStates = async (icao24Array) => {
try {
const icao24String = (',');
const response = await (`${BASE_URL}/states/all`, {
auth: auth,
params: {
icao24: icao24String,
},
});
return ;
} catch (error) {
('Error fetching states:', error);
return null;
}
};
And in the
import React, { useEffect, useState } from 'react';
import { fetchStates } from './api';
import './';
const calculateCO2Emission = (distance) => {
const kgCO2PerKm = 0.115; // Average CO2 emission per km
return distance * kgCO2PerKm;
};
function App() {
const [aircraftData, setAircraftData] = useState([]);
const [co2Emission, setCO2Emission] = useState(0);
useEffect(() => {
// Replace this array with the ICAO24 addresses of the aircraft you want to track
const icao24Array = ['3E17D6', '3D49DE'];
const fetchData = async () => {
const allData = await fetchStates(icao24Array);
if (allData) {
const flBschftBMVgData = ((aircraft) => {
return aircraft[1] && aircraft[1].startsWith("GAF");
});
setAircraftData(flBschftBMVgData);
} else {
("Error fetching aircraft data");
}
};
fetchData();
}, []);
useEffect(() => {
const totalDistance = ((total, aircraft) => {
// Assuming aircraft[9] is the distance traveled by the aircraft in kilometers
return total + (aircraft[9] || 0);
}, 0);
setCO2Emission(calculateCO2Emission(totalDistance));
}, [aircraftData]);
return (
<div className="App">
<header className="App-header">
<h1 className="neon-title">SkySnitch</h1>
<h2 className="neon-subtitle">Tracking Friedrich Merz' CO2 Flight Emissions</h2>
</header>
<main>
<section>
<h2>CO2 Emission Counter</h2>
<div className="counter-container">
<div className="counter">{(2)} kg</div>
</div>
);
}
export default App;