22e3a7f25ddab066606b32cc236466b2ba889b2c
1 import { JsonType
} from
'@/type/JsonType';
7 } from
'@/type/UIProtocol';
8 import Utils from
'./Utils';
9 import config from
'@/assets/config';
10 import { v4
as uuidv4
} from
'uuid';
12 type ResponseHandler
= {
13 resolve
: (value
: ResponsePayload
| PromiseLike
<ResponsePayload
>) => void;
14 reject
: (reason
?: any) => void;
15 procedureName
: ProcedureName
;
18 export default class UIClient
{
19 private static _instance
: UIClient
| null = null;
21 private _ws
!: WebSocket
;
22 private _responseHandlers
: Map
<string, ResponseHandler
>;
24 private constructor() {
26 this._responseHandlers
= new Map
<string, ResponseHandler
>();
29 public static get
instance() {
30 if (UIClient
._instance
=== null) {
31 UIClient
._instance
= new UIClient();
33 return UIClient
._instance
;
36 public registerWSonOpenListener(listener
: (this: WebSocket
, ev
: 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 listChargingStations(): Promise
<ResponsePayload
> {
49 return this.sendRequest(ProcedureName
.LIST_CHARGING_STATIONS
, {});
52 public async startChargingStation(hashId
: string): Promise
<ResponsePayload
> {
53 return this.sendRequest(ProcedureName
.START_CHARGING_STATION
, { hashId
});
56 public async stopChargingStation(hashId
: string): Promise
<ResponsePayload
> {
57 return this.sendRequest(ProcedureName
.STOP_CHARGING_STATION
, { hashId
});
60 public async openConnection(hashId
: string): Promise
<ResponsePayload
> {
61 return this.sendRequest(ProcedureName
.OPEN_CONNECTION
, {
66 public async closeConnection(hashId
: string): Promise
<ResponsePayload
> {
67 return this.sendRequest(ProcedureName
.CLOSE_CONNECTION
, {
72 public async startTransaction(
75 idTag
: string | undefined
76 ): Promise
<ResponsePayload
> {
77 return this.sendRequest(ProcedureName
.START_TRANSACTION
, {
84 public async stopTransaction(
86 transactionId
: number | undefined
87 ): Promise
<ResponsePayload
> {
88 return this.sendRequest(ProcedureName
.STOP_TRANSACTION
, {
94 private openWS(): void {
95 this._ws
= new WebSocket(
96 `ws://${config.emobility.host}:${config.emobility.port}`,
97 config
.emobility
.protocol
99 this._ws
.onmessage
= this.responseHandler
.bind(this);
102 private setResponseHandler(
104 resolve
: (value
: ResponsePayload
| PromiseLike
<ResponsePayload
>) => void,
105 reject
: (reason
?: any) => void,
106 procedureName
: ProcedureName
108 this._responseHandlers
.set(id
, { resolve
, reject
, procedureName
});
111 private getResponseHandler(id
: string): ResponseHandler
| undefined {
112 return this._responseHandlers
.get(id
);
115 private async sendRequest(command
: ProcedureName
, data
: JsonType
): Promise
<ResponsePayload
> {
117 return Utils
.promiseWithTimeout(
118 new Promise((resolve
, reject
) => {
120 const msg
= JSON
.stringify([uuid
, command
, data
]);
122 if (this._ws
.readyState
!== WebSocket
.OPEN
) {
125 if (this._ws
.readyState
=== WebSocket
.OPEN
) {
128 throw new Error(`Send request ${command} message: connection not opened`);
131 this.setResponseHandler(uuid
, resolve
, reject
, command
);
134 Error(`Send request ${command} message timeout`),
136 this._responseHandlers
.delete(uuid
);
141 private responseHandler(messageEvent
: MessageEvent
<string>): void {
142 const data
= JSON
.parse(messageEvent
.data
) as ProtocolResponse
;
144 if (Array.isArray(data
) === false) {
145 throw new Error('Response not an array: ' + JSON
.stringify(data
, null, 2));
148 const [uuid
, response
] = data
;
150 if (this._responseHandlers
.has(uuid
) === true) {
151 switch (response
.status) {
152 case ResponseStatus
.SUCCESS
:
153 this.getResponseHandler(uuid
)?.resolve(response
);
155 case ResponseStatus
.FAILURE
:
156 this.getResponseHandler(uuid
)?.reject(response
);
159 throw new Error(`Response status not supported: ${response.status}`);
162 throw new Error('Not a response to a request: ' + JSON
.stringify(data
, null, 2));