type StopTransactionRequest,
type StopTransactionResponse
} from '../../types/index.js'
-import { Constants, convertToInt, isEmptyObject, logger } from '../../utils/index.js'
+import {
+ Constants,
+ convertToInt,
+ isAsyncFunction,
+ isEmptyObject,
+ logger
+} from '../../utils/index.js'
import type { ChargingStation } from '../ChargingStation.js'
import { getConfigurationKey } from '../ConfigurationKeyUtils.js'
import { buildMeterValue } from '../ocpp/index.js'
type CommandHandler = (
requestPayload?: BroadcastChannelRequestPayload
// eslint-disable-next-line @typescript-eslint/no-invalid-void-type
-) => Promise<CommandResponse | void> | void
+) => Promise<CommandResponse | void> | CommandResponse | void
export class ChargingStationWorkerBroadcastChannel extends WorkerBroadcastChannel {
private readonly commandHandlers: Map<BroadcastChannelProcedureName, CommandHandler>
await this.chargingStation.stop()
}
],
+ [
+ BroadcastChannelProcedureName.DELETE_CHARGING_STATIONS,
+ async (requestPayload?: BroadcastChannelRequestPayload) => {
+ await this.chargingStation.delete(requestPayload?.deleteConfiguration as boolean)
+ }
+ ],
[
BroadcastChannelProcedureName.OPEN_CONNECTION,
() => {
return
}
let responsePayload: BroadcastChannelResponsePayload | undefined
- // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
- let commandResponse: CommandResponse | void
this.commandHandler(command, requestPayload)
.then(commandResponse => {
if (commandResponse == null || isEmptyObject(commandResponse)) {
status: ResponseStatus.FAILURE,
command,
requestPayload,
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
- commandResponse: commandResponse!,
errorMessage: (error as OCPPError).message,
errorStack: (error as OCPPError).stack,
errorDetails: (error as OCPPError).details
- }
+ } satisfies BroadcastChannelResponsePayload
})
.finally(() => {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
if (this.commandHandlers.has(command)) {
this.cleanRequestPayload(command, requestPayload)
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
- return await this.commandHandlers.get(command)!(requestPayload)
+ const commandHandler = this.commandHandlers.get(command)!
+ if (isAsyncFunction(commandHandler)) {
+ return await commandHandler(requestPayload)
+ }
+ return (
+ commandHandler as (
+ requestPayload?: BroadcastChannelRequestPayload
+ // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
+ ) => CommandResponse | void
+ )(requestPayload)
}
throw new BaseError(`Unknown worker broadcast channel command: '${command}'`)
}