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 registerWSonOpenListener(listener
: (event
: Event
) => void) {
37 this.ws
.addEventListener('open', listener
)
40 public async startSimulator(): Promise
<ResponsePayload
> {
41 return this.sendRequest(ProcedureName
.START_SIMULATOR
, {})
44 public async stopSimulator(): Promise
<ResponsePayload
> {
45 return this.sendRequest(ProcedureName
.STOP_SIMULATOR
, {})
48 public async deleteChargingStation(hashId
: string): Promise
<ResponsePayload
> {
49 return this.sendRequest(ProcedureName
.DELETE_CHARGING_STATIONS
, { hashIds
: [hashId
] })
52 public async listChargingStations(): Promise
<ResponsePayload
> {
53 return this.sendRequest(ProcedureName
.LIST_CHARGING_STATIONS
, {})
56 public async startChargingStation(hashId
: string): Promise
<ResponsePayload
> {
57 return this.sendRequest(ProcedureName
.START_CHARGING_STATION
, { hashIds
: [hashId
] })
60 public async stopChargingStation(hashId
: string): Promise
<ResponsePayload
> {
61 return this.sendRequest(ProcedureName
.STOP_CHARGING_STATION
, { hashIds
: [hashId
] })
64 public async openConnection(hashId
: string): Promise
<ResponsePayload
> {
65 return this.sendRequest(ProcedureName
.OPEN_CONNECTION
, {
70 public async closeConnection(hashId
: string): Promise
<ResponsePayload
> {
71 return this.sendRequest(ProcedureName
.CLOSE_CONNECTION
, {
76 public async startTransaction(
79 idTag
: string | undefined
80 ): Promise
<ResponsePayload
> {
81 return this.sendRequest(ProcedureName
.START_TRANSACTION
, {
88 public async stopTransaction(
90 transactionId
: number | undefined
91 ): Promise
<ResponsePayload
> {
92 return this.sendRequest(ProcedureName
.STOP_TRANSACTION
, {
98 public async startAutomaticTransactionGenerator(
101 ): Promise
<ResponsePayload
> {
102 return this.sendRequest(ProcedureName
.START_AUTOMATIC_TRANSACTION_GENERATOR
, {
104 connectorIds
: [connectorId
]
108 public async stopAutomaticTransactionGenerator(
111 ): Promise
<ResponsePayload
> {
112 return this.sendRequest(ProcedureName
.STOP_AUTOMATIC_TRANSACTION_GENERATOR
, {
114 connectorIds
: [connectorId
]
118 private openWS(): void {
120 this.configuration
.uiServer
.authentication
?.enabled
=== true &&
121 this.configuration
.uiServer
.authentication
?.type === AuthenticationType
.PROTOCOL_BASIC_AUTH
123 `${this.configuration.uiServer.protocol}${this.configuration.uiServer.version}`,
124 `authorization.basic.${btoa(`${this.configuration.uiServer.authentication.username}:${this.configuration.uiServer.authentication.password}`).replace(/={1,2}$
/, '')}`
126 : `${this.configuration.uiServer.protocol}${this.configuration.uiServer.version}
`
127 this.ws = new WebSocket(
128 `${this.configuration.uiServer.secure === true ? ApplicationProtocol.WSS : ApplicationProtocol.WS}
://${this.configuration.uiServer.host}:${this.configuration.uiServer.port}`,
131 this.ws
.onopen
= openEvent
=> {
132 console
.info('WebSocket opened', openEvent
)
134 this.ws
.onmessage
= this.responseHandler
.bind(this)
135 this.ws
.onerror
= errorEvent
=> {
136 console
.error('WebSocket error: ', errorEvent
)
138 this.ws
.onclose
= closeEvent
=> {
139 console
.info('WebSocket closed: ', closeEvent
)
143 private async sendRequest(
144 procedureName
: ProcedureName
,
145 payload
: RequestPayload
146 ): Promise
<ResponsePayload
> {
147 return new Promise
<ResponsePayload
>((resolve
, reject
) => {
148 if (this.ws
.readyState
=== WebSocket
.OPEN
) {
149 const uuid
= crypto
.randomUUID()
150 const msg
= JSON
.stringify([uuid
, procedureName
, payload
])
151 const sendTimeout
= setTimeout(() => {
152 this.responseHandlers
.delete(uuid
)
153 return reject(new Error(`Send request '${procedureName}' message: connection timeout`))
157 this.responseHandlers
.set(uuid
, { procedureName
, resolve
, reject
})
159 this.responseHandlers
.delete(uuid
)
162 clearTimeout(sendTimeout
)
165 reject(new Error(`Send request '${procedureName}' message: connection closed`))
170 private responseHandler(messageEvent
: MessageEvent
<string>): void {
171 const response
= JSON
.parse(messageEvent
.data
) as ProtocolResponse
173 if (Array.isArray(response
) === false) {
174 throw new Error(`Response not an array: ${JSON.stringify(response, undefined, 2)}`)
177 const [uuid
, responsePayload
] = response
179 if (this.responseHandlers
.has(uuid
) === true) {
180 const { procedureName
, resolve
, reject
} = this.responseHandlers
.get(uuid
)!
181 switch (responsePayload
.status) {
182 case ResponseStatus
.SUCCESS
:
183 resolve(responsePayload
)
185 case ResponseStatus
.FAILURE
:
186 reject(responsePayload
)
190 `Response status for procedure '${procedureName}' not supported: '${responsePayload.status}'`
193 this.responseHandlers
.delete(uuid
)
195 throw new Error(`Not a response to a request: ${JSON.stringify(response, undefined, 2)}`)