9 type UIServerConfigurationSection
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 uiServerConfiguration
: UIServerConfigurationSection
) {
26 this.responseHandlers
= new Map
<string, ResponseHandler
>()
29 public static getInstance(uiServerConfiguration
: UIServerConfigurationSection
): UIClient
{
30 if (UIClient
.instance
=== null) {
31 UIClient
.instance
= new UIClient(uiServerConfiguration
)
33 return UIClient
.instance
36 public setConfiguration(uiServerConfiguration
: UIServerConfigurationSection
): void {
38 this.uiServerConfiguration
= uiServerConfiguration
42 public registerWSEventListener
<K
extends keyof WebSocketEventMap
>(
44 listener
: (event
: WebSocketEventMap
[K
]) => void
46 this.ws
.addEventListener(event
, listener
)
49 public async startSimulator(): Promise
<ResponsePayload
> {
50 return this.sendRequest(ProcedureName
.START_SIMULATOR
, {})
53 public async stopSimulator(): Promise
<ResponsePayload
> {
54 return this.sendRequest(ProcedureName
.STOP_SIMULATOR
, {})
57 public async listTemplates(): Promise
<ResponsePayload
> {
58 return this.sendRequest(ProcedureName
.LIST_TEMPLATES
, {})
61 public async listChargingStations(): Promise
<ResponsePayload
> {
62 return this.sendRequest(ProcedureName
.LIST_CHARGING_STATIONS
, {})
65 public async addChargingStations(
67 numberOfStations
: number
68 ): Promise
<ResponsePayload
> {
69 return this.sendRequest(ProcedureName
.ADD_CHARGING_STATIONS
, { template
, numberOfStations
})
72 public async deleteChargingStation(hashId
: string): Promise
<ResponsePayload
> {
73 return this.sendRequest(ProcedureName
.DELETE_CHARGING_STATIONS
, { hashIds
: [hashId
] })
76 public async setSupervisionUrl(hashId
: string, supervisionUrl
: string): Promise
<ResponsePayload
> {
77 return this.sendRequest(ProcedureName
.SET_SUPERVISION_URL
, {
83 public async startChargingStation(hashId
: string): Promise
<ResponsePayload
> {
84 return this.sendRequest(ProcedureName
.START_CHARGING_STATION
, { hashIds
: [hashId
] })
87 public async stopChargingStation(hashId
: string): Promise
<ResponsePayload
> {
88 return this.sendRequest(ProcedureName
.STOP_CHARGING_STATION
, { hashIds
: [hashId
] })
91 public async openConnection(hashId
: string): Promise
<ResponsePayload
> {
92 return this.sendRequest(ProcedureName
.OPEN_CONNECTION
, {
97 public async closeConnection(hashId
: string): Promise
<ResponsePayload
> {
98 return this.sendRequest(ProcedureName
.CLOSE_CONNECTION
, {
103 public async startTransaction(
106 idTag
: string | undefined
107 ): Promise
<ResponsePayload
> {
108 return this.sendRequest(ProcedureName
.START_TRANSACTION
, {
115 public async stopTransaction(
117 transactionId
: number | undefined
118 ): Promise
<ResponsePayload
> {
119 return this.sendRequest(ProcedureName
.STOP_TRANSACTION
, {
125 public async startAutomaticTransactionGenerator(
128 ): Promise
<ResponsePayload
> {
129 return this.sendRequest(ProcedureName
.START_AUTOMATIC_TRANSACTION_GENERATOR
, {
131 connectorIds
: [connectorId
]
135 public async stopAutomaticTransactionGenerator(
138 ): Promise
<ResponsePayload
> {
139 return this.sendRequest(ProcedureName
.STOP_AUTOMATIC_TRANSACTION_GENERATOR
, {
141 connectorIds
: [connectorId
]
145 private openWS(): void {
147 this.uiServerConfiguration
.authentication
?.enabled
=== true &&
148 this.uiServerConfiguration
.authentication
?.type === AuthenticationType
.PROTOCOL_BASIC_AUTH
150 `${this.uiServerConfiguration.protocol}${this.uiServerConfiguration.version}`,
151 `authorization.basic.${btoa(`${this.uiServerConfiguration.authentication.username}:${this.uiServerConfiguration.authentication.password}`).replace(/={1,2}$
/, '')}`
153 : `${this.uiServerConfiguration.protocol}${this.uiServerConfiguration.version}
`
154 this.ws = new WebSocket(
155 `${this.uiServerConfiguration.secure === true ? ApplicationProtocol.WSS : ApplicationProtocol.WS}
://${this.uiServerConfiguration.host}:${this.uiServerConfiguration.port}`,
158 this.ws
.onopen
= openEvent
=> {
159 console
.info('WebSocket opened', openEvent
)
161 this.ws
.onmessage
= this.responseHandler
.bind(this)
162 this.ws
.onerror
= errorEvent
=> {
163 console
.error('WebSocket error: ', errorEvent
)
165 this.ws
.onclose
= closeEvent
=> {
166 console
.info('WebSocket closed: ', closeEvent
)
170 private async sendRequest(
171 procedureName
: ProcedureName
,
172 payload
: RequestPayload
173 ): Promise
<ResponsePayload
> {
174 return new Promise
<ResponsePayload
>((resolve
, reject
) => {
175 if (this.ws
.readyState
=== WebSocket
.OPEN
) {
176 const uuid
= crypto
.randomUUID()
177 const msg
= JSON
.stringify([uuid
, procedureName
, payload
])
178 const sendTimeout
= setTimeout(() => {
179 this.responseHandlers
.delete(uuid
)
180 return reject(new Error(`Send request '${procedureName}' message: connection timeout`))
184 this.responseHandlers
.set(uuid
, { procedureName
, resolve
, reject
})
186 this.responseHandlers
.delete(uuid
)
189 clearTimeout(sendTimeout
)
192 reject(new Error(`Send request '${procedureName}' message: connection closed`))
197 private responseHandler(messageEvent
: MessageEvent
<string>): void {
198 const response
= JSON
.parse(messageEvent
.data
) as ProtocolResponse
200 if (Array.isArray(response
) === false) {
201 throw new Error(`Response not an array: ${JSON.stringify(response, undefined, 2)}`)
204 const [uuid
, responsePayload
] = response
206 if (this.responseHandlers
.has(uuid
) === true) {
207 const { procedureName
, resolve
, reject
} = this.responseHandlers
.get(uuid
)!
208 switch (responsePayload
.status) {
209 case ResponseStatus
.SUCCESS
:
210 resolve(responsePayload
)
212 case ResponseStatus
.FAILURE
:
213 reject(responsePayload
)
218 `Response status for procedure '${procedureName}' not supported: '${responsePayload.status}'`
222 this.responseHandlers
.delete(uuid
)
224 throw new Error(`Not a response to a request: ${JSON.stringify(response, undefined, 2)}`)