From f22266fd29f7f11ba883d7f799ef607be05d4232 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sun, 13 Mar 2022 14:29:29 +0100 Subject: [PATCH] Strong type sendMessageHandler response with generics MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- .../AutomaticTransactionGenerator.ts | 67 ++++---- src/charging-station/ChargingStation.ts | 153 +++++++++++------- .../ocpp/1.6/OCPP16IncomingRequestService.ts | 70 ++++---- .../ocpp/1.6/OCPP16RequestService.ts | 45 ++---- .../ocpp/1.6/OCPP16ResponseService.ts | 24 +-- .../ocpp/OCPPRequestService.ts | 4 +- src/types/ocpp/1.6/MeterValues.ts | 2 +- src/types/ocpp/1.6/Responses.ts | 4 +- src/types/ocpp/Responses.ts | 9 ++ 9 files changed, 210 insertions(+), 168 deletions(-) diff --git a/src/charging-station/AutomaticTransactionGenerator.ts b/src/charging-station/AutomaticTransactionGenerator.ts index 985e8e47..32d98973 100644 --- a/src/charging-station/AutomaticTransactionGenerator.ts +++ b/src/charging-station/AutomaticTransactionGenerator.ts @@ -10,6 +10,7 @@ import { import type ChargingStation from './ChargingStation'; import Constants from '../utils/Constants'; +import { MeterValuesResponse } from '../types/ocpp/Responses'; import { OCPP16ServiceUtils } from './ocpp/1.6/OCPP16ServiceUtils'; import PerformanceStatistics from '../performance/PerformanceStatistics'; import { RequestCommand } from '../types/ocpp/Requests'; @@ -274,24 +275,25 @@ export default class AutomaticTransactionGenerator { this.chargingStation.getConnectorStatus(connectorId).authorizeIdTag = idTag; // Authorize idTag const authorizeResponse: AuthorizeResponse = - (await this.chargingStation.ocppRequestService.sendMessageHandler( + await this.chargingStation.ocppRequestService.sendMessageHandler( RequestCommand.AUTHORIZE, { idTag, } - )) as AuthorizeResponse; + ); this.connectorsStatus.get(connectorId).authorizeRequests++; if (authorizeResponse?.idTagInfo?.status === AuthorizationStatus.ACCEPTED) { this.connectorsStatus.get(connectorId).acceptedAuthorizeRequests++; logger.info(this.logPrefix(connectorId) + ' start transaction for idTag ' + idTag); // Start transaction - startResponse = (await this.chargingStation.ocppRequestService.sendMessageHandler( - RequestCommand.START_TRANSACTION, - { - connectorId, - idTag, - } - )) as StartTransactionResponse; + startResponse = + await this.chargingStation.ocppRequestService.sendMessageHandler( + RequestCommand.START_TRANSACTION, + { + connectorId, + idTag, + } + ); PerformanceStatistics.endMeasure(measureId, beginId); return startResponse; } @@ -301,21 +303,23 @@ export default class AutomaticTransactionGenerator { } logger.info(this.logPrefix(connectorId) + ' start transaction for idTag ' + idTag); // Start transaction - startResponse = (await this.chargingStation.ocppRequestService.sendMessageHandler( - RequestCommand.START_TRANSACTION, - { - connectorId, - idTag, - } - )) as StartTransactionResponse; + startResponse = + await this.chargingStation.ocppRequestService.sendMessageHandler( + RequestCommand.START_TRANSACTION, + { + connectorId, + idTag, + } + ); PerformanceStatistics.endMeasure(measureId, beginId); return startResponse; } logger.info(this.logPrefix(connectorId) + ' start transaction without an idTag'); - startResponse = (await this.chargingStation.ocppRequestService.sendMessageHandler( - RequestCommand.START_TRANSACTION, - { connectorId } - )) as StartTransactionResponse; + startResponse = + await this.chargingStation.ocppRequestService.sendMessageHandler( + RequestCommand.START_TRANSACTION, + { connectorId } + ); PerformanceStatistics.endMeasure(measureId, beginId); return startResponse; } @@ -341,7 +345,7 @@ export default class AutomaticTransactionGenerator { connectorId, this.chargingStation.getEnergyActiveImportRegisterByTransactionId(transactionId) ); - await this.chargingStation.ocppRequestService.sendMessageHandler( + await this.chargingStation.ocppRequestService.sendMessageHandler( RequestCommand.METER_VALUES, { connectorId, @@ -350,16 +354,17 @@ export default class AutomaticTransactionGenerator { } ); } - stopResponse = (await this.chargingStation.ocppRequestService.sendMessageHandler( - RequestCommand.STOP_TRANSACTION, - { - transactionId, - meterStop: - this.chargingStation.getEnergyActiveImportRegisterByTransactionId(transactionId), - idTag: this.chargingStation.getTransactionIdTag(transactionId), - reason, - } - )) as StopTransactionResponse; + stopResponse = + await this.chargingStation.ocppRequestService.sendMessageHandler( + RequestCommand.STOP_TRANSACTION, + { + transactionId, + meterStop: + this.chargingStation.getEnergyActiveImportRegisterByTransactionId(transactionId), + idTag: this.chargingStation.getTransactionIdTag(transactionId), + reason, + } + ); this.connectorsStatus.get(connectorId).stopTransactionRequests++; } else { logger.warn( diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index f9175803..bd355771 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -8,7 +8,13 @@ import { IncomingRequestCommand, RequestCommand, } from '../types/ocpp/Requests'; -import { BootNotificationResponse, RegistrationStatus } from '../types/ocpp/Responses'; +import { + BootNotificationResponse, + HeartbeatResponse, + MeterValuesResponse, + RegistrationStatus, + StatusNotificationResponse, +} from '../types/ocpp/Responses'; import ChargingStationConfiguration, { ConfigurationKey, } from '../types/ChargingStationConfiguration'; @@ -24,6 +30,7 @@ import { VendorDefaultParametersKey, } from '../types/ocpp/Configuration'; import { MeterValue, MeterValueMeasurand, MeterValuePhase } from '../types/ocpp/MeterValues'; +import { StopTransactionReason, StopTransactionResponse } from '../types/ocpp/Transaction'; import { WSError, WebSocketCloseEventStatusCode } from '../types/WebSocket'; import WebSocket, { ClientOptions, Data, OPEN, RawData } from 'ws'; @@ -52,7 +59,6 @@ import OCPPRequestService from './ocpp/OCPPRequestService'; import { OCPPVersion } from '../types/ocpp/OCPPVersion'; import PerformanceStatistics from '../performance/PerformanceStatistics'; import { SampledValueTemplate } from '../types/MeasurandPerPhaseSampledValueTemplates'; -import { StopTransactionReason } from '../types/ocpp/Transaction'; import { SupervisionUrlDistribution } from '../types/ConfigurationData'; import { URL } from 'url'; import Utils from '../utils/Utils'; @@ -395,7 +401,9 @@ export default class ChargingStation { ) { // eslint-disable-next-line @typescript-eslint/no-misused-promises this.heartbeatSetInterval = setInterval(async (): Promise => { - await this.ocppRequestService.sendMessageHandler(RequestCommand.HEARTBEAT); + await this.ocppRequestService.sendMessageHandler( + RequestCommand.HEARTBEAT + ); }, this.getHeartbeatInterval()); logger.info( this.logPrefix() + @@ -465,11 +473,14 @@ export default class ChargingStation { this.getConnectorStatus(connectorId).transactionId, interval ); - await this.ocppRequestService.sendMessageHandler(RequestCommand.METER_VALUES, { - connectorId, - transactionId: this.getConnectorStatus(connectorId).transactionId, - meterValue: [meterValue], - }); + await this.ocppRequestService.sendMessageHandler( + RequestCommand.METER_VALUES, + { + connectorId, + transactionId: this.getConnectorStatus(connectorId).transactionId, + meterValue: [meterValue], + } + ); }, interval ); @@ -567,11 +578,14 @@ export default class ChargingStation { await this.stopMessageSequence(reason); for (const connectorId of this.connectors.keys()) { if (connectorId > 0) { - await this.ocppRequestService.sendMessageHandler(RequestCommand.STATUS_NOTIFICATION, { - connectorId, - status: ChargePointStatus.UNAVAILABLE, - errorCode: ChargePointErrorCode.NO_ERROR, - }); + await this.ocppRequestService.sendMessageHandler( + RequestCommand.STATUS_NOTIFICATION, + { + connectorId, + status: ChargePointStatus.UNAVAILABLE, + errorCode: ChargePointErrorCode.NO_ERROR, + } + ); this.getConnectorStatus(connectorId).status = ChargePointStatus.UNAVAILABLE; } } @@ -1116,21 +1130,22 @@ export default class ChargingStation { // Send BootNotification let registrationRetryCount = 0; do { - this.bootNotificationResponse = (await this.ocppRequestService.sendMessageHandler( - RequestCommand.BOOT_NOTIFICATION, - { - chargePointModel: this.bootNotificationRequest.chargePointModel, - chargePointVendor: this.bootNotificationRequest.chargePointVendor, - chargeBoxSerialNumber: this.bootNotificationRequest.chargeBoxSerialNumber, - firmwareVersion: this.bootNotificationRequest.firmwareVersion, - chargePointSerialNumber: this.bootNotificationRequest.chargePointSerialNumber, - iccid: this.bootNotificationRequest.iccid, - imsi: this.bootNotificationRequest.imsi, - meterSerialNumber: this.bootNotificationRequest.meterSerialNumber, - meterType: this.bootNotificationRequest.meterType, - }, - { skipBufferingOnError: true } - )) as BootNotificationResponse; + this.bootNotificationResponse = + await this.ocppRequestService.sendMessageHandler( + RequestCommand.BOOT_NOTIFICATION, + { + chargePointModel: this.bootNotificationRequest.chargePointModel, + chargePointVendor: this.bootNotificationRequest.chargePointVendor, + chargeBoxSerialNumber: this.bootNotificationRequest.chargeBoxSerialNumber, + firmwareVersion: this.bootNotificationRequest.firmwareVersion, + chargePointSerialNumber: this.bootNotificationRequest.chargePointSerialNumber, + iccid: this.bootNotificationRequest.iccid, + imsi: this.bootNotificationRequest.imsi, + meterSerialNumber: this.bootNotificationRequest.meterSerialNumber, + meterType: this.bootNotificationRequest.meterType, + }, + { skipBufferingOnError: true } + ); if (!this.isInAcceptedState()) { this.getRegistrationMaxRetries() !== -1 && registrationRetryCount++; await Utils.sleep( @@ -1419,7 +1434,7 @@ export default class ChargingStation { private async startMessageSequence(): Promise { if (this.stationInfo.autoRegister) { - await this.ocppRequestService.sendMessageHandler( + await this.ocppRequestService.sendMessageHandler( RequestCommand.BOOT_NOTIFICATION, { chargePointModel: this.bootNotificationRequest.chargePointModel, @@ -1449,11 +1464,14 @@ export default class ChargingStation { this.getConnectorStatus(connectorId)?.bootStatus ) { // Send status in template at startup - await this.ocppRequestService.sendMessageHandler(RequestCommand.STATUS_NOTIFICATION, { - connectorId, - status: this.getConnectorStatus(connectorId).bootStatus, - errorCode: ChargePointErrorCode.NO_ERROR, - }); + await this.ocppRequestService.sendMessageHandler( + RequestCommand.STATUS_NOTIFICATION, + { + connectorId, + status: this.getConnectorStatus(connectorId).bootStatus, + errorCode: ChargePointErrorCode.NO_ERROR, + } + ); this.getConnectorStatus(connectorId).status = this.getConnectorStatus(connectorId).bootStatus; } else if ( @@ -1462,27 +1480,36 @@ export default class ChargingStation { this.getConnectorStatus(connectorId)?.bootStatus ) { // Send status in template after reset - await this.ocppRequestService.sendMessageHandler(RequestCommand.STATUS_NOTIFICATION, { - connectorId, - status: this.getConnectorStatus(connectorId).bootStatus, - errorCode: ChargePointErrorCode.NO_ERROR, - }); + await this.ocppRequestService.sendMessageHandler( + RequestCommand.STATUS_NOTIFICATION, + { + connectorId, + status: this.getConnectorStatus(connectorId).bootStatus, + errorCode: ChargePointErrorCode.NO_ERROR, + } + ); this.getConnectorStatus(connectorId).status = this.getConnectorStatus(connectorId).bootStatus; } else if (!this.stopped && this.getConnectorStatus(connectorId)?.status) { // Send previous status at template reload - await this.ocppRequestService.sendMessageHandler(RequestCommand.STATUS_NOTIFICATION, { - connectorId, - status: this.getConnectorStatus(connectorId).status, - errorCode: ChargePointErrorCode.NO_ERROR, - }); + await this.ocppRequestService.sendMessageHandler( + RequestCommand.STATUS_NOTIFICATION, + { + connectorId, + status: this.getConnectorStatus(connectorId).status, + errorCode: ChargePointErrorCode.NO_ERROR, + } + ); } else { // Send default status - await this.ocppRequestService.sendMessageHandler(RequestCommand.STATUS_NOTIFICATION, { - connectorId, - status: ChargePointStatus.AVAILABLE, - errorCode: ChargePointErrorCode.NO_ERROR, - }); + await this.ocppRequestService.sendMessageHandler( + RequestCommand.STATUS_NOTIFICATION, + { + connectorId, + status: ChargePointStatus.AVAILABLE, + errorCode: ChargePointErrorCode.NO_ERROR, + } + ); this.getConnectorStatus(connectorId).status = ChargePointStatus.AVAILABLE; } } @@ -1529,18 +1556,24 @@ export default class ChargingStation { connectorId, this.getEnergyActiveImportRegisterByTransactionId(transactionId) ); - await this.ocppRequestService.sendMessageHandler(RequestCommand.METER_VALUES, { - connectorId, - transactionId, - meterValue: transactionEndMeterValue, - }); + await this.ocppRequestService.sendMessageHandler( + RequestCommand.METER_VALUES, + { + connectorId, + transactionId, + meterValue: transactionEndMeterValue, + } + ); } - await this.ocppRequestService.sendMessageHandler(RequestCommand.STOP_TRANSACTION, { - transactionId, - meterStop: this.getEnergyActiveImportRegisterByTransactionId(transactionId), - idTag: this.getTransactionIdTag(transactionId), - reason, - }); + await this.ocppRequestService.sendMessageHandler( + RequestCommand.STOP_TRANSACTION, + { + transactionId, + meterStop: this.getEnergyActiveImportRegisterByTransactionId(transactionId), + idTag: this.getTransactionIdTag(transactionId), + reason, + } + ); } } } diff --git a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts index bfb229be..6e66807d 100644 --- a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts @@ -21,8 +21,12 @@ import { ChangeAvailabilityResponse, ChangeConfigurationResponse, ClearChargingProfileResponse, + DiagnosticsStatusNotificationResponse, GetConfigurationResponse, GetDiagnosticsResponse, + OCPP16BootNotificationResponse, + OCPP16HeartbeatResponse, + OCPP16StatusNotificationResponse, OCPP16TriggerMessageResponse, SetChargingProfileResponse, UnlockConnectorResponse, @@ -49,6 +53,7 @@ import { JsonType } from '../../../types/JsonType'; import { OCPP16ChargePointErrorCode } from '../../../types/ocpp/1.6/ChargePointErrorCode'; import { OCPP16ChargePointStatus } from '../../../types/ocpp/1.6/ChargePointStatus'; import { OCPP16DiagnosticsStatus } from '../../../types/ocpp/1.6/DiagnosticsStatus'; +import { OCPP16MeterValuesResponse } from '../../../types/ocpp/1.6/MeterValues'; import { OCPP16ServiceUtils } from './OCPP16ServiceUtils'; import { OCPP16StandardParametersKey } from '../../../types/ocpp/1.6/Configuration'; import { OCPPConfigurationKey } from '../../../types/ocpp/Configuration'; @@ -217,7 +222,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer connectorId, this.chargingStation.getEnergyActiveImportRegisterByTransactionId(transactionId) ); - await this.chargingStation.ocppRequestService.sendMessageHandler( + await this.chargingStation.ocppRequestService.sendMessageHandler( OCPP16RequestCommand.METER_VALUES, { connectorId, @@ -226,22 +231,23 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer } ); } - const stopResponse = (await this.chargingStation.ocppRequestService.sendMessageHandler( - OCPP16RequestCommand.STOP_TRANSACTION, - { - transactionId, - meterStop: - this.chargingStation.getEnergyActiveImportRegisterByTransactionId(transactionId), - idTag: this.chargingStation.getTransactionIdTag(transactionId), - reason: OCPP16StopTransactionReason.UNLOCK_COMMAND, - } - )) as OCPP16StopTransactionResponse; + const stopResponse = + await this.chargingStation.ocppRequestService.sendMessageHandler( + OCPP16RequestCommand.STOP_TRANSACTION, + { + transactionId, + meterStop: + this.chargingStation.getEnergyActiveImportRegisterByTransactionId(transactionId), + idTag: this.chargingStation.getTransactionIdTag(transactionId), + reason: OCPP16StopTransactionReason.UNLOCK_COMMAND, + } + ); if (stopResponse.idTagInfo?.status === OCPP16AuthorizationStatus.ACCEPTED) { return Constants.OCPP_RESPONSE_UNLOCKED; } return Constants.OCPP_RESPONSE_UNLOCK_FAILED; } - await this.chargingStation.ocppRequestService.sendMessageHandler( + await this.chargingStation.ocppRequestService.sendMessageHandler( OCPP16RequestCommand.STATUS_NOTIFICATION, { connectorId, @@ -489,7 +495,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer } this.chargingStation.getConnectorStatus(id).availability = commandPayload.type; if (response === Constants.OCPP_AVAILABILITY_RESPONSE_ACCEPTED) { - await this.chargingStation.ocppRequestService.sendMessageHandler( + await this.chargingStation.ocppRequestService.sendMessageHandler( OCPP16RequestCommand.STATUS_NOTIFICATION, { connectorId: id, @@ -514,7 +520,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer return Constants.OCPP_AVAILABILITY_RESPONSE_SCHEDULED; } this.chargingStation.getConnectorStatus(connectorId).availability = commandPayload.type; - await this.chargingStation.ocppRequestService.sendMessageHandler( + await this.chargingStation.ocppRequestService.sendMessageHandler( OCPP16RequestCommand.STATUS_NOTIFICATION, { connectorId, status: chargePointStatus, errorCode: OCPP16ChargePointErrorCode.NO_ERROR } ); @@ -530,7 +536,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer const transactionConnectorId = commandPayload.connectorId; const connectorStatus = this.chargingStation.getConnectorStatus(transactionConnectorId); if (transactionConnectorId) { - await this.chargingStation.ocppRequestService.sendMessageHandler( + await this.chargingStation.ocppRequestService.sendMessageHandler( OCPP16RequestCommand.STATUS_NOTIFICATION, { connectorId: transactionConnectorId, @@ -554,12 +560,12 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer } else if (this.chargingStation.getMayAuthorizeAtRemoteStart()) { connectorStatus.authorizeIdTag = commandPayload.idTag; const authorizeResponse: OCPP16AuthorizeResponse = - (await this.chargingStation.ocppRequestService.sendMessageHandler( + await this.chargingStation.ocppRequestService.sendMessageHandler( OCPP16RequestCommand.AUTHORIZE, { idTag: commandPayload.idTag, } - )) as OCPP16AuthorizeResponse; + ); if (authorizeResponse?.idTagInfo?.status === OCPP16AuthorizationStatus.ACCEPTED) { authorized = true; } @@ -579,13 +585,13 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer connectorStatus.transactionRemoteStarted = true; if ( ( - (await this.chargingStation.ocppRequestService.sendMessageHandler( + await this.chargingStation.ocppRequestService.sendMessageHandler( OCPP16RequestCommand.START_TRANSACTION, { connectorId: transactionConnectorId, idTag: commandPayload.idTag, } - )) as OCPP16StartTransactionResponse + ) ).idTagInfo.status === OCPP16AuthorizationStatus.ACCEPTED ) { logger.debug( @@ -624,13 +630,13 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer connectorStatus.transactionRemoteStarted = true; if ( ( - (await this.chargingStation.ocppRequestService.sendMessageHandler( + await this.chargingStation.ocppRequestService.sendMessageHandler( OCPP16RequestCommand.START_TRANSACTION, { connectorId: transactionConnectorId, idTag: commandPayload.idTag, } - )) as OCPP16StartTransactionResponse + ) ).idTagInfo.status === OCPP16AuthorizationStatus.ACCEPTED ) { logger.debug( @@ -670,7 +676,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer this.chargingStation.getConnectorStatus(connectorId).status !== OCPP16ChargePointStatus.AVAILABLE ) { - await this.chargingStation.ocppRequestService.sendMessageHandler( + await this.chargingStation.ocppRequestService.sendMessageHandler( OCPP16RequestCommand.STATUS_NOTIFICATION, { connectorId, @@ -727,7 +733,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer connectorId > 0 && this.chargingStation.getConnectorStatus(connectorId)?.transactionId === transactionId ) { - await this.chargingStation.ocppRequestService.sendMessageHandler( + await this.chargingStation.ocppRequestService.sendMessageHandler( OCPP16RequestCommand.STATUS_NOTIFICATION, { connectorId, @@ -748,7 +754,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer connectorId, this.chargingStation.getEnergyActiveImportRegisterByTransactionId(transactionId) ); - await this.chargingStation.ocppRequestService.sendMessageHandler( + await this.chargingStation.ocppRequestService.sendMessageHandler( OCPP16RequestCommand.METER_VALUES, { connectorId, @@ -757,7 +763,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer } ); } - await this.chargingStation.ocppRequestService.sendMessageHandler( + await this.chargingStation.ocppRequestService.sendMessageHandler( OCPP16RequestCommand.STOP_TRANSACTION, { transactionId, @@ -814,7 +820,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer info.bytes / 1024 } bytes transferred from diagnostics archive ${info.name}` ); - await this.chargingStation.ocppRequestService.sendMessageHandler( + await this.chargingStation.ocppRequestService.sendMessageHandler( OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION, { status: OCPP16DiagnosticsStatus.Uploading, @@ -826,7 +832,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer uri.pathname + diagnosticsArchive ); if (uploadResponse.code === 226) { - await this.chargingStation.ocppRequestService.sendMessageHandler( + await this.chargingStation.ocppRequestService.sendMessageHandler( OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION, { status: OCPP16DiagnosticsStatus.Uploaded, @@ -853,7 +859,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer OCPP16IncomingRequestCommand.GET_DIAGNOSTICS ); } catch (error) { - await this.chargingStation.ocppRequestService.sendMessageHandler( + await this.chargingStation.ocppRequestService.sendMessageHandler( OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION, { status: OCPP16DiagnosticsStatus.UploadFailed, @@ -874,7 +880,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer uri.protocol } to transfer the diagnostic logs archive` ); - await this.chargingStation.ocppRequestService.sendMessageHandler( + await this.chargingStation.ocppRequestService.sendMessageHandler( OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION, { status: OCPP16DiagnosticsStatus.UploadFailed, @@ -892,7 +898,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer case MessageTrigger.BootNotification: setTimeout(() => { this.chargingStation.ocppRequestService - .sendMessageHandler( + .sendMessageHandler( OCPP16RequestCommand.BOOT_NOTIFICATION, { chargePointModel: @@ -921,7 +927,9 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer case MessageTrigger.Heartbeat: setTimeout(() => { this.chargingStation.ocppRequestService - .sendMessageHandler(OCPP16RequestCommand.HEARTBEAT, null, { triggerMessage: true }) + .sendMessageHandler(OCPP16RequestCommand.HEARTBEAT, null, { + triggerMessage: true, + }) .catch(() => { /* This is intentional */ }); diff --git a/src/charging-station/ocpp/1.6/OCPP16RequestService.ts b/src/charging-station/ocpp/1.6/OCPP16RequestService.ts index 571ac066..3d91576b 100644 --- a/src/charging-station/ocpp/1.6/OCPP16RequestService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16RequestService.ts @@ -1,28 +1,15 @@ // Partial Copyright Jerome Benoit. 2021. All Rights Reserved. -import { - AuthorizeRequest, - StartTransactionRequest, - StopTransactionRequest, -} from '../../../types/ocpp/1.6/Transaction'; -import { - DiagnosticsStatusNotificationRequest, - HeartbeatRequest, - OCPP16BootNotificationRequest, - OCPP16RequestCommand, - StatusNotificationRequest, -} from '../../../types/ocpp/1.6/Requests'; -import { ResponseType, SendParams } from '../../../types/ocpp/Requests'; - import type ChargingStation from '../../ChargingStation'; import Constants from '../../../utils/Constants'; import { ErrorType } from '../../../types/ocpp/ErrorType'; import { JsonType } from '../../../types/JsonType'; -import { MeterValuesRequest } from '../../../types/ocpp/1.6/MeterValues'; +import { OCPP16RequestCommand } from '../../../types/ocpp/1.6/Requests'; import { OCPP16ServiceUtils } from './OCPP16ServiceUtils'; import OCPPError from '../../../exception/OCPPError'; import OCPPRequestService from '../OCPPRequestService'; import type OCPPResponseService from '../OCPPResponseService'; +import { SendParams } from '../../../types/ocpp/Requests'; import Utils from '../../../utils/Utils'; const moduleName = 'OCPP16RequestService'; @@ -35,18 +22,18 @@ export default class OCPP16RequestService extends OCPPRequestService { super(chargingStation, ocppResponseService); } - public async sendMessageHandler( + public async sendMessageHandler( commandName: OCPP16RequestCommand, commandParams?: JsonType, params?: SendParams - ): Promise { + ): Promise { if (Object.values(OCPP16RequestCommand).includes(commandName)) { - return this.sendMessage( + return (await this.sendMessage( Utils.generateUUID(), this.buildCommandPayload(commandName, commandParams), commandName, params - ); + )) as unknown as Response; } throw new OCPPError( ErrorType.NOT_SUPPORTED, @@ -56,10 +43,10 @@ export default class OCPP16RequestService extends OCPPRequestService { ); } - private buildCommandPayload( + private buildCommandPayload( commandName: OCPP16RequestCommand, commandParams?: JsonType - ): JsonType { + ): Request { let connectorId: number; switch (commandName) { case OCPP16RequestCommand.AUTHORIZE: @@ -67,7 +54,7 @@ export default class OCPP16RequestService extends OCPPRequestService { ...(!Utils.isUndefined(commandParams?.idTag) ? { idTag: commandParams.idTag } : { idTag: Constants.DEFAULT_IDTAG }), - } as AuthorizeRequest; + } as unknown as Request; case OCPP16RequestCommand.BOOT_NOTIFICATION: return { chargePointModel: commandParams?.chargePointModel, @@ -89,13 +76,13 @@ export default class OCPP16RequestService extends OCPPRequestService { ...(!Utils.isUndefined(commandParams?.meterType) && { meterType: commandParams.meterType, }), - } as OCPP16BootNotificationRequest; + } as unknown as Request; case OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION: return { status: commandParams?.diagnosticsStatus, - } as DiagnosticsStatusNotificationRequest; + } as unknown as Request; case OCPP16RequestCommand.HEARTBEAT: - return {} as HeartbeatRequest; + return {} as unknown as Request; case OCPP16RequestCommand.METER_VALUES: return { connectorId: commandParams?.connectorId, @@ -103,13 +90,13 @@ export default class OCPP16RequestService extends OCPPRequestService { meterValue: Array.isArray(commandParams?.meterValue) ? commandParams?.meterValue : [commandParams?.meterValue], - } as MeterValuesRequest; + } as unknown as Request; case OCPP16RequestCommand.STATUS_NOTIFICATION: return { connectorId: commandParams?.connectorId, status: commandParams?.status, errorCode: commandParams?.errorCode, - } as StatusNotificationRequest; + } as unknown as Request; case OCPP16RequestCommand.START_TRANSACTION: return { connectorId: commandParams?.connectorId, @@ -120,7 +107,7 @@ export default class OCPP16RequestService extends OCPPRequestService { commandParams?.connectorId as number ), timestamp: new Date().toISOString(), - } as StartTransactionRequest; + } as unknown as Request; case OCPP16RequestCommand.STOP_TRANSACTION: connectorId = this.chargingStation.getConnectorIdByTransactionId( commandParams?.transactionId as number @@ -141,7 +128,7 @@ export default class OCPP16RequestService extends OCPPRequestService { ) ), }), - } as StopTransactionRequest; + } as unknown as Request; default: throw new OCPPError( ErrorType.NOT_SUPPORTED, diff --git a/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts b/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts index 9eb82d2a..92fee660 100644 --- a/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts @@ -14,13 +14,13 @@ import { OCPP16RequestCommand, StatusNotificationRequest, } from '../../../types/ocpp/1.6/Requests'; +import { MeterValuesRequest, OCPP16MeterValuesResponse } from '../../../types/ocpp/1.6/MeterValues'; import { - HeartbeatResponse, OCPP16BootNotificationResponse, + OCPP16HeartbeatResponse, OCPP16RegistrationStatus, - StatusNotificationResponse, + OCPP16StatusNotificationResponse, } from '../../../types/ocpp/1.6/Responses'; -import { MeterValuesRequest, MeterValuesResponse } from '../../../types/ocpp/1.6/MeterValues'; import type ChargingStation from '../../ChargingStation'; import { ErrorType } from '../../../types/ocpp/ErrorType'; @@ -135,7 +135,7 @@ export default class OCPP16ResponseService extends OCPPResponseService { } private handleResponseHeartbeat( - payload: HeartbeatResponse, + payload: OCPP16HeartbeatResponse, requestPayload: HeartbeatRequest ): void { logger.debug( @@ -313,7 +313,7 @@ export default class OCPP16ResponseService extends OCPPResponseService { requestPayload.meterStart ); this.chargingStation.getBeginEndMeterValues() && - (await this.chargingStation.ocppRequestService.sendMessageHandler( + (await this.chargingStation.ocppRequestService.sendMessageHandler( OCPP16RequestCommand.METER_VALUES, { connectorId, @@ -322,7 +322,7 @@ export default class OCPP16ResponseService extends OCPPResponseService { this.chargingStation.getConnectorStatus(connectorId).transactionBeginMeterValue, } )); - await this.chargingStation.ocppRequestService.sendMessageHandler( + await this.chargingStation.ocppRequestService.sendMessageHandler( OCPP16RequestCommand.STATUS_NOTIFICATION, { connectorId, @@ -375,7 +375,7 @@ export default class OCPP16ResponseService extends OCPPResponseService { this.chargingStation.getConnectorStatus(connectorId).status !== OCPP16ChargePointStatus.AVAILABLE ) { - await this.chargingStation.ocppRequestService.sendMessageHandler( + await this.chargingStation.ocppRequestService.sendMessageHandler( OCPP16RequestCommand.STATUS_NOTIFICATION, { connectorId, @@ -407,7 +407,7 @@ export default class OCPP16ResponseService extends OCPPResponseService { this.chargingStation.getBeginEndMeterValues() && !this.chargingStation.getOcppStrictCompliance() && this.chargingStation.getOutOfOrderEndMeterValues() && - (await this.chargingStation.ocppRequestService.sendMessageHandler( + (await this.chargingStation.ocppRequestService.sendMessageHandler( OCPP16RequestCommand.METER_VALUES, { connectorId: transactionConnectorId, @@ -423,7 +423,7 @@ export default class OCPP16ResponseService extends OCPPResponseService { !this.chargingStation.isChargingStationAvailable() || !this.chargingStation.isConnectorAvailable(transactionConnectorId) ) { - await this.chargingStation.ocppRequestService.sendMessageHandler( + await this.chargingStation.ocppRequestService.sendMessageHandler( OCPP16RequestCommand.STATUS_NOTIFICATION, { connectorId: transactionConnectorId, @@ -434,7 +434,7 @@ export default class OCPP16ResponseService extends OCPPResponseService { this.chargingStation.getConnectorStatus(transactionConnectorId).status = OCPP16ChargePointStatus.UNAVAILABLE; } else { - await this.chargingStation.ocppRequestService.sendMessageHandler( + await this.chargingStation.ocppRequestService.sendMessageHandler( OCPP16RequestCommand.STATUS_NOTIFICATION, { connectorId: transactionConnectorId, @@ -472,7 +472,7 @@ export default class OCPP16ResponseService extends OCPPResponseService { private handleResponseStatusNotification( payload: StatusNotificationRequest, - requestPayload: StatusNotificationResponse + requestPayload: OCPP16StatusNotificationResponse ): void { logger.debug( this.chargingStation.logPrefix() + @@ -484,7 +484,7 @@ export default class OCPP16ResponseService extends OCPPResponseService { private handleResponseMeterValues( payload: MeterValuesRequest, - requestPayload: MeterValuesResponse + requestPayload: OCPP16MeterValuesResponse ): void { logger.debug( this.chargingStation.logPrefix() + diff --git a/src/charging-station/ocpp/OCPPRequestService.ts b/src/charging-station/ocpp/OCPPRequestService.ts index 4ab79d9d..7b50b62a 100644 --- a/src/charging-station/ocpp/OCPPRequestService.ts +++ b/src/charging-station/ocpp/OCPPRequestService.ts @@ -315,9 +315,9 @@ export default abstract class OCPPRequestService { } } - public abstract sendMessageHandler( + public abstract sendMessageHandler( commandName: RequestCommand, commandParams?: JsonType, params?: SendParams - ): Promise; + ): Promise; } diff --git a/src/types/ocpp/1.6/MeterValues.ts b/src/types/ocpp/1.6/MeterValues.ts index 282df11a..70967dde 100644 --- a/src/types/ocpp/1.6/MeterValues.ts +++ b/src/types/ocpp/1.6/MeterValues.ts @@ -103,4 +103,4 @@ export interface MeterValuesRequest extends JsonType { meterValue: OCPP16MeterValue[]; } -export type MeterValuesResponse = EmptyObject; +export type OCPP16MeterValuesResponse = EmptyObject; diff --git a/src/types/ocpp/1.6/Responses.ts b/src/types/ocpp/1.6/Responses.ts index d6a5d347..ac59c291 100644 --- a/src/types/ocpp/1.6/Responses.ts +++ b/src/types/ocpp/1.6/Responses.ts @@ -2,7 +2,7 @@ import { EmptyObject } from '../../EmptyObject'; import { JsonType } from '../../JsonType'; import { OCPPConfigurationKey } from '../Configuration'; -export interface HeartbeatResponse extends JsonType { +export interface OCPP16HeartbeatResponse extends JsonType { currentTime: string; } @@ -39,7 +39,7 @@ export interface OCPP16BootNotificationResponse extends JsonType { interval: number; } -export type StatusNotificationResponse = EmptyObject; +export type OCPP16StatusNotificationResponse = EmptyObject; export interface GetConfigurationResponse extends JsonType { configurationKey: OCPPConfigurationKey[]; diff --git a/src/types/ocpp/Responses.ts b/src/types/ocpp/Responses.ts index 50f1d2b1..d7e6db55 100644 --- a/src/types/ocpp/Responses.ts +++ b/src/types/ocpp/Responses.ts @@ -4,12 +4,15 @@ import { OCPP16ChargingProfileStatus, OCPP16ClearChargingProfileStatus, OCPP16ConfigurationStatus, + OCPP16HeartbeatResponse, OCPP16RegistrationStatus, + OCPP16StatusNotificationResponse, OCPP16TriggerMessageStatus, OCPP16UnlockStatus, } from './1.6/Responses'; import { JsonType } from '../JsonType'; +import { OCPP16MeterValuesResponse } from './1.6/MeterValues'; export type ResponseHandler = ( payload: JsonType | string, @@ -18,6 +21,12 @@ export type ResponseHandler = ( export type BootNotificationResponse = OCPP16BootNotificationResponse; +export type HeartbeatResponse = OCPP16HeartbeatResponse; + +export type StatusNotificationResponse = OCPP16StatusNotificationResponse; + +export type MeterValuesResponse = OCPP16MeterValuesResponse; + export enum DefaultStatus { ACCEPTED = 'Accepted', REJECTED = 'Rejected', -- 2.34.1