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