import { NextRequest, NextResponse } from 'next/server'; import { SensorDataStorage } from '@/services/SensorDataStorage'; export async function GET(request: NextRequest) { try { const { searchParams } = new URL(request.url); const sensorId = searchParams.get('sensorId'); const timespan = parseInt(searchParams.get('timespan') || '86400000'); // Default 24 hours in ms if (!sensorId) { return NextResponse.json({ success: false, error: 'sensorId parameter is required' }, { status: 400 }); } const sensorDataStorage = SensorDataStorage.getInstance(); const sensorData = await sensorDataStorage.getDataForSensor(sensorId, timespan); const timeSeriesData = sensorDataStorage.generateTimeSeriesData(sensorData, timespan); return NextResponse.json({ success: true, sensorId, timespan, dataPoints: sensorData.length, data: timeSeriesData, timestamp: new Date().toISOString() }); } catch (error) { console.error('Sensor history API error:', error); return NextResponse.json({ success: false, error: 'Failed to get sensor history data' }, { status: 500 }); } }