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