From: Jérôme Benoit Date: Sun, 27 Mar 2022 18:47:01 +0000 (+0200) Subject: Strong type OCPP message sending X-Git-Tag: v1.1.57~25 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=ef6fa3fb6f4872fc57bae634ac3edb8164a0bc79;p=e-mobility-charging-stations-simulator.git Strong type OCPP message sending Signed-off-by: Jérôme Benoit --- diff --git a/src/charging-station/AutomaticTransactionGenerator.ts b/src/charging-station/AutomaticTransactionGenerator.ts index 32d98973..a8bd79ac 100644 --- a/src/charging-station/AutomaticTransactionGenerator.ts +++ b/src/charging-station/AutomaticTransactionGenerator.ts @@ -2,18 +2,21 @@ import { AuthorizationStatus, + AuthorizeRequest, AuthorizeResponse, + StartTransactionRequest, StartTransactionResponse, StopTransactionReason, + StopTransactionRequest, StopTransactionResponse, } from '../types/ocpp/Transaction'; +import { MeterValuesRequest, RequestCommand } from '../types/ocpp/Requests'; 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'; import { Status } from '../types/AutomaticTransactionGenerator'; import Utils from '../utils/Utils'; import logger from '../utils/Logger'; @@ -275,25 +278,24 @@ export default class AutomaticTransactionGenerator { this.chargingStation.getConnectorStatus(connectorId).authorizeIdTag = idTag; // Authorize idTag const authorizeResponse: AuthorizeResponse = - await this.chargingStation.ocppRequestService.sendMessageHandler( - RequestCommand.AUTHORIZE, - { - idTag, - } - ); + await this.chargingStation.ocppRequestService.sendMessageHandler< + AuthorizeRequest, + AuthorizeResponse + >(RequestCommand.AUTHORIZE, { + idTag, + }); 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, - } - ); + startResponse = await this.chargingStation.ocppRequestService.sendMessageHandler< + StartTransactionRequest, + StartTransactionResponse + >(RequestCommand.START_TRANSACTION, { + connectorId, + idTag, + }); PerformanceStatistics.endMeasure(measureId, beginId); return startResponse; } @@ -303,23 +305,21 @@ 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, - } - ); + startResponse = await this.chargingStation.ocppRequestService.sendMessageHandler< + StartTransactionRequest, + StartTransactionResponse + >(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 } - ); + startResponse = await this.chargingStation.ocppRequestService.sendMessageHandler< + StartTransactionRequest, + StartTransactionResponse + >(RequestCommand.START_TRANSACTION, { connectorId }); PerformanceStatistics.endMeasure(measureId, beginId); return startResponse; } @@ -345,26 +345,24 @@ export default class AutomaticTransactionGenerator { connectorId, this.chargingStation.getEnergyActiveImportRegisterByTransactionId(transactionId) ); - await this.chargingStation.ocppRequestService.sendMessageHandler( - RequestCommand.METER_VALUES, - { - connectorId, - transactionId, - meterValue: transactionEndMeterValue, - } - ); + await this.chargingStation.ocppRequestService.sendMessageHandler< + MeterValuesRequest, + MeterValuesResponse + >(RequestCommand.METER_VALUES, { + connectorId, + transactionId, + meterValue: transactionEndMeterValue, + }); } - stopResponse = - await this.chargingStation.ocppRequestService.sendMessageHandler( - RequestCommand.STOP_TRANSACTION, - { - transactionId, - meterStop: - this.chargingStation.getEnergyActiveImportRegisterByTransactionId(transactionId), - idTag: this.chargingStation.getTransactionIdTag(transactionId), - reason, - } - ); + stopResponse = await this.chargingStation.ocppRequestService.sendMessageHandler< + StopTransactionRequest, + StopTransactionResponse + >(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 418035a7..bceba844 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -5,9 +5,12 @@ import { AvailabilityType, BootNotificationRequest, CachedRequest, + HeartbeatRequest, IncomingRequest, IncomingRequestCommand, + MeterValuesRequest, RequestCommand, + StatusNotificationRequest, } from '../types/ocpp/Requests'; import { BootNotificationResponse, @@ -39,7 +42,11 @@ import { VendorDefaultParametersKey, } from '../types/ocpp/Configuration'; import { MeterValue, MeterValueMeasurand, MeterValuePhase } from '../types/ocpp/MeterValues'; -import { StopTransactionReason, StopTransactionResponse } from '../types/ocpp/Transaction'; +import { + StopTransactionReason, + StopTransactionRequest, + StopTransactionResponse, +} from '../types/ocpp/Transaction'; import { WSError, WebSocketCloseEventStatusCode } from '../types/WebSocket'; import WebSocket, { Data, OPEN, RawData } from 'ws'; @@ -437,7 +444,7 @@ export default class ChargingStation { ) { // eslint-disable-next-line @typescript-eslint/no-misused-promises this.heartbeatSetInterval = setInterval(async (): Promise => { - await this.ocppRequestService.sendMessageHandler( + await this.ocppRequestService.sendMessageHandler( RequestCommand.HEARTBEAT ); }, this.getHeartbeatInterval()); @@ -509,7 +516,7 @@ export default class ChargingStation { this.getConnectorStatus(connectorId).transactionId, interval ); - await this.ocppRequestService.sendMessageHandler( + await this.ocppRequestService.sendMessageHandler( RequestCommand.METER_VALUES, { connectorId, @@ -614,14 +621,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< + StatusNotificationRequest, + StatusNotificationResponse + >(RequestCommand.STATUS_NOTIFICATION, { + connectorId, + status: ChargePointStatus.UNAVAILABLE, + errorCode: ChargePointErrorCode.NO_ERROR, + }); this.getConnectorStatus(connectorId).status = ChargePointStatus.UNAVAILABLE; } } @@ -1379,22 +1386,24 @@ 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 } - ); + this.bootNotificationResponse = await this.ocppRequestService.sendMessageHandler< + BootNotificationRequest, + BootNotificationResponse + >( + 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( @@ -1724,7 +1733,10 @@ export default class ChargingStation { private async startMessageSequence(): Promise { if (this.stationInfo.autoRegister) { - await this.ocppRequestService.sendMessageHandler( + await this.ocppRequestService.sendMessageHandler< + BootNotificationRequest, + BootNotificationResponse + >( RequestCommand.BOOT_NOTIFICATION, { chargePointModel: this.bootNotificationRequest.chargePointModel, @@ -1754,14 +1766,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< + StatusNotificationRequest, + StatusNotificationResponse + >(RequestCommand.STATUS_NOTIFICATION, { + connectorId, + status: this.getConnectorStatus(connectorId).bootStatus, + errorCode: ChargePointErrorCode.NO_ERROR, + }); this.getConnectorStatus(connectorId).status = this.getConnectorStatus(connectorId).bootStatus; } else if ( @@ -1770,36 +1782,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< + StatusNotificationRequest, + StatusNotificationResponse + >(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< + StatusNotificationRequest, + StatusNotificationResponse + >(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< + StatusNotificationRequest, + StatusNotificationResponse + >(RequestCommand.STATUS_NOTIFICATION, { + connectorId, + status: ChargePointStatus.AVAILABLE, + errorCode: ChargePointErrorCode.NO_ERROR, + }); this.getConnectorStatus(connectorId).status = ChargePointStatus.AVAILABLE; } } @@ -1846,24 +1858,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.STOP_TRANSACTION, - { + await this.ocppRequestService.sendMessageHandler< + MeterValuesRequest, + MeterValuesResponse + >(RequestCommand.METER_VALUES, { + connectorId, transactionId, - meterStop: this.getEnergyActiveImportRegisterByTransactionId(transactionId), - idTag: this.getTransactionIdTag(transactionId), - reason, - } - ); + meterValue: transactionEndMeterValue, + }); + } + await this.ocppRequestService.sendMessageHandler< + StopTransactionRequest, + StopTransactionResponse + >(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 298e50da..7e04e6bf 100644 --- a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts @@ -4,12 +4,16 @@ import { ChangeAvailabilityRequest, ChangeConfigurationRequest, ClearChargingProfileRequest, + DiagnosticsStatusNotificationRequest, GetConfigurationRequest, GetDiagnosticsRequest, MessageTrigger, OCPP16AvailabilityType, + OCPP16BootNotificationRequest, + OCPP16HeartbeatRequest, OCPP16IncomingRequestCommand, OCPP16RequestCommand, + OCPP16StatusNotificationRequest, OCPP16TriggerMessageRequest, RemoteStartTransactionRequest, RemoteStopTransactionRequest, @@ -38,11 +42,18 @@ import { import { Client, FTPResponse } from 'basic-ftp'; import { OCPP16AuthorizationStatus, + OCPP16AuthorizeRequest, OCPP16AuthorizeResponse, + OCPP16StartTransactionRequest, OCPP16StartTransactionResponse, OCPP16StopTransactionReason, + OCPP16StopTransactionRequest, OCPP16StopTransactionResponse, } from '../../../types/ocpp/1.6/Transaction'; +import { + OCPP16MeterValuesRequest, + OCPP16MeterValuesResponse, +} from '../../../types/ocpp/1.6/MeterValues'; import type ChargingStation from '../../ChargingStation'; import Constants from '../../../utils/Constants'; @@ -53,7 +64,6 @@ 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'; @@ -222,39 +232,37 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer connectorId, this.chargingStation.getEnergyActiveImportRegisterByTransactionId(transactionId) ); - await this.chargingStation.ocppRequestService.sendMessageHandler( - OCPP16RequestCommand.METER_VALUES, - { - connectorId, - transactionId, - meterValue: transactionEndMeterValue, - } - ); + await this.chargingStation.ocppRequestService.sendMessageHandler< + OCPP16MeterValuesRequest, + OCPP16MeterValuesResponse + >(OCPP16RequestCommand.METER_VALUES, { + connectorId, + transactionId, + meterValue: transactionEndMeterValue, + }); } - 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, - } - ); + const stopResponse = await this.chargingStation.ocppRequestService.sendMessageHandler< + OCPP16StopTransactionRequest, + OCPP16StopTransactionResponse + >(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( - OCPP16RequestCommand.STATUS_NOTIFICATION, - { - connectorId, - status: OCPP16ChargePointStatus.AVAILABLE, - errorCode: OCPP16ChargePointErrorCode.NO_ERROR, - } - ); + await this.chargingStation.ocppRequestService.sendMessageHandler< + OCPP16StatusNotificationRequest, + OCPP16StatusNotificationResponse + >(OCPP16RequestCommand.STATUS_NOTIFICATION, { + connectorId, + status: OCPP16ChargePointStatus.AVAILABLE, + errorCode: OCPP16ChargePointErrorCode.NO_ERROR, + }); this.chargingStation.getConnectorStatus(connectorId).status = OCPP16ChargePointStatus.AVAILABLE; return Constants.OCPP_RESPONSE_UNLOCKED; } @@ -501,14 +509,14 @@ 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( - OCPP16RequestCommand.STATUS_NOTIFICATION, - { - connectorId: id, - status: chargePointStatus, - errorCode: OCPP16ChargePointErrorCode.NO_ERROR, - } - ); + await this.chargingStation.ocppRequestService.sendMessageHandler< + OCPP16StatusNotificationRequest, + OCPP16StatusNotificationResponse + >(OCPP16RequestCommand.STATUS_NOTIFICATION, { + connectorId: id, + status: chargePointStatus, + errorCode: OCPP16ChargePointErrorCode.NO_ERROR, + }); this.chargingStation.getConnectorStatus(id).status = chargePointStatus; } } @@ -526,10 +534,14 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer return Constants.OCPP_AVAILABILITY_RESPONSE_SCHEDULED; } this.chargingStation.getConnectorStatus(connectorId).availability = commandPayload.type; - await this.chargingStation.ocppRequestService.sendMessageHandler( - OCPP16RequestCommand.STATUS_NOTIFICATION, - { connectorId, status: chargePointStatus, errorCode: OCPP16ChargePointErrorCode.NO_ERROR } - ); + await this.chargingStation.ocppRequestService.sendMessageHandler< + OCPP16StatusNotificationRequest, + OCPP16StatusNotificationResponse + >(OCPP16RequestCommand.STATUS_NOTIFICATION, { + connectorId, + status: chargePointStatus, + errorCode: OCPP16ChargePointErrorCode.NO_ERROR, + }); this.chargingStation.getConnectorStatus(connectorId).status = chargePointStatus; return Constants.OCPP_AVAILABILITY_RESPONSE_ACCEPTED; } @@ -542,14 +554,14 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer const transactionConnectorId = commandPayload.connectorId; const connectorStatus = this.chargingStation.getConnectorStatus(transactionConnectorId); if (transactionConnectorId) { - await this.chargingStation.ocppRequestService.sendMessageHandler( - OCPP16RequestCommand.STATUS_NOTIFICATION, - { - connectorId: transactionConnectorId, - status: OCPP16ChargePointStatus.PREPARING, - errorCode: OCPP16ChargePointErrorCode.NO_ERROR, - } - ); + await this.chargingStation.ocppRequestService.sendMessageHandler< + OCPP16StatusNotificationRequest, + OCPP16StatusNotificationResponse + >(OCPP16RequestCommand.STATUS_NOTIFICATION, { + connectorId: transactionConnectorId, + status: OCPP16ChargePointStatus.PREPARING, + errorCode: OCPP16ChargePointErrorCode.NO_ERROR, + }); connectorStatus.status = OCPP16ChargePointStatus.PREPARING; if (this.chargingStation.isChargingStationAvailable() && connectorStatus) { // Check if authorized @@ -566,12 +578,12 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer } else if (this.chargingStation.getMayAuthorizeAtRemoteStart()) { connectorStatus.authorizeIdTag = commandPayload.idTag; const authorizeResponse: OCPP16AuthorizeResponse = - await this.chargingStation.ocppRequestService.sendMessageHandler( - OCPP16RequestCommand.AUTHORIZE, - { - idTag: commandPayload.idTag, - } - ); + await this.chargingStation.ocppRequestService.sendMessageHandler< + OCPP16AuthorizeRequest, + OCPP16AuthorizeResponse + >(OCPP16RequestCommand.AUTHORIZE, { + idTag: commandPayload.idTag, + }); if (authorizeResponse?.idTagInfo?.status === OCPP16AuthorizationStatus.ACCEPTED) { authorized = true; } @@ -591,13 +603,13 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer connectorStatus.transactionRemoteStarted = true; if ( ( - await this.chargingStation.ocppRequestService.sendMessageHandler( - OCPP16RequestCommand.START_TRANSACTION, - { - connectorId: transactionConnectorId, - idTag: commandPayload.idTag, - } - ) + await this.chargingStation.ocppRequestService.sendMessageHandler< + OCPP16StartTransactionRequest, + OCPP16StartTransactionResponse + >(OCPP16RequestCommand.START_TRANSACTION, { + connectorId: transactionConnectorId, + idTag: commandPayload.idTag, + }) ).idTagInfo.status === OCPP16AuthorizationStatus.ACCEPTED ) { logger.debug( @@ -636,13 +648,13 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer connectorStatus.transactionRemoteStarted = true; if ( ( - await this.chargingStation.ocppRequestService.sendMessageHandler( - OCPP16RequestCommand.START_TRANSACTION, - { - connectorId: transactionConnectorId, - idTag: commandPayload.idTag, - } - ) + await this.chargingStation.ocppRequestService.sendMessageHandler< + OCPP16StartTransactionRequest, + OCPP16StartTransactionResponse + >(OCPP16RequestCommand.START_TRANSACTION, { + connectorId: transactionConnectorId, + idTag: commandPayload.idTag, + }) ).idTagInfo.status === OCPP16AuthorizationStatus.ACCEPTED ) { logger.debug( @@ -682,14 +694,14 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer this.chargingStation.getConnectorStatus(connectorId).status !== OCPP16ChargePointStatus.AVAILABLE ) { - await this.chargingStation.ocppRequestService.sendMessageHandler( - OCPP16RequestCommand.STATUS_NOTIFICATION, - { - connectorId, - status: OCPP16ChargePointStatus.AVAILABLE, - errorCode: OCPP16ChargePointErrorCode.NO_ERROR, - } - ); + await this.chargingStation.ocppRequestService.sendMessageHandler< + OCPP16StatusNotificationRequest, + OCPP16StatusNotificationResponse + >(OCPP16RequestCommand.STATUS_NOTIFICATION, { + connectorId, + status: OCPP16ChargePointStatus.AVAILABLE, + errorCode: OCPP16ChargePointErrorCode.NO_ERROR, + }); this.chargingStation.getConnectorStatus(connectorId).status = OCPP16ChargePointStatus.AVAILABLE; } @@ -739,14 +751,14 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer connectorId > 0 && this.chargingStation.getConnectorStatus(connectorId)?.transactionId === transactionId ) { - await this.chargingStation.ocppRequestService.sendMessageHandler( - OCPP16RequestCommand.STATUS_NOTIFICATION, - { - connectorId, - status: OCPP16ChargePointStatus.FINISHING, - errorCode: OCPP16ChargePointErrorCode.NO_ERROR, - } - ); + await this.chargingStation.ocppRequestService.sendMessageHandler< + OCPP16StatusNotificationRequest, + OCPP16StatusNotificationResponse + >(OCPP16RequestCommand.STATUS_NOTIFICATION, { + connectorId, + status: OCPP16ChargePointStatus.FINISHING, + errorCode: OCPP16ChargePointErrorCode.NO_ERROR, + }); this.chargingStation.getConnectorStatus(connectorId).status = OCPP16ChargePointStatus.FINISHING; if ( @@ -760,24 +772,24 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer connectorId, this.chargingStation.getEnergyActiveImportRegisterByTransactionId(transactionId) ); - await this.chargingStation.ocppRequestService.sendMessageHandler( - OCPP16RequestCommand.METER_VALUES, - { - connectorId, - transactionId, - meterValue: transactionEndMeterValue, - } - ); - } - await this.chargingStation.ocppRequestService.sendMessageHandler( - OCPP16RequestCommand.STOP_TRANSACTION, - { + await this.chargingStation.ocppRequestService.sendMessageHandler< + OCPP16MeterValuesRequest, + OCPP16MeterValuesResponse + >(OCPP16RequestCommand.METER_VALUES, { + connectorId, transactionId, - meterStop: - this.chargingStation.getEnergyActiveImportRegisterByTransactionId(transactionId), - idTag: this.chargingStation.getTransactionIdTag(transactionId), - } - ); + meterValue: transactionEndMeterValue, + }); + } + await this.chargingStation.ocppRequestService.sendMessageHandler< + OCPP16StopTransactionRequest, + OCPP16StopTransactionResponse + >(OCPP16RequestCommand.STOP_TRANSACTION, { + transactionId, + meterStop: + this.chargingStation.getEnergyActiveImportRegisterByTransactionId(transactionId), + idTag: this.chargingStation.getTransactionIdTag(transactionId), + }); return Constants.OCPP_RESPONSE_ACCEPTED; } } @@ -826,24 +838,24 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer info.bytes / 1024 } bytes transferred from diagnostics archive ${info.name}` ); - await this.chargingStation.ocppRequestService.sendMessageHandler( - OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION, - { - status: OCPP16DiagnosticsStatus.Uploading, - } - ); + await this.chargingStation.ocppRequestService.sendMessageHandler< + DiagnosticsStatusNotificationRequest, + DiagnosticsStatusNotificationResponse + >(OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION, { + status: OCPP16DiagnosticsStatus.Uploading, + }); }); uploadResponse = await ftpClient.uploadFrom( path.join(path.resolve(__dirname, '../../../../'), diagnosticsArchive), uri.pathname + diagnosticsArchive ); if (uploadResponse.code === 226) { - await this.chargingStation.ocppRequestService.sendMessageHandler( - OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION, - { - status: OCPP16DiagnosticsStatus.Uploaded, - } - ); + await this.chargingStation.ocppRequestService.sendMessageHandler< + DiagnosticsStatusNotificationRequest, + DiagnosticsStatusNotificationResponse + >(OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION, { + status: OCPP16DiagnosticsStatus.Uploaded, + }); if (ftpClient) { ftpClient.close(); } @@ -865,12 +877,12 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer OCPP16IncomingRequestCommand.GET_DIAGNOSTICS ); } catch (error) { - await this.chargingStation.ocppRequestService.sendMessageHandler( - OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION, - { - status: OCPP16DiagnosticsStatus.UploadFailed, - } - ); + await this.chargingStation.ocppRequestService.sendMessageHandler< + DiagnosticsStatusNotificationRequest, + DiagnosticsStatusNotificationResponse + >(OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION, { + status: OCPP16DiagnosticsStatus.UploadFailed, + }); if (ftpClient) { ftpClient.close(); } @@ -886,12 +898,12 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer uri.protocol } to transfer the diagnostic logs archive` ); - await this.chargingStation.ocppRequestService.sendMessageHandler( - OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION, - { - status: OCPP16DiagnosticsStatus.UploadFailed, - } - ); + await this.chargingStation.ocppRequestService.sendMessageHandler< + DiagnosticsStatusNotificationRequest, + DiagnosticsStatusNotificationResponse + >(OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION, { + status: OCPP16DiagnosticsStatus.UploadFailed, + }); return Constants.OCPP_RESPONSE_EMPTY; } } @@ -904,7 +916,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer case MessageTrigger.BootNotification: setTimeout(() => { this.chargingStation.ocppRequestService - .sendMessageHandler( + .sendMessageHandler( OCPP16RequestCommand.BOOT_NOTIFICATION, { chargePointModel: @@ -936,9 +948,13 @@ 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 3d91576b..64ce459c 100644 --- a/src/charging-station/ocpp/1.6/OCPP16RequestService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16RequestService.ts @@ -22,7 +22,7 @@ export default class OCPP16RequestService extends OCPPRequestService { super(chargingStation, ocppResponseService); } - public async sendMessageHandler( + public async sendMessageHandler( commandName: OCPP16RequestCommand, commandParams?: JsonType, params?: SendParams @@ -30,7 +30,7 @@ export default class OCPP16RequestService extends OCPPRequestService { if (Object.values(OCPP16RequestCommand).includes(commandName)) { return (await this.sendMessage( Utils.generateUUID(), - this.buildCommandPayload(commandName, commandParams), + this.buildCommandPayload(commandName, commandParams), commandName, params )) as unknown as Response; diff --git a/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts b/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts index 08197142..c7bf6fdb 100644 --- a/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16ResponseService.ts @@ -1,26 +1,30 @@ // Partial Copyright Jerome Benoit. 2021. All Rights Reserved. import { - AuthorizeRequest, OCPP16AuthorizationStatus, + OCPP16AuthorizeRequest, OCPP16AuthorizeResponse, + OCPP16StartTransactionRequest, OCPP16StartTransactionResponse, + OCPP16StopTransactionRequest, OCPP16StopTransactionResponse, - StartTransactionRequest, - StopTransactionRequest, } from '../../../types/ocpp/1.6/Transaction'; import { - HeartbeatRequest, + OCPP16BootNotificationRequest, + OCPP16HeartbeatRequest, OCPP16RequestCommand, - StatusNotificationRequest, + OCPP16StatusNotificationRequest, } from '../../../types/ocpp/1.6/Requests'; -import { MeterValuesRequest, OCPP16MeterValuesResponse } from '../../../types/ocpp/1.6/MeterValues'; import { OCPP16BootNotificationResponse, OCPP16HeartbeatResponse, OCPP16RegistrationStatus, OCPP16StatusNotificationResponse, } from '../../../types/ocpp/1.6/Responses'; +import { + OCPP16MeterValuesRequest, + OCPP16MeterValuesResponse, +} from '../../../types/ocpp/1.6/MeterValues'; import type ChargingStation from '../../ChargingStation'; import { ErrorType } from '../../../types/ocpp/ErrorType'; @@ -136,7 +140,7 @@ export default class OCPP16ResponseService extends OCPPResponseService { private handleResponseHeartbeat( payload: OCPP16HeartbeatResponse, - requestPayload: HeartbeatRequest + requestPayload: OCPP16HeartbeatRequest ): void { logger.debug( this.chargingStation.logPrefix() + @@ -148,7 +152,7 @@ export default class OCPP16ResponseService extends OCPPResponseService { private handleResponseAuthorize( payload: OCPP16AuthorizeResponse, - requestPayload: AuthorizeRequest + requestPayload: OCPP16AuthorizeRequest ): void { let authorizeConnectorId: number; for (const connectorId of this.chargingStation.connectors.keys()) { @@ -181,7 +185,7 @@ export default class OCPP16ResponseService extends OCPPResponseService { private async handleResponseStartTransaction( payload: OCPP16StartTransactionResponse, - requestPayload: StartTransactionRequest + requestPayload: OCPP16StartTransactionRequest ): Promise { const connectorId = requestPayload.connectorId; @@ -313,23 +317,23 @@ export default class OCPP16ResponseService extends OCPPResponseService { requestPayload.meterStart ); this.chargingStation.getBeginEndMeterValues() && - (await this.chargingStation.ocppRequestService.sendMessageHandler( - OCPP16RequestCommand.METER_VALUES, - { - connectorId, - transactionId: payload.transactionId, - meterValue: - this.chargingStation.getConnectorStatus(connectorId).transactionBeginMeterValue, - } - )); - await this.chargingStation.ocppRequestService.sendMessageHandler( - OCPP16RequestCommand.STATUS_NOTIFICATION, - { + (await this.chargingStation.ocppRequestService.sendMessageHandler< + OCPP16MeterValuesRequest, + OCPP16MeterValuesResponse + >(OCPP16RequestCommand.METER_VALUES, { connectorId, - status: OCPP16ChargePointStatus.CHARGING, - errorCode: OCPP16ChargePointErrorCode.NO_ERROR, - } - ); + transactionId: payload.transactionId, + meterValue: + this.chargingStation.getConnectorStatus(connectorId).transactionBeginMeterValue, + })); + await this.chargingStation.ocppRequestService.sendMessageHandler< + OCPP16StatusNotificationRequest, + OCPP16StatusNotificationResponse + >(OCPP16RequestCommand.STATUS_NOTIFICATION, { + connectorId, + status: OCPP16ChargePointStatus.CHARGING, + errorCode: OCPP16ChargePointErrorCode.NO_ERROR, + }); this.chargingStation.getConnectorStatus(connectorId).status = OCPP16ChargePointStatus.CHARGING; logger.info( @@ -375,14 +379,14 @@ export default class OCPP16ResponseService extends OCPPResponseService { this.chargingStation.getConnectorStatus(connectorId).status !== OCPP16ChargePointStatus.AVAILABLE ) { - await this.chargingStation.ocppRequestService.sendMessageHandler( - OCPP16RequestCommand.STATUS_NOTIFICATION, - { - connectorId, - status: OCPP16ChargePointStatus.AVAILABLE, - errorCode: OCPP16ChargePointErrorCode.NO_ERROR, - } - ); + await this.chargingStation.ocppRequestService.sendMessageHandler< + OCPP16StatusNotificationRequest, + OCPP16StatusNotificationResponse + >(OCPP16RequestCommand.STATUS_NOTIFICATION, { + connectorId, + status: OCPP16ChargePointStatus.AVAILABLE, + errorCode: OCPP16ChargePointErrorCode.NO_ERROR, + }); this.chargingStation.getConnectorStatus(connectorId).status = OCPP16ChargePointStatus.AVAILABLE; } @@ -390,7 +394,7 @@ export default class OCPP16ResponseService extends OCPPResponseService { private async handleResponseStopTransaction( payload: OCPP16StopTransactionResponse, - requestPayload: StopTransactionRequest + requestPayload: OCPP16StopTransactionRequest ): Promise { const transactionConnectorId = this.chargingStation.getConnectorIdByTransactionId( requestPayload.transactionId @@ -407,41 +411,41 @@ export default class OCPP16ResponseService extends OCPPResponseService { this.chargingStation.getBeginEndMeterValues() && !this.chargingStation.getOcppStrictCompliance() && this.chargingStation.getOutOfOrderEndMeterValues() && - (await this.chargingStation.ocppRequestService.sendMessageHandler( - OCPP16RequestCommand.METER_VALUES, - { - connectorId: transactionConnectorId, - transactionId: requestPayload.transactionId, - meterValue: OCPP16ServiceUtils.buildTransactionEndMeterValue( - this.chargingStation, - transactionConnectorId, - requestPayload.meterStop - ), - } - )); + (await this.chargingStation.ocppRequestService.sendMessageHandler< + OCPP16MeterValuesRequest, + OCPP16MeterValuesResponse + >(OCPP16RequestCommand.METER_VALUES, { + connectorId: transactionConnectorId, + transactionId: requestPayload.transactionId, + meterValue: OCPP16ServiceUtils.buildTransactionEndMeterValue( + this.chargingStation, + transactionConnectorId, + requestPayload.meterStop + ), + })); if ( !this.chargingStation.isChargingStationAvailable() || !this.chargingStation.isConnectorAvailable(transactionConnectorId) ) { - await this.chargingStation.ocppRequestService.sendMessageHandler( - OCPP16RequestCommand.STATUS_NOTIFICATION, - { - connectorId: transactionConnectorId, - status: OCPP16ChargePointStatus.UNAVAILABLE, - errorCode: OCPP16ChargePointErrorCode.NO_ERROR, - } - ); + await this.chargingStation.ocppRequestService.sendMessageHandler< + OCPP16StatusNotificationRequest, + OCPP16StatusNotificationResponse + >(OCPP16RequestCommand.STATUS_NOTIFICATION, { + connectorId: transactionConnectorId, + status: OCPP16ChargePointStatus.UNAVAILABLE, + errorCode: OCPP16ChargePointErrorCode.NO_ERROR, + }); this.chargingStation.getConnectorStatus(transactionConnectorId).status = OCPP16ChargePointStatus.UNAVAILABLE; } else { - await this.chargingStation.ocppRequestService.sendMessageHandler( - OCPP16RequestCommand.STATUS_NOTIFICATION, - { - connectorId: transactionConnectorId, - status: OCPP16ChargePointStatus.AVAILABLE, - errorCode: OCPP16ChargePointErrorCode.NO_ERROR, - } - ); + await this.chargingStation.ocppRequestService.sendMessageHandler< + OCPP16BootNotificationRequest, + OCPP16BootNotificationResponse + >(OCPP16RequestCommand.STATUS_NOTIFICATION, { + connectorId: transactionConnectorId, + status: OCPP16ChargePointStatus.AVAILABLE, + errorCode: OCPP16ChargePointErrorCode.NO_ERROR, + }); this.chargingStation.getConnectorStatus(transactionConnectorId).status = OCPP16ChargePointStatus.AVAILABLE; } @@ -471,7 +475,7 @@ export default class OCPP16ResponseService extends OCPPResponseService { } private handleResponseStatusNotification( - payload: StatusNotificationRequest, + payload: OCPP16StatusNotificationRequest, requestPayload: OCPP16StatusNotificationResponse ): void { logger.debug( @@ -483,7 +487,7 @@ export default class OCPP16ResponseService extends OCPPResponseService { } private handleResponseMeterValues( - payload: MeterValuesRequest, + payload: OCPP16MeterValuesRequest, requestPayload: OCPP16MeterValuesResponse ): void { logger.debug( diff --git a/src/charging-station/ocpp/OCPPRequestService.ts b/src/charging-station/ocpp/OCPPRequestService.ts index 89766371..abef0808 100644 --- a/src/charging-station/ocpp/OCPPRequestService.ts +++ b/src/charging-station/ocpp/OCPPRequestService.ts @@ -317,7 +317,8 @@ export default abstract class OCPPRequestService { } } - public abstract sendMessageHandler( + // eslint-disable-next-line @typescript-eslint/no-unused-vars + public abstract sendMessageHandler( commandName: RequestCommand, commandParams?: JsonType, params?: SendParams diff --git a/src/types/ocpp/1.6/MeterValues.ts b/src/types/ocpp/1.6/MeterValues.ts index 70967dde..54c283fb 100644 --- a/src/types/ocpp/1.6/MeterValues.ts +++ b/src/types/ocpp/1.6/MeterValues.ts @@ -97,7 +97,7 @@ export interface OCPP16MeterValue extends JsonType { sampledValue: OCPP16SampledValue[]; } -export interface MeterValuesRequest extends JsonType { +export interface OCPP16MeterValuesRequest extends JsonType { connectorId: number; transactionId?: number; meterValue: OCPP16MeterValue[]; diff --git a/src/types/ocpp/1.6/Requests.ts b/src/types/ocpp/1.6/Requests.ts index 1b3dc4e7..6518213c 100644 --- a/src/types/ocpp/1.6/Requests.ts +++ b/src/types/ocpp/1.6/Requests.ts @@ -33,7 +33,7 @@ export enum OCPP16IncomingRequestCommand { TRIGGER_MESSAGE = 'TriggerMessage', } -export type HeartbeatRequest = EmptyObject; +export type OCPP16HeartbeatRequest = EmptyObject; export interface OCPP16BootNotificationRequest extends JsonType { chargeBoxSerialNumber?: string; @@ -47,7 +47,7 @@ export interface OCPP16BootNotificationRequest extends JsonType { meterType?: string; } -export interface StatusNotificationRequest extends JsonType { +export interface OCPP16StatusNotificationRequest extends JsonType { connectorId: number; errorCode: OCPP16ChargePointErrorCode; info?: string; diff --git a/src/types/ocpp/1.6/Transaction.ts b/src/types/ocpp/1.6/Transaction.ts index 1b921d4e..13d6df64 100644 --- a/src/types/ocpp/1.6/Transaction.ts +++ b/src/types/ocpp/1.6/Transaction.ts @@ -30,7 +30,7 @@ export interface IdTagInfo extends JsonType { expiryDate?: Date; } -export interface AuthorizeRequest extends JsonType { +export interface OCPP16AuthorizeRequest extends JsonType { idTag: string; } @@ -38,7 +38,7 @@ export interface OCPP16AuthorizeResponse extends JsonType { idTagInfo: IdTagInfo; } -export interface StartTransactionRequest extends JsonType { +export interface OCPP16StartTransactionRequest extends JsonType { connectorId: number; idTag: string; meterStart: number; @@ -51,7 +51,7 @@ export interface OCPP16StartTransactionResponse extends JsonType { transactionId: number; } -export interface StopTransactionRequest extends JsonType { +export interface OCPP16StopTransactionRequest extends JsonType { idTag?: string; meterStop: number; timestamp: string; diff --git a/src/types/ocpp/Requests.ts b/src/types/ocpp/Requests.ts index c5c813f2..c8c4007d 100644 --- a/src/types/ocpp/Requests.ts +++ b/src/types/ocpp/Requests.ts @@ -1,13 +1,16 @@ import { OCPP16AvailabilityType, OCPP16BootNotificationRequest, + OCPP16HeartbeatRequest, OCPP16IncomingRequestCommand, OCPP16RequestCommand, + OCPP16StatusNotificationRequest, } from './1.6/Requests'; import { JsonType } from '../JsonType'; import { MessageType } from './MessageType'; import { OCPP16DiagnosticsStatus } from './1.6/DiagnosticsStatus'; +import { OCPP16MeterValuesRequest } from './1.6/MeterValues'; import OCPPError from '../../exception/OCPPError'; export interface SendParams { @@ -21,6 +24,12 @@ export type ResponseType = JsonType | OCPPError | string; export type BootNotificationRequest = OCPP16BootNotificationRequest; +export type HeartbeatRequest = OCPP16HeartbeatRequest; + +export type StatusNotificationRequest = OCPP16StatusNotificationRequest; + +export type MeterValuesRequest = OCPP16MeterValuesRequest; + export type AvailabilityType = OCPP16AvailabilityType; export const AvailabilityType = { diff --git a/src/types/ocpp/Transaction.ts b/src/types/ocpp/Transaction.ts index cee8f3cf..3dd499c9 100644 --- a/src/types/ocpp/Transaction.ts +++ b/src/types/ocpp/Transaction.ts @@ -1,8 +1,11 @@ import { OCPP16AuthorizationStatus, + OCPP16AuthorizeRequest, OCPP16AuthorizeResponse, + OCPP16StartTransactionRequest, OCPP16StartTransactionResponse, OCPP16StopTransactionReason, + OCPP16StopTransactionRequest, OCPP16StopTransactionResponse, } from './1.6/Transaction'; @@ -12,6 +15,8 @@ export const AuthorizationStatus = { ...OCPP16AuthorizationStatus, }; +export type AuthorizeRequest = OCPP16AuthorizeRequest; + export type AuthorizeResponse = OCPP16AuthorizeResponse; export type StopTransactionReason = OCPP16StopTransactionReason; @@ -20,6 +25,10 @@ export const StopTransactionReason = { ...OCPP16StopTransactionReason, }; +export type StartTransactionRequest = OCPP16StartTransactionRequest; + export type StartTransactionResponse = OCPP16StartTransactionResponse; +export type StopTransactionRequest = OCPP16StopTransactionRequest; + export type StopTransactionResponse = OCPP16StopTransactionResponse;