repositories
/
e-mobility-charging-stations-simulator.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
feat(simulator): allow to deprecate only template configuration key
[e-mobility-charging-stations-simulator.git]
/
src
/
charging-station
/
IdTagsCache.ts
diff --git
a/src/charging-station/IdTagsCache.ts
b/src/charging-station/IdTagsCache.ts
index 7b49b590d17332a47bc17cbe077ebe664b16b7f0..26ef71318af3bf9cb4834c9b4e07e29faa70e1ba 100644
(file)
--- a/
src/charging-station/IdTagsCache.ts
+++ b/
src/charging-station/IdTagsCache.ts
@@
-1,12
+1,20
@@
-import
fs
from 'node:fs';
+import
{ type FSWatcher, readFileSync }
from 'node:fs';
-import { type ChargingStation, ChargingStationUtils } from './internal';
+import type { ChargingStation } from './ChargingStation';
+import { getIdTagsFile } from './ChargingStationUtils';
import { FileType, IdTagDistribution } from '../types';
import { FileType, IdTagDistribution } from '../types';
-import { FileUtils, Utils, logger } from '../utils';
+import {
+ handleFileException,
+ isNotEmptyString,
+ logPrefix,
+ logger,
+ secureRandom,
+ watchJsonFile,
+} from '../utils';
type IdTagsCacheValueType = {
idTags: string[];
type IdTagsCacheValueType = {
idTags: string[];
- idTagsFileWatcher:
fs.
FSWatcher | undefined;
+ idTagsFileWatcher: FSWatcher | undefined;
};
export class IdTagsCache {
};
export class IdTagsCache {
@@
-26,13
+34,22
@@
export class IdTagsCache {
return IdTagsCache.instance;
}
return IdTagsCache.instance;
}
+ /**
+ * Gets one idtag from the cache given the distribution
+ * Must be called after checking the cache is not an empty array
+ *
+ * @param distribution -
+ * @param chargingStation -
+ * @param connectorId -
+ * @returns
+ */
public getIdTag(
distribution: IdTagDistribution,
chargingStation: ChargingStation,
public getIdTag(
distribution: IdTagDistribution,
chargingStation: ChargingStation,
- connectorId: number
+ connectorId: number
,
): string {
const hashId = chargingStation.stationInfo.hashId;
): string {
const hashId = chargingStation.stationInfo.hashId;
- const idTagsFile =
ChargingStationUtils.getAuthorization
File(chargingStation.stationInfo);
+ const idTagsFile =
getIdTags
File(chargingStation.stationInfo);
switch (distribution) {
case IdTagDistribution.RANDOM:
return this.getRandomIdTag(hashId, idTagsFile);
switch (distribution) {
case IdTagDistribution.RANDOM:
return this.getRandomIdTag(hashId, idTagsFile);
@@
-45,6
+62,13
@@
export class IdTagsCache {
}
}
}
}
+ /**
+ * Gets all idtags from the cache
+ * Must be called after checking the cache is not an empty array
+ *
+ * @param file -
+ * @returns
+ */
public getIdTags(file: string): string[] | undefined {
if (this.hasIdTagsCache(file) === false) {
this.setIdTagsCache(file, this.getIdTagsFromFile(file));
public getIdTags(file: string): string[] | undefined {
if (this.hasIdTagsCache(file) === false) {
this.setIdTagsCache(file, this.getIdTagsFromFile(file));
@@
-53,39
+77,41
@@
export class IdTagsCache {
}
public deleteIdTags(file: string): boolean {
}
public deleteIdTags(file: string): boolean {
- return this.deleteIdTagsCache(file);
+ return this.deleteIdTagsCache(file)
&& this.deleteIdTagsCacheIndexes(file)
;
}
private getRandomIdTag(hashId: string, file: string): string {
const idTags = this.getIdTags(file);
}
private getRandomIdTag(hashId: string, file: string): string {
const idTags = this.getIdTags(file);
- const addressableKey =
file + hashId
;
+ const addressableKey =
this.getIdTagsCacheIndexesAddressableKey(file, hashId)
;
this.idTagsCachesAddressableIndexes.set(
addressableKey,
this.idTagsCachesAddressableIndexes.set(
addressableKey,
- Math.floor(
Utils.secureRandom() * idTags.length)
+ Math.floor(
secureRandom() * idTags.length),
);
return idTags[this.idTagsCachesAddressableIndexes.get(addressableKey)];
}
private getRoundRobinIdTag(hashId: string, file: string): string {
const idTags = this.getIdTags(file);
);
return idTags[this.idTagsCachesAddressableIndexes.get(addressableKey)];
}
private getRoundRobinIdTag(hashId: string, file: string): string {
const idTags = this.getIdTags(file);
- const addressableKey =
file + hashId
;
+ const addressableKey =
this.getIdTagsCacheIndexesAddressableKey(file, hashId)
;
const idTagIndex = this.idTagsCachesAddressableIndexes.get(addressableKey) ?? 0;
const idTag = idTags[idTagIndex];
this.idTagsCachesAddressableIndexes.set(
addressableKey,
const idTagIndex = this.idTagsCachesAddressableIndexes.get(addressableKey) ?? 0;
const idTag = idTags[idTagIndex];
this.idTagsCachesAddressableIndexes.set(
addressableKey,
- idTagIndex === idTags.length - 1 ? 0 : idTagIndex + 1
+ idTagIndex === idTags.length - 1 ? 0 : idTagIndex + 1
,
);
return idTag;
}
private getConnectorAffinityIdTag(chargingStation: ChargingStation, connectorId: number): string {
);
return idTag;
}
private getConnectorAffinityIdTag(chargingStation: ChargingStation, connectorId: number): string {
- const file =
ChargingStationUtils.getAuthorization
File(chargingStation.stationInfo);
+ const file =
getIdTags
File(chargingStation.stationInfo);
const idTags = this.getIdTags(file);
const idTags = this.getIdTags(file);
- const hashId = chargingStation.stationInfo.hashId;
- const addressableKey = file + hashId;
+ const addressableKey = this.getIdTagsCacheIndexesAddressableKey(
+ file,
+ chargingStation.stationInfo.hashId,
+ );
this.idTagsCachesAddressableIndexes.set(
addressableKey,
this.idTagsCachesAddressableIndexes.set(
addressableKey,
- (chargingStation.index - 1 + (connectorId - 1)) % idTags.length
+ (chargingStation.index - 1 + (connectorId - 1)) % idTags.length
,
);
return idTags[this.idTagsCachesAddressableIndexes.get(addressableKey)];
}
);
return idTags[this.idTagsCachesAddressableIndexes.get(addressableKey)];
}
@@
-97,32
+123,32
@@
export class IdTagsCache {
private setIdTagsCache(file: string, idTags: string[]) {
return this.idTagsCaches.set(file, {
idTags,
private setIdTagsCache(file: string, idTags: string[]) {
return this.idTagsCaches.set(file, {
idTags,
- idTagsFileWatcher:
FileUtils.
watchJsonFile(
+ idTagsFileWatcher: watchJsonFile(
file,
FileType.Authorization,
this.logPrefix(file),
undefined,
(event, filename) => {
file,
FileType.Authorization,
this.logPrefix(file),
undefined,
(event, filename) => {
- if (
Utils.
isNotEmptyString(filename) && event === 'change') {
+ if (isNotEmptyString(filename) && event === 'change') {
try {
logger.debug(
try {
logger.debug(
- `${this.logPrefix(file)} ${FileType.Authorization} file have changed, reload`
+ `${this.logPrefix(file)} ${FileType.Authorization} file have changed, reload`
,
);
this.deleteIdTagsCache(file);
this.deleteIdTagsCacheIndexes(file);
} catch (error) {
);
this.deleteIdTagsCache(file);
this.deleteIdTagsCacheIndexes(file);
} catch (error) {
-
FileUtils.
handleFileException(
+ handleFileException(
file,
FileType.Authorization,
error as NodeJS.ErrnoException,
this.logPrefix(file),
{
throwError: false,
file,
FileType.Authorization,
error as NodeJS.ErrnoException,
this.logPrefix(file),
{
throwError: false,
- }
+ }
,
);
}
}
);
}
}
- }
+ }
,
),
});
}
),
});
}
@@
-136,35
+162,37
@@
export class IdTagsCache {
return this.idTagsCaches.delete(file);
}
return this.idTagsCaches.delete(file);
}
- private deleteIdTagsCacheIndexes(file: string): void {
+ private deleteIdTagsCacheIndexes(file: string): boolean {
+ let deleted: boolean[];
for (const [key] of this.idTagsCachesAddressableIndexes) {
if (key.startsWith(file)) {
for (const [key] of this.idTagsCachesAddressableIndexes) {
if (key.startsWith(file)) {
-
this.idTagsCachesAddressableIndexes.delete(key
);
+
deleted.push(this.idTagsCachesAddressableIndexes.delete(key)
);
}
}
}
}
+ return !deleted.some((value) => value === false);
+ }
+
+ private getIdTagsCacheIndexesAddressableKey(prefix: string, uid: string): string {
+ return `${prefix}${uid}`;
}
private getIdTagsFromFile(file: string): string[] {
}
private getIdTagsFromFile(file: string): string[] {
- let idTags: string[] = [];
- if (file) {
+ if (isNotEmptyString(file)) {
try {
try {
- // Load id tags file
- idTags = JSON.parse(fs.readFileSync(file, 'utf8')) as string[];
+ return JSON.parse(readFileSync(file, 'utf8')) as string[];
} catch (error) {
} catch (error) {
-
FileUtils.
handleFileException(
+ handleFileException(
file,
FileType.Authorization,
error as NodeJS.ErrnoException,
file,
FileType.Authorization,
error as NodeJS.ErrnoException,
- this.logPrefix(file)
+ this.logPrefix(file)
,
);
}
);
}
- } else {
- logger.info(`${this.logPrefix(file)} No id tags file given`);
}
}
- return
idTags
;
+ return
[]
;
}
private logPrefix = (file: string): string => {
}
private logPrefix = (file: string): string => {
- return
Utils.
logPrefix(` Id tags cache for id tags file '${file}' |`);
+ return logPrefix(` Id tags cache for id tags file '${file}' |`);
};
}
};
}