feat(actions): implement action system for UPS state management with shutdown, webhook, and script actions
This commit is contained in:
91
ts/actions/index.ts
Normal file
91
ts/actions/index.ts
Normal file
@@ -0,0 +1,91 @@
|
||||
/**
|
||||
* Action system exports and ActionManager
|
||||
*
|
||||
* This module provides the central coordination for the action system.
|
||||
* The ActionManager is responsible for creating and executing actions.
|
||||
*/
|
||||
|
||||
import { logger } from '../logger.ts';
|
||||
import type { Action, IActionConfig, IActionContext } from './base-action.ts';
|
||||
import { ShutdownAction } from './shutdown-action.ts';
|
||||
import { WebhookAction } from './webhook-action.ts';
|
||||
import { ScriptAction } from './script-action.ts';
|
||||
|
||||
// Re-export types for convenience
|
||||
export type { IActionConfig, IActionContext, TPowerStatus } from './base-action.ts';
|
||||
export { Action } from './base-action.ts';
|
||||
export { ShutdownAction } from './shutdown-action.ts';
|
||||
export { WebhookAction } from './webhook-action.ts';
|
||||
export { ScriptAction } from './script-action.ts';
|
||||
|
||||
/**
|
||||
* ActionManager - Coordinates action creation and execution
|
||||
*
|
||||
* Provides factory methods for creating actions from configuration
|
||||
* and orchestrates action execution with error handling.
|
||||
*/
|
||||
export class ActionManager {
|
||||
/**
|
||||
* Create an action instance from configuration
|
||||
* @param config Action configuration
|
||||
* @returns Instantiated action
|
||||
* @throws Error if action type is unknown
|
||||
*/
|
||||
static createAction(config: IActionConfig): Action {
|
||||
switch (config.type) {
|
||||
case 'shutdown':
|
||||
return new ShutdownAction(config);
|
||||
case 'webhook':
|
||||
return new WebhookAction(config);
|
||||
case 'script':
|
||||
return new ScriptAction(config);
|
||||
default:
|
||||
throw new Error(`Unknown action type: ${(config as IActionConfig).type}`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute a sequence of actions with the given context
|
||||
* Each action runs sequentially, and failures are logged but don't stop the chain
|
||||
* @param actions Array of action configurations to execute
|
||||
* @param context Action context with UPS state
|
||||
*/
|
||||
static async executeActions(
|
||||
actions: IActionConfig[],
|
||||
context: IActionContext,
|
||||
): Promise<void> {
|
||||
if (!actions || actions.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
logger.log('');
|
||||
logger.logBoxTitle(`Executing ${actions.length} Action(s)`, 60, 'info');
|
||||
logger.logBoxLine(`Trigger: ${context.triggerReason}`);
|
||||
logger.logBoxLine(`UPS: ${context.upsName} (${context.upsId})`);
|
||||
logger.logBoxLine(`Power: ${context.powerStatus}`);
|
||||
logger.logBoxLine(`Battery: ${context.batteryCapacity}% / ${context.batteryRuntime} min`);
|
||||
logger.logBoxEnd();
|
||||
logger.log('');
|
||||
|
||||
for (let i = 0; i < actions.length; i++) {
|
||||
const actionConfig = actions[i];
|
||||
try {
|
||||
logger.info(`[${i + 1}/${actions.length}] ${actionConfig.type} action...`);
|
||||
|
||||
const action = this.createAction(actionConfig);
|
||||
await action.execute(context);
|
||||
} catch (error) {
|
||||
logger.error(
|
||||
`Action ${actionConfig.type} failed: ${
|
||||
error instanceof Error ? error.message : String(error)
|
||||
}`,
|
||||
);
|
||||
// Continue with next action despite failure
|
||||
}
|
||||
}
|
||||
|
||||
logger.log('');
|
||||
logger.success('Action execution completed');
|
||||
logger.log('');
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user