Fix heartbeat start + cleanups.
authorJérôme Benoit <jerome.benoit@sap.com>
Sun, 2 May 2021 20:12:23 +0000 (22:12 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Sun, 2 May 2021 20:12:23 +0000 (22:12 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/charging-station/AutomaticTransactionGenerator.ts
src/charging-station/ChargingStation.ts
src/charging-station/ocpp/1.6/OCCP16IncomingRequestService.ts
src/charging-station/ocpp/1.6/OCPP16ResponseService.ts
src/charging-station/ocpp/OCPPRequestService.ts
src/utils/PerformanceStatistics.ts [moved from src/utils/Statistics.ts with 96% similarity]

index e675550f348d2f4fddf87cdffd49df634321672d..a1e55989da82658ba03ced852611667ae7989f84 100644 (file)
@@ -17,7 +17,7 @@ export default class AutomaticTransactionGenerator {
     if (this.chargingStation.getEnableStatistics()) {
       this.performanceObserver = new PerformanceObserver((list) => {
         const entry = list.getEntries()[0];
-        this.chargingStation.statistics.logPerformance(entry, Constants.ENTITY_AUTOMATIC_TRANSACTION_GENERATOR);
+        this.chargingStation.performanceStatistics.logPerformance(entry, Constants.ENTITY_AUTOMATIC_TRANSACTION_GENERATOR);
         this.performanceObserver.disconnect();
       });
     }
@@ -96,7 +96,7 @@ export default class AutomaticTransactionGenerator {
           startResponse = await this.startTransaction(connectorId, this);
         }
         if (startResponse?.idTagInfo?.status !== AuthorizationStatus.ACCEPTED) {
-          logger.info(this.logPrefix(connectorId) + ' transaction rejected');
+          logger.warn(this.logPrefix(connectorId) + ' transaction rejected');
           await Utils.sleep(Constants.CHARGING_STATION_ATG_WAIT_TIME);
         } else {
           // Wait until end of transaction
@@ -154,8 +154,8 @@ export default class AutomaticTransactionGenerator {
 
   private logPrefix(connectorId: number = null): string {
     if (connectorId) {
-      return Utils.logPrefix(' ' + this.chargingStation.stationInfo.chargingStationId + ' ATG on connector #' + connectorId.toString() + ':');
+      return Utils.logPrefix(' ' + this.chargingStation.stationInfo.chargingStationId + ' ATG on connector #' + connectorId.toString() + ':');
     }
-    return Utils.logPrefix(' ' + this.chargingStation.stationInfo.chargingStationId + ' ATG:');
+    return Utils.logPrefix(' ' + this.chargingStation.stationInfo.chargingStationId + ' ATG:');
   }
 }
index 6bff621fec5b50ead39cba724edf97f029a985d4..a5e31588f0e3eb38f324888fcf05a423d8840b27 100644 (file)
@@ -21,8 +21,8 @@ import OCPPError from './OcppError';
 import OCPPIncomingRequestService from './ocpp/OCPPIncomingRequestService';
 import OCPPRequestService from './ocpp/OCPPRequestService';
 import { OCPPVersion } from '../types/ocpp/OCPPVersion';
+import PerformanceStatistics from '../utils/PerformanceStatistics';
 import { StandardParametersKey } from '../types/ocpp/Configuration';
-import Statistics from '../utils/Statistics';
 import { StopTransactionReason } from '../types/ocpp/Transaction';
 import Utils from '../utils/Utils';
 import { WebSocketCloseEventStatusCode } from '../types/WebSocket';
@@ -41,7 +41,7 @@ export default class ChargingStation {
   public wsConnection: WebSocket;
   public requests: Requests;
   public messageQueue: string[];
-  public statistics: Statistics;
+  public performanceStatistics: PerformanceStatistics;
   public heartbeatSetInterval: NodeJS.Timeout;
   public ocppIncomingRequestService: OCPPIncomingRequestService;
   public ocppRequestService: OCPPRequestService;
@@ -74,7 +74,7 @@ export default class ChargingStation {
   }
 
   public logPrefix(): string {
-    return Utils.logPrefix(` ${this.stationInfo.chargingStationId}:`);
+    return Utils.logPrefix(` ${this.stationInfo.chargingStationId} |`);
   }
 
   public getRandomTagId(): string {
@@ -181,7 +181,7 @@ export default class ChargingStation {
       }, this.getHeartbeatInterval());
       logger.info(this.logPrefix() + ' Heartbeat started every ' + Utils.milliSecondsToHHMMSS(this.getHeartbeatInterval()));
     } else if (this.heartbeatSetInterval) {
-      logger.info(this.logPrefix() + ' Heartbeat every ' + Utils.milliSecondsToHHMMSS(this.getHeartbeatInterval()) + ' already started');
+      logger.info(this.logPrefix() + ' Heartbeat already started every ' + Utils.milliSecondsToHHMMSS(this.getHeartbeatInterval()));
     } else {
       logger.error(`${this.logPrefix()} Heartbeat interval set to ${this.getHeartbeatInterval() ? Utils.milliSecondsToHHMMSS(this.getHeartbeatInterval()) : this.getHeartbeatInterval()}, not starting the heartbeat`);
     }
@@ -465,10 +465,10 @@ export default class ChargingStation {
     }
     this.stationInfo.powerDivider = this.getPowerDivider();
     if (this.getEnableStatistics()) {
-      this.statistics = new Statistics(this.stationInfo.chargingStationId);
+      this.performanceStatistics = new PerformanceStatistics(this.stationInfo.chargingStationId);
       this.performanceObserver = new PerformanceObserver((list) => {
         const entry = list.getEntries()[0];
-        this.statistics.logPerformance(entry, Constants.ENTITY_CHARGING_STATION);
+        this.performanceStatistics.logPerformance(entry, Constants.ENTITY_CHARGING_STATION);
         this.performanceObserver.disconnect();
       });
     }
@@ -530,7 +530,7 @@ export default class ChargingStation {
         // Incoming Message
         case MessageType.CALL_MESSAGE:
           if (this.getEnableStatistics()) {
-            this.statistics.addMessage(commandName, messageType);
+            this.performanceStatistics.addMessage(commandName, messageType);
           }
           // Process the call
           await this.ocppIncomingRequestService.handleRequest(messageId, commandName, commandPayload);
@@ -733,7 +733,7 @@ export default class ChargingStation {
       }
     }
     if (this.getEnableStatistics()) {
-      this.statistics.start();
+      this.performanceStatistics.start();
     }
   }
 
index 6b8443520490718cd4075e5fcb420b9f2f2324ee..d74de14e9b84fc123e04882488d2b4465a6c0985 100644 (file)
@@ -271,7 +271,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
           logger.debug(this.chargingStation.logPrefix() + ' Transaction remotely STARTED on ' + this.chargingStation.stationInfo.chargingStationId + '#' + transactionConnectorID.toString() + ' for idTag ' + commandPayload.idTag);
           return Constants.OCPP_RESPONSE_ACCEPTED;
         }
-        logger.error(this.chargingStation.logPrefix() + ' Remote starting transaction REJECTED on connector Id ' + transactionConnectorID.toString() + ', idTag ' + commandPayload.idTag);
+        logger.warn(this.chargingStation.logPrefix() + ' Remote starting transaction REJECTED on connector Id ' + transactionConnectorID.toString() + ', idTag ' + commandPayload.idTag);
         return Constants.OCPP_RESPONSE_REJECTED;
       }
       await this.chargingStation.ocppRequestService.sendStatusNotification(transactionConnectorID, OCPP16ChargePointStatus.PREPARING);
@@ -287,7 +287,7 @@ export default class OCPP16IncomingRequestService extends OCPPIncomingRequestSer
       logger.debug(this.chargingStation.logPrefix() + ' Transaction remotely STARTED on ' + this.chargingStation.stationInfo.chargingStationId + '#' + transactionConnectorID.toString() + ' for idTag ' + commandPayload.idTag);
       return Constants.OCPP_RESPONSE_ACCEPTED;
     }
