This commit is contained in:
legop3
2026-04-29 18:45:41 -04:00
parent 01a8d044e7
commit 9bf449ca19
12 changed files with 219 additions and 127 deletions
@@ -0,0 +1,86 @@
// Idle Service Actions
// Purpose: Implements the expandable action pipeline executed when the system remains idle.
// Scope: Provides isolated action handlers and shared execution helpers for idle automation.
const logger = require('../../globals/logger').child('idleService');
const roverManager = require('../roverManager');
const { issueCommand } = require('../commandService');
const homeAssistantService = require('../homeAssistantService');
const neatoService = require('../neatoService');
const {
NIGHT_VISION_DISABLE_ACTION,
DOCK_COMMAND_BASE64,
} = require('./constants');
async function turnOffRoomControls() {
await homeAssistantService.setAllControllableEntitiesState('off');
return { action: 'roomControlsOff' };
}
async function dockAllRovers() {
const attempted = [];
const failed = [];
roverManager.rovers.forEach((record) => {
if (!record?.ws) return;
const roverId = String(record.id);
try {
issueCommand(roverId, { type: 'raw', raw: DOCK_COMMAND_BASE64 });
attempted.push(roverId);
} catch (err) {
failed.push({ roverId, error: err.message });
}
});
return { action: 'dockAllRovers', attempted, failed };
}
async function disableAllRoverNightVision() {
const attempted = [];
const failed = [];
roverManager.rovers.forEach((record) => {
if (!record?.ws) return;
const roverId = String(record.id);
try {
issueCommand(roverId, {
type: 'nightVision',
nightVision: { action: NIGHT_VISION_DISABLE_ACTION },
});
attempted.push(roverId);
} catch (err) {
failed.push({ roverId, error: err.message });
}
});
return { action: 'disableRoverNightVision', attempted, failed };
}
async function sendNeatoHome() {
try {
await neatoService.sendHome();
return { action: 'neatoSendHome', success: true };
} catch (err) {
return { action: 'neatoSendHome', success: false, error: err.message };
}
}
const idleActions = [
turnOffRoomControls,
dockAllRovers,
disableAllRoverNightVision,
sendNeatoHome,
];
async function runIdleActions() {
const results = [];
for (const action of idleActions) {
try {
const result = await action();
results.push({ ok: true, result });
} catch (err) {
logger.warn('Idle action failed', { action: action.name, error: err.message });
results.push({ ok: false, action: action.name, error: err.message });
}
}
return results;
}
module.exports = {
runIdleActions,
};