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

Commit

Permalink
Convert sendStatusNotification to OCPP message sending handler
Browse files Browse the repository at this point in the history
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
  • Loading branch information
Jérôme Benoit committed Mar 7, 2022
1 parent 6a8b180 commit 93b4a42
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 65 deletions.
36 changes: 21 additions & 15 deletions src/charging-station/ChargingStation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { WSError, WebSocketCloseEventStatusCode } from '../types/WebSocket';
import WebSocket, { ClientOptions, Data, OPEN, RawData } from 'ws';

import AutomaticTransactionGenerator from './AutomaticTransactionGenerator';
import { ChargePointErrorCode } from '../types/ocpp/ChargePointErrorCode';
import { ChargePointStatus } from '../types/ocpp/ChargePointStatus';
import { ChargingProfile } from '../types/ocpp/ChargingProfile';
import ChargingStationInfo from '../types/ChargingStationInfo';
Expand Down Expand Up @@ -513,10 +514,11 @@ export default class ChargingStation {
await this.stopMessageSequence(reason);
for (const connectorId of this.connectors.keys()) {
if (connectorId > 0) {
await this.ocppRequestService.sendStatusNotification(
await this.ocppRequestService.sendMessageHandler(RequestCommand.STATUS_NOTIFICATION, {
connectorId,
ChargePointStatus.UNAVAILABLE
);
status: ChargePointStatus.UNAVAILABLE,
errorCode: ChargePointErrorCode.NO_ERROR,
});
this.getConnectorStatus(connectorId).status = ChargePointStatus.UNAVAILABLE;
}
}
Expand Down Expand Up @@ -1266,10 +1268,11 @@ export default class ChargingStation {
this.getConnectorStatus(connectorId)?.bootStatus
) {
// Send status in template at startup
await this.ocppRequestService.sendStatusNotification(
await this.ocppRequestService.sendMessageHandler(RequestCommand.STATUS_NOTIFICATION, {
connectorId,
this.getConnectorStatus(connectorId).bootStatus
);
status: this.getConnectorStatus(connectorId).bootStatus,
errorCode: ChargePointErrorCode.NO_ERROR,
});
this.getConnectorStatus(connectorId).status =
this.getConnectorStatus(connectorId).bootStatus;
} else if (
Expand All @@ -1278,24 +1281,27 @@ export default class ChargingStation {
this.getConnectorStatus(connectorId)?.bootStatus
) {
// Send status in template after reset
await this.ocppRequestService.sendStatusNotification(
await this.ocppRequestService.sendMessageHandler(RequestCommand.STATUS_NOTIFICATION, {
connectorId,
this.getConnectorStatus(connectorId).bootStatus
);
status: this.getConnectorStatus(connectorId).bootStatus,
errorCode: ChargePointErrorCode.NO_ERROR,
});
this.getConnectorStatus(connectorId).status =
this.getConnectorStatus(connectorId).bootStatus;
} else if (!this.stopped && this.getConnectorStatus(connectorId)?.status) {
// Send previous status at template reload
await this.ocppRequestService.sendStatusNotification(
await this.ocppRequestService.sendMessageHandler(RequestCommand.STATUS_NOTIFICATION, {
connectorId,
this.getConnectorStatus(connectorId).status
);
status: this.getConnectorStatus(connectorId).status,
errorCode: ChargePointErrorCode.NO_ERROR,
});
} else {
// Send default status
await this.ocppRequestService.sendStatusNotification(
await this.ocppRequestService.sendMessageHandler(RequestCommand.STATUS_NOTIFICATION, {
connectorId,
ChargePointStatus.AVAILABLE
);
status: ChargePointStatus.AVAILABLE,
errorCode: ChargePointErrorCode.NO_ERROR,
});
this.getConnectorStatus(connectorId).status = ChargePointStatus.AVAILABLE;
}
}
Expand Down
57 changes: 39 additions & 18 deletions src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import { DefaultResponse } from '../../../types/ocpp/Responses';
import { ErrorType } from '../../../types/ocpp/ErrorType';
import { IncomingRequestHandler } from '../../../types/ocpp/Requests';
import { JsonType } from '../../../types/JsonType';
import { OCPP16ChargePointErrorCode } from '../../../types/ocpp/1.6/ChargePointErrorCode';
import { OCPP16ChargePointStatus } from '../../../types/ocpp/1.6/ChargePointStatus';
import { OCPP16DiagnosticsStatus } from '../../../types/ocpp/1.6/DiagnosticsStatus';
import { OCPP16StandardParametersKey } from '../../../types/ocpp/1.6/Configuration';
Expand Down Expand Up @@ -212,9 +213,13 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
}
return Constants.OCPP_RESPONSE_UNLOCK_FAILED;
}
await this.chargingStation.ocppRequestService.sendStatusNotification(
connectorId,
OCPP16ChargePointStatus.AVAILABLE
await this.chargingStation.ocppRequestService.sendMessageHandler(
OCPP16RequestCommand.STATUS_NOTIFICATION,
{
connectorId,
status: OCPP16ChargePointStatus.AVAILABLE,
errorCode: OCPP16ChargePointErrorCode.NO_ERROR,
}
);
this.chargingStation.getConnectorStatus(connectorId).status = OCPP16ChargePointStatus.AVAILABLE;
return Constants.OCPP_RESPONSE_UNLOCKED;
Expand Down Expand Up @@ -462,9 +467,13 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
}
this.chargingStation.getConnectorStatus(id).availability = commandPayload.type;
if (response === Constants.OCPP_AVAILABILITY_RESPONSE_ACCEPTED) {
await this.chargingStation.ocppRequestService.sendStatusNotification(
id,
chargePointStatus
await this.chargingStation.ocppRequestService.sendMessageHandler(
OCPP16RequestCommand.STATUS_NOTIFICATION,
{
connectorId: id,
status: chargePointStatus,
errorCode: OCPP16ChargePointErrorCode.NO_ERROR,
}
);
this.chargingStation.getConnectorStatus(id).status = chargePointStatus;
}
Expand All @@ -483,9 +492,9 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
return Constants.OCPP_AVAILABILITY_RESPONSE_SCHEDULED;
}
this.chargingStation.getConnectorStatus(connectorId).availability = commandPayload.type;
await this.chargingStation.ocppRequestService.sendStatusNotification(
connectorId,
chargePointStatus
await this.chargingStation.ocppRequestService.sendMessageHandler(
OCPP16RequestCommand.STATUS_NOTIFICATION,
{ connectorId, status: chargePointStatus, errorCode: OCPP16ChargePointErrorCode.NO_ERROR }
);
this.chargingStation.getConnectorStatus(connectorId).status = chargePointStatus;
return Constants.OCPP_AVAILABILITY_RESPONSE_ACCEPTED;
Expand All @@ -498,9 +507,13 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
): Promise<DefaultResponse> {
const transactionConnectorId: number = commandPayload.connectorId;
if (transactionConnectorId) {
await this.chargingStation.ocppRequestService.sendStatusNotification(
transactionConnectorId,
OCPP16ChargePointStatus.PREPARING
await this.chargingStation.ocppRequestService.sendMessageHandler(
OCPP16RequestCommand.STATUS_NOTIFICATION,
{
connectorId: transactionConnectorId,
status: OCPP16ChargePointStatus.PREPARING,
errorCode: OCPP16ChargePointErrorCode.NO_ERROR,
}
);
this.chargingStation.getConnectorStatus(transactionConnectorId).status =
OCPP16ChargePointStatus.PREPARING;
Expand Down Expand Up @@ -633,9 +646,13 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
this.chargingStation.getConnectorStatus(connectorId).status !==
OCPP16ChargePointStatus.AVAILABLE
) {
await this.chargingStation.ocppRequestService.sendStatusNotification(
connectorId,
OCPP16ChargePointStatus.AVAILABLE
await this.chargingStation.ocppRequestService.sendMessageHandler(
OCPP16RequestCommand.STATUS_NOTIFICATION,
{
connectorId,
status: OCPP16ChargePointStatus.AVAILABLE,
errorCode: OCPP16ChargePointErrorCode.NO_ERROR,
}
);
this.chargingStation.getConnectorStatus(connectorId).status =
OCPP16ChargePointStatus.AVAILABLE;
Expand Down Expand Up @@ -686,9 +703,13 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
connectorId > 0 &&
this.chargingStation.getConnectorStatus(connectorId)?.transactionId === transactionId
) {
await this.chargingStation.ocppRequestService.sendStatusNotification(
connectorId,
OCPP16ChargePointStatus.FINISHING
await this.chargingStation.ocppRequestService.sendMessageHandler(
OCPP16RequestCommand.STATUS_NOTIFICATION,
{
connectorId,
status: OCPP16ChargePointStatus.FINISHING,
errorCode: OCPP16ChargePointErrorCode.NO_ERROR,
}
);
this.chargingStation.getConnectorStatus(connectorId).status =
OCPP16ChargePointStatus.FINISHING;
Expand Down
15 changes: 1 addition & 14 deletions src/charging-station/ocpp/1.6/OCPP16RequestService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,6 @@ export default class OCPP16RequestService extends OCPPRequestService {
);
}

public async sendStatusNotification(
connectorId: number,
status: OCPP16ChargePointStatus,
errorCode: OCPP16ChargePointErrorCode = OCPP16ChargePointErrorCode.NO_ERROR
): Promise<void> {
const payload: StatusNotificationRequest = {
connectorId,
errorCode,
status,
};
await this.sendMessage(Utils.generateUUID(), payload, OCPP16RequestCommand.STATUS_NOTIFICATION);
}

public async sendAuthorize(
connectorId: number,
idTag?: string
Expand Down Expand Up @@ -263,8 +250,8 @@ export default class OCPP16RequestService extends OCPPRequestService {
case OCPP16RequestCommand.STATUS_NOTIFICATION:
return {
connectorId: commandParams?.connectorId,
errorCode: commandParams?.errorCode,
status: commandParams?.status,
errorCode: commandParams?.errorCode,
} as StatusNotificationRequest;
case OCPP16RequestCommand.START_TRANSACTION:
return {
Expand Down
41 changes: 29 additions & 12 deletions src/charging-station/ocpp/1.6/OCPP16ResponseService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { MeterValuesRequest, MeterValuesResponse } from '../../../types/ocpp/1.6
import type ChargingStation from '../../ChargingStation';
import { ErrorType } from '../../../types/ocpp/ErrorType';
import { JsonType } from '../../../types/JsonType';
import { OCPP16ChargePointErrorCode } from '../../../types/ocpp/1.6/ChargePointErrorCode';
import { OCPP16ChargePointStatus } from '../../../types/ocpp/1.6/ChargePointStatus';
import { OCPP16ServiceUtils } from './OCPP16ServiceUtils';
import { OCPP16StandardParametersKey } from '../../../types/ocpp/1.6/Configuration';
Expand Down Expand Up @@ -314,9 +315,13 @@ export default class OCPP16ResponseService extends OCPPResponseService {
payload.transactionId,
this.chargingStation.getConnectorStatus(connectorId).transactionBeginMeterValue
));
await this.chargingStation.ocppRequestService.sendStatusNotification(
connectorId,
OCPP16ChargePointStatus.CHARGING
await this.chargingStation.ocppRequestService.sendMessageHandler(
OCPP16RequestCommand.STATUS_NOTIFICATION,
{
connectorId,
status: OCPP16ChargePointStatus.CHARGING,
errorCode: OCPP16ChargePointErrorCode.NO_ERROR,
}
);
this.chargingStation.getConnectorStatus(connectorId).status =
OCPP16ChargePointStatus.CHARGING;
Expand Down Expand Up @@ -363,9 +368,13 @@ export default class OCPP16ResponseService extends OCPPResponseService {
this.chargingStation.getConnectorStatus(connectorId).status !==
OCPP16ChargePointStatus.AVAILABLE
) {
await this.chargingStation.ocppRequestService.sendStatusNotification(
connectorId,
OCPP16ChargePointStatus.AVAILABLE
await this.chargingStation.ocppRequestService.sendMessageHandler(
OCPP16RequestCommand.STATUS_NOTIFICATION,
{
connectorId,
status: OCPP16ChargePointStatus.AVAILABLE,
errorCode: OCPP16ChargePointErrorCode.NO_ERROR,
}
);
this.chargingStation.getConnectorStatus(connectorId).status =
OCPP16ChargePointStatus.AVAILABLE;
Expand Down Expand Up @@ -412,16 +421,24 @@ export default class OCPP16ResponseService extends OCPPResponseService {
!this.chargingStation.isChargingStationAvailable() ||
!this.chargingStation.isConnectorAvailable(transactionConnectorId)
) {
await this.chargingStation.ocppRequestService.sendStatusNotification(
transactionConnectorId,
OCPP16ChargePointStatus.UNAVAILABLE
await this.chargingStation.ocppRequestService.sendMessageHandler(
OCPP16RequestCommand.STATUS_NOTIFICATION,
{
connectorId: transactionConnectorId,
status: OCPP16ChargePointStatus.UNAVAILABLE,
errorCode: OCPP16ChargePointErrorCode.NO_ERROR,
}
);
this.chargingStation.getConnectorStatus(transactionConnectorId).status =
OCPP16ChargePointStatus.UNAVAILABLE;
} else {
await this.chargingStation.ocppRequestService.sendStatusNotification(
transactionConnectorId,
OCPP16ChargePointStatus.AVAILABLE
await this.chargingStation.ocppRequestService.sendMessageHandler(
OCPP16RequestCommand.STATUS_NOTIFICATION,
{
connectorId: transactionConnectorId,
status: OCPP16ChargePointStatus.AVAILABLE,
errorCode: OCPP16ChargePointErrorCode.NO_ERROR,
}
);
this.chargingStation.getConnectorStatus(transactionConnectorId).status =
OCPP16ChargePointStatus.AVAILABLE;
Expand Down
6 changes: 0 additions & 6 deletions src/charging-station/ocpp/OCPPRequestService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,12 +333,6 @@ export default abstract class OCPPRequestService {
params?: SendParams
): Promise<ResponseType>;

public abstract sendStatusNotification(
connectorId: number,
status: ChargePointStatus,
errorCode?: ChargePointErrorCode
): Promise<void>;

public abstract sendAuthorize(connectorId: number, idTag?: string): Promise<AuthorizeResponse>;
public abstract sendStartTransaction(
connectorId: number,
Expand Down

0 comments on commit 93b4a42

Please sign in to comment.