-    logger.error(this.chargingStation.logPrefix() + ' Remote starting transaction REJECTED on unavailable connector Id ' + transactionConnectorID.toString() + ', idTag ' + commandPayload.idTag);
+    logger.warn(this.chargingStation.logPrefix() + ' Remote starting transaction REJECTED on unavailable connector Id ' + transactionConnectorID.toString() + ', idTag ' + commandPayload.idTag);
     return Constants.OCPP_RESPONSE_REJECTED;
   }
 
index cf1c9212d87ee4ff26be6888edd26ac378fb37e1..b06f1c74811dbc33a5a54ee37d6dbbae7bdfae7d 100644 (file)
@@ -21,13 +21,13 @@ export default class OCPP16ResponseService extends OCPPResponseService {
 
   private handleResponseBootNotification(payload: OCPP16BootNotificationResponse, requestPayload: OCPP16BootNotificationRequest): void {
     if (payload.status === OCPP16RegistrationStatus.ACCEPTED) {
-      this.chargingStation.heartbeatSetInterval ? this.chargingStation.restartHeartbeat() : this.chargingStation.startHeartbeat();
       this.chargingStation.addConfigurationKey(OCPP16StandardParametersKey.HeartBeatInterval, payload.interval.toString());
       this.chargingStation.addConfigurationKey(OCPP16StandardParametersKey.HeartbeatInterval, payload.interval.toString(), false, false);
+      this.chargingStation.heartbeatSetInterval ? this.chargingStation.restartHeartbeat() : this.chargingStation.startHeartbeat();
     } else if (payload.status === OCPP16RegistrationStatus.PENDING) {
       logger.info(this.chargingStation.logPrefix() + ' Charging station in pending state on the central server');
     } else {
-      logger.info(this.chargingStation.logPrefix() + ' Charging station rejected by the central server');
+      logger.warn(this.chargingStation.logPrefix() + ' Charging station rejected by the central server');
     }
   }
 
@@ -64,7 +64,7 @@ export default class OCPP16ResponseService extends OCPPResponseService {
       const configuredMeterValueSampleInterval = this.chargingStation.getConfigurationKey(OCPP16StandardParametersKey.MeterValueSampleInterval);
       this.chargingStation.startMeterValues(connectorId, configuredMeterValueSampleInterval ? Utils.convertToInt(configuredMeterValueSampleInterval.value) * 1000 : 60000);
     } else {
-      logger.error(this.chargingStation.logPrefix() + ' Starting transaction id ' + payload.transactionId.toString() + ' REJECTED with status ' + payload?.idTagInfo?.status + ', idTag ' + requestPayload.idTag);
+      logger.warn(this.chargingStation.logPrefix() + ' Starting transaction id ' + payload.transactionId.toString() + ' REJECTED with status ' + payload?.idTagInfo?.status + ', idTag ' + requestPayload.idTag);
       this.chargingStation.resetTransactionOnConnector(connectorId);
       await this.chargingStation.ocppRequestService.sendStatusNotification(connectorId, OCPP16ChargePointStatus.AVAILABLE);
       this.chargingStation.getConnector(connectorId).status = OCPP16ChargePointStatus.AVAILABLE;
@@ -97,7 +97,7 @@ export default class OCPP16ResponseService extends OCPPResponseService {
       logger.info(this.chargingStation.logPrefix() + ' Transaction ' + requestPayload.transactionId.toString() + ' STOPPED on ' + this.chargingStation.stationInfo.chargingStationId + '#' + transactionConnectorId.toString());
       this.chargingStation.resetTransactionOnConnector(transactionConnectorId);
     } else {
-      logger.error(this.chargingStation.logPrefix() + ' Stopping transaction id ' + requestPayload.transactionId.toString() + ' REJECTED with status ' + payload.idTagInfo?.status);
+      logger.warn(this.chargingStation.logPrefix() + ' Stopping transaction id ' + requestPayload.transactionId.toString() + ' REJECTED with status ' + payload.idTagInfo?.status);
     }
   }
 
index 20163f7d7d157186c56556c553963816f32b47cc..1a91cc8be75124b0b50ad881243c47c4c88c846e 100644 (file)
@@ -49,7 +49,7 @@ export default abstract class OCPPRequestService {
       // Check if wsConnection opened and charging station registered
       if (this.chargingStation.isWebSocketOpen() && (this.chargingStation.isRegistered() || commandName === RequestCommand.BOOT_NOTIFICATION)) {
         if (this.chargingStation.getEnableStatistics()) {
-          this.chargingStation.statistics.addMessage(commandName, messageType);
+          this.chargingStation.performanceStatistics.addMessage(commandName, messageType);
         }
         // Yes: Send Message
         this.chargingStation.wsConnection.send(messageToSend);
@@ -76,7 +76,7 @@ export default abstract class OCPPRequestService {
        */
       async function responseCallback(payload: Record<string, unknown> | string, requestPayload: Record<string, unknown>): Promise<void> {
         if (self.chargingStation.getEnableStatistics()) {
-          self.chargingStation.statistics.addMessage(commandName, MessageType.CALL_RESULT_MESSAGE);
+          self.chargingStation.performanceStatistics.addMessage(commandName, MessageType.CALL_RESULT_MESSAGE);
         }
         // Send the response
         await self.ocppResponseService.handleResponse(commandName as RequestCommand, payload, requestPayload);
@@ -90,7 +90,7 @@ export default abstract class OCPPRequestService {
        */
       function rejectCallback(error: OCPPError): void {
         if (self.chargingStation.getEnableStatistics()) {
-          self.chargingStation.statistics.addMessage(commandName, MessageType.CALL_ERROR_MESSAGE);
+          self.chargingStation.performanceStatistics.addMessage(commandName, MessageType.CALL_ERROR_MESSAGE);
         }
         logger.debug(`${self.chargingStation.logPrefix()} Error: %j occurred when calling command %s with parameters: %j`, error, commandName, commandParams);
         // Build Exception
similarity index 96%
rename from src/utils/Statistics.ts
rename to src/utils/PerformanceStatistics.ts
index 65e3be5b516c747050e2a38156e01c7b5807343d..d7a4388edf3f95c7d6776c193e980064ba3c7e9d 100644 (file)
@@ -8,7 +8,7 @@ import { PerformanceEntry } from 'perf_hooks';
 import Utils from './Utils';
 import logger from './Logger';
 
-export default class Statistics {
+export default class PerformanceStatistics {
   private objId: string;
   private commandsStatistics: CommandStatistics;
 
@@ -52,7 +52,7 @@ export default class Statistics {
         }
         break;
       default:
-        logger.error(`${this.logPrefix()} Wrong message type ${messageType}`);
+        logger.error(`${this.logPrefix()} wrong message type ${messageType}`);
         break;
     }
   }
@@ -64,7 +64,7 @@ export default class Statistics {
     perfEntry.entryType = entry.entryType;
     perfEntry.startTime = entry.startTime;
     perfEntry.duration = entry.duration;
-    logger.info(`${this.logPrefix()} object ${className} method(s) performance entry: %j`, perfEntry);
+    logger.info(`${this.logPrefix()} ${className} method(s) entry: %j`, perfEntry);
   }
 
   public start(): void {
@@ -137,6 +137,6 @@ export default class Statistics {
   }
 
   private logPrefix(): string {
-    return Utils.logPrefix(` ${this.objId} Statistics:`);
+    return Utils.logPrefix(` ${this.objId} | Performance statistics`);
   }
 }