Skip to content

Commit

Permalink
Feat(ts-ocpp): Allows central-system to close chargepoint connections…
Browse files Browse the repository at this point in the history
… with an application defined code
  • Loading branch information
Antonio Luiz committed Sep 16, 2024
1 parent 958944a commit f7978b5
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 9 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,6 @@
],
"publishConfig": {
"registry": "https://npm.pkg.github.com/"
}
},
"packageManager": "[email protected]+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e"
}
8 changes: 4 additions & 4 deletions src/cs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,21 +152,21 @@ export default class CentralSystem {
this.listeners.push(listener);
}

public async close(): Promise<void> {
public async close(code?: number): Promise<void> {
Object.values(this.connections).map((chargepointConnections) => {
chargepointConnections.map((connection) => {
connection.close();
connection.close(code);
});
})
const httpClosing = new Promise(resolve => this.httpServer.close(resolve));
const wsClosing = new Promise(resolve => this.websocketsServer.close(resolve));
return Promise.all([httpClosing, wsClosing]).then(() => { });
}

public async closeConnection(chargePointId: string): Promise<void> {
public async closeConnection(chargePointId: string, code?: number): Promise<void> {
const connections = this.connections[chargePointId];
if (connections) {
await Promise.all(connections.map((connection) => connection.close()));
await Promise.all(connections.map((connection) => connection.close(code)));
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/ws/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,13 @@ export default class Connection<ReqAction extends ActionName<'v1.6-json'>> {
})
}

public close() {
public close(code?: number) {
Object.entries(this.timeouts).forEach(([requestId, timeout]) => {
clearTimeout(timeout);
delete this.messageTriggers[requestId];
delete this.timeouts[requestId];
});
this.socket.close();
this.socket.close(code);
}

private async sendOCPPMessage(message: OCPPJMessage): Promise<void> {
Expand Down Expand Up @@ -154,4 +154,4 @@ export default class Connection<ReqAction extends ActionName<'v1.6-json'>> {
return Nothing;
})
}
}
}
3 changes: 2 additions & 1 deletion src/ws/constants.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export const OCPP_1_6 = 'ocpp1.6';
export const SUPPORTED_PROTOCOLS = [OCPP_1_6];
export const SUPPORTED_PROTOCOLS = [OCPP_1_6];
export const CONCURRENT_CONNECTION_TERMINATED_CODE = 4500;

0 comments on commit f7978b5

Please sign in to comment.