mirror of
https://github.com/ckaczor/HomeMonitor.git
synced 2026-01-14 01:25:38 -05:00
Switch new display to Vue
This commit is contained in:
41
WebDisplay/src/stores/powerStore.ts
Normal file
41
WebDisplay/src/stores/powerStore.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import { defineStore } from 'pinia';
|
||||
import { HubConnection, HubConnectionBuilder } from '@microsoft/signalr';
|
||||
import Environment from '@/environment';
|
||||
import PowerStatus from '@/models/power/power-status';
|
||||
|
||||
export const usePowerStore = defineStore('power', {
|
||||
state: () => {
|
||||
return {
|
||||
current: null as PowerStatus | null,
|
||||
_connection: null as HubConnection | null
|
||||
};
|
||||
},
|
||||
actions: {
|
||||
async start() {
|
||||
if (this._connection) {
|
||||
return;
|
||||
}
|
||||
|
||||
this._connection = new HubConnectionBuilder()
|
||||
.withUrl(Environment.getUrlPrefix() + '/api/hub/power', {
|
||||
withCredentials: false
|
||||
})
|
||||
.build();
|
||||
|
||||
await this._connection.start();
|
||||
|
||||
this._connection.on('LatestSample', (message: string) => {
|
||||
this.$patch({ current: JSON.parse(message) });
|
||||
});
|
||||
},
|
||||
async stop() {
|
||||
if (!this._connection) {
|
||||
return;
|
||||
}
|
||||
|
||||
await this._connection.stop();
|
||||
|
||||
this._connection = null;
|
||||
}
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user