3 type ConfigurationData
,
11 type ResponseHandler
= {
12 procedureName
: ProcedureName
13 resolve
: (value
: ResponsePayload
| PromiseLike
<ResponsePayload
>) => void
14 reject
: (reason
?: unknown
) => void
17 export class UIClient
{
18 private static instance
: UIClient
| null = null
20 private ws
!: WebSocket
21 private responseHandlers
: Map
<string, ResponseHandler
>
23 private constructor(private configuration
: ConfigurationData
) {
25 this.responseHandlers
= new Map
<string, ResponseHandler
>()
28 public static getInstance(configuration
: ConfigurationData
) {
29 if (UIClient
.instance
=== null) {
30 UIClient
.instance
= new UIClient(configuration
)
32 return UIClient
.instance
35 public registerWSonOpenListener(listener
: (event
: Event
) => void) {
36 this.ws
.addEventListener('open', listener
)
39 public async startSimulator(): Promise
<ResponsePayload
> {
40 return this.sendRequest(ProcedureName
.START_SIMULATOR
, {})
43 public async stopSimulator(): Promise
<ResponsePayload
> {
44 return this.sendRequest(ProcedureName
.STOP_SIMULATOR
, {})
47 public async listChargingStations(): Promise
<ResponsePayload
> {
48 return this.sendRequest(ProcedureName
.LIST_CHARGING_STATIONS
, {})
51 public async startChargingStation(hashId
: string): Promise
<ResponsePayload
> {
52 return this.sendRequest(ProcedureName
.START_CHARGING_STATION
, { hashIds
: [hashId
] })
55 public async stopChargingStation(hashId
: string): Promise
<ResponsePayload
> {
56 return this.sendRequest(ProcedureName
.STOP_CHARGING_STATION
, { hashIds
: [hashId
] })
59 public async openConnection(hashId
: string): Promise
<ResponsePayload
> {
60 return this.sendRequest(ProcedureName
.OPEN_CONNECTION
, {
65 public async closeConnection(hashId
: string): Promise
<ResponsePayload
> {
66 return this.sendRequest(ProcedureName
.CLOSE_CONNECTION
, {
71 public async startTransaction(
74 idTag
: string | undefined
75 ): Promise
<ResponsePayload
> {
76 return this.sendRequest(ProcedureName
.START_TRANSACTION
, {
83 public async stopTransaction(
85 transactionId
: number | undefined
86 ): Promise
<ResponsePayload
> {
87 return this.sendRequest(ProcedureName
.STOP_TRANSACTION
, {
93 public async startAutomaticTransactionGenerator(
96 ): Promise
<ResponsePayload
> {
97 return this.sendRequest(ProcedureName
.START_AUTOMATIC_TRANSACTION_GENERATOR
, {
99 connectorIds
: [connectorId
]
103 public async stopAutomaticTransactionGenerator(
106 ): Promise
<ResponsePayload
> {
107 return this.sendRequest(ProcedureName
.STOP_AUTOMATIC_TRANSACTION_GENERATOR
, {
109 connectorIds
: [connectorId
]
113 private openWS(): void {
114 this.ws
= new WebSocket(
115 `${this.configuration.uiServer.secure === true ? ApplicationProtocol.WSS : ApplicationProtocol.WS}://${this.configuration.uiServer.host}:${this.configuration.uiServer.port}`,
116 `${this.configuration.uiServer.protocol}${this.configuration.uiServer.version}`
118 this.ws
.onmessage
= this.responseHandler
.bind(this)
119 this.ws
.onerror
= errorEvent
=> {
120 console
.error('WebSocket error: ', errorEvent
)
122 this.ws
.onclose
= closeEvent
=> {
123 console
.info('WebSocket closed: ', closeEvent
)
127 private async sendRequest(
128 procedureName
: ProcedureName
,
129 payload
: RequestPayload
130 ): Promise
<ResponsePayload
> {
131 return new Promise
<ResponsePayload
>((resolve
, reject
) => {
132 if (this.ws
.readyState
!== WebSocket
.OPEN
) {
135 if (this.ws
.readyState
=== WebSocket
.OPEN
) {
136 const uuid
= crypto
.randomUUID()
137 const msg
= JSON
.stringify([uuid
, procedureName
, payload
])
138 const sendTimeout
= setTimeout(() => {
139 this.responseHandlers
.delete(uuid
)
140 return reject(new Error(`Send request '${procedureName}' message timeout`))
144 this.responseHandlers
.set(uuid
, { procedureName
, resolve
, reject
})
146 this.responseHandlers
.delete(uuid
)
149 clearTimeout(sendTimeout
)
152 throw new Error(`Send request '${procedureName}' message: connection not opened`)
157 private responseHandler(messageEvent
: MessageEvent
<string>): void {
158 const response
= JSON
.parse(messageEvent
.data
) as ProtocolResponse
160 if (Array.isArray(response
) === false) {
161 throw new Error(`Response not an array: ${JSON.stringify(response, undefined, 2)}`)
164 const [uuid
, responsePayload
] = response
166 if (this.responseHandlers
.has(uuid
) === true) {
167 const { procedureName
, resolve
, reject
} = this.responseHandlers
.get(uuid
)!
168 switch (responsePayload
.status) {
169 case ResponseStatus
.SUCCESS
:
170 resolve(responsePayload
)
172 case ResponseStatus
.FAILURE
:
173 reject(responsePayload
)
177 `Response status for procedure '${procedureName}' not supported: '${responsePayload.status}'`
180 this.responseHandlers
.delete(uuid
)
182 throw new Error(`Not a response to a request: ${JSON.stringify(response, undefined, 2)}`)