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
refactor: cleanup imports
[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 f19f66f31948b270de58a115fee550a4a6fcbdcb..c0f4d2b9b1e655c80b8280eb60e92b29ffa17688 100644
(file)
--- a/
src/charging-station/IdTagsCache.ts
+++ b/
src/charging-station/IdTagsCache.ts
@@
-1,12
+1,13
@@
-import
fs
from 'node:fs';
+import
{ type FSWatcher, readFileSync }
from 'node:fs';
-import { type ChargingStation, ChargingStationUtils } from './internal';
+import type { ChargingStation } from './ChargingStation';
+import { ChargingStationUtils } from './ChargingStationUtils';
import { FileType, IdTagDistribution } from '../types';
import { FileType, IdTagDistribution } from '../types';
-import {
FileUtils, Utils, logger
} from '../utils';
+import {
Utils, handleFileException, logger, 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,6
+27,15
@@
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,
@@
-45,6
+55,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,12
+70,12
@@
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 = this.getIdTagsCacheAddressableKey(file, hashId);
+ const addressableKey = this.getIdTagsCache
Indexes
AddressableKey(file, hashId);
this.idTagsCachesAddressableIndexes.set(
addressableKey,
Math.floor(Utils.secureRandom() * idTags.length)
this.idTagsCachesAddressableIndexes.set(
addressableKey,
Math.floor(Utils.secureRandom() * idTags.length)
@@
-68,7
+85,7
@@
export class IdTagsCache {
private getRoundRobinIdTag(hashId: string, file: string): string {
const idTags = this.getIdTags(file);
private getRoundRobinIdTag(hashId: string, file: string): string {
const idTags = this.getIdTags(file);
- const addressableKey = this.getIdTagsCacheAddressableKey(file, hashId);
+ const addressableKey = this.getIdTagsCache
Indexes
AddressableKey(file, hashId);
const idTagIndex = this.idTagsCachesAddressableIndexes.get(addressableKey) ?? 0;
const idTag = idTags[idTagIndex];
this.idTagsCachesAddressableIndexes.set(
const idTagIndex = this.idTagsCachesAddressableIndexes.get(addressableKey) ?? 0;
const idTag = idTags[idTagIndex];
this.idTagsCachesAddressableIndexes.set(
@@
-81,8
+98,10
@@
export class IdTagsCache {
private getConnectorAffinityIdTag(chargingStation: ChargingStation, connectorId: number): string {
const file = ChargingStationUtils.getIdTagsFile(chargingStation.stationInfo);
const idTags = this.getIdTags(file);
private getConnectorAffinityIdTag(chargingStation: ChargingStation, connectorId: number): string {
const file = ChargingStationUtils.getIdTagsFile(chargingStation.stationInfo);
const idTags = this.getIdTags(file);
- const hashId = chargingStation.stationInfo.hashId;
- const addressableKey = this.getIdTagsCacheAddressableKey(file, hashId);
+ const addressableKey = this.getIdTagsCacheIndexesAddressableKey(
+ file,
+ chargingStation.stationInfo.hashId
+ );
this.idTagsCachesAddressableIndexes.set(
addressableKey,
(chargingStation.index - 1 + (connectorId - 1)) % idTags.length
this.idTagsCachesAddressableIndexes.set(
addressableKey,
(chargingStation.index - 1 + (connectorId - 1)) % idTags.length
@@
-97,7
+116,7
@@
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),
file,
FileType.Authorization,
this.logPrefix(file),
@@
-111,7
+130,7
@@
export class IdTagsCache {
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,
file,
FileType.Authorization,
error as NodeJS.ErrnoException,
@@
-136,36
+155,34
@@
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 getIdTagsCacheAddressableKey(prefix: string, uid: string): string {
+ private getIdTagsCache
Indexes
AddressableKey(prefix: string, uid: string): string {
return `${prefix}${uid}`;
}
private getIdTagsFromFile(file: string): string[] {
return `${prefix}${uid}`;
}
private getIdTagsFromFile(file: string): string[] {
- let idTags: string[] = [];
- if (file) {
+ if (Utils.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,
this.logPrefix(file)
);
}
file,
FileType.Authorization,
error as NodeJS.ErrnoException,
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 => {