}
}
this._stationInfo.powerDivider = this._getPowerDivider();
- // FIXME: Conditionally initialize or use singleton design pattern per charging station
- this._statistics = new Statistics(this._stationInfo.name);
+ this._statistics = Statistics.getInstance();
+ this._statistics.objName = this._stationInfo.name;
this._performanceObserver = new PerformanceObserver((list) => {
const entry = list.getEntries()[0];
this._statistics.logPerformance(entry, 'ChargingStation');
const Utils = require('./Utils');
class Statistics {
- constructor(objName) {
- this._objName = objName;
+ static instance;
+
+ constructor() {
this._statistics = {};
}
+ set objName(objName) {
+ this._objName = objName;
+ }
+
+ static getInstance() {
+ if (!Statistics.instance) {
+ Statistics.instance = new Statistics();
+ }
+ return Statistics.instance;
+ }
+
_logPrefix() {
return Utils.logPrefix(` ${this._objName} Statistics:`);
}
return date.toISOString().substr(11, 8);
}
- static convertToDate(date) {
- // Check
- if (!date) {
- return date;
- }
- // Check Type
- if (!(date instanceof Date)) {
- return new Date(date);
- }
- return date;
- }
-
static isIterable(obj) {
if (obj) {
return typeof obj[Symbol.iterator] === 'function';
}
}
+ static convertToDate(date) {
+ // Check
+ if (!date) {
+ return date;
+ }
+ // Check Type
+ if (!(date instanceof Date)) {
+ return new Date(date);
+ }
+ return date;
+ }
+
static convertToObjectID(id) {
let changedID = id;
// Check
return result;
}
- static getRandomInt(max, min) {
+ static getRandomFloat(max, min = 0) {
+ if (min) {
+ return Math.random() * (max - min + 1) + min;
+ }
+ return Math.random() * max + 1;
+ }
+
+ static getRandomInt(max, min = 0) {
if (min) {
- return Math.floor((Math.random() * (max - min + 1)) + min);
+ return Math.floor(Utils.getRandomFloat(max, min));
}
- return Math.floor((Math.random() * max + 1));
+ return Math.floor(Utils.getRandomFloat(max));
+ }
+
+ static roundTo(number, scale) {
+ return Utils.convertToFloat(number.toFixed(scale));
+ }
+
+ static getRandomFloatRounded(max, min = 0, scale = 2) {
+ if (min) {
+ return Utils.roundTo(Utils.getRandomFloat(max, min), scale);
+ }
+ return Utils.roundTo(Utils.getRandomFloat(max), scale);
}
static logPrefix(prefixString = '') {
return JSON.parse(JSON.stringify(jsonDocument));
}
+ static isString(value) {
+ return typeof value === 'string';
+ }
+
static isUndefined(value) {
return typeof value === 'undefined';
}