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'; |
f27eb751 | 18 | import { ResponseStatus } from '../ui/web/src/types/UIProtocol'; |
6c8f5d90 | 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 | } | |
852a4c5f | 41 | this.validateMessageEvent(messageEvent); |
89b7a234 | 42 | |
6c8f5d90 JB |
43 | const [uuid, command, requestPayload] = messageEvent.data as BroadcastChannelRequest; |
44 | ||
4eca248c JB |
45 | if ( |
46 | requestPayload?.hashId === undefined && | |
47 | (requestPayload?.hashIds as string[])?.includes(this.chargingStation.hashId) === false | |
48 | ) { | |
89b7a234 JB |
49 | return; |
50 | } | |
4eca248c JB |
51 | if ( |
52 | requestPayload?.hashIds === undefined && | |
53 | requestPayload?.hashId !== this.chargingStation.hashId | |
54 | ) { | |
55 | return; | |
56 | } | |
57 | if (requestPayload?.hashId !== undefined) { | |
58 | logger.warn( | |
59 | `${this.chargingStation.logPrefix()} ${moduleName}.requestHandler: 'hashId' field usage in PDU is deprecated, use 'hashIds' instead` | |
60 | ); | |
61 | } | |
89b7a234 | 62 | |
6c8f5d90 JB |
63 | let responsePayload: BroadcastChannelResponsePayload; |
64 | let commandResponse: CommandResponse; | |
65 | try { | |
66 | commandResponse = await this.commandHandler(command, requestPayload); | |
67 | if (commandResponse === undefined) { | |
10d244c0 JB |
68 | responsePayload = { |
69 | hashId: this.chargingStation.hashId, | |
70 | status: ResponseStatus.SUCCESS, | |
71 | }; | |
6c8f5d90 | 72 | } else { |
10d244c0 JB |
73 | responsePayload = { |
74 | hashId: this.chargingStation.hashId, | |
75 | status: this.commandResponseToResponseStatus(commandResponse), | |
76 | }; | |
6c8f5d90 JB |
77 | } |
78 | } catch (error) { | |
79 | logger.error( | |
80 | `${this.chargingStation.logPrefix()} ${moduleName}.requestHandler: Handle request error:`, | |
81 | error | |
82 | ); | |
83 | responsePayload = { | |
10d244c0 | 84 | hashId: this.chargingStation.hashId, |
6c8f5d90 JB |
85 | status: ResponseStatus.FAILURE, |
86 | command, | |
87 | requestPayload, | |
88 | commandResponse, | |
89 | errorMessage: (error as Error).message, | |
90 | errorStack: (error as Error).stack, | |
91 | }; | |
92 | } | |
93 | this.sendResponse([uuid, responsePayload]); | |
94 | } | |
95 | ||
96 | private messageErrorHandler(messageEvent: MessageEvent): void { | |
97 | logger.error( | |
98 | `${this.chargingStation.logPrefix()} ${moduleName}.messageErrorHandler: Error at handling message:`, | |
99 | { messageEvent, messageEventData: messageEvent.data } | |
100 | ); | |
101 | } | |
102 | ||
103 | private async commandHandler( | |
104 | command: BroadcastChannelProcedureName, | |
105 | requestPayload: BroadcastChannelRequestPayload | |
106 | ): Promise<CommandResponse | undefined> { | |
89b7a234 JB |
107 | switch (command) { |
108 | case BroadcastChannelProcedureName.START_TRANSACTION: | |
6c8f5d90 | 109 | return this.chargingStation.ocppRequestService.requestHandler< |
89b7a234 JB |
110 | StartTransactionRequest, |
111 | StartTransactionResponse | |
112 | >(this.chargingStation, RequestCommand.START_TRANSACTION, { | |
6c8f5d90 JB |
113 | connectorId: requestPayload.connectorId, |
114 | idTag: requestPayload.idTag, | |
89b7a234 | 115 | }); |
89b7a234 | 116 | case BroadcastChannelProcedureName.STOP_TRANSACTION: |
6c8f5d90 | 117 | return this.chargingStation.ocppRequestService.requestHandler< |
89b7a234 JB |
118 | StopTransactionRequest, |
119 | StopTransactionResponse | |
120 | >(this.chargingStation, RequestCommand.STOP_TRANSACTION, { | |
6c8f5d90 | 121 | transactionId: requestPayload.transactionId, |
89b7a234 | 122 | meterStop: this.chargingStation.getEnergyActiveImportRegisterByTransactionId( |
6c8f5d90 | 123 | requestPayload.transactionId |
89b7a234 | 124 | ), |
6c8f5d90 | 125 | idTag: this.chargingStation.getTransactionIdTag(requestPayload.transactionId), |
89b7a234 JB |
126 | reason: StopTransactionReason.NONE, |
127 | }); | |
89b7a234 JB |
128 | case BroadcastChannelProcedureName.START_CHARGING_STATION: |
129 | this.chargingStation.start(); | |
130 | break; | |
131 | case BroadcastChannelProcedureName.STOP_CHARGING_STATION: | |
132 | await this.chargingStation.stop(); | |
133 | break; | |
db2336d9 JB |
134 | case BroadcastChannelProcedureName.OPEN_CONNECTION: |
135 | this.chargingStation.openWSConnection(); | |
136 | break; | |
137 | case BroadcastChannelProcedureName.CLOSE_CONNECTION: | |
138 | this.chargingStation.closeWSConnection(); | |
139 | break; | |
6c8f5d90 JB |
140 | default: |
141 | // eslint-disable-next-line @typescript-eslint/restrict-template-expressions | |
ce7a4fc3 | 142 | throw new BaseError(`Unknown worker broadcast channel command: ${command}`); |
6c8f5d90 JB |
143 | } |
144 | } | |
145 | ||
146 | private commandResponseToResponseStatus(commandResponse: CommandResponse): ResponseStatus { | |
147 | if (commandResponse?.idTagInfo?.status === AuthorizationStatus.ACCEPTED) { | |
148 | return ResponseStatus.SUCCESS; | |
89b7a234 | 149 | } |
6c8f5d90 | 150 | return ResponseStatus.FAILURE; |
89b7a234 JB |
151 | } |
152 | } |