Refactor HomeView to use energyStore for energy data Refactor HomeView

to use energyStore for energy data
This commit is contained in:
rafaeldpsilva
2025-10-01 12:57:09 +01:00
parent 6ee4801071
commit cb659c93bb
5 changed files with 90 additions and 41 deletions

View File

@@ -14,8 +14,8 @@ export const useSensorStore = defineStore('sensor', () => {
const latestReadings = reactive<Map<string, SensorReading>>(new Map())
const sensorsData = reactive<Map<string, any>>(new Map()) // Legacy support
const recentlyUpdatedSensors = reactive<Set<string>>(new Set()) // Track recently updated sensors
const totalReadings = ref(0) // Total number of readings across all sensors
const apiLoading = ref(false)
const totalReadings = ref<number>(0) // Total number of readings across all sensors
const apiLoading = ref<boolean>(false)
const apiError = ref<string | null>(null)
// Computed properties
@@ -23,12 +23,12 @@ export const useSensorStore = defineStore('sensor', () => {
const activeSensors = computed(() => {
return Array.from(sensorDevices.values()).filter(
sensor => sensor.status === 'active' || sensor.status === 'online'
(sensor) => sensor.status === 'active' || sensor.status === 'online',
).length
})
// Actions
function updateSensorRoom(sensorId: string, newRoom: string) {
function updateSensorRoom(sensorId: string, newRoom: string): void {
const sensor = sensorDevices.get(sensorId)
if (sensor) {
sensor.room = newRoom
@@ -36,14 +36,14 @@ export const useSensorStore = defineStore('sensor', () => {
}
}
async function executeSensorAction(sensorId: string, actionId: string) {
async function executeSensorAction(sensorId: string, actionId: string): Promise<boolean> {
const sensor = sensorDevices.get(sensorId)
if (!sensor) return false
const action = sensor.capabilities.actions.find((a) => a.id === actionId)
if (!action) return false
return new Promise((resolve) => {
return new Promise<boolean>((resolve) => {
setTimeout(() => {
console.log(`Action ${action.name} executed successfully on ${sensor.name}`)
resolve(true)
@@ -59,11 +59,11 @@ export const useSensorStore = defineStore('sensor', () => {
return Array.from(sensorDevices.values()).filter((sensor) => sensor.type === type)
}
function updateEnergySensors(data: Sensor) {
function updateEnergySensors(data: SensorReading): void {
console.log(data)
}
function updateLatestReading(reading: SensorReading) {
function updateLatestReading(reading: SensorReading): void {
latestReadings.set(reading.sensor_id, reading)
// Increment total readings count
@@ -191,12 +191,12 @@ export const useSensorStore = defineStore('sensor', () => {
console.log(result)
// Check if result has a sensors property (common API pattern)
if (result.sensors && Array.isArray(result.sensors)) {
let totalReadingsCount = 0
let totalReadingsCount: number = 0
result.sensors.forEach((sensor) => {
const sensorKey = sensor._id || sensor.sensor_id
const sensorType = sensor.sensor_type || sensor.type
const sensorName = sensor.name || ''
const sensorKey: string = sensor._id || sensor.sensor_id
const sensorType: string = sensor.sensor_type || sensor.type
const sensorName: string = sensor.name || ''
// Accumulate total readings
if (sensor.total_readings) {