Fetch only updated speed values for animating speedometer for live session usage. #10
-
|
First of all i'm suoer thankful for this api. Thanks for the people that make this possible! I am new to programming so i have a neewbie question. As the titel says i am animating a speedometer for usage during live sessions. For non-live sessions the speedometer is working perfectly. I fetch the whole speed data of a race via api and store it in an array. In my main loop of the programm, I iterate over this array to get the next recorded speed value and calculated the angle of rotation so that the needle shows the accurate speed on the speedometer (picture attached). Now my question is how will this work in a live session? I assume the database will get updated at a frequency of the 3.7 Hz as mentioned. But fetching the whole speed data as a json file 4 times a second seems extremly ineffcient to just get the latest updated speed values. How can i just grab the latest updated speed values from the database so i don't have to reload all the data? Cheers and thanks for the help |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
Thank you so much for your kind words and support towards the API! To efficiently grab the latest updated speed values from the API without having to reload all the data, you can implement a strategy where you keep track of the most recent data you've fetched. Specifically, at each call to the API, take note of the latest "date" value of the returned data. When you make the next query to the API, you can include a parameter in your request that specifies you only want data that has been recorded after this latest "date" value. This can be achieved by adding the "date" query parameter to the API URL. Also, you do not need to call the API multiple times per second for this use case. Please use a buffer mechanism, like this (untested Python code): import requests
import threading
import time
from datetime import datetime, timedelta
QUERY_FREQUENCY = 3 # Query data every 3 seconds
DELAY = 10 # Delay between the event on track and the speedometer (10 seconds)
# Shared buffer for holding the latest speed data
buffer = []
buffer_lock = threading.Lock()
def fetch_speed_data():
last_query_time = datetime.now() - timedelta(seconds=DELAY)
while True:
try:
# Format the timestamp for the API call
formatted_time = last_query_time.strftime('%Y-%m-%dT%H:%M:%S.%f+00:00')
url = f"https://api.openf1.org/v1/car_data?driver_number=55&session_key=latest&date>{formatted_time}"
response = requests.get(url)
data = response.json()
with buffer_lock:
buffer.extend(data) # Add new data to the buffer
# Update last_query_time to the time of the last data point received
if data:
last_query_time = datetime.strptime(data[-1]['date'], '%Y-%m-%dT%H:%M:%S.%f+00:00')
time.sleep(QUERY_FREQUENCY) # Wait for a few seconds before next API call
except Exception as e:
print(f"Error fetching speed data: {e}")
time.sleep(QUERY_FREQUENCY) # In case of an error, wait before trying again
def display_speed_data():
while True:
with buffer_lock:
if buffer:
# Take the most recent value and remove it from the buffer
current = buffer.pop(0)
date = datetime.strptime(current['date'], '%Y-%m-%dT%H:%M:%S.%f+00:00')
waiting_time = max(0, DELAY - (datetime.now() - date).total_seconds())
time.sleep(waiting_time)
print(f"Current Speed: {current['speed']} km/h")
else:
print("Waiting for new speed data...")
# Start the data fetching thread
fetching_thread = threading.Thread(target=fetch_speed_data)
fetching_thread.start()
# Start the display thread
display_thread = threading.Thread(target=display_speed_data)
display_thread.start() |
Beta Was this translation helpful? Give feedback.
Thank you so much for your kind words and support towards the API!
To efficiently grab the latest updated speed values from the API without having to reload all the data, you can implement a strategy where you keep track of the most recent data you've fetched. Specifically, at each call to the API, take note of the latest "date" value of the returned data. When you make the next query to the API, you can include a parameter in your request that specifies you only want data that has been recorded after this latest "date" value. This can be achieved by adding the "date" query parameter to the API URL.
Also, you do not need to call the API multiple times per second for this use case. Please …