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