X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2Fui-server%2Fui-services%2FAbstractUIService.ts;h=9d044a09e2a0ce11a0514884fa2e58ca3e64d6c6;hb=55ae7b758f478a2beb4557bbc96363fb913dcc73;hp=448734ba70583b5de1e0997e6e5b618028ab499e;hpb=66a7748ddeda8c94d7562a1ce58d440319654a4c;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/ui-server/ui-services/AbstractUIService.ts b/src/charging-station/ui-server/ui-services/AbstractUIService.ts index 448734ba..9d044a09 100644 --- a/src/charging-station/ui-server/ui-services/AbstractUIService.ts +++ b/src/charging-station/ui-server/ui-services/AbstractUIService.ts @@ -12,7 +12,7 @@ import { type ResponsePayload, ResponseStatus } from '../../../types/index.js' -import { isNotEmptyArray, isNullOrUndefined, logger } from '../../../utils/index.js' +import { isAsyncFunction, isNotEmptyArray, logger } from '../../../utils/index.js' import { Bootstrap } from '../../Bootstrap.js' import { UIServiceWorkerBroadcastChannel } from '../../broadcast-channel/UIServiceWorkerBroadcastChannel.js' import type { AbstractUIServer } from '../AbstractUIServer.js' @@ -65,7 +65,9 @@ export abstract class AbstractUIService { this.uiServer = uiServer this.version = version this.requestHandlers = new Map([ + [ProcedureName.LIST_TEMPLATES, this.handleListTemplates.bind(this)], [ProcedureName.LIST_CHARGING_STATIONS, this.handleListChargingStations.bind(this)], + [ProcedureName.ADD_CHARGING_STATIONS, this.handleAddChargingStations.bind(this)], [ProcedureName.START_SIMULATOR, this.handleStartSimulator.bind(this)], [ProcedureName.STOP_SIMULATOR, this.handleStopSimulator.bind(this)] ]) @@ -93,7 +95,18 @@ export abstract class AbstractUIService { // Call the request handler to build the response payload // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - responsePayload = await this.requestHandlers.get(command)!(messageId, command, requestPayload) + const requestHandler = this.requestHandlers.get(command)! + if (isAsyncFunction(requestHandler)) { + responsePayload = await requestHandler(messageId, command, requestPayload) + } else { + responsePayload = ( + requestHandler as ( + uuid?: string, + procedureName?: ProcedureName, + payload?: RequestPayload + ) => undefined | ResponsePayload + )(messageId, command, requestPayload) + } } catch (error) { // Log logger.error(`${this.logPrefix(moduleName, 'requestHandler')} Handle request error:`, error) @@ -106,11 +119,11 @@ export abstract class AbstractUIService { errorMessage: (error as OCPPError).message, errorStack: (error as OCPPError).stack, errorDetails: (error as OCPPError).details - } + } satisfies ResponsePayload } - if (!isNullOrUndefined(responsePayload)) { + if (responsePayload != null) { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - return this.uiServer.buildProtocolResponse(messageId!, responsePayload!) + return this.uiServer.buildProtocolResponse(messageId!, responsePayload) } } @@ -163,8 +176,8 @@ export abstract class AbstractUIService { ): void { if (isNotEmptyArray(payload.hashIds)) { payload.hashIds = payload.hashIds - ?.map((hashId) => { - if (hashId !== undefined && this.uiServer.chargingStations.has(hashId)) { + .map(hashId => { + if (this.uiServer.chargingStations.has(hashId)) { return hashId } logger.warn( @@ -175,7 +188,7 @@ export abstract class AbstractUIService { ) return undefined }) - ?.filter((hashId) => !isNullOrUndefined(hashId)) as string[] + .filter(hashId => hashId != null) as string[] } else { delete payload.hashIds } @@ -191,6 +204,13 @@ export abstract class AbstractUIService { this.broadcastChannelRequests.set(uuid, expectedNumberOfResponses) } + private handleListTemplates (): ResponsePayload { + return { + status: ResponseStatus.SUCCESS, + templates: [...this.uiServer.chargingStationTemplates.values()] as JsonType[] + } satisfies ResponsePayload + } + private handleListChargingStations (): ResponsePayload { return { status: ResponseStatus.SUCCESS, @@ -198,6 +218,40 @@ export abstract class AbstractUIService { } satisfies ResponsePayload } + private async handleAddChargingStations ( + messageId?: string, + procedureName?: ProcedureName, + requestPayload?: RequestPayload + ): Promise { + const { template, numberOfStations } = requestPayload as { + template: string + numberOfStations: number + } + if (!this.uiServer.chargingStationTemplates.has(template)) { + return { + status: ResponseStatus.FAILURE, + errorMessage: `Template '${template}' not found` + } satisfies ResponsePayload + } + for (let i = 0; i < numberOfStations; i++) { + try { + await Bootstrap.getInstance().addChargingStation( + Bootstrap.getInstance().getLastIndex(template) + 1, + `${template}.json` + ) + } catch (error) { + return { + status: ResponseStatus.FAILURE, + errorMessage: (error as Error).message, + errorStack: (error as Error).stack + } satisfies ResponsePayload + } + } + return { + status: ResponseStatus.SUCCESS + } + } + private async handleStartSimulator (): Promise { try { await Bootstrap.getInstance().start()