+import { useToast } from 'vue-toast-notification'
import {
ApplicationProtocol,
AuthenticationType,
+ type ChargingStationOptions,
ProcedureName,
type ProtocolResponse,
type RequestPayload,
}
export class UIClient {
- private static readonly instances: Map<number, UIClient> = new Map<number, UIClient>()
+ private static instance: UIClient | null = null
private ws!: WebSocket
private responseHandlers: Map<string, ResponseHandler>
this.responseHandlers = new Map<string, ResponseHandler>()
}
- public static getInstance(
- serverId: number,
- uiServerConfiguration?: UIServerConfigurationSection
- ): UIClient {
- if (!UIClient.instances.has(serverId) && uiServerConfiguration != null) {
- UIClient.instances.set(serverId, new UIClient(uiServerConfiguration))
- } else if (!UIClient.instances.has(serverId)) {
- throw new Error(`UI client instance not found for server id: ${serverId}`)
+ public static getInstance(uiServerConfiguration: UIServerConfigurationSection): UIClient {
+ if (UIClient.instance === null) {
+ UIClient.instance = new UIClient(uiServerConfiguration)
}
- return UIClient.instances.get(serverId)!
+ return UIClient.instance
+ }
+
+ public setConfiguration(uiServerConfiguration: UIServerConfigurationSection): void {
+ this.ws.close()
+ this.uiServerConfiguration = uiServerConfiguration
+ this.openWS()
}
public registerWSEventListener<K extends keyof WebSocketEventMap>(
public async addChargingStations(
template: string,
- numberOfStations: number
+ numberOfStations: number,
+ options?: ChargingStationOptions
): Promise<ResponsePayload> {
- return this.sendRequest(ProcedureName.ADD_CHARGING_STATIONS, { template, numberOfStations })
+ return this.sendRequest(ProcedureName.ADD_CHARGING_STATIONS, {
+ template,
+ numberOfStations,
+ options
+ })
}
public async deleteChargingStation(hashId: string): Promise<ResponsePayload> {
`${this.uiServerConfiguration.secure === true ? ApplicationProtocol.WSS : ApplicationProtocol.WS}://${this.uiServerConfiguration.host}:${this.uiServerConfiguration.port}`,
protocols
)
- this.ws.onopen = openEvent => {
- console.info('WebSocket opened', openEvent)
+ this.ws.onopen = () => {
+ useToast().success(
+ `WebSocket to UI server '${this.uiServerConfiguration.host}' successfully opened`
+ )
}
this.ws.onmessage = this.responseHandler.bind(this)
this.ws.onerror = errorEvent => {
- console.error('WebSocket error: ', errorEvent)
+ useToast().error(`Error in WebSocket to UI server '${this.uiServerConfiguration.host}'`)
+ console.error(
+ `Error in WebSocket to UI server '${this.uiServerConfiguration.host}'`,
+ errorEvent
+ )
}
- this.ws.onclose = closeEvent => {
- console.info('WebSocket closed: ', closeEvent)
+ this.ws.onclose = () => {
+ useToast().info(`WebSocket to UI server '${this.uiServerConfiguration.host}' closed`)
}
}
reject(responsePayload)
break
default:
- console.error(
- `Response status for procedure '${procedureName}' not supported: '${responsePayload.status}'`
+ reject(
+ new Error(
+ `Response status for procedure '${procedureName}' not supported: '${responsePayload.status}'`
+ )
)
}
this.responseHandlers.delete(uuid)