X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2Focpp%2F1.6%2FOCPP16RequestService.ts;h=eb3474809edca7e437ef223443137e9f6d23b4d0;hb=1b6498baa159ac6ebd4eda9008cde367640bf3ed;hp=acc7fd2eb532131ada1f72ff243cf39d438fe2b7;hpb=443c064190213667582c916489760732147c48aa;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 acc7fd2e..eb347480 100644 --- a/src/charging-station/ocpp/1.6/OCPP16RequestService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16RequestService.ts @@ -1,29 +1,16 @@ // 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 { MeterValuesRequest, OCPP16MeterValue } from '../../../types/ocpp/1.6/MeterValues'; -import { ResponseType, SendParams } from '../../../types/ocpp/Requests'; +import { JsonObject, JsonType } from '../../../types/JsonType'; import type ChargingStation from '../../ChargingStation'; import Constants from '../../../utils/Constants'; import { ErrorType } from '../../../types/ocpp/ErrorType'; -import { JsonType } from '../../../types/JsonType'; -import { OCPP16DiagnosticsStatus } from '../../../types/ocpp/1.6/DiagnosticsStatus'; +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 { RequestParams } from '../../../types/ocpp/Requests'; import Utils from '../../../utils/Utils'; const moduleName = 'OCPP16RequestService'; @@ -36,65 +23,40 @@ export default class OCPP16RequestService extends OCPPRequestService { super(chargingStation, ocppResponseService); } - public async sendMessageHandler( + public async requestHandler( commandName: OCPP16RequestCommand, commandParams?: JsonType, - params?: SendParams - ): Promise { + params?: RequestParams + ): Promise { if (Object.values(OCPP16RequestCommand).includes(commandName)) { - return this.sendMessage( + return (await this.sendMessage( Utils.generateUUID(), - this.buildCommandPayload(commandName, commandParams), + this.buildRequestPayload(commandName, commandParams), commandName, params - ); + )) as unknown as Response; } throw new OCPPError( ErrorType.NOT_SUPPORTED, - `${moduleName}.sendMessageHandler: Unsupported OCPP command ${commandName}`, + `${moduleName}.requestHandler: Unsupported OCPP command ${commandName}`, commandName, { commandName } ); } - public async sendTransactionEndMeterValues( - connectorId: number, - transactionId: number, - endMeterValue: OCPP16MeterValue - ): Promise { - const payload: MeterValuesRequest = { - connectorId, - transactionId, - meterValue: [endMeterValue], - }; - await this.sendMessage(Utils.generateUUID(), payload, OCPP16RequestCommand.METER_VALUES); - } - - public async sendDiagnosticsStatusNotification( - diagnosticsStatus: OCPP16DiagnosticsStatus - ): Promise { - const payload: DiagnosticsStatusNotificationRequest = { - status: diagnosticsStatus, - }; - await this.sendMessage( - Utils.generateUUID(), - payload, - OCPP16RequestCommand.DIAGNOSTICS_STATUS_NOTIFICATION - ); - } - - private buildCommandPayload( + private buildRequestPayload( commandName: OCPP16RequestCommand, commandParams?: JsonType - ): JsonType { + ): Request { let connectorId: number; + commandParams = commandParams as JsonObject; switch (commandName) { case OCPP16RequestCommand.AUTHORIZE: return { ...(!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, @@ -116,27 +78,27 @@ 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, transactionId: commandParams?.transactionId, - meterValue: Array.isArray(commandParams?.meterValues) - ? commandParams?.meterValues + 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, @@ -147,18 +109,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 }), @@ -175,12 +130,12 @@ export default class OCPP16RequestService extends OCPPRequestService { ) ), }), - } as StopTransactionRequest; + } as unknown as Request; default: throw new OCPPError( ErrorType.NOT_SUPPORTED, // eslint-disable-next-line @typescript-eslint/restrict-template-expressions - `${moduleName}.buildCommandPayload: Unsupported OCPP command: ${commandName}`, + `${moduleName}.buildRequestPayload: Unsupported OCPP command: ${commandName}`, commandName, { commandName } );