Keep track of ATG running time
authorJérôme Benoit <jerome.benoit@sap.com>
Sat, 18 Sep 2021 12:12:41 +0000 (14:12 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Sat, 18 Sep 2021 12:12:41 +0000 (14:12 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
src/charging-station/AutomaticTransactionGenerator.ts
src/charging-station/ChargingStation.ts

index 5526339f8a8a2e1f024887b05e41632d68400ec3..6f77199ab850aaeaa991668d182d63001b799e26 100644 (file)
@@ -12,6 +12,7 @@ export default class AutomaticTransactionGenerator {
   public timeToStop: boolean;
   private startDate!: Date;
   private stopDate!: Date;
+  private runningDuration!: number;
   private chargingStation: ChargingStation;
 
   constructor(chargingStation: ChargingStation) {
@@ -21,7 +22,9 @@ export default class AutomaticTransactionGenerator {
 
   public start(): void {
     this.startDate = new Date();
-    this.stopDate = new Date(this.startDate.getTime() + (this.chargingStation.stationInfo?.AutomaticTransactionGenerator?.stopAfterHours ?? Constants.CHARGING_STATION_ATG_DEFAULT_STOP_AFTER_HOURS) * 3600 * 1000);
+    this.stopDate = new Date(this.startDate.getTime()
+      + (this.chargingStation.stationInfo?.AutomaticTransactionGenerator?.stopAfterHours ?? Constants.CHARGING_STATION_ATG_DEFAULT_STOP_AFTER_HOURS) * 3600 * 1000
+      - (this.runningDuration ?? 0));
     this.timeToStop = false;
     for (const connector in this.chargingStation.connectors) {
       if (Utils.convertToInt(connector) > 0) {
@@ -35,7 +38,7 @@ export default class AutomaticTransactionGenerator {
   }
 
   public async stop(reason: StopTransactionReason = StopTransactionReason.NONE): Promise<void> {
-    logger.info(this.logPrefix() + ' over. Stopping all transactions');
+    logger.info(`${this.logPrefix()} over and lasted for ${Utils.formatDurationMilliSeconds(this.runningDuration ?? 0)}. Stopping all transactions`);
     for (const connector in this.chargingStation.connectors) {
       const transactionId = this.chargingStation.getConnector(Utils.convertToInt(connector)).transactionId;
       if (this.chargingStation.getConnector(Utils.convertToInt(connector)).transactionStarted) {
@@ -104,6 +107,7 @@ export default class AutomaticTransactionGenerator {
         totalTransactionSkip++;
         logger.info(this.logPrefix(connectorId) + ' skipped transaction ' + transactionSkip.toString() + '/' + totalTransactionSkip.toString());
       }
+      this.runningDuration = (new Date()).getTime() - this.startDate.getTime();
     }
     logger.info(this.logPrefix(connectorId) + ' stopped on connector');
   }
index 9cb5e066cb7d9f342e39efa9e0108ced776c07e0..ff7ce6cd31a4bf12edece490175abc44f497226d 100644 (file)
@@ -8,7 +8,7 @@ import { ConnectorPhaseRotation, StandardParametersKey, SupportedFeatureProfiles
 import Connectors, { Connector, SampledValueTemplate } from '../types/Connectors';
 import { MeterValueMeasurand, MeterValuePhase } from '../types/ocpp/MeterValues';
 import { WSError, WebSocketCloseEventStatusCode } from '../types/WebSocket';
-import WebSocket, { ClientOptions, Data } from 'ws';
+import WebSocket, { ClientOptions, Data, OPEN } from 'ws';
 
 import AutomaticTransactionGenerator from './AutomaticTransactionGenerator';
 import { ChargePointStatus } from '../types/ocpp/ChargePointStatus';
@@ -111,7 +111,7 @@ export default class ChargingStation {
   }
 
   public isWebSocketConnectionOpened(): boolean {
-    return this.wsConnection?.readyState === WebSocket.OPEN;
+    return this.wsConnection?.readyState === OPEN;
   }
 
   public isRegistered(): boolean {