u
This commit is contained in:
25
uiserver/api/devices.js
Normal file
25
uiserver/api/devices.js
Normal file
@@ -0,0 +1,25 @@
|
||||
/**
|
||||
* 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 });
|
||||
}
|
||||
});
|
||||
};
|
||||
Reference in New Issue
Block a user