Skip to content

Commit

Permalink
Merge pull request #12 from juherr/feature/clean-stop-transaction
Browse files Browse the repository at this point in the history
Clean transaction when the startTransaction message is refused
  • Loading branch information
shiv3 authored Oct 9, 2024
2 parents 00e62ac + 7ca6ae6 commit 90fba7c
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 14 deletions.
3 changes: 1 addition & 2 deletions src/components/Connector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ const Connector: React.FC<ConnectorProps> = ({id: connector_id, cp,idTag}) => {

const handleStopTransaction = () => {
if (cp) {
const tagId = localStorage.getItem("TAG") || "DEADBEEF";
cp.stopTransaction(tagId, connector_id);
cp.stopTransaction(connector_id);
}
};

Expand Down
2 changes: 1 addition & 1 deletion src/components/TopPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ const ExperimentalView: React.FC<ExperimentalProps> = ({cps, tagIDs}) => {

const handleAllStopTransaction = () => {
transactions.forEach((t) => {
cps.find((cp) => cp.id === t.cpID)?.stopTransaction(t.tagID, t.connectorID);
cps.find((cp) => cp.id === t.cpID)?.stopTransaction(t.connectorID);
// transactions.splice(transactions.indexOf(t), 1);
})
}
Expand Down
13 changes: 9 additions & 4 deletions src/cp/ChargePoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ export class ChargePoint {
meterStop: null,
startTime: new Date(),
stopTime: null,
meterSent: false,
};
connector.transaction = transaction;
this._messageHandler.startTransaction(transaction, connectorId);
Expand All @@ -189,7 +190,7 @@ export class ChargePoint {
}
}

public stopTransaction(tagId: string, connectorId: number): void {
public stopTransaction(connectorId: number): void {
const connector = this.getConnector(connectorId);
if (connector) {
connector.transaction!.stopTime = new Date();
Expand All @@ -198,13 +199,17 @@ export class ChargePoint {
connector.transaction!,
connector.id
);
this.updateConnectorStatus(connector.id, OCPPStatus.Finishing);
this._autoMeterValueSetting && this.stopAutoMeterValue(connectorId);
this.cleanTransaction(connectorId);
} else {
this._logger.error(`Transaction for tag ${tagId} not found`);
this._logger.error(`Connector for id ${connectorId} not found`);
}
}

public cleanTransaction(connectorId: number): void {
this.updateConnectorStatus(connectorId, OCPPStatus.Finishing);
this._autoMeterValueSetting && this.stopAutoMeterValue(connectorId);
}

public sendHeartbeat(): void {
this._messageHandler.sendHeartbeat();
}
Expand Down
23 changes: 16 additions & 7 deletions src/cp/OCPPMessageHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ export class OCPPMessageHandler {
this.handleHeartbeatResponse(payload as response.HeartbeatResponse);
break;
case OCPPAction.MeterValues:
this.handleMeterValuesResponse(payload as response.MeterValuesResponse);
this.handleMeterValuesResponse(payload as response.MeterValuesResponse, request?.payload as request.MeterValuesRequest);
break;
case OCPPAction.StatusNotification:
this.handleStatusNotificationResponse(
Expand Down Expand Up @@ -344,15 +344,11 @@ export class OCPPMessageHandler {
);

if (connector) {
const tagId = connector.transaction?.tagId || "";
if (tagId === "") {
throw new Error("Tag ID not found");
}
this._chargePoint.updateConnectorStatus(
connector.id,
OCPPStatus.SuspendedEVSE
);
this._chargePoint.stopTransaction(tagId, connector.id);
this._chargePoint.stopTransaction(connector.id);
return {status: "Accepted"};
} else {
return {status: "Rejected"};
Expand Down Expand Up @@ -420,6 +416,13 @@ export class OCPPMessageHandler {
this._logger.log("Failed to start transaction");
if (connector) {
connector.status = OCPPStatus.Faulted;
if (connector.transaction && connector.transaction.meterSent) {
this._chargePoint.stopTransaction(connectorId);
} else {
this._chargePoint.cleanTransaction(connectorId);
}
} else {
this._chargePoint.cleanTransaction(connectorId);
}
}
}
Expand All @@ -443,7 +446,13 @@ export class OCPPMessageHandler {
this._logger.log(`Received heartbeat response: ${payload.currentTime}`);
}

private handleMeterValuesResponse(payload: response.MeterValuesResponse): void {
private handleMeterValuesResponse(payload: response.MeterValuesResponse, request?: request.MeterValuesRequest): void {
if (request) {
const connector = this._chargePoint.getConnector(request.connectorId);
if (connector && connector.transaction) {
connector.transaction.meterSent = true;
}
}
this._logger.log(`Meter values sent successfully: ${JSON.stringify(payload)}`);
}

Expand Down
1 change: 1 addition & 0 deletions src/cp/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ export interface Transaction {
meterStop: number | null;
startTime: Date;
stopTime: Date | null;
meterSent: boolean;
}

0 comments on commit 90fba7c

Please sign in to comment.