+ public static startHeartbeatInterval(chargingStation: ChargingStation, interval: number): void {
+ if (!chargingStation.heartbeatSetInterval) {
+ chargingStation.startHeartbeat();
+ } else if (chargingStation.getHeartbeatInterval() !== interval) {
+ chargingStation.restartHeartbeat();
+ }
+ }
+
+ public static async sendAndSetConnectorStatus(
+ chargingStation: ChargingStation,
+ connectorId: number,
+ status: ConnectorStatusEnum,
+ evseId?: number,
+ options?: { send: boolean },
+ ) {
+ options = { send: true, ...options };
+ if (options.send) {
+ OCPPServiceUtils.checkConnectorStatusTransition(chargingStation, connectorId, status);
+ await chargingStation.ocppRequestService.requestHandler<
+ StatusNotificationRequest,
+ StatusNotificationResponse
+ >(
+ chargingStation,
+ RequestCommand.STATUS_NOTIFICATION,
+ OCPPServiceUtils.buildStatusNotificationRequest(
+ chargingStation,
+ connectorId,
+ status,
+ evseId,
+ ),
+ );
+ }
+ chargingStation.getConnectorStatus(connectorId)!.status = status;
+ }
+
+ public static async isIdTagAuthorized(
+ chargingStation: ChargingStation,
+ connectorId: number,
+ idTag: string,
+ ): Promise<boolean> {
+ if (!chargingStation.getLocalAuthListEnabled() && !chargingStation.getRemoteAuthorization()) {
+ logger.warn(
+ `${chargingStation.logPrefix()} The charging station expects to authorize RFID tags but nor local authorization nor remote authorization are enabled. Misbehavior may occur`,
+ );
+ }
+ let authorized = false;
+ if (
+ chargingStation.getLocalAuthListEnabled() === true &&
+ OCPPServiceUtils.isIdTagLocalAuthorized(chargingStation, idTag)
+ ) {
+ const connectorStatus: ConnectorStatus = chargingStation.getConnectorStatus(connectorId)!;
+ connectorStatus.localAuthorizeIdTag = idTag;
+ connectorStatus.idTagLocalAuthorized = true;
+ authorized = true;
+ } else if (chargingStation.getRemoteAuthorization()) {
+ authorized = await OCPPServiceUtils.isIdTagRemoteAuthorized(
+ chargingStation,
+ connectorId,
+ idTag,
+ );
+ }
+ return authorized;
+ }
+
+ protected static checkConnectorStatusTransition(
+ chargingStation: ChargingStation,
+ connectorId: number,
+ status: ConnectorStatusEnum,
+ ): boolean {
+ const fromStatus = chargingStation.getConnectorStatus(connectorId)!.status;
+ let transitionAllowed = false;
+ switch (chargingStation.stationInfo.ocppVersion) {
+ case OCPPVersion.VERSION_16:
+ if (
+ (connectorId === 0 &&
+ OCPP16Constants.ChargePointStatusChargingStationTransitions.findIndex(
+ (transition) => transition.from === fromStatus && transition.to === status,
+ ) !== -1) ||
+ (connectorId > 0 &&
+ OCPP16Constants.ChargePointStatusConnectorTransitions.findIndex(
+ (transition) => transition.from === fromStatus && transition.to === status,
+ ) !== -1)
+ ) {
+ transitionAllowed = true;
+ }
+ break;
+ case OCPPVersion.VERSION_20:
+ case OCPPVersion.VERSION_201:
+ if (
+ (connectorId === 0 &&
+ OCPP20Constants.ChargingStationStatusTransitions.findIndex(
+ (transition) => transition.from === fromStatus && transition.to === status,
+ ) !== -1) ||
+ (connectorId > 0 &&
+ OCPP20Constants.ConnectorStatusTransitions.findIndex(
+ (transition) => transition.from === fromStatus && transition.to === status,
+ ) !== -1)
+ ) {
+ transitionAllowed = true;
+ }
+ break;
+ default:
+ throw new BaseError(
+ // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
+ `Cannot check connector status transition: OCPP version ${chargingStation.stationInfo.ocppVersion} not supported`,
+ );
+ }
+ if (transitionAllowed === false) {
+ logger.warn(
+ `${chargingStation.logPrefix()} OCPP ${
+ chargingStation.stationInfo.ocppVersion
+ } connector id ${connectorId} status transition from '${
+ chargingStation.getConnectorStatus(connectorId)!.status
+ }' to '${status}' is not allowed`,
+ );
+ }
+ return transitionAllowed;
+ }
+
+ protected static parseJsonSchemaFile<T extends JsonType>(
+ relativePath: string,
+ ocppVersion: OCPPVersion,
+ moduleName?: string,
+ methodName?: string,
+ ): JSONSchemaType<T> {
+ const filePath = join(dirname(fileURLToPath(import.meta.url)), relativePath);
+ try {
+ return JSON.parse(readFileSync(filePath, 'utf8')) as JSONSchemaType<T>;
+ } catch (error) {
+ handleFileException(
+ filePath,
+ FileType.JsonSchema,
+ error as NodeJS.ErrnoException,
+ OCPPServiceUtils.logPrefix(ocppVersion, moduleName, methodName),
+ { throwError: false },
+ );
+ return {} as JSONSchemaType<T>;
+ }