-// Partial Copyright Jerome Benoit. 2021. All Rights Reserved.
+// Partial Copyright Jerome Benoit. 2021-2023. All Rights Reserved.
import fs from 'fs';
import path from 'path';
import type { JsonObject, JsonType } from '../../../types/JsonType';
import type { OCPP16MeterValuesRequest } from '../../../types/ocpp/1.6/MeterValues';
import {
- DiagnosticsStatusNotificationRequest,
- OCPP16BootNotificationRequest,
- OCPP16HeartbeatRequest,
+ type DiagnosticsStatusNotificationRequest,
+ type OCPP16BootNotificationRequest,
+ type OCPP16DataTransferRequest,
+ type OCPP16HeartbeatRequest,
OCPP16RequestCommand,
- OCPP16StatusNotificationRequest,
+ type OCPP16StatusNotificationRequest,
} from '../../../types/ocpp/1.6/Requests';
import type {
OCPP16AuthorizeRequest,
OCPP16StopTransactionRequest,
} from '../../../types/ocpp/1.6/Transaction';
import { ErrorType } from '../../../types/ocpp/ErrorType';
+import { OCPPVersion } from '../../../types/ocpp/OCPPVersion';
import type { RequestParams } from '../../../types/ocpp/Requests';
import Constants from '../../../utils/Constants';
import logger from '../../../utils/Logger';
import Utils from '../../../utils/Utils';
import type ChargingStation from '../../ChargingStation';
-import { ChargingStationUtils } from '../../ChargingStationUtils';
import OCPPRequestService from '../OCPPRequestService';
import type OCPPResponseService from '../OCPPResponseService';
import { OCPP16ServiceUtils } from './OCPP16ServiceUtils';
if (new.target?.name === moduleName) {
throw new TypeError(`Cannot construct ${new.target?.name} instances directly`);
}
- super(ocppResponseService);
+ super(OCPPVersion.VERSION_16, ocppResponseService);
this.jsonSchemas = new Map<OCPP16RequestCommand, JSONSchemaType<JsonObject>>([
[
OCPP16RequestCommand.AUTHORIZE,
)
) as JSONSchemaType<OCPP16StopTransactionRequest>,
],
+ [
+ OCPP16RequestCommand.DATA_TRANSFER,
+ JSON.parse(
+ fs.readFileSync(
+ path.resolve(
+ path.dirname(fileURLToPath(import.meta.url)),
+ '../../../assets/json-schemas/ocpp/1.6/DataTransfer.json'
+ ),
+ 'utf8'
+ )
+ ) as JSONSchemaType<OCPP16DataTransferRequest>,
+ ],
]);
this.buildRequestPayload.bind(this);
- this.validatePayload.bind(this);
}
public async requestHandler<RequestType extends JsonType, ResponseType extends JsonType>(
commandParams?: JsonType,
params?: RequestParams
): Promise<ResponseType> {
- if (ChargingStationUtils.isRequestCommandSupported(commandName, chargingStation)) {
+ if (OCPP16ServiceUtils.isRequestCommandSupported(chargingStation, commandName) === true) {
const requestPayload = this.buildRequestPayload<RequestType>(
chargingStation,
commandName,
commandParams
);
- this.validatePayload(chargingStation, commandName, requestPayload);
return (await this.sendMessage(
chargingStation,
Utils.generateUUID(),
params
)) as unknown as ResponseType;
}
+ // OCPPError usage here is debatable: it's an error in the OCPP stack but not targeted to sendError().
throw new OCPPError(
ErrorType.NOT_SUPPORTED,
`Unsupported OCPP command '${commandName}'`,
);
}
+ protected getRequestPayloadValidationSchema(
+ chargingStation: ChargingStation,
+ commandName: OCPP16RequestCommand
+ ): JSONSchemaType<JsonObject> | false {
+ if (this.jsonSchemas.has(commandName) === true) {
+ return this.jsonSchemas.get(commandName);
+ }
+ logger.warn(
+ `${chargingStation.logPrefix()} ${moduleName}.getPayloadValidationSchema: No JSON schema found for command ${commandName} PDU validation`
+ );
+ return false;
+ }
+
private buildRequestPayload<Request extends JsonType>(
chargingStation: ChargingStation,
commandName: OCPP16RequestCommand,
meterStart: chargingStation.getEnergyActiveImportRegisterByConnectorId(
commandParams?.connectorId as number
),
- timestamp: new Date().toISOString(),
+ timestamp: new Date(),
} as unknown as Request;
case OCPP16RequestCommand.STOP_TRANSACTION:
connectorId = chargingStation.getConnectorIdByTransactionId(
commandParams?.transactionId as number
);
- energyActiveImportRegister = chargingStation.getEnergyActiveImportRegisterByTransactionId(
- commandParams?.transactionId as number,
- true
- );
+ commandParams?.meterStop &&
+ (energyActiveImportRegister =
+ chargingStation.getEnergyActiveImportRegisterByTransactionId(
+ commandParams?.transactionId as number,
+ true
+ ));
return {
transactionId: commandParams?.transactionId,
idTag:
commandParams?.idTag ??
chargingStation.getTransactionIdTag(commandParams?.transactionId as number),
meterStop: commandParams?.meterStop ?? energyActiveImportRegister,
- timestamp: new Date().toISOString(),
+ timestamp: new Date(),
reason: commandParams?.reason,
...(chargingStation.getTransactionDataMeterValues() && {
transactionData: OCPP16ServiceUtils.buildTransactionDataMeterValues(
),
}),
} as unknown as Request;
+ case OCPP16RequestCommand.DATA_TRANSFER:
+ return commandParams as unknown as Request;
default:
+ // OCPPError usage here is debatable: it's an error in the OCPP stack but not targeted to sendError().
throw new OCPPError(
ErrorType.NOT_SUPPORTED,
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
);
}
}
-
- private validatePayload<Request extends JsonType>(
- chargingStation: ChargingStation,
- commandName: OCPP16RequestCommand,
- requestPayload: Request
- ): boolean {
- if (this.jsonSchemas.has(commandName)) {
- return this.validateRequestPayload(
- chargingStation,
- commandName,
- this.jsonSchemas.get(commandName),
- requestPayload
- );
- }
- logger.warn(
- `${chargingStation.logPrefix()} ${moduleName}.validatePayload: No JSON schema found for command ${commandName} PDU validation`
- );
- return false;
- }
}