Skip to content
This repository has been archived by the owner on Jun 12, 2024. It is now read-only.

Commit

Permalink
Introduce a generic OCPP message sending handler
Browse files Browse the repository at this point in the history
And start making use of it

Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
  • Loading branch information
Jérôme Benoit committed Mar 7, 2022
1 parent 78085c4 commit 94a464f
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/charging-station/ChargingStation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ export default class ChargingStation {
) {
// eslint-disable-next-line @typescript-eslint/no-misused-promises
this.heartbeatSetInterval = setInterval(async (): Promise<void> => {
await this.ocppRequestService.sendHeartbeat();
await this.ocppRequestService.sendMessageHandler(RequestCommand.HEARTBEAT);
}, this.getHeartbeatInterval());
logger.info(
this.logPrefix() +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
MessageTrigger,
OCPP16AvailabilityType,
OCPP16IncomingRequestCommand,
OCPP16RequestCommand,
OCPP16TriggerMessageRequest,
RemoteStartTransactionRequest,
RemoteStopTransactionRequest,
Expand Down Expand Up @@ -830,7 +831,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
case MessageTrigger.Heartbeat:
setTimeout(() => {
this.chargingStation.ocppRequestService
.sendHeartbeat({ triggerMessage: true })
.sendMessageHandler(OCPP16RequestCommand.HEARTBEAT, null, { triggerMessage: true })
.catch(() => {
/* This is intentional */
});
Expand Down
30 changes: 23 additions & 7 deletions src/charging-station/ocpp/1.6/OCPP16RequestService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
StatusNotificationRequest,
} from '../../../types/ocpp/1.6/Requests';
import { MeterValuesRequest, OCPP16MeterValue } from '../../../types/ocpp/1.6/MeterValues';
import { ResponseType, SendParams } from '../../../types/ocpp/Requests';

import type ChargingStation from '../../ChargingStation';
import Constants from '../../../utils/Constants';
Expand All @@ -30,7 +31,6 @@ 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';
Expand All @@ -43,9 +43,25 @@ export default class OCPP16RequestService extends OCPPRequestService {
super(chargingStation, ocppResponseService);
}

public async sendHeartbeat(params?: SendParams): Promise<void> {
const payload: HeartbeatRequest = {};
await this.sendMessage(Utils.generateUUID(), payload, OCPP16RequestCommand.HEARTBEAT, params);
public async sendMessageHandler(
commandName: OCPP16RequestCommand,
commandParams?: JsonType,
params?: SendParams
): Promise<ResponseType> {
if (Object.values(OCPP16RequestCommand).includes(commandName)) {
return this.sendMessage(
Utils.generateUUID(),
this.buildCommandPayload(commandName, commandParams),
commandName,
params
);
}
throw new OCPPError(
ErrorType.NOT_SUPPORTED,
`${moduleName}.sendMessageHandler: Unsupported OCPP command ${commandName}`,
commandName,
{ commandName }
);
}

public async sendBootNotification(
Expand Down Expand Up @@ -176,7 +192,7 @@ export default class OCPP16RequestService extends OCPPRequestService {
transactionId: number,
interval: number
): Promise<void> {
const meterValue = OCPP16ServiceUtils.buildMeterValue(
const meterValue: OCPP16MeterValue = OCPP16ServiceUtils.buildMeterValue(
this.chargingStation,
connectorId,
transactionId,
Expand Down Expand Up @@ -231,7 +247,7 @@ export default class OCPP16RequestService extends OCPPRequestService {

private buildCommandPayload(
commandName: OCPP16RequestCommand,
commandParams: JsonType
commandParams?: JsonType
): JsonType {
switch (commandName) {
case OCPP16RequestCommand.AUTHORIZE:
Expand Down Expand Up @@ -316,7 +332,7 @@ export default class OCPP16RequestService extends OCPPRequestService {
throw new OCPPError(
ErrorType.NOT_SUPPORTED,
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
`Unsupported OCPP command: ${commandName}`,
`${moduleName}.buildCommandPayload: Unsupported OCPP command: ${commandName}`,
commandName,
{ commandName }
);
Expand Down
8 changes: 7 additions & 1 deletion src/charging-station/ocpp/OCPPRequestService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export default abstract class OCPPRequestService {
) {
this.chargingStation = chargingStation;
this.ocppResponseService = ocppResponseService;
this.sendMessageHandler.bind(this);
}

public static getInstance<T extends OCPPRequestService>(
Expand Down Expand Up @@ -326,7 +327,12 @@ export default abstract class OCPPRequestService {
}
}

public abstract sendHeartbeat(params?: SendParams): Promise<void>;
public abstract sendMessageHandler(
commandName: RequestCommand,
commandParams?: JsonType,
params?: SendParams
): Promise<ResponseType>;

public abstract sendBootNotification(
chargePointModel: string,
chargePointVendor: string,
Expand Down

0 comments on commit 94a464f

Please sign in to comment.