Help for coding: Tacking a planes flying time with API

1 year 3 months ago #2374 by jeangleur
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 


[code]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;



 
[/code]

Please Log in or Create an account to join the conversation.

1 year 3 months ago #2375 by jluebbe
Hi,
I have no experience with axios as I am usually using Python. However, you may mix up F. Merz private flights (aircraft icao24 value 3E17D6) and Bundeswehr flights (callsign starts with GAF) which is a huge difference in fuel consumption.
To find flights of the DA62, you may try the following query (this request covers a few days around the trip to a wedding on Sylt)
https://your-name:your-password@opensky-network.org/api/flights/aircraft?icao24=3E17D6&begin=1655856000&end=1658361600
Engine performance data is available here:
www.diamondaircraft.com/en/private-owner...aft/da62/tech-specs/
The fuel consumption may be roughly 15 liters/100km which is similar to that of a protected car used by politicians.
You need to calculate the distance between arrival and departure airport to estimate the amount of total fuel consumption and the corresponding CO2 emission.

Please Log in or Create an account to join the conversation.

Powered by Kunena Forum
This website uses cookies to offer you the best experience of our services. By using this website you agree to our privacy policy!