| 1 | // Partial Copyright Jerome Benoit. 2021. All Rights Reserved. |
| 2 | |
| 3 | import fs from 'fs'; |
| 4 | import path from 'path'; |
| 5 | import { fileURLToPath } from 'url'; |
| 6 | |
| 7 | import type { JSONSchemaType } from 'ajv'; |
| 8 | |
| 9 | import OCPPError from '../../../exception/OCPPError'; |
| 10 | import type { JsonObject, JsonType } from '../../../types/JsonType'; |
| 11 | import { OCPP16ChargePointErrorCode } from '../../../types/ocpp/1.6/ChargePointErrorCode'; |
| 12 | import { OCPP16ChargePointStatus } from '../../../types/ocpp/1.6/ChargePointStatus'; |
| 13 | import { OCPP16StandardParametersKey } from '../../../types/ocpp/1.6/Configuration'; |
| 14 | import type { |
| 15 | OCPP16MeterValuesRequest, |
| 16 | OCPP16MeterValuesResponse, |
| 17 | } from '../../../types/ocpp/1.6/MeterValues'; |
| 18 | import { |
| 19 | OCPP16BootNotificationRequest, |
| 20 | OCPP16RequestCommand, |
| 21 | OCPP16StatusNotificationRequest, |
| 22 | } from '../../../types/ocpp/1.6/Requests'; |
| 23 | import { |
| 24 | DiagnosticsStatusNotificationResponse, |
| 25 | OCPP16BootNotificationResponse, |
| 26 | OCPP16HeartbeatResponse, |
| 27 | OCPP16RegistrationStatus, |
| 28 | OCPP16StatusNotificationResponse, |
| 29 | } from '../../../types/ocpp/1.6/Responses'; |
| 30 | import { |
| 31 | OCPP16AuthorizationStatus, |
| 32 | OCPP16AuthorizeRequest, |
| 33 | OCPP16AuthorizeResponse, |
| 34 | OCPP16StartTransactionRequest, |
| 35 | OCPP16StartTransactionResponse, |
| 36 | OCPP16StopTransactionRequest, |
| 37 | OCPP16StopTransactionResponse, |
| 38 | } from '../../../types/ocpp/1.6/Transaction'; |
| 39 | import { ErrorType } from '../../../types/ocpp/ErrorType'; |
| 40 | import type { ResponseHandler } from '../../../types/ocpp/Responses'; |
| 41 | import Constants from '../../../utils/Constants'; |
| 42 | import logger from '../../../utils/Logger'; |
| 43 | import Utils from '../../../utils/Utils'; |
| 44 | import type ChargingStation from '../../ChargingStation'; |
| 45 | import { ChargingStationConfigurationUtils } from '../../ChargingStationConfigurationUtils'; |
| 46 | import { ChargingStationUtils } from '../../ChargingStationUtils'; |
| 47 | import OCPPResponseService from '../OCPPResponseService'; |
| 48 | import { OCPP16ServiceUtils } from './OCPP16ServiceUtils'; |
| 49 | |
| 50 | const moduleName = 'OCPP16ResponseService'; |
| 51 | |
| 52 | export default class OCPP16ResponseService extends OCPPResponseService { |
| 53 | private responseHandlers: Map<OCPP16RequestCommand, ResponseHandler>; |
| 54 | private jsonSchemas: Map<OCPP16RequestCommand, JSONSchemaType<JsonObject>>; |
| 55 | |
| 56 | public constructor() { |
| 57 | if (new.target?.name === moduleName) { |
| 58 | throw new TypeError(`Cannot construct ${new.target?.name} instances directly`); |
| 59 | } |
| 60 | super(); |
| 61 | this.responseHandlers = new Map<OCPP16RequestCommand, ResponseHandler>([ |
| 62 | [OCPP16RequestCommand.BOOT_NOTIFICATION, this.handleResponseBootNotification.bind(this)], |
| 63 | [OCPP16RequestCommand.HEARTBEAT, this.emptyResponseHandler.bind(this)], |
| 64 | [OCPP16RequestCommand.AUTHORIZE, this.handleResponseAuthorize.bind(this)], |
| 65 | [OCPP16RequestCommand.START_TRANSACTION, this.handleResponseStartTransaction.bind(this)], |
| 66 | [OCPP16RequestCommand.STOP_TRANSACTION, this.handleResponseStopTransaction.bind(this)], |
| 67 | [OCPP16RequestCommand.STATUS_NOTIFICATION, this.emptyResponseHandler.bind(this)], |
| 68 | [OCPP16RequestCommand.METER_VALUES, this.emptyResponseHandler.bind(this)], |
| 69 | [OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION, this.emptyResponseHandler.bind(this)], |
| 70 | ]); |
| 71 | this.jsonSchemas = new Map<OCPP16RequestCommand, JSONSchemaType<JsonObject>>([ |
| 72 | [ |
| 73 | OCPP16RequestCommand.BOOT_NOTIFICATION, |
| 74 | JSON.parse( |
| 75 | fs.readFileSync( |
| 76 | path.resolve( |
| 77 | path.dirname(fileURLToPath(import.meta.url)), |
| 78 | '../../../assets/json-schemas/ocpp/1.6/BootNotificationResponse.json' |
| 79 | ), |
| 80 | 'utf8' |
| 81 | ) |
| 82 | ) as JSONSchemaType<OCPP16BootNotificationResponse>, |
| 83 | ], |
| 84 | [ |
| 85 | OCPP16RequestCommand.HEARTBEAT, |
| 86 | JSON.parse( |
| 87 | fs.readFileSync( |
| 88 | path.resolve( |
| 89 | path.dirname(fileURLToPath(import.meta.url)), |
| 90 | '../../../assets/json-schemas/ocpp/1.6/HeartbeatResponse.json' |
| 91 | ), |
| 92 | 'utf8' |
| 93 | ) |
| 94 | ) as JSONSchemaType<OCPP16HeartbeatResponse>, |
| 95 | ], |
| 96 | [ |
| 97 | OCPP16RequestCommand.AUTHORIZE, |
| 98 | JSON.parse( |
| 99 | fs.readFileSync( |
| 100 | path.resolve( |
| 101 | path.dirname(fileURLToPath(import.meta.url)), |
| 102 | '../../../assets/json-schemas/ocpp/1.6/AuthorizeResponse.json' |
| 103 | ), |
| 104 | 'utf8' |
| 105 | ) |
| 106 | ) as JSONSchemaType<OCPP16AuthorizeResponse>, |
| 107 | ], |
| 108 | [ |
| 109 | OCPP16RequestCommand.START_TRANSACTION, |
| 110 | JSON.parse( |
| 111 | fs.readFileSync( |
| 112 | path.resolve( |
| 113 | path.dirname(fileURLToPath(import.meta.url)), |
| 114 | '../../../assets/json-schemas/ocpp/1.6/StartTransactionResponse.json' |
| 115 | ), |
| 116 | 'utf8' |
| 117 | ) |
| 118 | ) as JSONSchemaType<OCPP16StartTransactionResponse>, |
| 119 | ], |
| 120 | [ |
| 121 | OCPP16RequestCommand.STOP_TRANSACTION, |
| 122 | JSON.parse( |
| 123 | fs.readFileSync( |
| 124 | path.resolve( |
| 125 | path.dirname(fileURLToPath(import.meta.url)), |
| 126 | '../../../assets/json-schemas/ocpp/1.6/StopTransactionResponse.json' |
| 127 | ), |
| 128 | 'utf8' |
| 129 | ) |
| 130 | ) as JSONSchemaType<OCPP16StopTransactionResponse>, |
| 131 | ], |
| 132 | [ |
| 133 | OCPP16RequestCommand.STATUS_NOTIFICATION, |
| 134 | JSON.parse( |
| 135 | fs.readFileSync( |
| 136 | path.resolve( |
| 137 | path.dirname(fileURLToPath(import.meta.url)), |
| 138 | '../../../assets/json-schemas/ocpp/1.6/StatusNotificationResponse.json' |
| 139 | ), |
| 140 | 'utf8' |
| 141 | ) |
| 142 | ) as JSONSchemaType<OCPP16StatusNotificationResponse>, |
| 143 | ], |
| 144 | [ |
| 145 | OCPP16RequestCommand.METER_VALUES, |
| 146 | JSON.parse( |
| 147 | fs.readFileSync( |
| 148 | path.resolve( |
| 149 | path.dirname(fileURLToPath(import.meta.url)), |
| 150 | '../../../assets/json-schemas/ocpp/1.6/MeterValuesResponse.json' |
| 151 | ), |
| 152 | 'utf8' |
| 153 | ) |
| 154 | ) as JSONSchemaType<OCPP16MeterValuesResponse>, |
| 155 | ], |
| 156 | [ |
| 157 | OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION, |
| 158 | JSON.parse( |
| 159 | fs.readFileSync( |
| 160 | path.resolve( |
| 161 | path.dirname(fileURLToPath(import.meta.url)), |
| 162 | '../../../assets/json-schemas/ocpp/1.6/DiagnosticsStatusNotificationResponse.json' |
| 163 | ), |
| 164 | 'utf8' |
| 165 | ) |
| 166 | ) as JSONSchemaType<DiagnosticsStatusNotificationResponse>, |
| 167 | ], |
| 168 | ]); |
| 169 | this.validatePayload.bind(this); |
| 170 | } |
| 171 | |
| 172 | public async responseHandler( |
| 173 | chargingStation: ChargingStation, |
| 174 | commandName: OCPP16RequestCommand, |
| 175 | payload: JsonType, |
| 176 | requestPayload: JsonType |
| 177 | ): Promise<void> { |
| 178 | if (chargingStation.isRegistered() || commandName === OCPP16RequestCommand.BOOT_NOTIFICATION) { |
| 179 | if ( |
| 180 | this.responseHandlers.has(commandName) && |
| 181 | ChargingStationUtils.isRequestCommandSupported(commandName, chargingStation) |
| 182 | ) { |
| 183 | try { |
| 184 | this.validatePayload(chargingStation, commandName, payload); |
| 185 | await this.responseHandlers.get(commandName)(chargingStation, payload, requestPayload); |
| 186 | } catch (error) { |
| 187 | logger.error( |
| 188 | `${chargingStation.logPrefix()} ${moduleName}.responseHandler: Handle response error:`, |
| 189 | error |
| 190 | ); |
| 191 | throw error; |
| 192 | } |
| 193 | } else { |
| 194 | // Throw exception |
| 195 | throw new OCPPError( |
| 196 | ErrorType.NOT_IMPLEMENTED, |
| 197 | `${commandName} is not implemented to handle response PDU ${JSON.stringify( |
| 198 | payload, |
| 199 | null, |
| 200 | 2 |
| 201 | )}`, |
| 202 | commandName, |
| 203 | payload |
| 204 | ); |
| 205 | } |
| 206 | } else { |
| 207 | throw new OCPPError( |
| 208 | ErrorType.SECURITY_ERROR, |
| 209 | `${commandName} cannot be issued to handle response PDU ${JSON.stringify( |
| 210 | payload, |
| 211 | null, |
| 212 | 2 |
| 213 | )} while the charging station is not registered on the central server. `, |
| 214 | commandName, |
| 215 | payload |
| 216 | ); |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | private validatePayload( |
| 221 | chargingStation: ChargingStation, |
| 222 | commandName: OCPP16RequestCommand, |
| 223 | payload: JsonType |
| 224 | ): boolean { |
| 225 | if (this.jsonSchemas.has(commandName)) { |
| 226 | return this.validateResponsePayload( |
| 227 | chargingStation, |
| 228 | commandName, |
| 229 | this.jsonSchemas.get(commandName), |
| 230 | payload |
| 231 | ); |
| 232 | } |
| 233 | logger.warn( |
| 234 | `${chargingStation.logPrefix()} ${moduleName}.validatePayload: No JSON schema found for command ${commandName} PDU validation` |
| 235 | ); |
| 236 | return false; |
| 237 | } |
| 238 | |
| 239 | private handleResponseBootNotification( |
| 240 | chargingStation: ChargingStation, |
| 241 | payload: OCPP16BootNotificationResponse |
| 242 | ): void { |
| 243 | if (payload.status === OCPP16RegistrationStatus.ACCEPTED) { |
| 244 | ChargingStationConfigurationUtils.addConfigurationKey( |
| 245 | chargingStation, |
| 246 | OCPP16StandardParametersKey.HeartbeatInterval, |
| 247 | payload.interval.toString(), |
| 248 | {}, |
| 249 | { overwrite: true, save: true } |
| 250 | ); |
| 251 | ChargingStationConfigurationUtils.addConfigurationKey( |
| 252 | chargingStation, |
| 253 | OCPP16StandardParametersKey.HeartBeatInterval, |
| 254 | payload.interval.toString(), |
| 255 | { visible: false }, |
| 256 | { overwrite: true, save: true } |
| 257 | ); |
| 258 | chargingStation.heartbeatSetInterval |
| 259 | ? chargingStation.restartHeartbeat() |
| 260 | : chargingStation.startHeartbeat(); |
| 261 | } |
| 262 | if (Object.values(OCPP16RegistrationStatus).includes(payload.status)) { |
| 263 | const logMsg = `${chargingStation.logPrefix()} Charging station in '${ |
| 264 | payload.status |
| 265 | }' state on the central server`; |
| 266 | payload.status === OCPP16RegistrationStatus.REJECTED |
| 267 | ? logger.warn(logMsg) |
| 268 | : logger.info(logMsg); |
| 269 | } else { |
| 270 | logger.error( |
| 271 | chargingStation.logPrefix() + |
| 272 | ' Charging station boot notification response received: %j with undefined registration status', |
| 273 | payload |
| 274 | ); |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | private handleResponseAuthorize( |
| 279 | chargingStation: ChargingStation, |
| 280 | payload: OCPP16AuthorizeResponse, |
| 281 | requestPayload: OCPP16AuthorizeRequest |
| 282 | ): void { |
| 283 | let authorizeConnectorId: number; |
| 284 | for (const connectorId of chargingStation.connectors.keys()) { |
| 285 | if ( |
| 286 | connectorId > 0 && |
| 287 | chargingStation.getConnectorStatus(connectorId)?.authorizeIdTag === requestPayload.idTag |
| 288 | ) { |
| 289 | authorizeConnectorId = connectorId; |
| 290 | break; |
| 291 | } |
| 292 | } |
| 293 | const isAuthorizeConnectorIdDefined = authorizeConnectorId !== undefined; |
| 294 | if (payload.idTagInfo.status === OCPP16AuthorizationStatus.ACCEPTED) { |
| 295 | isAuthorizeConnectorIdDefined && |
| 296 | (chargingStation.getConnectorStatus(authorizeConnectorId).idTagAuthorized = true); |
| 297 | logger.debug( |
| 298 | `${chargingStation.logPrefix()} IdTag '${requestPayload.idTag}' accepted${ |
| 299 | isAuthorizeConnectorIdDefined ? ` on connector ${authorizeConnectorId}` : '' |
| 300 | }` |
| 301 | ); |
| 302 | } else { |
| 303 | if (isAuthorizeConnectorIdDefined) { |
| 304 | chargingStation.getConnectorStatus(authorizeConnectorId).idTagAuthorized = false; |
| 305 | delete chargingStation.getConnectorStatus(authorizeConnectorId).authorizeIdTag; |
| 306 | } |
| 307 | logger.debug( |
| 308 | `${chargingStation.logPrefix()} IdTag '${requestPayload.idTag}' rejected with status '${ |
| 309 | payload.idTagInfo.status |
| 310 | }'${isAuthorizeConnectorIdDefined ? ` on connector ${authorizeConnectorId}` : ''}` |
| 311 | ); |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | private async handleResponseStartTransaction( |
| 316 | chargingStation: ChargingStation, |
| 317 | payload: OCPP16StartTransactionResponse, |
| 318 | requestPayload: OCPP16StartTransactionRequest |
| 319 | ): Promise<void> { |
| 320 | const connectorId = requestPayload.connectorId; |
| 321 | |
| 322 | let transactionConnectorId: number; |
| 323 | for (const id of chargingStation.connectors.keys()) { |
| 324 | if (id > 0 && id === connectorId) { |
| 325 | transactionConnectorId = id; |
| 326 | break; |
| 327 | } |
| 328 | } |
| 329 | if (!transactionConnectorId) { |
| 330 | logger.error( |
| 331 | chargingStation.logPrefix() + |
| 332 | ' Trying to start a transaction on a non existing connector Id ' + |
| 333 | connectorId.toString() |
| 334 | ); |
| 335 | return; |
| 336 | } |
| 337 | if ( |
| 338 | chargingStation.getConnectorStatus(connectorId).transactionRemoteStarted === true && |
| 339 | chargingStation.getAuthorizeRemoteTxRequests() && |
| 340 | chargingStation.getLocalAuthListEnabled() && |
| 341 | chargingStation.hasAuthorizedTags() && |
| 342 | chargingStation.getConnectorStatus(connectorId).idTagLocalAuthorized === false |
| 343 | ) { |
| 344 | logger.error( |
| 345 | chargingStation.logPrefix() + |
| 346 | ' Trying to start a transaction with a not local authorized idTag ' + |
| 347 | chargingStation.getConnectorStatus(connectorId).localAuthorizeIdTag + |
| 348 | ' on connector Id ' + |
| 349 | connectorId.toString() |
| 350 | ); |
| 351 | await this.resetConnectorOnStartTransactionError(chargingStation, connectorId); |
| 352 | return; |
| 353 | } |
| 354 | if ( |
| 355 | chargingStation.getConnectorStatus(connectorId).transactionRemoteStarted === true && |
| 356 | chargingStation.getAuthorizeRemoteTxRequests() && |
| 357 | chargingStation.getMustAuthorizeAtRemoteStart() && |
| 358 | chargingStation.getConnectorStatus(connectorId).idTagLocalAuthorized === false && |
| 359 | chargingStation.getConnectorStatus(connectorId).idTagAuthorized === false |
| 360 | ) { |
| 361 | logger.error( |
| 362 | chargingStation.logPrefix() + |
| 363 | ' Trying to start a transaction with a not authorized idTag ' + |
| 364 | chargingStation.getConnectorStatus(connectorId).authorizeIdTag + |
| 365 | ' on connector Id ' + |
| 366 | connectorId.toString() |
| 367 | ); |
| 368 | await this.resetConnectorOnStartTransactionError(chargingStation, connectorId); |
| 369 | return; |
| 370 | } |
| 371 | if ( |
| 372 | chargingStation.getConnectorStatus(connectorId).idTagAuthorized && |
| 373 | chargingStation.getConnectorStatus(connectorId).authorizeIdTag !== requestPayload.idTag |
| 374 | ) { |
| 375 | logger.error( |
| 376 | chargingStation.logPrefix() + |
| 377 | ' Trying to start a transaction with an idTag ' + |
| 378 | requestPayload.idTag + |
| 379 | ' different from the authorize request one ' + |
| 380 | chargingStation.getConnectorStatus(connectorId).authorizeIdTag + |
| 381 | ' on connector Id ' + |
| 382 | connectorId.toString() |
| 383 | ); |
| 384 | await this.resetConnectorOnStartTransactionError(chargingStation, connectorId); |
| 385 | return; |
| 386 | } |
| 387 | if ( |
| 388 | chargingStation.getConnectorStatus(connectorId).idTagLocalAuthorized && |
| 389 | chargingStation.getConnectorStatus(connectorId).localAuthorizeIdTag !== requestPayload.idTag |
| 390 | ) { |
| 391 | logger.error( |
| 392 | chargingStation.logPrefix() + |
| 393 | ' Trying to start a transaction with an idTag ' + |
| 394 | requestPayload.idTag + |
| 395 | ' different from the local authorized one ' + |
| 396 | chargingStation.getConnectorStatus(connectorId).localAuthorizeIdTag + |
| 397 | ' on connector Id ' + |
| 398 | connectorId.toString() |
| 399 | ); |
| 400 | await this.resetConnectorOnStartTransactionError(chargingStation, connectorId); |
| 401 | return; |
| 402 | } |
| 403 | if (chargingStation.getConnectorStatus(connectorId)?.transactionStarted === true) { |
| 404 | logger.debug( |
| 405 | chargingStation.logPrefix() + |
| 406 | ' Trying to start a transaction on an already used connector ' + |
| 407 | connectorId.toString() + |
| 408 | ': %j', |
| 409 | chargingStation.getConnectorStatus(connectorId) |
| 410 | ); |
| 411 | return; |
| 412 | } |
| 413 | if ( |
| 414 | chargingStation.getConnectorStatus(connectorId)?.status !== |
| 415 | OCPP16ChargePointStatus.AVAILABLE && |
| 416 | chargingStation.getConnectorStatus(connectorId)?.status !== OCPP16ChargePointStatus.PREPARING |
| 417 | ) { |
| 418 | logger.error( |
| 419 | `${chargingStation.logPrefix()} Trying to start a transaction on connector ${connectorId.toString()} with status ${ |
| 420 | chargingStation.getConnectorStatus(connectorId)?.status |
| 421 | }` |
| 422 | ); |
| 423 | return; |
| 424 | } |
| 425 | // if (!Number.isInteger(payload.transactionId)) { |
| 426 | // logger.warn( |
| 427 | // `${chargingStation.logPrefix()} Trying to start a transaction on connector ${connectorId.toString()} with a non integer transaction Id ${ |
| 428 | // payload.transactionId |
| 429 | // }, converting to integer` |
| 430 | // ); |
| 431 | // payload.transactionId = Utils.convertToInt(payload.transactionId); |
| 432 | // } |
| 433 | |
| 434 | if (payload.idTagInfo?.status === OCPP16AuthorizationStatus.ACCEPTED) { |
| 435 | chargingStation.getConnectorStatus(connectorId).transactionStarted = true; |
| 436 | chargingStation.getConnectorStatus(connectorId).transactionId = payload.transactionId; |
| 437 | chargingStation.getConnectorStatus(connectorId).transactionIdTag = requestPayload.idTag; |
| 438 | chargingStation.getConnectorStatus( |
| 439 | connectorId |
| 440 | ).transactionEnergyActiveImportRegisterValue = 0; |
| 441 | chargingStation.getConnectorStatus(connectorId).transactionBeginMeterValue = |
| 442 | OCPP16ServiceUtils.buildTransactionBeginMeterValue( |
| 443 | chargingStation, |
| 444 | connectorId, |
| 445 | requestPayload.meterStart |
| 446 | ); |
| 447 | chargingStation.getBeginEndMeterValues() && |
| 448 | (await chargingStation.ocppRequestService.requestHandler< |
| 449 | OCPP16MeterValuesRequest, |
| 450 | OCPP16MeterValuesResponse |
| 451 | >(chargingStation, OCPP16RequestCommand.METER_VALUES, { |
| 452 | connectorId, |
| 453 | transactionId: payload.transactionId, |
| 454 | meterValue: [chargingStation.getConnectorStatus(connectorId).transactionBeginMeterValue], |
| 455 | })); |
| 456 | await chargingStation.ocppRequestService.requestHandler< |
| 457 | OCPP16StatusNotificationRequest, |
| 458 | OCPP16StatusNotificationResponse |
| 459 | >(chargingStation, OCPP16RequestCommand.STATUS_NOTIFICATION, { |
| 460 | connectorId, |
| 461 | status: OCPP16ChargePointStatus.CHARGING, |
| 462 | errorCode: OCPP16ChargePointErrorCode.NO_ERROR, |
| 463 | }); |
| 464 | chargingStation.getConnectorStatus(connectorId).status = OCPP16ChargePointStatus.CHARGING; |
| 465 | logger.info( |
| 466 | chargingStation.logPrefix() + |
| 467 | ' Transaction ' + |
| 468 | payload.transactionId.toString() + |
| 469 | ' STARTED on ' + |
| 470 | chargingStation.stationInfo.chargingStationId + |
| 471 | '#' + |
| 472 | connectorId.toString() + |
| 473 | " for idTag '" + |
| 474 | requestPayload.idTag + |
| 475 | "'" |
| 476 | ); |
| 477 | if (chargingStation.stationInfo.powerSharedByConnectors) { |
| 478 | chargingStation.powerDivider++; |
| 479 | } |
| 480 | const configuredMeterValueSampleInterval = |
| 481 | ChargingStationConfigurationUtils.getConfigurationKey( |
| 482 | chargingStation, |
| 483 | OCPP16StandardParametersKey.MeterValueSampleInterval |
| 484 | ); |
| 485 | chargingStation.startMeterValues( |
| 486 | connectorId, |
| 487 | configuredMeterValueSampleInterval |
| 488 | ? Utils.convertToInt(configuredMeterValueSampleInterval.value) * 1000 |
| 489 | : Constants.DEFAULT_METER_VALUES_INTERVAL |
| 490 | ); |
| 491 | } else { |
| 492 | logger.warn( |
| 493 | chargingStation.logPrefix() + |
| 494 | ' Starting transaction id ' + |
| 495 | payload.transactionId.toString() + |
| 496 | " REJECTED with status '" + |
| 497 | payload?.idTagInfo?.status + |
| 498 | "', idTag '" + |
| 499 | requestPayload.idTag + |
| 500 | "'" |
| 501 | ); |
| 502 | await this.resetConnectorOnStartTransactionError(chargingStation, connectorId); |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | private async resetConnectorOnStartTransactionError( |
| 507 | chargingStation: ChargingStation, |
| 508 | connectorId: number |
| 509 | ): Promise<void> { |
| 510 | chargingStation.resetConnectorStatus(connectorId); |
| 511 | if ( |
| 512 | chargingStation.getConnectorStatus(connectorId).status !== OCPP16ChargePointStatus.AVAILABLE |
| 513 | ) { |
| 514 | await chargingStation.ocppRequestService.requestHandler< |
| 515 | OCPP16StatusNotificationRequest, |
| 516 | OCPP16StatusNotificationResponse |
| 517 | >(chargingStation, OCPP16RequestCommand.STATUS_NOTIFICATION, { |
| 518 | connectorId, |
| 519 | status: OCPP16ChargePointStatus.AVAILABLE, |
| 520 | errorCode: OCPP16ChargePointErrorCode.NO_ERROR, |
| 521 | }); |
| 522 | chargingStation.getConnectorStatus(connectorId).status = OCPP16ChargePointStatus.AVAILABLE; |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | private async handleResponseStopTransaction( |
| 527 | chargingStation: ChargingStation, |
| 528 | payload: OCPP16StopTransactionResponse, |
| 529 | requestPayload: OCPP16StopTransactionRequest |
| 530 | ): Promise<void> { |
| 531 | const transactionConnectorId = chargingStation.getConnectorIdByTransactionId( |
| 532 | requestPayload.transactionId |
| 533 | ); |
| 534 | if (!transactionConnectorId) { |
| 535 | logger.error( |
| 536 | chargingStation.logPrefix() + |
| 537 | ' Trying to stop a non existing transaction ' + |
| 538 | requestPayload.transactionId.toString() |
| 539 | ); |
| 540 | return; |
| 541 | } |
| 542 | if (payload.idTagInfo?.status === OCPP16AuthorizationStatus.ACCEPTED) { |
| 543 | chargingStation.getBeginEndMeterValues() && |
| 544 | !chargingStation.getOcppStrictCompliance() && |
| 545 | chargingStation.getOutOfOrderEndMeterValues() && |
| 546 | (await chargingStation.ocppRequestService.requestHandler< |
| 547 | OCPP16MeterValuesRequest, |
| 548 | OCPP16MeterValuesResponse |
| 549 | >(chargingStation, OCPP16RequestCommand.METER_VALUES, { |
| 550 | connectorId: transactionConnectorId, |
| 551 | transactionId: requestPayload.transactionId, |
| 552 | meterValue: [ |
| 553 | OCPP16ServiceUtils.buildTransactionEndMeterValue( |
| 554 | chargingStation, |
| 555 | transactionConnectorId, |
| 556 | requestPayload.meterStop |
| 557 | ), |
| 558 | ], |
| 559 | })); |
| 560 | if ( |
| 561 | !chargingStation.isChargingStationAvailable() || |
| 562 | !chargingStation.isConnectorAvailable(transactionConnectorId) |
| 563 | ) { |
| 564 | await chargingStation.ocppRequestService.requestHandler< |
| 565 | OCPP16StatusNotificationRequest, |
| 566 | OCPP16StatusNotificationResponse |
| 567 | >(chargingStation, OCPP16RequestCommand.STATUS_NOTIFICATION, { |
| 568 | connectorId: transactionConnectorId, |
| 569 | status: OCPP16ChargePointStatus.UNAVAILABLE, |
| 570 | errorCode: OCPP16ChargePointErrorCode.NO_ERROR, |
| 571 | }); |
| 572 | chargingStation.getConnectorStatus(transactionConnectorId).status = |
| 573 | OCPP16ChargePointStatus.UNAVAILABLE; |
| 574 | } else { |
| 575 | await chargingStation.ocppRequestService.requestHandler< |
| 576 | OCPP16BootNotificationRequest, |
| 577 | OCPP16BootNotificationResponse |
| 578 | >(chargingStation, OCPP16RequestCommand.STATUS_NOTIFICATION, { |
| 579 | connectorId: transactionConnectorId, |
| 580 | status: OCPP16ChargePointStatus.AVAILABLE, |
| 581 | errorCode: OCPP16ChargePointErrorCode.NO_ERROR, |
| 582 | }); |
| 583 | chargingStation.getConnectorStatus(transactionConnectorId).status = |
| 584 | OCPP16ChargePointStatus.AVAILABLE; |
| 585 | } |
| 586 | if (chargingStation.stationInfo.powerSharedByConnectors) { |
| 587 | chargingStation.powerDivider--; |
| 588 | } |
| 589 | chargingStation.resetConnectorStatus(transactionConnectorId); |
| 590 | logger.info( |
| 591 | chargingStation.logPrefix() + |
| 592 | ' Transaction ' + |
| 593 | requestPayload.transactionId.toString() + |
| 594 | ' STOPPED on ' + |
| 595 | chargingStation.stationInfo.chargingStationId + |
| 596 | '#' + |
| 597 | transactionConnectorId.toString() |
| 598 | ); |
| 599 | } else { |
| 600 | logger.warn( |
| 601 | chargingStation.logPrefix() + |
| 602 | ' Stopping transaction id ' + |
| 603 | requestPayload.transactionId.toString() + |
| 604 | " REJECTED with status '" + |
| 605 | payload.idTagInfo?.status + |
| 606 | "'" |
| 607 | ); |
| 608 | } |
| 609 | } |
| 610 | } |