Refactor sensor data handling for new API and WebSocket format
- Update SensorConsumptionTable to use new sensorStore and websocketStore - Normalize sensor and reading interfaces for consistency - Remove legacy energy data handling and mapping logic - Update API and store types for new backend schema - Fetch sensors on mount in SensorConsumptionTable - Simplify WebSocket data processing and remove legacy code
This commit is contained in:
@@ -3,37 +3,11 @@ import { ref, reactive } from 'vue'
|
||||
import {
|
||||
sensorsApi,
|
||||
SensorType,
|
||||
SensorStatus,
|
||||
type SensorDevice,
|
||||
type SensorAction,
|
||||
type SensorStatus,
|
||||
type SensorReading,
|
||||
} from '@/services'
|
||||
|
||||
interface SensorReading {
|
||||
id: string
|
||||
sensorId: string
|
||||
room: string
|
||||
timestamp: number
|
||||
energy: {
|
||||
value: number
|
||||
unit: string
|
||||
}
|
||||
co2: {
|
||||
value: number
|
||||
unit: string
|
||||
}
|
||||
temperature?: {
|
||||
value: number
|
||||
unit: string
|
||||
}
|
||||
}
|
||||
|
||||
interface LegacyEnergyData {
|
||||
sensorId: string
|
||||
timestamp: number
|
||||
value: number
|
||||
unit: string
|
||||
}
|
||||
|
||||
export const useSensorStore = defineStore('sensor', () => {
|
||||
// State
|
||||
const sensorDevices = reactive<Map<string, SensorDevice>>(new Map())
|
||||
@@ -52,7 +26,7 @@ export const useSensorStore = defineStore('sensor', () => {
|
||||
}
|
||||
}
|
||||
|
||||
async function executeSensorAction(sensorId: string, actionId: string, parameters?: any) {
|
||||
async function executeSensorAction(sensorId: string, actionId: string) {
|
||||
const sensor = sensorDevices.get(sensorId)
|
||||
if (!sensor) return false
|
||||
|
||||
@@ -75,49 +49,18 @@ export const useSensorStore = defineStore('sensor', () => {
|
||||
return Array.from(sensorDevices.values()).filter((sensor) => sensor.type === type)
|
||||
}
|
||||
|
||||
function updateSensorData(data: LegacyEnergyData) {
|
||||
const existingSensor = sensorsData.get(data.sensorId)
|
||||
|
||||
if (existingSensor) {
|
||||
const newTotal = existingSensor.totalConsumption + data.value
|
||||
const dataPoints = Math.floor((data.timestamp - existingSensor.lastUpdated) / 60) + 1
|
||||
|
||||
sensorsData.set(data.sensorId, {
|
||||
...existingSensor,
|
||||
latestValue: data.value,
|
||||
totalConsumption: newTotal,
|
||||
averageConsumption: newTotal / dataPoints,
|
||||
lastUpdated: data.timestamp,
|
||||
})
|
||||
} else {
|
||||
sensorsData.set(data.sensorId, {
|
||||
sensorId: data.sensorId,
|
||||
latestValue: data.value,
|
||||
totalConsumption: data.value,
|
||||
averageConsumption: data.value,
|
||||
lastUpdated: data.timestamp,
|
||||
unit: data.unit,
|
||||
})
|
||||
}
|
||||
|
||||
// Mark sensor as recently updated for legacy data as well
|
||||
recentlyUpdatedSensors.add(data.sensorId)
|
||||
|
||||
// Remove from recently updated after 2 seconds
|
||||
setTimeout(() => {
|
||||
recentlyUpdatedSensors.delete(data.sensorId)
|
||||
}, 2000)
|
||||
function updateEnergySensors(data: Sensor) {
|
||||
console.log(data)
|
||||
}
|
||||
|
||||
function updateLatestReading(reading: SensorReading) {
|
||||
latestReadings.set(reading.sensorId, reading)
|
||||
latestReadings.set(reading.sensor_id, reading)
|
||||
|
||||
// Mark sensor as recently updated
|
||||
recentlyUpdatedSensors.add(reading.sensorId)
|
||||
console.log(reading.sensor_type)
|
||||
recentlyUpdatedSensors.add(reading.sensor_id)
|
||||
// Remove from recently updated after 2 seconds
|
||||
setTimeout(() => {
|
||||
recentlyUpdatedSensors.delete(reading.sensorId)
|
||||
recentlyUpdatedSensors.delete(reading.sensor_id)
|
||||
}, 2000)
|
||||
}
|
||||
|
||||
@@ -225,17 +168,21 @@ export const useSensorStore = defineStore('sensor', () => {
|
||||
}
|
||||
|
||||
// Sensors API functions
|
||||
async function fetchApiSensors(params?: { room?: string; sensor_type?: any; status?: any }) {
|
||||
async function fetchApiSensors(params?: {
|
||||
room?: string
|
||||
sensor_type?: SensorType
|
||||
status?: SensorStatus
|
||||
}) {
|
||||
const result = await handleApiCall(() => sensorsApi.getSensors(params))
|
||||
if (result) {
|
||||
console.log(result)
|
||||
// Check if result has a sensors property (common API pattern)
|
||||
if (result.sensors && Array.isArray(result.sensors)) {
|
||||
result.sensors.forEach((sensor) => {
|
||||
const sensorKey = sensor.id || sensor._id || sensor.sensor_id
|
||||
const sensorKey = sensor._id || sensor.sensor_id
|
||||
const sensorType = sensor.sensor_type || sensor.type
|
||||
const sensorName = sensor.name || ''
|
||||
|
||||
// Normalize sensor data structure for frontend compatibility
|
||||
const normalizedSensor = {
|
||||
...sensor,
|
||||
id: sensorKey,
|
||||
@@ -255,58 +202,18 @@ export const useSensorStore = defineStore('sensor', () => {
|
||||
signalStrength: sensor.metadata?.signalStrength,
|
||||
...sensor.metadata,
|
||||
},
|
||||
tags: sensor.tags || [],
|
||||
lastSeen: sensor.last_seen || sensor.lastSeen || Date.now() / 1000,
|
||||
lastSeen: sensor.last_seen || Date.now() / 1000,
|
||||
}
|
||||
|
||||
sensorDevices.set(sensorKey, normalizedSensor)
|
||||
})
|
||||
}
|
||||
// Check if result is directly an array
|
||||
else if (Array.isArray(result)) {
|
||||
console.log('Result is direct array:', result)
|
||||
result.forEach((sensor) => {
|
||||
console.log('Adding sensor:', sensor)
|
||||
const sensorKey = sensor.id || sensor._id || sensor.sensor_id
|
||||
const sensorType = sensor.sensor_type || sensor.type
|
||||
const sensorName = sensor.name || ''
|
||||
|
||||
// Normalize sensor data structure for frontend compatibility
|
||||
const normalizedSensor = {
|
||||
...sensor,
|
||||
id: sensorKey,
|
||||
type: sensorType,
|
||||
capabilities: {
|
||||
actions: [], // Default empty actions array
|
||||
monitoring:
|
||||
sensor.capabilities?.monitoring ||
|
||||
getDefaultMonitoringCapabilities(sensorType, sensorName),
|
||||
...sensor.capabilities,
|
||||
},
|
||||
metadata: {
|
||||
model: sensor.metadata?.model || 'Unknown',
|
||||
firmware: sensor.metadata?.firmware || 'Unknown',
|
||||
location: sensor.metadata?.location || sensor.room || 'Unknown',
|
||||
battery: sensor.metadata?.battery,
|
||||
signalStrength: sensor.metadata?.signalStrength,
|
||||
...sensor.metadata,
|
||||
},
|
||||
tags: sensor.tags || [],
|
||||
lastSeen: sensor.last_seen || sensor.lastSeen || Date.now() / 1000,
|
||||
}
|
||||
|
||||
sensorDevices.set(sensorKey, normalizedSensor)
|
||||
})
|
||||
}
|
||||
// Log what we actually got
|
||||
else {
|
||||
console.log('Unexpected result format:', typeof result, result)
|
||||
} else {
|
||||
console.warn('Unexpected result format:', typeof result, result)
|
||||
}
|
||||
} else {
|
||||
console.log('No result received from API')
|
||||
console.error('No result received from API')
|
||||
}
|
||||
|
||||
console.log('Current sensor devices:', Array.from(sensorDevices.entries()))
|
||||
return result
|
||||
}
|
||||
|
||||
@@ -344,11 +251,11 @@ export const useSensorStore = defineStore('sensor', () => {
|
||||
apiError,
|
||||
|
||||
// Actions
|
||||
updateEnergySensors,
|
||||
updateSensorRoom,
|
||||
executeSensorAction,
|
||||
getSensorsByRoom,
|
||||
getSensorsByType,
|
||||
updateSensorData,
|
||||
updateLatestReading,
|
||||
|
||||
// API functions
|
||||
|
||||
Reference in New Issue
Block a user