Continue messing with stores
This commit is contained in:
parent
c7df38e6ce
commit
52558ed3b2
7 changed files with 148 additions and 83 deletions
|
|
@ -3,8 +3,8 @@ import type { LatLngExpression } from "leaflet";
|
|||
import L from "leaflet";
|
||||
|
||||
import { getCsrfToken } from "./auth";
|
||||
import type { PredictionStage, Prediction, ParsedPrediction } from "./types";
|
||||
import { latestPrediction, latestPredictionParsed } from "./stores";
|
||||
import type { PredictionStage, RawPrediction, Prediction } from "./types";
|
||||
import { PredictionStore, RawPredictionStore, writeLocalStorage } from "./stores";
|
||||
|
||||
function getLatestDataset() {
|
||||
const now = new Date();
|
||||
|
|
@ -76,8 +76,10 @@ export const getForecast = async (
|
|||
const data = await response.json();
|
||||
console.log("Forecast response:", data);
|
||||
|
||||
latestPrediction.set(data.result);
|
||||
latestPredictionParsed.set(parsePrediction(data.result.prediction));
|
||||
RawPredictionStore.set(data.result as RawPrediction);
|
||||
PredictionStore.set(parsePrediction(data.result.prediction) as Prediction);
|
||||
writeLocalStorage("rawPrediction", data.result as RawPrediction);
|
||||
writeLocalStorage("prediction", parsePrediction(data.result.prediction) as Prediction);
|
||||
|
||||
alert("Forecast request successful!");
|
||||
// Handle the response data as needed
|
||||
|
|
@ -87,7 +89,7 @@ export const getForecast = async (
|
|||
}
|
||||
};
|
||||
|
||||
export function parsePrediction(prediction: PredictionStage[]): ParsedPrediction {
|
||||
export function parsePrediction(prediction: PredictionStage[]): Prediction {
|
||||
const flight_path: [number, number, number][] = [];
|
||||
const launch: { latlng: LatLngExpression; datetime: Date } = {} as any;
|
||||
const burst: { latlng: LatLngExpression; datetime: Date } = {} as any;
|
||||
|
|
|
|||
|
|
@ -1,8 +1,40 @@
|
|||
import { writable } from "svelte/store";
|
||||
import type { FlightParameters, Telemetry, ParsedTelemetry } from "./types";
|
||||
import type { Prediction, ParsedPrediction } from "./types";
|
||||
import type { FlightParameters, RawTelemetry, Telemetry } from "./types";
|
||||
import type { RawPrediction, Prediction } from "./types";
|
||||
|
||||
export const flightParametersStore = writable<FlightParameters>({
|
||||
export const readLocalStorage = <T>(key: string, defaultValue: T): T => {
|
||||
const item = localStorage.getItem(key);
|
||||
if (item) {
|
||||
try {
|
||||
const parsed = JSON.parse(item);
|
||||
if (typeof parsed === "object" && parsed !== null) {
|
||||
return parsed as T;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`Error parsing ${key} from localStorage:`, error);
|
||||
}
|
||||
}
|
||||
return defaultValue;
|
||||
};
|
||||
|
||||
export const writeLocalStorage = <T>(key: string, value: T): void => {
|
||||
try {
|
||||
localStorage.setItem(key, JSON.stringify(value));
|
||||
} catch (error) {
|
||||
console.error(`Error writing ${key} to localStorage:`, error);
|
||||
}
|
||||
}
|
||||
|
||||
export const clearLocalStorage = (key: string): void => {
|
||||
try {
|
||||
localStorage.removeItem(key);
|
||||
}
|
||||
catch (error) {
|
||||
console.error(`Error clearing ${key} from localStorage:`, error);
|
||||
}
|
||||
}
|
||||
|
||||
const flightParametersDefaults: FlightParameters = {
|
||||
ascent_rate: 5.0,
|
||||
burst_altitude: 30000.0,
|
||||
dataset: "",
|
||||
|
|
@ -14,10 +46,24 @@ export const flightParametersStore = writable<FlightParameters>({
|
|||
launch_longitude: 129.1234,
|
||||
profile: "standard_profile",
|
||||
version: 2,
|
||||
});
|
||||
};
|
||||
|
||||
export const latestTelemetry = writable({} as Telemetry);
|
||||
export const latestTelemetryParsed = writable({} as ParsedTelemetry);
|
||||
export const FlightParametersStore = writable<FlightParameters>(
|
||||
readLocalStorage<FlightParameters>("flightParameters", flightParametersDefaults)
|
||||
);
|
||||
|
||||
export const latestPrediction = writable({} as Prediction);
|
||||
export const latestPredictionParsed = writable({} as ParsedPrediction);
|
||||
export const RawTelemetryStore = writable<RawTelemetry>(
|
||||
readLocalStorage<RawTelemetry>("rawTelemetry", {} as RawTelemetry)
|
||||
);
|
||||
|
||||
export const TelemetryStore = writable<Telemetry>(
|
||||
readLocalStorage<Telemetry>("telemetry", {} as Telemetry)
|
||||
);
|
||||
|
||||
export const RawPredictionStore = writable<RawPrediction>(
|
||||
readLocalStorage<RawPrediction>("rawPrediction", {} as RawPrediction)
|
||||
);
|
||||
|
||||
export const PredictionStore = writable<Prediction>(
|
||||
readLocalStorage<Prediction>("prediction", {} as Prediction)
|
||||
);
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
import { writable } from "svelte/store"
|
||||
import L from "leaflet";
|
||||
|
||||
import type { TelemetryPoint, ParsedTelemetry } from "./types";
|
||||
import type { TelemetryPoint, Telemetry } from "./types";
|
||||
|
||||
export function parseTelemetry(telemetry: TelemetryPoint[]): ParsedTelemetry {
|
||||
export function parseTelemetry(telemetry: TelemetryPoint[]): Telemetry {
|
||||
const flight_path: [number, number, number][] = telemetry.map((point) => [
|
||||
point.latitude,
|
||||
point.longitude,
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
import type { LatLngExpression } from "leaflet";
|
||||
import type { LatLngExpression, LatLngLiteral } from "leaflet";
|
||||
|
||||
export const PROFILE_MAP = {
|
||||
Normal: "standard_profile",
|
||||
|
|
@ -31,7 +31,17 @@ export interface TelemetryPoint {
|
|||
payload: string;
|
||||
}
|
||||
|
||||
export interface ParsedTelemetry {
|
||||
export interface TelemetryMetadata {
|
||||
complete_datetime: string;
|
||||
start_datetime: string;
|
||||
}
|
||||
|
||||
export interface RawTelemetry {
|
||||
metadata: TelemetryMetadata;
|
||||
telemetry: TelemetryPoint[];
|
||||
}
|
||||
|
||||
export interface Telemetry {
|
||||
flight_path: [number, number, number][];
|
||||
launch: {
|
||||
latlng: LatLngExpression;
|
||||
|
|
@ -40,15 +50,6 @@ export interface ParsedTelemetry {
|
|||
datapoints: TelemetryPoint[];
|
||||
}
|
||||
|
||||
export interface ParsedTelemetryMetadata {
|
||||
complete_datetime: string;
|
||||
start_datetime: string;
|
||||
}
|
||||
|
||||
export interface Telemetry {
|
||||
metadata: ParsedTelemetryMetadata;
|
||||
telemetry: TelemetryPoint[];
|
||||
}
|
||||
|
||||
export interface TrajectoryPoint {
|
||||
altitude: number;
|
||||
|
|
@ -62,7 +63,17 @@ export interface PredictionStage {
|
|||
trajectory: TrajectoryPoint[];
|
||||
}
|
||||
|
||||
export interface ParsedPrediction {
|
||||
export interface PredictionMetadata {
|
||||
complete_datetime: string;
|
||||
start_datetime: string;
|
||||
}
|
||||
|
||||
export interface RawPrediction {
|
||||
metadata: PredictionMetadata;
|
||||
prediction: PredictionStage[];
|
||||
}
|
||||
|
||||
export interface Prediction {
|
||||
flight_path: [number, number, number][];
|
||||
launch: {
|
||||
latlng: LatLngExpression;
|
||||
|
|
@ -80,12 +91,21 @@ export interface ParsedPrediction {
|
|||
flight_time: number;
|
||||
}
|
||||
|
||||
export interface ParsedPredictionMetadata {
|
||||
complete_datetime: string;
|
||||
start_datetime: string;
|
||||
export interface Point {
|
||||
latlng: LatLngLiteral & { alt: number };
|
||||
datetime: Date;
|
||||
}
|
||||
|
||||
export interface Prediction {
|
||||
metadata: ParsedPredictionMetadata;
|
||||
prediction: PredictionStage[];
|
||||
export interface PredictionData {
|
||||
launch: Point;
|
||||
landing: Point;
|
||||
burst: Point;
|
||||
flight_path: LatLngExpression[];
|
||||
flight_time: number;
|
||||
}
|
||||
|
||||
export interface TelemetryData {
|
||||
launch: Point;
|
||||
datapoints: TelemetryPoint[];
|
||||
flight_path: LatLngExpression[];
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue