public readonly chargingStations: Map<string, ChargingStationData>
public readonly chargingStationTemplates: Set<string>
protected readonly httpServer: Server | Http2Server
- protected readonly responseHandlers: Map<string, ServerResponse | WebSocket>
+ protected readonly responseHandlers: Map<
+ `${string}-${string}-${string}-${string}-${string}`,
+ ServerResponse | WebSocket
+ >
+
protected readonly uiServices: Map<ProtocolVersion, AbstractUIService>
public constructor (protected readonly uiServerConfiguration: UIServerConfiguration) {
`Unsupported application protocol version ${this.uiServerConfiguration.version}`
)
}
- this.responseHandlers = new Map<string, ServerResponse | WebSocket>()
+ this.responseHandlers = new Map<
+ `${string}-${string}-${string}-${string}-${string}`,
+ ServerResponse | WebSocket
+ >()
this.uiServices = new Map<ProtocolVersion, AbstractUIService>()
}
public buildProtocolRequest (
- id: string,
+ uuid: `${string}-${string}-${string}-${string}-${string}`,
procedureName: ProcedureName,
requestPayload: RequestPayload
): ProtocolRequest {
- return [id, procedureName, requestPayload]
+ return [uuid, procedureName, requestPayload]
}
- public buildProtocolResponse (id: string, responsePayload: ResponsePayload): ProtocolResponse {
- return [id, responsePayload]
+ public buildProtocolResponse (
+ uuid: `${string}-${string}-${string}-${string}-${string}`,
+ responsePayload: ResponsePayload
+ ): ProtocolResponse {
+ return [uuid, responsePayload]
}
public stop (): void {
?.requestHandler(request) as Promise<ProtocolResponse>)
}
- public hasResponseHandler (id: string): boolean {
- return this.responseHandlers.has(id)
+ public hasResponseHandler (uuid: `${string}-${string}-${string}-${string}-${string}`): boolean {
+ return this.responseHandlers.has(uuid)
}
protected startHttpServer (): void {
private readonly version: ProtocolVersion
private readonly uiServer: AbstractUIServer
private readonly uiServiceWorkerBroadcastChannel: UIServiceWorkerBroadcastChannel
- private readonly broadcastChannelRequests: Map<string, number>
+ private readonly broadcastChannelRequests: Map<
+ `${string}-${string}-${string}-${string}-${string}`,
+ number
+ >
constructor (uiServer: AbstractUIServer, version: ProtocolVersion) {
this.uiServer = uiServer
[ProcedureName.STOP_SIMULATOR, this.handleStopSimulator.bind(this)]
])
this.uiServiceWorkerBroadcastChannel = new UIServiceWorkerBroadcastChannel(this)
- this.broadcastChannelRequests = new Map<string, number>()
+ this.broadcastChannelRequests = new Map<
+ `${string}-${string}-${string}-${string}-${string}`,
+ number
+ >()
}
public stop (): void {
}
public async requestHandler (request: ProtocolRequest): Promise<ProtocolResponse | undefined> {
- let messageId: string | undefined
+ let uuid: `${string}-${string}-${string}-${string}-${string}` | undefined
let command: ProcedureName | undefined
let requestPayload: RequestPayload | undefined
let responsePayload: ResponsePayload | undefined
try {
- [messageId, command, requestPayload] = request
+ [uuid, command, requestPayload] = request
if (!this.requestHandlers.has(command)) {
throw new BaseError(
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const requestHandler = this.requestHandlers.get(command)!
if (isAsyncFunction(requestHandler)) {
- responsePayload = await requestHandler(messageId, command, requestPayload)
+ responsePayload = await requestHandler(uuid, command, requestPayload)
} else {
responsePayload = (
requestHandler as (
procedureName?: ProcedureName,
payload?: RequestPayload
) => undefined | ResponsePayload
- )(messageId, command, requestPayload)
+ )(uuid, command, requestPayload)
}
} catch (error) {
// Log
}
if (responsePayload != null) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
- return this.uiServer.buildProtocolResponse(messageId!, responsePayload)
+ return this.uiServer.buildProtocolResponse(uuid!, responsePayload)
}
}
// public sendRequest (
- // messageId: string,
+ // uuid: `${string}-${string}-${string}-${string}-${string}`,
// procedureName: ProcedureName,
// requestPayload: RequestPayload
// ): void {
// this.uiServer.sendRequest(
- // this.uiServer.buildProtocolRequest(messageId, procedureName, requestPayload)
+ // this.uiServer.buildProtocolRequest(uuid, procedureName, requestPayload)
// )
// }
- public sendResponse (messageId: string, responsePayload: ResponsePayload): void {
- if (this.uiServer.hasResponseHandler(messageId)) {
- this.uiServer.sendResponse(this.uiServer.buildProtocolResponse(messageId, responsePayload))
+ public sendResponse (
+ uuid: `${string}-${string}-${string}-${string}-${string}`,
+ responsePayload: ResponsePayload
+ ): void {
+ if (this.uiServer.hasResponseHandler(uuid)) {
+ this.uiServer.sendResponse(this.uiServer.buildProtocolResponse(uuid, responsePayload))
}
}
return this.uiServer.logPrefix(modName, methodName, this.version)
}
- public deleteBroadcastChannelRequest (uuid: string): void {
+ public deleteBroadcastChannelRequest (
+ uuid: `${string}-${string}-${string}-${string}-${string}`
+ ): void {
this.broadcastChannelRequests.delete(uuid)
}
- public getBroadcastChannelExpectedResponses (uuid: string): number {
+ public getBroadcastChannelExpectedResponses (
+ uuid: `${string}-${string}-${string}-${string}-${string}`
+ ): number {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return this.broadcastChannelRequests.get(uuid)!
}
protected handleProtocolRequest (
- uuid: string,
+ uuid: `${string}-${string}-${string}-${string}-${string}`,
procedureName: ProcedureName,
payload: RequestPayload
): void {
}
private sendBroadcastChannelRequest (
- uuid: string,
+ uuid: `${string}-${string}-${string}-${string}-${string}`,
procedureName: BroadcastChannelProcedureName,
payload: BroadcastChannelRequestPayload
): void {
}
private async handleAddChargingStations (
- _messageId?: string,
+ _uuid?: `${string}-${string}-${string}-${string}-${string}`,
_procedureName?: ProcedureName,
requestPayload?: RequestPayload
): Promise<ResponsePayload> {
'0.0.1' = '0.0.1'
}
-export type ProtocolRequest = [string, ProcedureName, RequestPayload]
-export type ProtocolResponse = [string, ResponsePayload]
+export type ProtocolRequest = [
+ `${string}-${string}-${string}-${string}-${string}`,
+ ProcedureName,
+ RequestPayload
+]
+export type ProtocolResponse = [
+ `${string}-${string}-${string}-${string}-${string}`,
+ ResponsePayload
+]
export type ProtocolRequestHandler = (
- uuid?: string,
+ uuid?: `${string}-${string}-${string}-${string}-${string}`,
procedureName?: ProcedureName,
payload?: RequestPayload
) => undefined | Promise<undefined> | ResponsePayload | Promise<ResponsePayload>
import type { RequestPayload, ResponsePayload } from './UIProtocol.js'
export type BroadcastChannelRequest = [
- string,
+ `${string}-${string}-${string}-${string}-${string}`,
BroadcastChannelProcedureName,
BroadcastChannelRequestPayload
]
-export type BroadcastChannelResponse = [string, BroadcastChannelResponsePayload]
+export type BroadcastChannelResponse = [
+ `${string}-${string}-${string}-${string}-${string}`,
+ BroadcastChannelResponsePayload
+]
export enum BroadcastChannelProcedureName {
START_CHARGING_STATION = 'startChargingStation',
return `${new Date().toLocaleString()}${prefixString}`
}
-export const generateUUID = (): string => {
+export const generateUUID = (): `${string}-${string}-${string}-${string}-${string}` => {
return randomUUID()
}
-export const validateUUID = (uuid: string): boolean => {
+export const validateUUID = (
+ uuid: `${string}-${string}-${string}-${string}-${string}`
+): uuid is `${string}-${string}-${string}-${string}-${string}` => {
return /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-4[0-9a-fA-F]{3}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/.test(uuid)
}
return isString(value) && value.trim().length > 0
}
-export const isEmptyArray = (value: unknown): value is never[] => {
+export const isEmptyArray = (value: unknown): value is [] => {
return Array.isArray(value) && value.length === 0
}
private static instance: UIClient | null = null
private ws?: WebSocket
- private responseHandlers: Map<string, ResponseHandler>
+ private responseHandlers: Map<
+ `${string}-${string}-${string}-${string}-${string}`,
+ ResponseHandler
+ >
private constructor(private uiServerConfiguration: UIServerConfigurationSection) {
this.openWS()
- this.responseHandlers = new Map<string, ResponseHandler>()
+ this.responseHandlers = new Map<
+ `${string}-${string}-${string}-${string}-${string}`,
+ ResponseHandler
+ >()
}
public static getInstance(uiServerConfiguration?: UIServerConfigurationSection): UIClient {
}
private responseHandler(messageEvent: MessageEvent<string>): void {
- const response = JSON.parse(messageEvent.data) as ProtocolResponse
+ let response: ProtocolResponse
+ try {
+ response = JSON.parse(messageEvent.data) as ProtocolResponse
+ } catch (error) {
+ useToast().error('Invalid response format')
+ console.error('Invalid response format', error)
+ return
+ }
- if (Array.isArray(response) === false) {
- throw new Error(`Response not an array: ${JSON.stringify(response, undefined, 2)}`)
+ if (!Array.isArray(response)) {
+ useToast().error(`Response not an array`)
+ console.error(`Response not an array:`, response)
+ return
}
const [uuid, responsePayload] = response
- if (this.responseHandlers.has(uuid) === true) {
+ if (this.responseHandlers.has(uuid)) {
const { procedureName, resolve, reject } = this.responseHandlers.get(uuid)!
switch (responsePayload.status) {
case ResponseStatus.SUCCESS:
PROTOCOL_BASIC_AUTH = 'protocol-basic-auth'
}
-export type ProtocolRequest = [string, ProcedureName, RequestPayload]
-export type ProtocolResponse = [string, ResponsePayload]
+export type ProtocolRequest = [
+ `${string}-${string}-${string}-${string}-${string}`,
+ ProcedureName,
+ RequestPayload
+]
+export type ProtocolResponse = [
+ `${string}-${string}-${string}-${string}-${string}`,
+ ResponsePayload
+]
export type ProtocolRequestHandler = (
payload: RequestPayload