Move attributes init to the right place.
[e-mobility-charging-stations-simulator.git] / src / charging-station / ChargingStation.js
index f64a2747ae780f538b14edb65628fa8d78504cde..2c699be8618bd394ddfc6b66258f1506be3e7055 100644 (file)
@@ -7,12 +7,14 @@ const OCPPError = require('./OcppError');
 const AutomaticTransactionGenerator = require('./AutomaticTransactionGenerator');
 const Statistics = require('../utils/Statistics');
 const fs = require('fs');
+const crypto = require('crypto');
 const {performance, PerformanceObserver} = require('perf_hooks');
 
 class ChargingStation {
   constructor(index, stationTemplateFile) {
     this._index = index;
     this._stationTemplateFile = stationTemplateFile;
+    this._connectors = {};
     this._initialize();
 
     this._isSocketRestart = false;
@@ -65,9 +67,10 @@ class ChargingStation {
     // Build connectors if needed
     const maxConnectors = this._getMaxConnectors();
     const connectorsConfig = Utils.cloneJSonDocument(this._stationInfo.Connectors);
-    const connectorsConfigLength = Utils.convertToBoolean(this._stationInfo.useConnectorId0) && Object.keys(connectorsConfig).includes('0') ? Object.keys(connectorsConfig).length : Object.keys(connectorsConfig).length - 1;
-    if (!this._connectors || (this._connectors && Object.keys(this._connectors).length !== connectorsConfigLength)) {
-      this._connectors = {};
+    const connectorsConfigHash = crypto.createHash('sha256').update(JSON.stringify(connectorsConfig) + maxConnectors.toString()).digest('hex');
+    // FIXME: Handle shrinking the number of connectors
+    if (!this._connectors || (this._connectors && this._connectorsConfigurationHash !== connectorsConfigHash)) {
+      this._connectorsConfigurationHash = connectorsConfigHash;
       // Determine number of customized connectors
       let lastConnector;
       for (lastConnector in connectorsConfig) {
@@ -90,6 +93,7 @@ class ChargingStation {
         this._initTransactionOnConnector(connector);
       }
     }
+    // FIXME: Conditionally initialize or use singleton design pattern per charging station
     this._statistics = new Statistics(this._stationInfo.name);
     this._performanceObserver = new PerformanceObserver((list) => {
       const entry = list.getEntries()[0];
@@ -623,7 +627,7 @@ class ChargingStation {
         // Error Message
         case Constants.OCPP_JSON_CALL_ERROR_MESSAGE:
           // Build Message
-          this._statistics.addMessage(`Error ${command.code ? command.code : Constants.OCPP_ERROR_GENERIC_ERROR} on ${commandName || ''}`);
+          this._statistics.addMessage(`Error ${command.code ? command.code : Constants.OCPP_ERROR_GENERIC_ERROR} on ${commandName}`);
           messageToSend = JSON.stringify([messageType, messageId, command.code ? command.code : Constants.OCPP_ERROR_GENERIC_ERROR, command.message ? command.message : '', command.details ? command.details : {}]);
           break;
       }
@@ -660,7 +664,7 @@ class ChargingStation {
 
       // Function that will receive the request's rejection
       function rejectCallback(reason) {
-        self._statistics.addMessage(`Error ${command.code ? command.code : Constants.OCPP_ERROR_GENERIC_ERROR} on ${commandName || ''}`, true);
+        self._statistics.addMessage(`Error ${command.code ? command.code : Constants.OCPP_ERROR_GENERIC_ERROR} on ${commandName}`, true);
         // Build Exception
         // eslint-disable-next-line no-empty-function
         self._requests[messageId] = [() => { }, () => { }, '']; // Properly format the request