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 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 setSupervisionUrl(hashId
: string, supervisionUrl
: string): Promise
<ResponsePayload
> {
71 return this.sendRequest(ProcedureName
.SET_SUPERVISION_URL
, {
77 public async startChargingStation(hashId
: string): Promise
<ResponsePayload
> {
78 return this.sendRequest(ProcedureName
.START_CHARGING_STATION
, { hashIds
: [hashId
] })
81 public async stopChargingStation(hashId
: string): Promise
<ResponsePayload
> {
82 return this.sendRequest(ProcedureName
.STOP_CHARGING_STATION
, { hashIds
: [hashId
] })
85 public async openConnection(hashId
: string): Promise
<ResponsePayload
> {
86 return this.sendRequest(ProcedureName
.OPEN_CONNECTION
, {
91 public async closeConnection(hashId
: string): Promise
<ResponsePayload
> {
92 return this.sendRequest(ProcedureName
.CLOSE_CONNECTION
, {
97 public async startTransaction(
100 idTag
: string | undefined
101 ): Promise
<ResponsePayload
> {
102 return this.sendRequest(ProcedureName
.START_TRANSACTION
, {
109 public async stopTransaction(
111 transactionId
: number | undefined
112 ): Promise
<ResponsePayload
> {
113 return this.sendRequest(ProcedureName
.STOP_TRANSACTION
, {
119 public async startAutomaticTransactionGenerator(
122 ): Promise
<ResponsePayload
> {
123 return this.sendRequest(ProcedureName
.START_AUTOMATIC_TRANSACTION_GENERATOR
, {
125 connectorIds
: [connectorId
]
129 public async stopAutomaticTransactionGenerator(
132 ): Promise
<ResponsePayload
> {
133 return this.sendRequest(ProcedureName
.STOP_AUTOMATIC_TRANSACTION_GENERATOR
, {
135 connectorIds
: [connectorId
]
139 private openWS(): void {
141 this.uiServerConfiguration
.authentication
?.enabled
=== true &&
142 this.uiServerConfiguration
.authentication
?.type === AuthenticationType
.PROTOCOL_BASIC_AUTH
144 `${this.uiServerConfiguration.protocol}${this.uiServerConfiguration.version}`,
145 `authorization.basic.${btoa(`${this.uiServerConfiguration.authentication.username}:${this.uiServerConfiguration.authentication.password}`).replace(/={1,2}$
/, '')}`
147 : `${this.uiServerConfiguration.protocol}${this.uiServerConfiguration.version}
`
148 this.ws = new WebSocket(
149 `${this.uiServerConfiguration.secure === true ? ApplicationProtocol.WSS : ApplicationProtocol.WS}
://${this.uiServerConfiguration.host}:${this.uiServerConfiguration.port}`,
152 this.ws
.onopen
= openEvent
=> {
153 console
.info('WebSocket opened', openEvent
)
155 this.ws
.onmessage
= this.responseHandler
.bind(this)
156 this.ws
.onerror
= errorEvent
=> {
157 console
.error('WebSocket error: ', errorEvent
)
159 this.ws
.onclose
= closeEvent
=> {
160 console
.info('WebSocket closed: ', closeEvent
)
164 private async sendRequest(
165 procedureName
: ProcedureName
,
166 payload
: RequestPayload
167 ): Promise
<ResponsePayload
> {
168 return new Promise
<ResponsePayload
>((resolve
, reject
) => {
169 if (this.ws
.readyState
=== WebSocket
.OPEN
) {
170 const uuid
= crypto
.randomUUID()
171 const msg
= JSON
.stringify([uuid
, procedureName
, payload
])
172 const sendTimeout
= setTimeout(() => {
173 this.responseHandlers
.delete(uuid
)
174 return reject(new Error(`Send request '${procedureName}' message: connection timeout`))
178 this.responseHandlers
.set(uuid
, { procedureName
, resolve
, reject
})
180 this.responseHandlers
.delete(uuid
)
183 clearTimeout(sendTimeout
)
186 reject(new Error(`Send request '${procedureName}' message: connection closed`))
191 private responseHandler(messageEvent
: MessageEvent
<string>): void {
192 const response
= JSON
.parse(messageEvent
.data
) as ProtocolResponse
194 if (Array.isArray(response
) === false) {
195 throw new Error(`Response not an array: ${JSON.stringify(response, undefined, 2)}`)
198 const [uuid
, responsePayload
] = response
200 if (this.responseHandlers
.has(uuid
) === true) {
201 const { procedureName
, resolve
, reject
} = this.responseHandlers
.get(uuid
)!
202 switch (responsePayload
.status) {
203 case ResponseStatus
.SUCCESS
:
204 resolve(responsePayload
)
206 case ResponseStatus
.FAILURE
:
207 reject(responsePayload
)
212 `Response status for procedure '${procedureName}' not supported: '${responsePayload.status}'`
216 this.responseHandlers
.delete(uuid
)
218 throw new Error(`Not a response to a request: ${JSON.stringify(response, undefined, 2)}`)