refactor: trivial cleanups
[e-mobility-charging-stations-simulator.git] / src / charging-station / ui-server / AbstractUIServer.ts
index ca53d558894c5db16449d8534ebc0770307dbea7..e689441633b47e0c7a6b52cd4c90fb1de58221be 100644 (file)
@@ -2,18 +2,20 @@ import { type IncomingMessage, Server, type ServerResponse } from 'node:http';
 
 import type { WebSocket } from 'ws';
 
+import type { AbstractUIService } from './ui-services/AbstractUIService';
+import { UIServiceFactory } from './ui-services/UIServiceFactory';
+import { BaseError } from '../../exception';
 import {
   AuthenticationType,
   type ChargingStationData,
   type ProcedureName,
   type ProtocolRequest,
   type ProtocolResponse,
-  type ProtocolVersion,
+  ProtocolVersion,
   type RequestPayload,
   type ResponsePayload,
   type UIServerConfiguration,
 } from '../../types';
-import { type AbstractUIService, UIServiceFactory } from '../internal';
 
 export abstract class AbstractUIServer {
   public readonly chargingStations: Map<string, ChargingStationData>;
@@ -44,6 +46,16 @@ export abstract class AbstractUIServer {
     this.chargingStations.clear();
   }
 
+  public async sendInternalRequest(request: ProtocolRequest): Promise<ProtocolResponse> {
+    const protocolVersion = ProtocolVersion['0.0.1'];
+    this.registerProtocolVersionUIService(protocolVersion);
+    return this.uiServices.get(protocolVersion)?.requestHandler(request);
+  }
+
+  public hasResponseHandler(id: string): boolean {
+    return this.responseHandlers.has(id);
+  }
+
   protected startHttpServer(): void {
     if (this.httpServer.listening === false) {
       this.httpServer.listen(this.uiServerConfiguration.options);
@@ -59,7 +71,7 @@ export abstract class AbstractUIServer {
   protected authenticate(req: IncomingMessage, next: (err?: Error) => void): void {
     if (this.isBasicAuthEnabled() === true) {
       if (this.isValidBasicAuth(req) === false) {
-        next(new Error('Unauthorized'));
+        next(new BaseError('Unauthorized'));
       }
       next();
     }