-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
80 lines (77 loc) · 2.56 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import SaobracajnaApi, { SaobracajnaApiResponseStatus } from "./models/api/api";
import { RegistrationIndex } from "./models/api/types";
export default class MUPSaobracajnaApi {
private api: SaobracajnaApi;
device: string = "";
constructor(
device = "",
private cacheEnabled = false,
private isInit = false
) {
this.api = new SaobracajnaApi();
this.start();
if (device) this.selectReaderSync(device);
}
private start() {
const status = this.api.startup().status;
if (status !== SaobracajnaApiResponseStatus.OK) throw new Error(status);
this.isInit = true;
}
private selectReaderSync(device: string) {
const status = this.api.selectReader(device).status;
if (status !== SaobracajnaApiResponseStatus.OK) throw new Error(status);
}
async processNewCard() {
const status = this.api.processNewCard().status;
if (status !== SaobracajnaApiResponseStatus.OK) throw new Error(status);
}
async readAllData() {
if (!this.cacheEnabled) {
await this.processNewCard();
}
const res = await Promise.all([
this.readDocumentData(false),
this.readPersonalData(false),
this.readVehicleData(false),
]);
return { ...res[0], ...res[1], ...res[2] };
}
async readDocumentData(forceRead = !this.cacheEnabled) {
if (forceRead) await this.processNewCard();
const res = this.api.readDocumentData();
if (res.status !== SaobracajnaApiResponseStatus.OK)
throw new Error(res.status);
return res.data!;
}
async readVehicleData(forceRead = !this.cacheEnabled) {
if (forceRead) await this.processNewCard();
const res = this.api.readVehicleData();
if (res.status !== SaobracajnaApiResponseStatus.OK)
throw new Error(res.status);
return res.data!;
}
async readPersonalData(forceRead = !this.cacheEnabled) {
if (forceRead) await this.processNewCard();
const res = this.api.readPersonalData();
if (res.status !== SaobracajnaApiResponseStatus.OK)
throw new Error(res.status);
return res.data!;
}
async readRegistration(
regInd: RegistrationIndex,
forceRead = !this.cacheEnabled
) {
if (forceRead) await this.processNewCard();
const res = this.api.readRegistration(regInd);
if (res.status !== SaobracajnaApiResponseStatus.OK)
throw new Error(res.status);
return res.data!;
}
async end() {
if (!this.isInit) return;
const status = this.api.cleanup().status;
if (status !== SaobracajnaApiResponseStatus.OK) throw new Error(status);
this.isInit = false;
}
}
export { MUPSaobracajnaApi, RegistrationIndex };