dirname(fileURLToPath(import.meta.url)),
`ChargingStationWorker${extname(fileURLToPath(import.meta.url))}`,
);
- Configuration.getConfigurationSection<UIServerConfiguration>(ConfigurationSection.uiServer)
- .enabled === true &&
- (this.uiServer = UIServerFactory.getUIServerImplementation(
- Configuration.getConfigurationSection<UIServerConfiguration>(ConfigurationSection.uiServer),
- ));
- Configuration.getConfigurationSection<StorageConfiguration>(
- ConfigurationSection.performanceStorage,
- ).enabled === true &&
+ const uiServerConfiguration = Configuration.getConfigurationSection<UIServerConfiguration>(
+ ConfigurationSection.uiServer,
+ );
+ uiServerConfiguration.enabled === true &&
+ (this.uiServer = UIServerFactory.getUIServerImplementation(uiServerConfiguration));
+ const performanceStorageConfiguration =
+ Configuration.getConfigurationSection<StorageConfiguration>(
+ ConfigurationSection.performanceStorage,
+ );
+ performanceStorageConfiguration.enabled === true &&
(this.storage = StorageFactory.getStorage(
- Configuration.getConfigurationSection<StorageConfiguration>(
- ConfigurationSection.performanceStorage,
- ).type!,
- Configuration.getConfigurationSection<StorageConfiguration>(
- ConfigurationSection.performanceStorage,
- ).uri!,
+ performanceStorageConfiguration.type!,
+ performanceStorageConfiguration.uri!,
this.logPrefix(),
));
Configuration.setConfigurationChangeCallback(async () => Bootstrap.getInstance().restart());
if (this.starting === false) {
this.starting = true;
this.initializeCounters();
- this.initializeWorkerImplementation();
+ const workerConfiguration = Configuration.getConfigurationSection<WorkerConfiguration>(
+ ConfigurationSection.worker,
+ );
+ this.initializeWorkerImplementation(workerConfiguration);
await this.workerImplementation?.start();
await this.storage?.open();
this.uiServer?.start();
this.version
} started with ${this.numberOfChargingStations.toString()} charging station(s) from ${this.numberOfChargingStationTemplates.toString()} configured charging station template(s) and ${
Configuration.workerDynamicPoolInUse()
- ? `${Configuration.getConfigurationSection<WorkerConfiguration>(
- ConfigurationSection.worker,
- ).poolMinSize?.toString()}/`
+ ? `${workerConfiguration.poolMinSize?.toString()}/`
: ''
}${this.workerImplementation?.size}${
Configuration.workerPoolInUse()
- ? `/${Configuration.getConfigurationSection<WorkerConfiguration>(
- ConfigurationSection.worker,
- ).poolMaxSize?.toString()}`
+ ? `/${workerConfiguration.poolMaxSize?.toString()}`
: ''
- } worker(s) concurrently running in '${
- Configuration.getConfigurationSection<WorkerConfiguration>(
- ConfigurationSection.worker,
- ).processType
- }' mode${
+ } worker(s) concurrently running in '${workerConfiguration.processType}' mode${
!isNullOrUndefined(this.workerImplementation?.maxElementsPerWorker)
? ` (${this.workerImplementation?.maxElementsPerWorker} charging station(s) per worker)`
: ''
await this.start();
}
- private initializeWorkerImplementation(): void {
+ private initializeWorkerImplementation(workerConfiguration: WorkerConfiguration): void {
let elementsPerWorker: number | undefined;
- if (
- Configuration.getConfigurationSection<WorkerConfiguration>(ConfigurationSection.worker)
- ?.elementsPerWorker === 'auto'
- ) {
+ if (workerConfiguration?.elementsPerWorker === 'auto') {
elementsPerWorker =
this.numberOfChargingStations > availableParallelism()
? Math.round(this.numberOfChargingStations / availableParallelism())
this.workerImplementation === null &&
(this.workerImplementation = WorkerFactory.getWorkerImplementation<ChargingStationWorkerData>(
this.workerScript,
- Configuration.getConfigurationSection<WorkerConfiguration>(ConfigurationSection.worker)
- .processType!,
+ workerConfiguration.processType!,
{
- workerStartDelay: Configuration.getConfigurationSection<WorkerConfiguration>(
- ConfigurationSection.worker,
- ).startDelay,
- elementStartDelay: Configuration.getConfigurationSection<WorkerConfiguration>(
- ConfigurationSection.worker,
- ).elementStartDelay,
- poolMaxSize: Configuration.getConfigurationSection<WorkerConfiguration>(
- ConfigurationSection.worker,
- ).poolMaxSize!,
- poolMinSize: Configuration.getConfigurationSection<WorkerConfiguration>(
- ConfigurationSection.worker,
- ).poolMinSize!,
- elementsPerWorker:
- elementsPerWorker ??
- (Configuration.getConfigurationSection<WorkerConfiguration>(ConfigurationSection.worker)
- .elementsPerWorker as number),
+ workerStartDelay: workerConfiguration.startDelay,
+ elementStartDelay: workerConfiguration.elementStartDelay,
+ poolMaxSize: workerConfiguration.poolMaxSize!,
+ poolMinSize: workerConfiguration.poolMinSize!,
+ elementsPerWorker: elementsPerWorker ?? (workerConfiguration.elementsPerWorker as number),
poolOptions: {
messageHandler: this.messageHandler.bind(this) as (message: unknown) => void,
},
import { UIHttpServer } from './UIHttpServer';
import { UIServerUtils } from './UIServerUtils';
import { UIWebSocketServer } from './UIWebSocketServer';
-import { ApplicationProtocol, ConfigurationSection, type UIServerConfiguration } from '../../types';
-import { Configuration } from '../../utils';
+import { ApplicationProtocol, type UIServerConfiguration } from '../../types';
export class UIServerFactory {
private constructor() {
}
public static getUIServerImplementation(
- uiServerConfiguration?: UIServerConfiguration,
+ uiServerConfiguration: UIServerConfiguration,
): AbstractUIServer | null {
- if (UIServerUtils.isLoopback(uiServerConfiguration!.options!.host!) === false) {
+ if (UIServerUtils.isLoopback(uiServerConfiguration.options!.host!) === false) {
console.warn(
chalk.yellow(
'Loopback address not detected in UI server configuration. This is not recommended.',
),
);
}
- switch (
- uiServerConfiguration?.type ??
- Configuration.getConfigurationSection<UIServerConfiguration>(ConfigurationSection.uiServer)
- .type
- ) {
+ switch (uiServerConfiguration.type) {
case ApplicationProtocol.WS:
- return new UIWebSocketServer(
- uiServerConfiguration ??
- Configuration.getConfigurationSection<UIServerConfiguration>(
- ConfigurationSection.uiServer,
- ),
- );
+ return new UIWebSocketServer(uiServerConfiguration);
case ApplicationProtocol.HTTP:
- return new UIHttpServer(
- uiServerConfiguration ??
- Configuration.getConfigurationSection<UIServerConfiguration>(
- ConfigurationSection.uiServer,
- ),
- );
+ return new UIHttpServer(uiServerConfiguration);
default:
return null;
}
public start(): void {
this.startLogStatisticsInterval();
- if (
+ const performanceStorageConfiguration =
Configuration.getConfigurationSection<StorageConfiguration>(
ConfigurationSection.performanceStorage,
- ).enabled
- ) {
+ );
+ if (performanceStorageConfiguration.enabled) {
logger.info(
- `${this.logPrefix()} storage enabled: type ${
- Configuration.getConfigurationSection<StorageConfiguration>(
- ConfigurationSection.performanceStorage,
- ).type
- }, uri: ${
- Configuration.getConfigurationSection<StorageConfiguration>(
- ConfigurationSection.performanceStorage,
- ).uri
+ `${this.logPrefix()} storage enabled: type ${performanceStorageConfiguration.type}, uri: ${
+ performanceStorageConfiguration.uri
}`,
);
}
}
private startLogStatisticsInterval(): void {
- const logStatisticsInterval = Configuration.getConfigurationSection<LogConfiguration>(
+ const logConfiguration = Configuration.getConfigurationSection<LogConfiguration>(
ConfigurationSection.log,
- ).enabled
- ? Configuration.getConfigurationSection<LogConfiguration>(ConfigurationSection.log)
- .statisticsInterval!
+ );
+ const logStatisticsInterval = logConfiguration.enabled
+ ? logConfiguration.statisticsInterval!
: 0;
if (logStatisticsInterval > 0 && !this.displayInterval) {
this.displayInterval = setInterval(() => {
logger.info(
`${this.logPrefix()} already logged every ${formatDurationSeconds(logStatisticsInterval)}`,
);
- } else if (
- Configuration.getConfigurationSection<LogConfiguration>(ConfigurationSection.log).enabled
- ) {
+ } else if (logConfiguration.enabled) {
logger.info(
`${this.logPrefix()} log interval is set to ${logStatisticsInterval?.toString()}. Not logging statistics`,
);
import { insertAt } from './Utils';
import { ConfigurationSection, type LogConfiguration } from '../types';
+const logConfiguration = Configuration.getConfigurationSection<LogConfiguration>(
+ ConfigurationSection.log,
+);
let transports: transport[];
-if (
- Configuration.getConfigurationSection<LogConfiguration>(ConfigurationSection.log).rotate === true
-) {
- const logMaxFiles = Configuration.getConfigurationSection<LogConfiguration>(
- ConfigurationSection.log,
- ).maxFiles;
- const logMaxSize = Configuration.getConfigurationSection<LogConfiguration>(
- ConfigurationSection.log,
- ).maxSize;
+if (logConfiguration.rotate === true) {
+ const logMaxFiles = logConfiguration.maxFiles;
+ const logMaxSize = logConfiguration.maxSize;
transports = [
new DailyRotateFile({
filename: insertAt(
- Configuration.getConfigurationSection<LogConfiguration>(ConfigurationSection.log)
- .errorFile!,
+ logConfiguration.errorFile!,
'-%DATE%',
- Configuration.getConfigurationSection<LogConfiguration>(
- ConfigurationSection.log,
- ).errorFile!.indexOf('.log'),
+ logConfiguration.errorFile!.indexOf('.log'),
),
level: 'error',
...(logMaxFiles && { maxFiles: logMaxFiles }),
...(logMaxSize && { maxSize: logMaxSize }),
}),
new DailyRotateFile({
- filename: insertAt(
- Configuration.getConfigurationSection<LogConfiguration>(ConfigurationSection.log).file!,
- '-%DATE%',
- Configuration.getConfigurationSection<LogConfiguration>(
- ConfigurationSection.log,
- ).file!.indexOf('.log'),
- ),
+ filename: insertAt(logConfiguration.file!, '-%DATE%', logConfiguration.file!.indexOf('.log')),
...(logMaxFiles && { maxFiles: logMaxFiles }),
...(logMaxSize && { maxSize: logMaxSize }),
}),
} else {
transports = [
new TransportType.File({
- filename: Configuration.getConfigurationSection<LogConfiguration>(ConfigurationSection.log)
- .errorFile,
+ filename: logConfiguration.errorFile,
level: 'error',
}),
new TransportType.File({
- filename: Configuration.getConfigurationSection<LogConfiguration>(ConfigurationSection.log)
- .file,
+ filename: logConfiguration.file,
}),
];
}
export const logger = createLogger({
- silent: !Configuration.getConfigurationSection<LogConfiguration>(ConfigurationSection.log)
- .enabled,
- level: Configuration.getConfigurationSection<LogConfiguration>(ConfigurationSection.log).level,
- format: format.combine(
- format.splat(),
- (
- format[
- Configuration.getConfigurationSection<LogConfiguration>(ConfigurationSection.log).format!
- ] as FormatWrap
- )(),
- ),
+ silent: !logConfiguration.enabled,
+ level: logConfiguration.level,
+ format: format.combine(format.splat(), (format[logConfiguration.format!] as FormatWrap)()),
transports,
});
// If enabled, log to the `console` with the format:
// `${info.level}: ${info.message} JSON.stringify({ ...rest }) `
//
-if (Configuration.getConfigurationSection<LogConfiguration>(ConfigurationSection.log).console) {
+if (logConfiguration.console) {
logger.add(
new TransportType.Console({
- format: format.combine(
- format.splat(),
- (
- format[
- Configuration.getConfigurationSection<LogConfiguration>(ConfigurationSection.log)
- .format!
- ] as FormatWrap
- )(),
- ),
+ format: format.combine(format.splat(), (format[logConfiguration.format!] as FormatWrap)()),
}),
);
}