X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2Focpp%2F1.6%2FOCPP16RequestService.ts;h=3d91576bb5b85b41e667e5354174ae2d815ac119;hb=cc6e8ab5f669ff49b1d476efffc509be8128bccc;hp=ab45df16b83261b5385c6328bf60806036fee1ab;hpb=5e62b11972292c9962946f389689cb1b5f14604c;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/ocpp/1.6/OCPP16RequestService.ts b/src/charging-station/ocpp/1.6/OCPP16RequestService.ts index ab45df16..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,18 +107,11 @@ export default class OCPP16RequestService extends OCPPRequestService { commandParams?.connectorId as number ), timestamp: new Date().toISOString(), - } as StartTransactionRequest; + } as unknown as Request; case OCPP16RequestCommand.STOP_TRANSACTION: - for (const id of this.chargingStation.connectors.keys()) { - if ( - id > 0 && - this.chargingStation.getConnectorStatus(id)?.transactionId === - commandParams?.transactionId - ) { - connectorId = id; - break; - } - } + connectorId = this.chargingStation.getConnectorIdByTransactionId( + commandParams?.transactionId as number + ); return { transactionId: commandParams?.transactionId, ...(!Utils.isUndefined(commandParams?.idTag) && { idTag: commandParams.idTag }), @@ -148,7 +128,7 @@ export default class OCPP16RequestService extends OCPPRequestService { ) ), }), - } as StopTransactionRequest; + } as unknown as Request; default: throw new OCPPError( ErrorType.NOT_SUPPORTED,