Add device-type metrics and improve sensor capability detection

- Show device-specific metrics (e.g. brightness, setpoint) for lighting,
HVAC, and security sensors when no standard monitoring is present - Add
helper to infer monitoring capabilities from sensor type or name -
Animate sensor cards when recently updated - Remove debug console logs
from stores - Normalize sensor data structure and capability defaults in
store
This commit is contained in:
rafaeldpsilva
2025-09-30 15:07:50 +01:00
parent 3681890ec5
commit 5cb87ef5c5
4 changed files with 228 additions and 59 deletions

View File

@@ -59,8 +59,6 @@ export const useSensorStore = defineStore('sensor', () => {
const action = sensor.capabilities.actions.find((a) => a.id === actionId)
if (!action) return false
console.log(`Executing action ${actionId} on sensor ${sensorId}`, parameters)
return new Promise((resolve) => {
setTimeout(() => {
console.log(`Action ${action.name} executed successfully on ${sensor.name}`)
@@ -112,8 +110,6 @@ export const useSensorStore = defineStore('sensor', () => {
}
function updateLatestReading(reading: SensorReading) {
console.log('Updating latest reading for sensor:', reading.sensorId, reading)
latestReadings.set(reading.sensorId, reading)
// Mark sensor as recently updated
@@ -123,20 +119,14 @@ export const useSensorStore = defineStore('sensor', () => {
setTimeout(() => {
recentlyUpdatedSensors.delete(reading.sensorId)
}, 2000)
console.log('Latest readings now contains:', Array.from(latestReadings.keys()))
}
// API Integration Functions
async function handleApiCall<T>(apiCall: () => Promise<T>): Promise<T | null> {
apiLoading.value = true
apiError.value = null
console.log('Making API call...')
try {
const result = await apiCall()
console.log('API call successful:', result)
return result
} catch (error) {
const errorMessage = error instanceof Error ? error.message : 'Unknown error occurred'
@@ -190,6 +180,50 @@ export const useSensorStore = defineStore('sensor', () => {
}
}
// Helper function to get default monitoring capabilities based on sensor type
function getDefaultMonitoringCapabilities(sensorType: string, sensorName: string): string[] {
// First check the sensor name for clues
const nameLower = sensorName?.toLowerCase() || ''
if (nameLower.includes('energy') || nameLower.includes('power')) {
return ['energy']
}
if (nameLower.includes('temperature') || nameLower.includes('temp')) {
return ['temperature']
}
if (nameLower.includes('humidity')) {
return ['humidity']
}
if (nameLower.includes('co2') || nameLower.includes('air quality')) {
return ['co2']
}
if (nameLower.includes('motion')) {
return ['motion']
}
// Then fall back to sensor type
switch (sensorType?.toLowerCase()) {
case 'energy':
return ['energy']
case 'temperature':
return ['temperature']
case 'humidity':
return ['humidity']
case 'co2':
return ['co2']
case 'motion':
return ['motion']
case 'hvac':
return ['temperature']
case 'lighting':
return [] // Lighting sensors don't typically monitor environmental data
case 'security':
return ['motion']
default:
return [] // No default monitoring capabilities
}
}
// Sensors API functions
async function fetchApiSensors(params?: { room?: string; sensor_type?: any; status?: any }) {
const result = await handleApiCall(() => sensorsApi.getSensors(params))
@@ -199,15 +233,19 @@ export const useSensorStore = defineStore('sensor', () => {
if (result.sensors && Array.isArray(result.sensors)) {
result.sensors.forEach((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: sensor.sensor_type || sensor.type,
type: sensorType,
capabilities: {
actions: [], // Default empty actions array
monitoring: sensor.capabilities?.monitoring || ['energy'], // Default monitoring capability
monitoring:
sensor.capabilities?.monitoring ||
getDefaultMonitoringCapabilities(sensorType, sensorName),
...sensor.capabilities,
},
metadata: {
@@ -231,15 +269,19 @@ export const useSensorStore = defineStore('sensor', () => {
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: sensor.sensor_type || sensor.type,
type: sensorType,
capabilities: {
actions: [], // Default empty actions array
monitoring: sensor.capabilities?.monitoring || ['energy'], // Default monitoring capability
monitoring:
sensor.capabilities?.monitoring ||
getDefaultMonitoringCapabilities(sensorType, sensorName),
...sensor.capabilities,
},
metadata: {