interface GeoLocation {
srs: string;
latitude: string | number;
longitude: string | number;
}
interface CurrentValue {
status: string;
datetime: number;
date: string;
value: string | number;
volume?: number;
}
interface CurrentValueCorrected extends CurrentValue {
value: number;
}
interface Location {
station_no?: string;
name: string;
short_name: string;
watershed: string;
subwatershed?: string;
type: string;
unitname: string;
unitsymbol: string;
category: string;
url: string;
geoLocation: GeoLocation;
recent_values?: Array<{
status: string;
datetime: number;
date: string;
value: string | number;
volume?: number;
}>;
current_value?: CurrentValue;
}
interface Data {
unitTypes: Array<{
type: string;
label: string;
description: string;
unitCode: string;
}>;
locations: {
[key: string]: Location;
};
}
function getCurrentValue(jsonData: Data, locationKey: string): CurrentValueCorrected | undefined {
const location = jsonData.locations[locationKey];
const currentLocation = location ? location.current_value : undefined;
if (currentLocation) {
currentLocation.value = Number(currentLocation.value);
}
return currentLocation;
}
async function getData() {
const url = "https://minnehahacreek.org/data-json/request/";
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
return data;
} catch (error) {
console.error("Error fetching data:", error);
}
}
export async function getFlowLevel() {
const data = await getData();
const graysBayDamCurrentValue = getCurrentValue(data, "mcwdgraysdischarge");
const hiawathaCurrentValue = getCurrentValue(data, "usgshiawatha");
return { graysBayDamCurrentValue, hiawathaCurrentValue };
}
const test = (async () => {
const results = await getFlowLevel();
console.log(results);
})();