Permit to run code in async scope in the OCPP stack
authorJérôme Benoit <jerome.benoit@sap.com>
Mon, 3 Oct 2022 09:59:40 +0000 (11:59 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Mon, 3 Oct 2022 09:59:40 +0000 (11:59 +0200)
Use it to reset a charging station

Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
.gitignore
package.json
src/charging-station/AutomaticTransactionGenerator.ts
src/charging-station/Bootstrap.ts
src/charging-station/ocpp/1.6/OCPP16IncomingRequestService.ts
src/charging-station/ocpp/OCPPIncomingRequestService.ts
src/ui/web/.gitignore

index 271df212f966d46c1e4172567b6f3ee1f3f77b0d..92bdcf304e3059126e0d36db4facdfd814992bf7 100644 (file)
@@ -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
index 29bca2e9d0ca0ca59b9410cc90e745695c4b5726..8f01dcd7c5759ba67f22fcd01fb225bcb1df212c 100644 (file)
@@ -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 <e-mobility@sap.com>",
   "contributors": [
     {
index 232ad2cd4533ce68ac79e05b676599cf4eeafd42..e32c3c98109f6f520142d1499411b35d12babfcc 100644 (file)
@@ -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
       );
index e29c6445fefcccb383b4387f93d670a948c57326..329c5546dcde0a255add2c4a136652d2b337cc61 100644 (file)
@@ -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})`
     );
index f5779ab2952bbf3eb3c660d79a7882539aa5a687..52751b4bac994ddfc9e2bc0f68746093b7c2bb55 100644 (file)
@@ -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<void> => {
-      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
index 509eebd57b16bd8beacb924aafba3b5e2c5e8423..06ccd312c0e970022c844f200fe3d0b631ef8c93 100644 (file)
@@ -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);
index e710601c5fe09c5065c655efb2c8d384800e184b..4bc4a6b8ddd5600e5dd833dd407130b176ea1beb 100644 (file)
@@ -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