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