4 type ConfigurationData
,
12 type ResponseHandler
= {
13 procedureName
: ProcedureName
14 resolve
: (value
: ResponsePayload
| PromiseLike
<ResponsePayload
>) => void
15 reject
: (reason
?: unknown
) => void
18 export class UIClient
{
19 private static instance
: UIClient
| null = null
21 private ws
!: WebSocket
22 private responseHandlers
: Map
<string, ResponseHandler
>
24 private constructor(private configuration
: ConfigurationData
) {
26 this.responseHandlers
= new Map
<string, ResponseHandler
>()
29 public static getInstance(configuration
: ConfigurationData
) {
30 if (UIClient
.instance
=== null) {
31 UIClient
.instance
= new UIClient(configuration
)
33 return UIClient
.instance
36 public registerWSEventListener
<K
extends keyof WebSocketEventMap
>(
38 listener
: (event
: WebSocketEventMap
[K
]) => void
40 this.ws
.addEventListener(event
, listener
)
43 public async startSimulator(): Promise
<ResponsePayload
> {
44 return this.sendRequest(ProcedureName
.START_SIMULATOR
, {})
47 public async stopSimulator(): Promise
<ResponsePayload
> {
48 return this.sendRequest(ProcedureName
.STOP_SIMULATOR
, {})
51 public async listTemplates(): Promise
<ResponsePayload
> {
52 return this.sendRequest(ProcedureName
.LIST_TEMPLATES
, {})
55 public async listChargingStations(): Promise
<ResponsePayload
> {
56 return this.sendRequest(ProcedureName
.LIST_CHARGING_STATIONS
, {})
59 public async addChargingStations(
61 numberOfStations
: number
62 ): Promise
<ResponsePayload
> {
63 return this.sendRequest(ProcedureName
.ADD_CHARGING_STATIONS
, { template
, numberOfStations
})
66 public async deleteChargingStation(hashId
: string): Promise
<ResponsePayload
> {
67 return this.sendRequest(ProcedureName
.DELETE_CHARGING_STATIONS
, { hashIds
: [hashId
] })
70 public async startChargingStation(hashId
: string): Promise
<ResponsePayload
> {
71 return this.sendRequest(ProcedureName
.START_CHARGING_STATION
, { hashIds
: [hashId
] })
74 public async stopChargingStation(hashId
: string): Promise
<ResponsePayload
> {
75 return this.sendRequest(ProcedureName
.STOP_CHARGING_STATION
, { hashIds
: [hashId
] })
78 public async openConnection(hashId
: string): Promise
<ResponsePayload
> {
79 return this.sendRequest(ProcedureName
.OPEN_CONNECTION
, {
84 public async closeConnection(hashId
: string): Promise
<ResponsePayload
> {
85 return this.sendRequest(ProcedureName
.CLOSE_CONNECTION
, {
90 public async startTransaction(
93 idTag
: string | undefined
94 ): Promise
<ResponsePayload
> {
95 return this.sendRequest(ProcedureName
.START_TRANSACTION
, {
102 public async stopTransaction(
104 transactionId
: number | undefined
105 ): Promise
<ResponsePayload
> {
106 return this.sendRequest(ProcedureName
.STOP_TRANSACTION
, {
112 public async startAutomaticTransactionGenerator(
115 ): Promise
<ResponsePayload
> {
116 return this.sendRequest(ProcedureName
.START_AUTOMATIC_TRANSACTION_GENERATOR
, {
118 connectorIds
: [connectorId
]
122 public async stopAutomaticTransactionGenerator(
125 ): Promise
<ResponsePayload
> {
126 return this.sendRequest(ProcedureName
.STOP_AUTOMATIC_TRANSACTION_GENERATOR
, {
128 connectorIds
: [connectorId
]
132 private openWS(): void {
134 this.configuration
.uiServer
.authentication
?.enabled
=== true &&
135 this.configuration
.uiServer
.authentication
?.type === AuthenticationType
.PROTOCOL_BASIC_AUTH
137 `${this.configuration.uiServer.protocol}${this.configuration.uiServer.version}`,
138 `authorization.basic.${btoa(`${this.configuration.uiServer.authentication.username}:${this.configuration.uiServer.authentication.password}`).replace(/={1,2}$
/, '')}`
140 : `${this.configuration.uiServer.protocol}${this.configuration.uiServer.version}
`
141 this.ws = new WebSocket(
142 `${this.configuration.uiServer.secure === true ? ApplicationProtocol.WSS : ApplicationProtocol.WS}
://${this.configuration.uiServer.host}:${this.configuration.uiServer.port}`,
145 this.ws
.onopen
= openEvent
=> {
146 console
.info('WebSocket opened', openEvent
)
148 this.ws
.onmessage
= this.responseHandler
.bind(this)
149 this.ws
.onerror
= errorEvent
=> {
150 console
.error('WebSocket error: ', errorEvent
)
152 this.ws
.onclose
= closeEvent
=> {
153 console
.info('WebSocket closed: ', closeEvent
)
157 private async sendRequest(
158 procedureName
: ProcedureName
,
159 payload
: RequestPayload
160 ): Promise
<ResponsePayload
> {
161 return new Promise
<ResponsePayload
>((resolve
, reject
) => {
162 if (this.ws
.readyState
=== WebSocket
.OPEN
) {
163 const uuid
= crypto
.randomUUID()
164 const msg
= JSON
.stringify([uuid
, procedureName
, payload
])
165 const sendTimeout
= setTimeout(() => {
166 this.responseHandlers
.delete(uuid
)
167 return reject(new Error(`Send request '${procedureName}' message: connection timeout`))
171 this.responseHandlers
.set(uuid
, { procedureName
, resolve
, reject
})
173 this.responseHandlers
.delete(uuid
)
176 clearTimeout(sendTimeout
)
179 reject(new Error(`Send request '${procedureName}' message: connection closed`))
184 private responseHandler(messageEvent
: MessageEvent
<string>): void {
185 const response
= JSON
.parse(messageEvent
.data
) as ProtocolResponse
187 if (Array.isArray(response
) === false) {
188 throw new Error(`Response not an array: ${JSON.stringify(response, undefined, 2)}`)
191 const [uuid
, responsePayload
] = response
193 if (this.responseHandlers
.has(uuid
) === true) {
194 const { procedureName
, resolve
, reject
} = this.responseHandlers
.get(uuid
)!
195 switch (responsePayload
.status) {
196 case ResponseStatus
.SUCCESS
:
197 resolve(responsePayload
)
199 case ResponseStatus
.FAILURE
:
200 reject(responsePayload
)
204 `Response status for procedure '${procedureName}' not supported: '${responsePayload.status}'`
207 this.responseHandlers
.delete(uuid
)
209 throw new Error(`Not a response to a request: ${JSON.stringify(response, undefined, 2)}`)