u
This commit is contained in:
@@ -266,6 +266,49 @@ module.exports = {
|
||||
}
|
||||
});
|
||||
|
||||
// Output bindings: map virtual outputs to physical devices
|
||||
// Format: outputChannel -> { device, channel, type }
|
||||
const OUTPUT_BINDINGS = {
|
||||
'BigDehumid': { device: 'tapo', channel: 'r0', type: 'switch' },
|
||||
'CO2Valve': { device: 'tapo', channel: 'c', type: 'switch' },
|
||||
};
|
||||
|
||||
// GET /api/outputs/commands - Get desired states for bound devices
|
||||
// Agents poll this to get commands. Returns { "device:channel": { state: 0|1 } }
|
||||
app.get('/api/outputs/commands', (req, res) => {
|
||||
try {
|
||||
if (!db) throw new Error('Database not connected');
|
||||
|
||||
// Get current output values
|
||||
const stmt = db.prepare(`
|
||||
SELECT channel, value FROM output_events
|
||||
WHERE id IN (
|
||||
SELECT MAX(id) FROM output_events GROUP BY channel
|
||||
)
|
||||
`);
|
||||
const rows = stmt.all();
|
||||
const outputValues = {};
|
||||
rows.forEach(row => {
|
||||
outputValues[row.channel] = row.value;
|
||||
});
|
||||
|
||||
// Map to device commands
|
||||
const commands = {};
|
||||
for (const [outputChannel, binding] of Object.entries(OUTPUT_BINDINGS)) {
|
||||
const value = outputValues[outputChannel] ?? 0;
|
||||
const deviceKey = `${binding.device}:${binding.channel}`;
|
||||
commands[deviceKey] = {
|
||||
state: value > 0 ? 1 : 0,
|
||||
source: outputChannel
|
||||
};
|
||||
}
|
||||
|
||||
res.json(commands);
|
||||
} catch (err) {
|
||||
res.status(500).json({ error: err.message });
|
||||
}
|
||||
});
|
||||
|
||||
// GET /api/rules - List all rules
|
||||
app.get('/api/rules', (req, res) => {
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user