constructor(chargingStation: ChargingStation) {
super();
this.chargingStation = chargingStation;
- this.onmessage = this.handleRequest.bind(this) as (message: MessageEvent) => void;
+ this.onmessage = this.requestHandler.bind(this) as (message: MessageEvent) => void;
}
- private async handleRequest(messageEvent: MessageEvent): Promise<void> {
+ private async requestHandler(messageEvent: MessageEvent): Promise<void> {
const [, command, payload] = messageEvent.data as BroadcastChannelRequest;
if (payload.hashId !== this.chargingStation.hashId) {
import { BroadcastChannel } from 'worker_threads';
-import { BroadcastChannelRequest } from '../types/WorkerBroadcastChannel';
+import { BroadcastChannelRequest, BroadcastChannelResponse } from '../types/WorkerBroadcastChannel';
export default class WorkerBroadcastChannel extends BroadcastChannel {
constructor() {
public sendRequest(request: BroadcastChannelRequest): void {
this.postMessage(request);
}
+
+ public sendResponse(response: BroadcastChannelResponse): void {
+ this.postMessage(response);
+ }
}
public abstract start(): void;
public abstract stop(): void;
- public abstract sendResponse(message: string): void;
+ public abstract sendRequest(request: string): void;
+ public abstract sendResponse(response: string): void;
public abstract logPrefix(modName?: string, methodName?: string): string;
}
socket.on('message', (messageData) => {
this.uiServices
.get(version)
- .messageHandler(messageData)
+ .requestHandler(messageData)
.catch((error) => {
logger.error(
`${this.logPrefix(
this.server.close();
}
- public sendResponse(message: string): void {
- this.broadcastToClients(message);
+ public sendRequest(request: string): void {
+ this.broadcastToClients(request);
+ }
+
+ public sendResponse(response: string): void {
+ this.broadcastToClients(response);
}
public logPrefix(modName?: string, methodName?: string): string {
export default abstract class AbstractUIService {
protected readonly version: ProtocolVersion;
protected readonly uiServer: AbstractUIServer;
- protected readonly messageHandlers: Map<ProcedureName, ProtocolRequestHandler>;
+ protected readonly requestHandlers: Map<ProcedureName, ProtocolRequestHandler>;
protected workerBroadcastChannel: WorkerBroadcastChannel;
constructor(uiServer: AbstractUIServer, version: ProtocolVersion) {
this.version = version;
this.uiServer = uiServer;
- this.messageHandlers = new Map<ProcedureName, ProtocolRequestHandler>([
+ this.requestHandlers = new Map<ProcedureName, ProtocolRequestHandler>([
[ProcedureName.LIST_CHARGING_STATIONS, this.handleListChargingStations.bind(this)],
[ProcedureName.START_SIMULATOR, this.handleStartSimulator.bind(this)],
[ProcedureName.STOP_SIMULATOR, this.handleStopSimulator.bind(this)],
this.workerBroadcastChannel = new WorkerBroadcastChannel();
}
- public async messageHandler(request: RawData): Promise<void> {
+ public async requestHandler(request: RawData): Promise<void> {
let messageId: string;
let command: ProcedureName;
let requestPayload: RequestPayload;
try {
[messageId, command, requestPayload] = this.dataValidation(request);
- if (this.messageHandlers.has(command) === false) {
+ if (this.requestHandlers.has(command) === false) {
throw new BaseError(
`${command} is not implemented to handle message payload ${JSON.stringify(
requestPayload,
}
// Call the message handler to build the response payload
- responsePayload = await this.messageHandlers.get(command)(messageId, requestPayload);
+ responsePayload = await this.requestHandlers.get(command)(messageId, requestPayload);
} catch (error) {
// Log
logger.error(
this.uiServer.sendResponse(this.buildProtocolResponse(messageId, responsePayload));
}
+ protected buildProtocolRequest(
+ messageId: string,
+ procedureName: ProcedureName,
+ payload: RequestPayload
+ ): string {
+ return JSON.stringify([messageId, procedureName, payload] as ProtocolRequest);
+ }
+
protected buildProtocolResponse(messageId: string, payload: ResponsePayload): string {
return JSON.stringify([messageId, payload] as ProtocolResponse);
}
export default class UIService001 extends AbstractUIService {
constructor(uiServer: AbstractUIServer) {
super(uiServer, ProtocolVersion['0.0.1']);
- this.messageHandlers.set(
+ this.requestHandlers.set(
ProcedureName.START_TRANSACTION,
this.handleStartTransaction.bind(this) as ProtocolRequestHandler
);
- this.messageHandlers.set(
+ this.requestHandlers.set(
ProcedureName.STOP_TRANSACTION,
this.handleStopTransaction.bind(this) as ProtocolRequestHandler
);
- this.messageHandlers.set(
+ this.requestHandlers.set(
ProcedureName.START_CHARGING_STATION,
this.handleStartChargingStation.bind(this) as ProtocolRequestHandler
);
- this.messageHandlers.set(
+ this.requestHandlers.set(
ProcedureName.STOP_CHARGING_STATION,
this.handleStopChargingStation.bind(this) as ProtocolRequestHandler
);