| 1 | // Partial Copyright Jerome Benoit. 2021. All Rights Reserved. |
| 2 | |
| 3 | import OCPPError from '../../../exception/OCPPError'; |
| 4 | import { JsonType } from '../../../types/JsonType'; |
| 5 | import { OCPP16ChargePointErrorCode } from '../../../types/ocpp/1.6/ChargePointErrorCode'; |
| 6 | import { OCPP16ChargePointStatus } from '../../../types/ocpp/1.6/ChargePointStatus'; |
| 7 | import { OCPP16StandardParametersKey } from '../../../types/ocpp/1.6/Configuration'; |
| 8 | import { |
| 9 | OCPP16MeterValuesRequest, |
| 10 | OCPP16MeterValuesResponse, |
| 11 | } from '../../../types/ocpp/1.6/MeterValues'; |
| 12 | import { |
| 13 | OCPP16BootNotificationRequest, |
| 14 | OCPP16RequestCommand, |
| 15 | OCPP16StatusNotificationRequest, |
| 16 | } from '../../../types/ocpp/1.6/Requests'; |
| 17 | import { |
| 18 | OCPP16BootNotificationResponse, |
| 19 | OCPP16RegistrationStatus, |
| 20 | OCPP16StatusNotificationResponse, |
| 21 | } from '../../../types/ocpp/1.6/Responses'; |
| 22 | import { |
| 23 | OCPP16AuthorizationStatus, |
| 24 | OCPP16AuthorizeRequest, |
| 25 | OCPP16AuthorizeResponse, |
| 26 | OCPP16StartTransactionRequest, |
| 27 | OCPP16StartTransactionResponse, |
| 28 | OCPP16StopTransactionRequest, |
| 29 | OCPP16StopTransactionResponse, |
| 30 | } from '../../../types/ocpp/1.6/Transaction'; |
| 31 | import { ErrorType } from '../../../types/ocpp/ErrorType'; |
| 32 | import { ResponseHandler } from '../../../types/ocpp/Responses'; |
| 33 | import logger from '../../../utils/Logger'; |
| 34 | import Utils from '../../../utils/Utils'; |
| 35 | import type ChargingStation from '../../ChargingStation'; |
| 36 | import { ChargingStationConfigurationUtils } from '../../ChargingStationConfigurationUtils'; |
| 37 | import { ChargingStationUtils } from '../../ChargingStationUtils'; |
| 38 | import OCPPResponseService from '../OCPPResponseService'; |
| 39 | import { OCPP16ServiceUtils } from './OCPP16ServiceUtils'; |
| 40 | |
| 41 | const moduleName = 'OCPP16ResponseService'; |
| 42 | |
| 43 | export default class OCPP16ResponseService extends OCPPResponseService { |
| 44 | private responseHandlers: Map<OCPP16RequestCommand, ResponseHandler>; |
| 45 | |
| 46 | public constructor() { |
| 47 | if (new.target?.name === moduleName) { |
| 48 | throw new TypeError(`Cannot construct ${new.target?.name} instances directly`); |
| 49 | } |
| 50 | super(); |
| 51 | this.responseHandlers = new Map<OCPP16RequestCommand, ResponseHandler>([ |
| 52 | [OCPP16RequestCommand.BOOT_NOTIFICATION, this.handleResponseBootNotification.bind(this)], |
| 53 | [OCPP16RequestCommand.HEARTBEAT, this.handleResponseHeartbeat.bind(this)], |
| 54 | [OCPP16RequestCommand.AUTHORIZE, this.handleResponseAuthorize.bind(this)], |
| 55 | [OCPP16RequestCommand.START_TRANSACTION, this.handleResponseStartTransaction.bind(this)], |
| 56 | [OCPP16RequestCommand.STOP_TRANSACTION, this.handleResponseStopTransaction.bind(this)], |
| 57 | [OCPP16RequestCommand.STATUS_NOTIFICATION, this.handleResponseStatusNotification.bind(this)], |
| 58 | [OCPP16RequestCommand.METER_VALUES, this.handleResponseMeterValues.bind(this)], |
| 59 | ]); |
| 60 | } |
| 61 | |
| 62 | public async responseHandler( |
| 63 | chargingStation: ChargingStation, |
| 64 | commandName: OCPP16RequestCommand, |
| 65 | payload: JsonType, |
| 66 | requestPayload: JsonType |
| 67 | ): Promise<void> { |
| 68 | if (chargingStation.isRegistered() || commandName === OCPP16RequestCommand.BOOT_NOTIFICATION) { |
| 69 | if ( |
| 70 | this.responseHandlers.has(commandName) && |
| 71 | ChargingStationUtils.isCommandSupported(commandName, chargingStation.stationInfo) |
| 72 | ) { |
| 73 | try { |
| 74 | await this.responseHandlers.get(commandName)(chargingStation, payload, requestPayload); |
| 75 | } catch (error) { |
| 76 | logger.error(chargingStation.logPrefix() + ' Handle request response error: %j', error); |
| 77 | throw error; |
| 78 | } |
| 79 | } else { |
| 80 | // Throw exception |
| 81 | throw new OCPPError( |
| 82 | ErrorType.NOT_IMPLEMENTED, |
| 83 | `${commandName} is not implemented to handle request response payload ${JSON.stringify( |
| 84 | payload, |
| 85 | null, |
| 86 | 2 |
| 87 | )}`, |
| 88 | commandName, |
| 89 | payload |
| 90 | ); |
| 91 | } |
| 92 | } else { |
| 93 | throw new OCPPError( |
| 94 | ErrorType.SECURITY_ERROR, |
| 95 | `${commandName} cannot be issued to handle request response payload ${JSON.stringify( |
| 96 | payload, |
| 97 | null, |
| 98 | 2 |
| 99 | )} while the charging station is not registered on the central server. `, |
| 100 | commandName, |
| 101 | payload |
| 102 | ); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | private handleResponseBootNotification( |
| 107 | chargingStation: ChargingStation, |
| 108 | payload: OCPP16BootNotificationResponse |
| 109 | ): void { |
| 110 | if (payload.status === OCPP16RegistrationStatus.ACCEPTED) { |
| 111 | ChargingStationConfigurationUtils.addConfigurationKey( |
| 112 | chargingStation, |
| 113 | OCPP16StandardParametersKey.HeartbeatInterval, |
| 114 | payload.interval.toString(), |
| 115 | {}, |
| 116 | { overwrite: true, save: true } |
| 117 | ); |
| 118 | ChargingStationConfigurationUtils.addConfigurationKey( |
| 119 | chargingStation, |
| 120 | OCPP16StandardParametersKey.HeartBeatInterval, |
| 121 | payload.interval.toString(), |
| 122 | { visible: false }, |
| 123 | { overwrite: true, save: true } |
| 124 | ); |
| 125 | chargingStation.heartbeatSetInterval |
| 126 | ? chargingStation.restartHeartbeat() |
| 127 | : chargingStation.startHeartbeat(); |
| 128 | } |
| 129 | if (Object.values(OCPP16RegistrationStatus).includes(payload.status)) { |
| 130 | const logMsg = `${chargingStation.logPrefix()} Charging station in '${ |
| 131 | payload.status |
| 132 | }' state on the central server`; |
| 133 | payload.status === OCPP16RegistrationStatus.REJECTED |
| 134 | ? logger.warn(logMsg) |
| 135 | : logger.info(logMsg); |
| 136 | } else { |
| 137 | logger.error( |
| 138 | chargingStation.logPrefix() + |
| 139 | ' Charging station boot notification response received: %j with undefined registration status', |
| 140 | payload |
| 141 | ); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | // eslint-disable-next-line @typescript-eslint/no-empty-function |
| 146 | private handleResponseHeartbeat(): void {} |
| 147 | |
| 148 | private handleResponseAuthorize( |
| 149 | chargingStation: ChargingStation, |
| 150 | payload: OCPP16AuthorizeResponse, |
| 151 | requestPayload: OCPP16AuthorizeRequest |
| 152 | ): void { |
| 153 | let authorizeConnectorId: number; |
| 154 | for (const connectorId of chargingStation.connectors.keys()) { |
| 155 | if ( |
| 156 | connectorId > 0 && |
| 157 | chargingStation.getConnectorStatus(connectorId)?.authorizeIdTag === requestPayload.idTag |
| 158 | ) { |
| 159 | authorizeConnectorId = connectorId; |
| 160 | break; |
| 161 | } |
| 162 | } |
| 163 | if (payload.idTagInfo.status === OCPP16AuthorizationStatus.ACCEPTED) { |
| 164 | chargingStation.getConnectorStatus(authorizeConnectorId).idTagAuthorized = true; |
| 165 | logger.debug( |
| 166 | `${chargingStation.logPrefix()} IdTag ${ |
| 167 | requestPayload.idTag |
| 168 | } authorized on connector ${authorizeConnectorId}` |
| 169 | ); |
| 170 | } else { |
| 171 | chargingStation.getConnectorStatus(authorizeConnectorId).idTagAuthorized = false; |
| 172 | delete chargingStation.getConnectorStatus(authorizeConnectorId).authorizeIdTag; |
| 173 | logger.debug( |
| 174 | `${chargingStation.logPrefix()} IdTag ${requestPayload.idTag} refused with status '${ |
| 175 | payload.idTagInfo.status |
| 176 | }' on connector ${authorizeConnectorId}` |
| 177 | ); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | private async handleResponseStartTransaction( |
| 182 | chargingStation: ChargingStation, |
| 183 | payload: OCPP16StartTransactionResponse, |
| 184 | requestPayload: OCPP16StartTransactionRequest |
| 185 | ): Promise<void> { |
| 186 | const connectorId = requestPayload.connectorId; |
| 187 | |
| 188 | let transactionConnectorId: number; |
| 189 | for (const id of chargingStation.connectors.keys()) { |
| 190 | if (id > 0 && id === connectorId) { |
| 191 | transactionConnectorId = id; |
| 192 | break; |
| 193 | } |
| 194 | } |
| 195 | if (!transactionConnectorId) { |
| 196 | logger.error( |
| 197 | chargingStation.logPrefix() + |
| 198 | ' Trying to start a transaction on a non existing connector Id ' + |
| 199 | connectorId.toString() |
| 200 | ); |
| 201 | return; |
| 202 | } |
| 203 | if ( |
| 204 | chargingStation.getConnectorStatus(connectorId).transactionRemoteStarted && |
| 205 | chargingStation.getAuthorizeRemoteTxRequests() && |
| 206 | chargingStation.getLocalAuthListEnabled() && |
| 207 | chargingStation.hasAuthorizedTags() && |
| 208 | !chargingStation.getConnectorStatus(connectorId).idTagLocalAuthorized |
| 209 | ) { |
| 210 | logger.error( |
| 211 | chargingStation.logPrefix() + |
| 212 | ' Trying to start a transaction with a not local authorized idTag ' + |
| 213 | chargingStation.getConnectorStatus(connectorId).localAuthorizeIdTag + |
| 214 | ' on connector Id ' + |
| 215 | connectorId.toString() |
| 216 | ); |
| 217 | await this.resetConnectorOnStartTransactionError(chargingStation, connectorId); |
| 218 | return; |
| 219 | } |
| 220 | if ( |
| 221 | chargingStation.getConnectorStatus(connectorId).transactionRemoteStarted && |
| 222 | chargingStation.getAuthorizeRemoteTxRequests() && |
| 223 | chargingStation.getMayAuthorizeAtRemoteStart() && |
| 224 | !chargingStation.getConnectorStatus(connectorId).idTagLocalAuthorized && |
| 225 | !chargingStation.getConnectorStatus(connectorId).idTagAuthorized |
| 226 | ) { |
| 227 | logger.error( |
| 228 | chargingStation.logPrefix() + |
| 229 | ' Trying to start a transaction with a not authorized idTag ' + |
| 230 | chargingStation.getConnectorStatus(connectorId).authorizeIdTag + |
| 231 | ' on connector Id ' + |
| 232 | connectorId.toString() |
| 233 | ); |
| 234 | await this.resetConnectorOnStartTransactionError(chargingStation, connectorId); |
| 235 | return; |
| 236 | } |
| 237 | if ( |
| 238 | chargingStation.getConnectorStatus(connectorId).idTagAuthorized && |
| 239 | chargingStation.getConnectorStatus(connectorId).authorizeIdTag !== requestPayload.idTag |
| 240 | ) { |
| 241 | logger.error( |
| 242 | chargingStation.logPrefix() + |
| 243 | ' Trying to start a transaction with an idTag ' + |
| 244 | requestPayload.idTag + |
| 245 | ' different from the authorize request one ' + |
| 246 | chargingStation.getConnectorStatus(connectorId).authorizeIdTag + |
| 247 | ' on connector Id ' + |
| 248 | connectorId.toString() |
| 249 | ); |
| 250 | await this.resetConnectorOnStartTransactionError(chargingStation, connectorId); |
| 251 | return; |
| 252 | } |
| 253 | if ( |
| 254 | chargingStation.getConnectorStatus(connectorId).idTagLocalAuthorized && |
| 255 | chargingStation.getConnectorStatus(connectorId).localAuthorizeIdTag !== requestPayload.idTag |
| 256 | ) { |
| 257 | logger.error( |
| 258 | chargingStation.logPrefix() + |
| 259 | ' Trying to start a transaction with an idTag ' + |
| 260 | requestPayload.idTag + |
| 261 | ' different from the local authorized one ' + |
| 262 | chargingStation.getConnectorStatus(connectorId).localAuthorizeIdTag + |
| 263 | ' on connector Id ' + |
| 264 | connectorId.toString() |
| 265 | ); |
| 266 | await this.resetConnectorOnStartTransactionError(chargingStation, connectorId); |
| 267 | return; |
| 268 | } |
| 269 | if (chargingStation.getConnectorStatus(connectorId)?.transactionStarted) { |
| 270 | logger.debug( |
| 271 | chargingStation.logPrefix() + |
| 272 | ' Trying to start a transaction on an already used connector ' + |
| 273 | connectorId.toString() + |
| 274 | ': %j', |
| 275 | chargingStation.getConnectorStatus(connectorId) |
| 276 | ); |
| 277 | return; |
| 278 | } |
| 279 | if ( |
| 280 | chargingStation.getConnectorStatus(connectorId)?.status !== |
| 281 | OCPP16ChargePointStatus.AVAILABLE && |
| 282 | chargingStation.getConnectorStatus(connectorId)?.status !== OCPP16ChargePointStatus.PREPARING |
| 283 | ) { |
| 284 | logger.error( |
| 285 | `${chargingStation.logPrefix()} Trying to start a transaction on connector ${connectorId.toString()} with status ${ |
| 286 | chargingStation.getConnectorStatus(connectorId)?.status |
| 287 | }` |
| 288 | ); |
| 289 | return; |
| 290 | } |
| 291 | if (!Number.isInteger(payload.transactionId)) { |
| 292 | logger.warn( |
| 293 | `${chargingStation.logPrefix()} Trying to start a transaction on connector ${connectorId.toString()} with a non integer transaction Id ${ |
| 294 | payload.transactionId |
| 295 | }, converting to integer` |
| 296 | ); |
| 297 | payload.transactionId = Utils.convertToInt(payload.transactionId); |
| 298 | } |
| 299 | |
| 300 | if (payload.idTagInfo?.status === OCPP16AuthorizationStatus.ACCEPTED) { |
| 301 | chargingStation.getConnectorStatus(connectorId).transactionStarted = true; |
| 302 | chargingStation.getConnectorStatus(connectorId).transactionId = payload.transactionId; |
| 303 | chargingStation.getConnectorStatus(connectorId).transactionIdTag = requestPayload.idTag; |
| 304 | chargingStation.getConnectorStatus( |
| 305 | connectorId |
| 306 | ).transactionEnergyActiveImportRegisterValue = 0; |
| 307 | chargingStation.getConnectorStatus(connectorId).transactionBeginMeterValue = |
| 308 | OCPP16ServiceUtils.buildTransactionBeginMeterValue( |
| 309 | chargingStation, |
| 310 | connectorId, |
| 311 | requestPayload.meterStart |
| 312 | ); |
| 313 | chargingStation.getBeginEndMeterValues() && |
| 314 | (await chargingStation.ocppRequestService.requestHandler< |
| 315 | OCPP16MeterValuesRequest, |
| 316 | OCPP16MeterValuesResponse |
| 317 | >(chargingStation, OCPP16RequestCommand.METER_VALUES, { |
| 318 | connectorId, |
| 319 | transactionId: payload.transactionId, |
| 320 | meterValue: [chargingStation.getConnectorStatus(connectorId).transactionBeginMeterValue], |
| 321 | })); |
| 322 | await chargingStation.ocppRequestService.requestHandler< |
| 323 | OCPP16StatusNotificationRequest, |
| 324 | OCPP16StatusNotificationResponse |
| 325 | >(chargingStation, OCPP16RequestCommand.STATUS_NOTIFICATION, { |
| 326 | connectorId, |
| 327 | status: OCPP16ChargePointStatus.CHARGING, |
| 328 | errorCode: OCPP16ChargePointErrorCode.NO_ERROR, |
| 329 | }); |
| 330 | chargingStation.getConnectorStatus(connectorId).status = OCPP16ChargePointStatus.CHARGING; |
| 331 | logger.info( |
| 332 | chargingStation.logPrefix() + |
| 333 | ' Transaction ' + |
| 334 | payload.transactionId.toString() + |
| 335 | ' STARTED on ' + |
| 336 | chargingStation.stationInfo.chargingStationId + |
| 337 | '#' + |
| 338 | connectorId.toString() + |
| 339 | ' for idTag ' + |
| 340 | requestPayload.idTag |
| 341 | ); |
| 342 | if (chargingStation.stationInfo.powerSharedByConnectors) { |
| 343 | chargingStation.powerDivider++; |
| 344 | } |
| 345 | const configuredMeterValueSampleInterval = |
| 346 | ChargingStationConfigurationUtils.getConfigurationKey( |
| 347 | chargingStation, |
| 348 | OCPP16StandardParametersKey.MeterValueSampleInterval |
| 349 | ); |
| 350 | chargingStation.startMeterValues( |
| 351 | connectorId, |
| 352 | configuredMeterValueSampleInterval |
| 353 | ? Utils.convertToInt(configuredMeterValueSampleInterval.value) * 1000 |
| 354 | : 60000 |
| 355 | ); |
| 356 | } else { |
| 357 | logger.warn( |
| 358 | chargingStation.logPrefix() + |
| 359 | ' Starting transaction id ' + |
| 360 | payload.transactionId.toString() + |
| 361 | " REJECTED with status '" + |
| 362 | payload?.idTagInfo?.status + |
| 363 | "', idTag " + |
| 364 | requestPayload.idTag |
| 365 | ); |
| 366 | await this.resetConnectorOnStartTransactionError(chargingStation, connectorId); |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | private async resetConnectorOnStartTransactionError( |
| 371 | chargingStation: ChargingStation, |
| 372 | connectorId: number |
| 373 | ): Promise<void> { |
| 374 | chargingStation.resetConnectorStatus(connectorId); |
| 375 | if ( |
| 376 | chargingStation.getConnectorStatus(connectorId).status !== OCPP16ChargePointStatus.AVAILABLE |
| 377 | ) { |
| 378 | await chargingStation.ocppRequestService.requestHandler< |
| 379 | OCPP16StatusNotificationRequest, |
| 380 | OCPP16StatusNotificationResponse |
| 381 | >(chargingStation, OCPP16RequestCommand.STATUS_NOTIFICATION, { |
| 382 | connectorId, |
| 383 | status: OCPP16ChargePointStatus.AVAILABLE, |
| 384 | errorCode: OCPP16ChargePointErrorCode.NO_ERROR, |
| 385 | }); |
| 386 | chargingStation.getConnectorStatus(connectorId).status = OCPP16ChargePointStatus.AVAILABLE; |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | private async handleResponseStopTransaction( |
| 391 | chargingStation: ChargingStation, |
| 392 | payload: OCPP16StopTransactionResponse, |
| 393 | requestPayload: OCPP16StopTransactionRequest |
| 394 | ): Promise<void> { |
| 395 | const transactionConnectorId = chargingStation.getConnectorIdByTransactionId( |
| 396 | requestPayload.transactionId |
| 397 | ); |
| 398 | if (!transactionConnectorId) { |
| 399 | logger.error( |
| 400 | chargingStation.logPrefix() + |
| 401 | ' Trying to stop a non existing transaction ' + |
| 402 | requestPayload.transactionId.toString() |
| 403 | ); |
| 404 | return; |
| 405 | } |
| 406 | if (payload.idTagInfo?.status === OCPP16AuthorizationStatus.ACCEPTED) { |
| 407 | chargingStation.getBeginEndMeterValues() && |
| 408 | !chargingStation.getOcppStrictCompliance() && |
| 409 | chargingStation.getOutOfOrderEndMeterValues() && |
| 410 | (await chargingStation.ocppRequestService.requestHandler< |
| 411 | OCPP16MeterValuesRequest, |
| 412 | OCPP16MeterValuesResponse |
| 413 | >(chargingStation, OCPP16RequestCommand.METER_VALUES, { |
| 414 | connectorId: transactionConnectorId, |
| 415 | transactionId: requestPayload.transactionId, |
| 416 | meterValue: [ |
| 417 | OCPP16ServiceUtils.buildTransactionEndMeterValue( |
| 418 | chargingStation, |
| 419 | transactionConnectorId, |
| 420 | requestPayload.meterStop |
| 421 | ), |
| 422 | ], |
| 423 | })); |
| 424 | if ( |
| 425 | !chargingStation.isChargingStationAvailable() || |
| 426 | !chargingStation.isConnectorAvailable(transactionConnectorId) |
| 427 | ) { |
| 428 | await chargingStation.ocppRequestService.requestHandler< |
| 429 | OCPP16StatusNotificationRequest, |
| 430 | OCPP16StatusNotificationResponse |
| 431 | >(chargingStation, OCPP16RequestCommand.STATUS_NOTIFICATION, { |
| 432 | connectorId: transactionConnectorId, |
| 433 | status: OCPP16ChargePointStatus.UNAVAILABLE, |
| 434 | errorCode: OCPP16ChargePointErrorCode.NO_ERROR, |
| 435 | }); |
| 436 | chargingStation.getConnectorStatus(transactionConnectorId).status = |
| 437 | OCPP16ChargePointStatus.UNAVAILABLE; |
| 438 | } else { |
| 439 | await chargingStation.ocppRequestService.requestHandler< |
| 440 | OCPP16BootNotificationRequest, |
| 441 | OCPP16BootNotificationResponse |
| 442 | >(chargingStation, OCPP16RequestCommand.STATUS_NOTIFICATION, { |
| 443 | connectorId: transactionConnectorId, |
| 444 | status: OCPP16ChargePointStatus.AVAILABLE, |
| 445 | errorCode: OCPP16ChargePointErrorCode.NO_ERROR, |
| 446 | }); |
| 447 | chargingStation.getConnectorStatus(transactionConnectorId).status = |
| 448 | OCPP16ChargePointStatus.AVAILABLE; |
| 449 | } |
| 450 | if (chargingStation.stationInfo.powerSharedByConnectors) { |
| 451 | chargingStation.powerDivider--; |
| 452 | } |
| 453 | logger.info( |
| 454 | chargingStation.logPrefix() + |
| 455 | ' Transaction ' + |
| 456 | requestPayload.transactionId.toString() + |
| 457 | ' STOPPED on ' + |
| 458 | chargingStation.stationInfo.chargingStationId + |
| 459 | '#' + |
| 460 | transactionConnectorId.toString() |
| 461 | ); |
| 462 | chargingStation.resetConnectorStatus(transactionConnectorId); |
| 463 | } else { |
| 464 | logger.warn( |
| 465 | chargingStation.logPrefix() + |
| 466 | ' Stopping transaction id ' + |
| 467 | requestPayload.transactionId.toString() + |
| 468 | " REJECTED with status '" + |
| 469 | payload.idTagInfo?.status + |
| 470 | "'" |
| 471 | ); |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | // eslint-disable-next-line @typescript-eslint/no-empty-function |
| 476 | private handleResponseStatusNotification(): void {} |
| 477 | |
| 478 | // eslint-disable-next-line @typescript-eslint/no-empty-function |
| 479 | private handleResponseMeterValues(): void {} |
| 480 | } |