'crypto',
'fs',
'http',
+ 'http-status-codes',
'mnemonist/lru-map-with-delete',
'moment',
'mongodb',
if (this.isResponse(messageEvent.data)) {
return;
}
- if (Array.isArray(messageEvent.data) === false) {
- throw new BaseError('Worker broadcast channel protocol request is not an array');
- }
+ this.validateMessageEvent(messageEvent);
const [uuid, command, requestPayload] = messageEvent.data as BroadcastChannelRequest;
-import BaseError from '../exception/BaseError';
import { BroadcastChannelResponse, MessageEvent } from '../types/WorkerBroadcastChannel';
import logger from '../utils/Logger';
import type AbstractUIService from './ui-server/ui-services/AbstractUIService';
if (this.isRequest(messageEvent.data)) {
return;
}
- if (Array.isArray(messageEvent.data) === false) {
- throw new BaseError('Worker broadcast channel protocol response is not an array');
- }
+ this.validateMessageEvent(messageEvent);
const [uuid, responsePayload] = messageEvent.data as BroadcastChannelResponse;
this.uiService.sendResponse(uuid, responsePayload);
import { BroadcastChannel } from 'worker_threads';
-import { BroadcastChannelRequest, BroadcastChannelResponse } from '../types/WorkerBroadcastChannel';
+import BaseError from '../exception/BaseError';
+import {
+ BroadcastChannelRequest,
+ BroadcastChannelResponse,
+ MessageEvent,
+} from '../types/WorkerBroadcastChannel';
export default abstract class WorkerBroadcastChannel extends BroadcastChannel {
protected constructor() {
protected isResponse(message: any): boolean {
return Array.isArray(message) && message.length === 2;
}
+
+ protected validateMessageEvent(messageEvent: MessageEvent): void {
+ if (Array.isArray(messageEvent.data) === false) {
+ throw new BaseError('Worker broadcast channel protocol message event data is not an array');
+ }
+ }
}
import WebSocket from 'ws';
import { ChargingStationData } from '../../types/ChargingStationWorker';
-import { ProtocolVersion } from '../../types/UIProtocol';
+import {
+ ProcedureName,
+ ProtocolRequest,
+ ProtocolResponse,
+ ProtocolVersion,
+ RequestPayload,
+ ResponsePayload,
+} from '../../types/UIProtocol';
import type AbstractUIService from './ui-services/AbstractUIService';
export abstract class AbstractUIServer {
this.uiServices = new Map<ProtocolVersion, AbstractUIService>();
}
+ public buildProtocolRequest(
+ id: string,
+ procedureName: ProcedureName,
+ requestPayload: RequestPayload
+ ): string {
+ return JSON.stringify([id, procedureName, requestPayload] as ProtocolRequest);
+ }
+
+ public buildProtocolResponse(id: string, responsePayload: ResponsePayload): string {
+ return JSON.stringify([id, responsePayload] as ProtocolResponse);
+ }
+
public abstract start(): void;
public abstract stop(): void;
public abstract sendRequest(request: string): void;
import {
ProcedureName,
Protocol,
- ProtocolRequest,
ProtocolResponse,
ProtocolVersion,
RequestPayload,
- ResponsePayload,
ResponseStatus,
} from '../../types/UIProtocol';
import Configuration from '../../utils/Configuration';
const body = JSON.parse(Buffer.concat(bodyBuffer).toString()) as RequestPayload;
this.uiServices
.get(version)
- .requestHandler(this.buildRequest(uuid, procedureName, body ?? {}))
+ .requestHandler(this.buildProtocolRequest(uuid, procedureName, body ?? {}))
.catch(() => {
- this.sendResponse(this.buildResponse(uuid, { status: ResponseStatus.FAILURE }));
+ this.sendResponse(
+ this.buildProtocolResponse(uuid, { status: ResponseStatus.FAILURE })
+ );
});
});
} else {
`${this.logPrefix(moduleName, 'requestListener')} Handle HTTP request error:`,
error
);
- this.sendResponse(this.buildResponse(uuid, { status: ResponseStatus.FAILURE }));
+ this.sendResponse(this.buildProtocolResponse(uuid, { status: ResponseStatus.FAILURE }));
}
}
- private buildRequest(
- id: string,
- procedureName: ProcedureName,
- requestPayload: RequestPayload
- ): string {
- return JSON.stringify([id, procedureName, requestPayload] as ProtocolRequest);
- }
-
- private buildResponse(id: string, responsePayload: ResponsePayload): string {
- return JSON.stringify([id, responsePayload] as ProtocolResponse);
- }
-
private responseStatusToStatusCode(status: ResponseStatus): StatusCodes {
switch (status) {
case ResponseStatus.SUCCESS:
ProcedureName,
ProtocolRequest,
ProtocolRequestHandler,
- ProtocolResponse,
ProtocolVersion,
RequestPayload,
ResponsePayload,
procedureName: ProcedureName,
requestPayload: RequestPayload
): void {
- this.uiServer.sendRequest(this.buildProtocolRequest(messageId, procedureName, requestPayload));
+ this.uiServer.sendRequest(
+ this.uiServer.buildProtocolRequest(messageId, procedureName, requestPayload)
+ );
}
public sendResponse(messageId: string, responsePayload: ResponsePayload): void {
- this.uiServer.sendResponse(this.buildProtocolResponse(messageId, responsePayload));
+ this.uiServer.sendResponse(this.uiServer.buildProtocolResponse(messageId, responsePayload));
}
public logPrefix(modName: string, methodName: string): string {
return this.uiServer.logPrefix(modName, methodName);
}
- private buildProtocolRequest(
- messageId: string,
- procedureName: ProcedureName,
- requestPayload: RequestPayload
- ): string {
- return JSON.stringify([messageId, procedureName, requestPayload] as ProtocolRequest);
- }
-
- private buildProtocolResponse(messageId: string, responsePayload: ResponsePayload): string {
- return JSON.stringify([messageId, responsePayload] as ProtocolResponse);
- }
-
// Validate the raw data received from the UI server
private requestValidation(rawData: RawData | JsonType): ProtocolRequest {
// logger.debug(