import { Device } from '../../domain/device/device.js' import { CWMPTaskQueue } from './task_queue.js' import { DeviceActionExecutor } from './device_action_executor.js' import { ExecutingCommandsState, DeviceContext, DeviceStateResult } from './main_state_machine.js' import logger from '@adonisjs/core/services/logger' /** * State machine for "6 CONNECTION REQUEST" events. * Only executes pending Immediate (I) commands - does NOT include NI commands. */ export class ImmediateCommandStateMachine { private currentState: ExecutingCommandsState private context: DeviceContext constructor(device: Device, session: string, taskQueue: CWMPTaskQueue) { // includeNI = false: only execute Immediate commands this.currentState = new ExecutingCommandsState(false) this.context = { device, session, taskQueue, services: { deviceAction: new DeviceActionExecutor(taskQueue), }, } } async start(): Promise { logger.info(`Starting immediate-command state machine for device ${this.context.device.deviceId?.serialNumber}`) return this.execute() } async handleResponse(success: boolean, message: any): Promise { this.context.message = { success, data: message } return this.execute() } getCurrentStateName(): string { return this.currentState.name } isCompleted(): boolean { return this.currentState.name === 'completed' } private async execute(): Promise { try { const result: DeviceStateResult = await this.currentState.handle(this.context) if (result.isComplete) { return '' } if (result.response && result.response !== '') { return result.response } return '' } catch (error) { logger.error(`Immediate-command state machine error: ${error}`) return '' } } }