From e6159ce8b782e3464a9a0dc377897bc4a4718121 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Mon, 3 Oct 2022 11:59:40 +0200 Subject: [PATCH] Permit to run code in async scope in the OCPP stack MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Use it to reset a charging station Signed-off-by: Jérôme Benoit --- .gitignore | 15 +++++++++++++++ package.json | 2 +- .../AutomaticTransactionGenerator.ts | 5 ++++- src/charging-station/Bootstrap.ts | 8 ++++---- .../ocpp/1.6/OCPP16IncomingRequestService.ts | 12 ++++++++---- .../ocpp/OCPPIncomingRequestService.ts | 4 ++++ src/ui/web/.gitignore | 14 ++++++++++++++ 7 files changed, 50 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index 271df212..92bdcf30 100644 --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,21 @@ mikro-orm.config*.ts !mikro-orm.config-template.ts manifest.yml +### Git ### +# Created by git for backups. To disable backups in Git: +# $ git config --global mergetool.keepBackup false +*.orig + +# Created by git when using merge tools for conflicts +*.BACKUP.* +*.BASE.* +*.LOCAL.* +*.REMOTE.* +*_BACKUP_*.txt +*_BASE_*.txt +*_LOCAL_*.txt +*_REMOTE_*.txt + # Logs logs *.log diff --git a/package.json b/package.json index 29bca2e9..8f01dcd7 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,7 @@ "type": "git", "url": "https://github.com/sap/e-mobility-charging-stations-simulator.git" }, - "description": "Electric Vehicle charging stations simulator", + "description": "Electric vehicle charging stations simulator", "author": "SAP E-Mobility ", "contributors": [ { diff --git a/src/charging-station/AutomaticTransactionGenerator.ts b/src/charging-station/AutomaticTransactionGenerator.ts index 232ad2cd..e32c3c98 100644 --- a/src/charging-station/AutomaticTransactionGenerator.ts +++ b/src/charging-station/AutomaticTransactionGenerator.ts @@ -93,7 +93,10 @@ export default class AutomaticTransactionGenerator extends AsyncResource { } if (this.connectorsStatus.get(connectorId)?.start === false) { this.runInAsyncScope( - this.internalStartConnector.bind(this) as (this: this, ...args: any[]) => unknown, + this.internalStartConnector.bind(this) as ( + this: AutomaticTransactionGenerator, + ...args: any[] + ) => void, this, connectorId ); diff --git a/src/charging-station/Bootstrap.ts b/src/charging-station/Bootstrap.ts index e29c6445..329c5546 100644 --- a/src/charging-station/Bootstrap.ts +++ b/src/charging-station/Bootstrap.ts @@ -229,9 +229,9 @@ export class Bootstrap { this.uiServer?.chargingStations.set(data.stationInfo.hashId, data); ++this.numberOfStartedChargingStations; logger.info( - `${this.logPrefix()} ${moduleName}.workerEventStarted: Charging station '${ + `${this.logPrefix()} ${moduleName}.workerEventStarted: Charging station ${ data.stationInfo.chargingStationId - } (hashId: ${data.stationInfo.hashId})' started (${ + } (hashId: ${data.stationInfo.hashId}) started (${ this.numberOfStartedChargingStations } started from ${this.numberOfChargingStations})` ); @@ -241,9 +241,9 @@ export class Bootstrap { this.uiServer?.chargingStations.set(data.stationInfo.hashId, data); --this.numberOfStartedChargingStations; logger.info( - `${this.logPrefix()} ${moduleName}.workerEventStopped: Charging station '${ + `${this.logPrefix()} ${moduleName}.workerEventStopped: Charging station ${ data.stationInfo.chargingStationId - } (hashId: ${data.stationInfo.hashId})' stopped (${ + } (hashId: ${data.stationInfo.hashId}) stopped (${ this.numberOfStartedChargingStations } started from ${this.numberOfChargingStations})` ); diff --git a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts index f5779ab2..52751b4b 100644 --- a/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts +++ b/src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts @@ -378,10 +378,14 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer chargingStation: ChargingStation, commandPayload: ResetRequest ): DefaultResponse { - // eslint-disable-next-line @typescript-eslint/no-misused-promises - setImmediate(async (): Promise => { - await chargingStation.reset((commandPayload.type + 'Reset') as OCPP16StopTransactionReason); - }); + this.asyncResource.runInAsyncScope( + chargingStation.reset.bind(chargingStation) as ( + this: ChargingStation, + ...args: any[] + ) => void, + chargingStation, + (commandPayload.type + 'Reset') as OCPP16StopTransactionReason + ); logger.info( `${chargingStation.logPrefix()} ${ commandPayload.type diff --git a/src/charging-station/ocpp/OCPPIncomingRequestService.ts b/src/charging-station/ocpp/OCPPIncomingRequestService.ts index 509eebd5..06ccd312 100644 --- a/src/charging-station/ocpp/OCPPIncomingRequestService.ts +++ b/src/charging-station/ocpp/OCPPIncomingRequestService.ts @@ -1,3 +1,5 @@ +import { AsyncResource } from 'async_hooks'; + import type { JSONSchemaType } from 'ajv'; import Ajv from 'ajv-draft-04'; import ajvFormats from 'ajv-formats'; @@ -14,9 +16,11 @@ const moduleName = 'OCPPIncomingRequestService'; export default abstract class OCPPIncomingRequestService { private static instance: OCPPIncomingRequestService | null = null; + protected asyncResource: AsyncResource; private ajv: Ajv; protected constructor() { + this.asyncResource = new AsyncResource(moduleName); this.ajv = new Ajv(); ajvFormats(this.ajv); this.incomingRequestHandler.bind(this); diff --git a/src/ui/web/.gitignore b/src/ui/web/.gitignore index e710601c..4bc4a6b8 100644 --- a/src/ui/web/.gitignore +++ b/src/ui/web/.gitignore @@ -2,6 +2,20 @@ node_modules /dist +### Git ### +# Created by git for backups. To disable backups in Git: +# $ git config --global mergetool.keepBackup false +*.orig + +# Created by git when using merge tools for conflicts +*.BACKUP.* +*.BASE.* +*.LOCAL.* +*.REMOTE.* +*_BACKUP_*.txt +*_BASE_*.txt +*_LOCAL_*.txt +*_REMOTE_*.txt # local env files .env.local -- 2.34.1