mirror of
https://github.com/ckaczor/HomeMonitor.git
synced 2026-06-21 04:35:07 -04:00
Switch new display to Vue
This commit is contained in:
68
WebDisplay/src/stores/weatherStore.ts
Normal file
68
WebDisplay/src/stores/weatherStore.ts
Normal file
@@ -0,0 +1,68 @@
|
||||
import { defineStore } from 'pinia';
|
||||
import { HubConnection, HubConnectionBuilder } from '@microsoft/signalr';
|
||||
import axios from 'axios';
|
||||
import Environment from '@/environment';
|
||||
import WeatherUpdate from '@/models/weather/weather-update';
|
||||
import WeatherRecent from '@/models/weather/weather-recent';
|
||||
import WeatherValueType from '@/models/weather/weather-value-type';
|
||||
import WeatherValueGrouped from '@/models/weather/weather-value-grouped';
|
||||
|
||||
export const useWeatherStore = defineStore('weather', {
|
||||
state: () => {
|
||||
return {
|
||||
current: null as WeatherUpdate | null,
|
||||
_connection: null as HubConnection | null
|
||||
};
|
||||
},
|
||||
actions: {
|
||||
async start() {
|
||||
if (this._connection) {
|
||||
return;
|
||||
}
|
||||
|
||||
this._connection = new HubConnectionBuilder()
|
||||
.withUrl(Environment.getUrlPrefix() + '/api/hub/weather', {
|
||||
withCredentials: false
|
||||
})
|
||||
.build();
|
||||
|
||||
await this._connection.start();
|
||||
|
||||
this._connection.on('LatestReading', (message: string) => {
|
||||
this.$patch({ current: JSON.parse(message) });
|
||||
});
|
||||
},
|
||||
async stop() {
|
||||
if (!this._connection) {
|
||||
return;
|
||||
}
|
||||
|
||||
await this._connection.stop();
|
||||
|
||||
this._connection = null;
|
||||
},
|
||||
async getLatest(): Promise<WeatherRecent> {
|
||||
const response = await axios.get<WeatherRecent>(
|
||||
Environment.getUrlPrefix() + `/api/weather/readings/recent`
|
||||
);
|
||||
|
||||
return response.data;
|
||||
},
|
||||
async getReadingValueHistoryGrouped(
|
||||
valueType: WeatherValueType,
|
||||
start: Date,
|
||||
end: Date,
|
||||
bucketMinutes: number
|
||||
): Promise<WeatherValueGrouped[]> {
|
||||
const startString = start.toISOString();
|
||||
const endString = end.toISOString();
|
||||
|
||||
const response = await axios.get<WeatherValueGrouped[]>(
|
||||
Environment.getUrlPrefix() +
|
||||
`/api/weather/readings/value-history-grouped?weatherValueType=${valueType}&start=${startString}&end=${endString}&bucketMinutes=${bucketMinutes}`
|
||||
);
|
||||
|
||||
return response.data;
|
||||
}
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user