Files
tischlerCtrl/uiserver/api/devices.js
sebseb7 758684c598 u
2025-12-26 01:41:49 +01:00

26 lines
943 B
JavaScript

/**
* Devices API - List unique device/channel pairs
*/
module.exports = function setupDevicesApi(app, { db, OUTPUT_CHANNELS }) {
// GET /api/devices - Returns list of unique device/channel pairs (sensors + outputs)
app.get('/api/devices', (req, res) => {
try {
if (!db) throw new Error('Database not connected');
// Get sensor channels
const sensorStmt = db.prepare("SELECT DISTINCT device, channel FROM sensor_events WHERE data_type = 'number' ORDER BY device, channel");
const sensorRows = sensorStmt.all();
// Add output channels with 'output' as device
const outputRows = OUTPUT_CHANNELS.map(ch => ({
device: 'output',
channel: ch.channel
}));
res.json([...sensorRows, ...outputRows]);
} catch (err) {
res.status(500).json({ error: err.message });
}
});
};