| 1 | import BaseError from '../exception/BaseError'; |
| 2 | import { RequestCommand } from '../types/ocpp/Requests'; |
| 3 | import { |
| 4 | AuthorizationStatus, |
| 5 | StartTransactionRequest, |
| 6 | StartTransactionResponse, |
| 7 | StopTransactionReason, |
| 8 | StopTransactionRequest, |
| 9 | StopTransactionResponse, |
| 10 | } from '../types/ocpp/Transaction'; |
| 11 | import { |
| 12 | BroadcastChannelProcedureName, |
| 13 | BroadcastChannelRequest, |
| 14 | BroadcastChannelRequestPayload, |
| 15 | BroadcastChannelResponsePayload, |
| 16 | MessageEvent, |
| 17 | } from '../types/WorkerBroadcastChannel'; |
| 18 | import { ResponseStatus } from '../ui/web/src/type/UIProtocol'; |
| 19 | import logger from '../utils/Logger'; |
| 20 | import type ChargingStation from './ChargingStation'; |
| 21 | import WorkerBroadcastChannel from './WorkerBroadcastChannel'; |
| 22 | |
| 23 | const moduleName = 'ChargingStationWorkerBroadcastChannel'; |
| 24 | |
| 25 | type CommandResponse = StartTransactionResponse | StopTransactionResponse; |
| 26 | |
| 27 | export default class ChargingStationWorkerBroadcastChannel extends WorkerBroadcastChannel { |
| 28 | private readonly chargingStation: ChargingStation; |
| 29 | |
| 30 | constructor(chargingStation: ChargingStation) { |
| 31 | super(); |
| 32 | this.chargingStation = chargingStation; |
| 33 | this.onmessage = this.requestHandler.bind(this) as (message: MessageEvent) => void; |
| 34 | this.onmessageerror = this.messageErrorHandler.bind(this) as (message: MessageEvent) => void; |
| 35 | } |
| 36 | |
| 37 | private async requestHandler(messageEvent: MessageEvent): Promise<void> { |
| 38 | if (this.isResponse(messageEvent.data)) { |
| 39 | return; |
| 40 | } |
| 41 | if (Array.isArray(messageEvent.data) === false) { |
| 42 | throw new BaseError('Worker broadcast channel protocol request is not an array'); |
| 43 | } |
| 44 | |
| 45 | const [uuid, command, requestPayload] = messageEvent.data as BroadcastChannelRequest; |
| 46 | |
| 47 | if (requestPayload?.hashId !== this.chargingStation.hashId) { |
| 48 | return; |
| 49 | } |
| 50 | |
| 51 | let responsePayload: BroadcastChannelResponsePayload; |
| 52 | let commandResponse: CommandResponse; |
| 53 | try { |
| 54 | commandResponse = await this.commandHandler(command, requestPayload); |
| 55 | if (commandResponse === undefined) { |
| 56 | responsePayload = { status: ResponseStatus.SUCCESS }; |
| 57 | } else { |
| 58 | responsePayload = { status: this.commandResponseToResponseStatus(commandResponse) }; |
| 59 | } |
| 60 | } catch (error) { |
| 61 | logger.error( |
| 62 | `${this.chargingStation.logPrefix()} ${moduleName}.requestHandler: Handle request error:`, |
| 63 | error |
| 64 | ); |
| 65 | responsePayload = { |
| 66 | status: ResponseStatus.FAILURE, |
| 67 | command, |
| 68 | requestPayload, |
| 69 | commandResponse, |
| 70 | errorMessage: (error as Error).message, |
| 71 | errorStack: (error as Error).stack, |
| 72 | }; |
| 73 | } |
| 74 | this.sendResponse([uuid, responsePayload]); |
| 75 | } |
| 76 | |
| 77 | private messageErrorHandler(messageEvent: MessageEvent): void { |
| 78 | logger.error( |
| 79 | `${this.chargingStation.logPrefix()} ${moduleName}.messageErrorHandler: Error at handling message:`, |
| 80 | { messageEvent, messageEventData: messageEvent.data } |
| 81 | ); |
| 82 | } |
| 83 | |
| 84 | private async commandHandler( |
| 85 | command: BroadcastChannelProcedureName, |
| 86 | requestPayload: BroadcastChannelRequestPayload |
| 87 | ): Promise<CommandResponse | undefined> { |
| 88 | switch (command) { |
| 89 | case BroadcastChannelProcedureName.START_TRANSACTION: |
| 90 | return this.chargingStation.ocppRequestService.requestHandler< |
| 91 | StartTransactionRequest, |
| 92 | StartTransactionResponse |
| 93 | >(this.chargingStation, RequestCommand.START_TRANSACTION, { |
| 94 | connectorId: requestPayload.connectorId, |
| 95 | idTag: requestPayload.idTag, |
| 96 | }); |
| 97 | case BroadcastChannelProcedureName.STOP_TRANSACTION: |
| 98 | return this.chargingStation.ocppRequestService.requestHandler< |
| 99 | StopTransactionRequest, |
| 100 | StopTransactionResponse |
| 101 | >(this.chargingStation, RequestCommand.STOP_TRANSACTION, { |
| 102 | transactionId: requestPayload.transactionId, |
| 103 | meterStop: this.chargingStation.getEnergyActiveImportRegisterByTransactionId( |
| 104 | requestPayload.transactionId |
| 105 | ), |
| 106 | idTag: this.chargingStation.getTransactionIdTag(requestPayload.transactionId), |
| 107 | reason: StopTransactionReason.NONE, |
| 108 | }); |
| 109 | case BroadcastChannelProcedureName.START_CHARGING_STATION: |
| 110 | this.chargingStation.start(); |
| 111 | break; |
| 112 | case BroadcastChannelProcedureName.STOP_CHARGING_STATION: |
| 113 | await this.chargingStation.stop(); |
| 114 | break; |
| 115 | case BroadcastChannelProcedureName.OPEN_CONNECTION: |
| 116 | this.chargingStation.openWSConnection(); |
| 117 | break; |
| 118 | case BroadcastChannelProcedureName.CLOSE_CONNECTION: |
| 119 | this.chargingStation.closeWSConnection(); |
| 120 | break; |
| 121 | default: |
| 122 | // eslint-disable-next-line @typescript-eslint/restrict-template-expressions |
| 123 | throw new BaseError(`Unknown worker broadcast channel command: ${command}`); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | private commandResponseToResponseStatus(commandResponse: CommandResponse): ResponseStatus { |
| 128 | if (commandResponse?.idTagInfo?.status === AuthorizationStatus.ACCEPTED) { |
| 129 | return ResponseStatus.SUCCESS; |
| 130 | } |
| 131 | return ResponseStatus.FAILURE; |
| 132 | } |
| 133 | } |