| 1 | // Partial Copyright Jerome Benoit. 2021-2024. All Rights Reserved. |
| 2 | |
| 3 | import type { ValidateFunction } from 'ajv' |
| 4 | |
| 5 | import { addConfigurationKey, type ChargingStation } from '../../../charging-station/index.js' |
| 6 | import { OCPPError } from '../../../exception/index.js' |
| 7 | import { |
| 8 | ChargingStationEvents, |
| 9 | ErrorType, |
| 10 | type JsonType, |
| 11 | type OCPP20BootNotificationResponse, |
| 12 | type OCPP20ClearCacheResponse, |
| 13 | type OCPP20HeartbeatResponse, |
| 14 | OCPP20IncomingRequestCommand, |
| 15 | OCPP20OptionalVariableName, |
| 16 | OCPP20RequestCommand, |
| 17 | type OCPP20StatusNotificationResponse, |
| 18 | OCPPVersion, |
| 19 | RegistrationStatusEnumType, |
| 20 | type ResponseHandler |
| 21 | } from '../../../types/index.js' |
| 22 | import { isAsyncFunction, logger } from '../../../utils/index.js' |
| 23 | import { OCPPResponseService } from '../OCPPResponseService.js' |
| 24 | import { OCPP20ServiceUtils } from './OCPP20ServiceUtils.js' |
| 25 | |
| 26 | const moduleName = 'OCPP20ResponseService' |
| 27 | |
| 28 | export class OCPP20ResponseService extends OCPPResponseService { |
| 29 | public incomingRequestResponsePayloadValidateFunctions: Map< |
| 30 | OCPP20IncomingRequestCommand, |
| 31 | ValidateFunction<JsonType> |
| 32 | > |
| 33 | |
| 34 | protected payloadValidateFunctions: Map<OCPP20RequestCommand, ValidateFunction<JsonType>> |
| 35 | private readonly responseHandlers: Map<OCPP20RequestCommand, ResponseHandler> |
| 36 | |
| 37 | public constructor () { |
| 38 | // if (new.target.name === moduleName) { |
| 39 | // throw new TypeError(`Cannot construct ${new.target.name} instances directly`) |
| 40 | // } |
| 41 | super(OCPPVersion.VERSION_201) |
| 42 | this.responseHandlers = new Map<OCPP20RequestCommand, ResponseHandler>([ |
| 43 | [ |
| 44 | OCPP20RequestCommand.BOOT_NOTIFICATION, |
| 45 | this.handleResponseBootNotification.bind(this) as ResponseHandler |
| 46 | ], |
| 47 | [OCPP20RequestCommand.HEARTBEAT, this.emptyResponseHandler], |
| 48 | [OCPP20RequestCommand.STATUS_NOTIFICATION, this.emptyResponseHandler] |
| 49 | ]) |
| 50 | this.payloadValidateFunctions = new Map<OCPP20RequestCommand, ValidateFunction<JsonType>>([ |
| 51 | [ |
| 52 | OCPP20RequestCommand.BOOT_NOTIFICATION, |
| 53 | this.ajv |
| 54 | .compile( |
| 55 | OCPP20ServiceUtils.parseJsonSchemaFile<OCPP20BootNotificationResponse>( |
| 56 | 'assets/json-schemas/ocpp/2.0/BootNotificationResponse.json', |
| 57 | moduleName, |
| 58 | 'constructor' |
| 59 | ) |
| 60 | ) |
| 61 | .bind(this) |
| 62 | ], |
| 63 | [ |
| 64 | OCPP20RequestCommand.HEARTBEAT, |
| 65 | this.ajv |
| 66 | .compile( |
| 67 | OCPP20ServiceUtils.parseJsonSchemaFile<OCPP20HeartbeatResponse>( |
| 68 | 'assets/json-schemas/ocpp/2.0/HeartbeatResponse.json', |
| 69 | moduleName, |
| 70 | 'constructor' |
| 71 | ) |
| 72 | ) |
| 73 | .bind(this) |
| 74 | ], |
| 75 | [ |
| 76 | OCPP20RequestCommand.STATUS_NOTIFICATION, |
| 77 | this.ajv |
| 78 | .compile( |
| 79 | OCPP20ServiceUtils.parseJsonSchemaFile<OCPP20StatusNotificationResponse>( |
| 80 | 'assets/json-schemas/ocpp/2.0/StatusNotificationResponse.json', |
| 81 | moduleName, |
| 82 | 'constructor' |
| 83 | ) |
| 84 | ) |
| 85 | .bind(this) |
| 86 | ] |
| 87 | ]) |
| 88 | this.incomingRequestResponsePayloadValidateFunctions = new Map< |
| 89 | OCPP20IncomingRequestCommand, |
| 90 | ValidateFunction<JsonType> |
| 91 | >([ |
| 92 | [ |
| 93 | OCPP20IncomingRequestCommand.CLEAR_CACHE, |
| 94 | this.ajvIncomingRequest |
| 95 | .compile( |
| 96 | OCPP20ServiceUtils.parseJsonSchemaFile<OCPP20ClearCacheResponse>( |
| 97 | 'assets/json-schemas/ocpp/2.0/ClearCacheResponse.json', |
| 98 | moduleName, |
| 99 | 'constructor' |
| 100 | ) |
| 101 | ) |
| 102 | .bind(this) |
| 103 | ] |
| 104 | ]) |
| 105 | this.validatePayload = this.validatePayload.bind(this) |
| 106 | } |
| 107 | |
| 108 | public async responseHandler<ReqType extends JsonType, ResType extends JsonType>( |
| 109 | chargingStation: ChargingStation, |
| 110 | commandName: OCPP20RequestCommand, |
| 111 | payload: ResType, |
| 112 | requestPayload: ReqType |
| 113 | ): Promise<void> { |
| 114 | if (chargingStation.isRegistered() || commandName === OCPP20RequestCommand.BOOT_NOTIFICATION) { |
| 115 | if ( |
| 116 | this.responseHandlers.has(commandName) && |
| 117 | OCPP20ServiceUtils.isRequestCommandSupported(chargingStation, commandName) |
| 118 | ) { |
| 119 | try { |
| 120 | this.validatePayload(chargingStation, commandName, payload) |
| 121 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
| 122 | const responseHandler = this.responseHandlers.get(commandName)! |
| 123 | if (isAsyncFunction(responseHandler)) { |
| 124 | await responseHandler(chargingStation, payload, requestPayload) |
| 125 | } else { |
| 126 | ( |
| 127 | responseHandler as ( |
| 128 | chargingStation: ChargingStation, |
| 129 | payload: JsonType, |
| 130 | requestPayload?: JsonType |
| 131 | ) => void |
| 132 | )(chargingStation, payload, requestPayload) |
| 133 | } |
| 134 | } catch (error) { |
| 135 | logger.error( |
| 136 | `${chargingStation.logPrefix()} ${moduleName}.responseHandler: Handle response error:`, |
| 137 | error |
| 138 | ) |
| 139 | throw error |
| 140 | } |
| 141 | } else { |
| 142 | // Throw exception |
| 143 | throw new OCPPError( |
| 144 | ErrorType.NOT_IMPLEMENTED, |
| 145 | `'${commandName}' is not implemented to handle response PDU ${JSON.stringify( |
| 146 | payload, |
| 147 | undefined, |
| 148 | 2 |
| 149 | )}`, |
| 150 | commandName, |
| 151 | payload |
| 152 | ) |
| 153 | } |
| 154 | } else { |
| 155 | throw new OCPPError( |
| 156 | ErrorType.SECURITY_ERROR, |
| 157 | `${commandName} cannot be issued to handle response PDU ${JSON.stringify( |
| 158 | payload, |
| 159 | undefined, |
| 160 | 2 |
| 161 | )} while the charging station is not registered on the central server`, |
| 162 | commandName, |
| 163 | payload |
| 164 | ) |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | private validatePayload ( |
| 169 | chargingStation: ChargingStation, |
| 170 | commandName: OCPP20RequestCommand, |
| 171 | payload: JsonType |
| 172 | ): boolean { |
| 173 | if (this.payloadValidateFunctions.has(commandName)) { |
| 174 | return this.validateResponsePayload(chargingStation, commandName, payload) |
| 175 | } |
| 176 | logger.warn( |
| 177 | `${chargingStation.logPrefix()} ${moduleName}.validatePayload: No JSON schema validation function found for command '${commandName}' PDU validation` |
| 178 | ) |
| 179 | return false |
| 180 | } |
| 181 | |
| 182 | private handleResponseBootNotification ( |
| 183 | chargingStation: ChargingStation, |
| 184 | payload: OCPP20BootNotificationResponse |
| 185 | ): void { |
| 186 | if (Object.values(RegistrationStatusEnumType).includes(payload.status)) { |
| 187 | chargingStation.bootNotificationResponse = payload |
| 188 | if (chargingStation.isRegistered()) { |
| 189 | chargingStation.emit(ChargingStationEvents.registered) |
| 190 | if (chargingStation.inAcceptedState()) { |
| 191 | addConfigurationKey( |
| 192 | chargingStation, |
| 193 | OCPP20OptionalVariableName.HeartbeatInterval, |
| 194 | payload.interval.toString(), |
| 195 | {}, |
| 196 | { overwrite: true, save: true } |
| 197 | ) |
| 198 | chargingStation.emit(ChargingStationEvents.accepted) |
| 199 | } |
| 200 | } else if (chargingStation.inRejectedState()) { |
| 201 | chargingStation.emit(ChargingStationEvents.rejected) |
| 202 | } |
| 203 | const logMsg = `${chargingStation.logPrefix()} Charging station in '${ |
| 204 | payload.status |
| 205 | }' state on the central server` |
| 206 | payload.status === RegistrationStatusEnumType.REJECTED |
| 207 | ? logger.warn(logMsg) |
| 208 | : logger.info(logMsg) |
| 209 | } else { |
| 210 | logger.error( |
| 211 | `${chargingStation.logPrefix()} Charging station boot notification response received: %j with undefined registration status`, |
| 212 | payload |
| 213 | ) |
| 214 | } |
| 215 | } |
| 216 | } |