diff --git a/WebApp/src/dashboard-page.tsx b/WebApp/src/dashboard-page.tsx index 307b602..3d914a0 100644 --- a/WebApp/src/dashboard-page.tsx +++ b/WebApp/src/dashboard-page.tsx @@ -2,11 +2,11 @@ import m from 'mithril'; import { MonitorApi, Status } from './monitor-api'; export default class DashboardPage { - api = new MonitorApi(); - status?: Status; + status: Status = null; autoRefresh = true; oninit() { + this.status = MonitorApi.get().getStatus(); this.refresh(); } @@ -15,7 +15,7 @@ export default class DashboardPage { } async refresh() { - this.status = await this.api.getStatus(); + this.status = await MonitorApi.get().fetchStatus(); if(this.autoRefresh) setTimeout(() => { if(this.autoRefresh) this.refresh(); }, 500); } diff --git a/WebApp/src/monitor-api.ts b/WebApp/src/monitor-api.ts index 4e11226..3441ded 100644 --- a/WebApp/src/monitor-api.ts +++ b/WebApp/src/monitor-api.ts @@ -18,12 +18,22 @@ interface ApiStatus { export class MonitorApi { private mockServer: boolean; + private lastStatus: Status = null; + private static api: MonitorApi = null; constructor() { this.mockServer = window.location.protocol == "file:"; } - async getStatus(): Promise { + static get() { + if(MonitorApi.api == null) + MonitorApi.api = new MonitorApi(); + return MonitorApi.api; + } + + getStatus() { return this.lastStatus; } + + async fetchStatus(): Promise { let apiStatus: ApiStatus; if(this.mockServer) { @@ -43,12 +53,14 @@ export class MonitorApi { }); } - return { + this.lastStatus = { batteryVoltage: apiStatus.v / 1000, motorCurrent: apiStatus.c / 1000, speed: apiStatus.s / 1000, temperature: apiStatus.t / 10, altitude: apiStatus.alt / 1000 }; + + return this.lastStatus; } } diff --git a/WebApp/src/raw-data-page.tsx b/WebApp/src/raw-data-page.tsx index ed4b273..a52a780 100644 --- a/WebApp/src/raw-data-page.tsx +++ b/WebApp/src/raw-data-page.tsx @@ -4,11 +4,11 @@ import { MonitorApi, Status } from './monitor-api'; require("./raw-data-page.css"); export default class RawDataPage { - api = new MonitorApi(); - status?: Status; + status: Status = null; autoRefresh = true; oninit() { + this.status = MonitorApi.get().getStatus(); this.refresh(); } @@ -17,7 +17,7 @@ export default class RawDataPage { } async refresh() { - this.status = await this.api.getStatus(); + this.status = await MonitorApi.get().fetchStatus(); if(this.autoRefresh) setTimeout(() => { if(this.autoRefresh) this.refresh(); }, 500); }