51 lines
1.2 KiB
TypeScript
51 lines
1.2 KiB
TypeScript
// app/utils/api.ts
|
|
export interface TimeSeriesEntry {
|
|
time: string;
|
|
value: number;
|
|
}
|
|
|
|
export interface TimeSeriesResponse {
|
|
consumption: TimeSeriesEntry[];
|
|
generation: TimeSeriesEntry[];
|
|
}
|
|
|
|
export async function fetchPowerTimeseries(
|
|
site: string,
|
|
start: string,
|
|
end: string
|
|
): Promise<TimeSeriesResponse> { // <-- Change here
|
|
const params = new URLSearchParams({ site, start, end });
|
|
|
|
const res = await fetch(`http://localhost:8000/power-timeseries?${params.toString()}`);
|
|
|
|
if (!res.ok) {
|
|
throw new Error(`Failed to fetch data: ${res.status}`);
|
|
}
|
|
|
|
const json = await res.json();
|
|
console.log(`🔍 API response from /power-timeseries?${params.toString()}:`, json); // ✅ log here
|
|
return json; // <-- This is a single object, not an array
|
|
}
|
|
|
|
export async function fetchForecast(
|
|
lat: number,
|
|
lon: number,
|
|
dec: number,
|
|
az: number,
|
|
kwp: number
|
|
): Promise<{ time: string; forecast: number }[]> {
|
|
const query = new URLSearchParams({
|
|
lat: lat.toString(),
|
|
lon: lon.toString(),
|
|
dec: dec.toString(),
|
|
az: az.toString(),
|
|
kwp: kwp.toString(),
|
|
}).toString();
|
|
|
|
const res = await fetch(`http://localhost:8000/forecast?${query}`);
|
|
if (!res.ok) throw new Error("Failed to fetch forecast");
|
|
|
|
return res.json();
|
|
}
|
|
|