95 lines
No EOL
2.7 KiB
TypeScript
95 lines
No EOL
2.7 KiB
TypeScript
import { writable } from "svelte/store";
|
|
import type { FlightParameters, RawTelemetry, Telemetry } from "./types";
|
|
import type { RawPrediction, Prediction } from "./types";
|
|
import type { SavedPoint, SavedFlightProfile, SavedScenario } from "./types";
|
|
|
|
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);
|
|
}
|
|
}
|
|
|
|
export const flightParametersDefaults: FlightParameters = {
|
|
ascent_rate: 5.0,
|
|
burst_altitude: 30000.0,
|
|
dataset: "",
|
|
descent_rate: 5.0,
|
|
format: "json",
|
|
launch_altitude: 0.0,
|
|
launch_latitude: 62.1234,
|
|
launch_longitude: 129.1234,
|
|
profile: "standard_profile",
|
|
version: 2,
|
|
};
|
|
|
|
export const FlightParametersStore = writable<FlightParameters>(
|
|
readLocalStorage<FlightParameters>("flightParameters", flightParametersDefaults)
|
|
);
|
|
|
|
export const templateDataDefaults = {
|
|
description: "",
|
|
prediction_mode: "",
|
|
model: "",
|
|
dataset: "",
|
|
flight_parameters: flightParametersDefaults,
|
|
};
|
|
|
|
export const scenarioDefaults: SavedScenario = {
|
|
id: -1,
|
|
name: "Новый сценарий",
|
|
...templateDataDefaults,
|
|
}
|
|
|
|
export const ScenarioStore = writable<SavedScenario>(
|
|
readLocalStorage<SavedScenario>("scenario", scenarioDefaults as SavedScenario)
|
|
);
|
|
|
|
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)
|
|
);
|
|
|
|
export const SavedPointsStore = writable<SavedPoint[]>([]);
|
|
|
|
// stub
|
|
export const SavedFlightProfilesStore = writable<SavedFlightProfile[]>([]);
|
|
|
|
// stub
|
|
export const SavedScenarioStore = writable<SavedScenario[]>([]); |