-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathcanadian.controller.ts
175 lines (150 loc) · 5.51 KB
/
canadian.controller.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import got from 'got';
import crypto from 'node:crypto';
import { fetch, Agent } from 'undici';
import { BlueLinkyConfig } from '../interfaces/common.interfaces';
import { CanadianBrandEnvironment, getBrandEnvironment } from '../constants/canada';
import { Vehicle } from '../vehicles/vehicle';
import CanadianVehicle from '../vehicles/canadian.vehicle';
import { SessionController } from './controller';
import logger from '../logger';
import { VehicleRegisterOptions } from '../interfaces/common.interfaces';
import { manageBluelinkyError } from '../tools/common.tools';
export interface CanadianBlueLinkyConfig extends BlueLinkyConfig {
region: 'CA';
}
export class CanadianController extends SessionController<CanadianBlueLinkyConfig> {
private _environment: CanadianBrandEnvironment;
constructor(userConfig: CanadianBlueLinkyConfig) {
super(userConfig);
logger.debug('CA Controller created');
this._environment = getBrandEnvironment(userConfig.brand);
}
public get environment(): CanadianBrandEnvironment {
return this._environment;
}
private vehicles: Array<CanadianVehicle> = [];
private timeOffset = -(new Date().getTimezoneOffset() / 60);
public async refreshAccessToken(): Promise<string> {
const shouldRefreshToken = Math.floor(Date.now() / 1000 - this.session.tokenExpiresAt) >= -10;
logger.debug('shouldRefreshToken: ' + shouldRefreshToken.toString());
if (this.session.refreshToken && shouldRefreshToken) {
// TODO: someone should find the refresh token API url then we dont have to do this hack
// the previously used CA_ENDPOINTS.verifyToken did not refresh it only provided if the token was valid
await this.login();
logger.debug('Token refreshed');
return 'Token refreshed';
}
logger.debug('Token not expired, no need to refresh');
return 'Token not expired, no need to refresh';
}
public async login(): Promise<string> {
logger.info('Begin login request');
try {
const response = await this.request(this.environment.endpoints.login, {
loginId: this.userConfig.username,
password: this.userConfig.password,
});
logger.debug(response.result);
this.session.accessToken = response.result.accessToken;
this.session.refreshToken = response.result.refreshToken;
this.session.tokenExpiresAt = Math.floor(+new Date() / 1000 + response.result.expireIn);
return 'login good';
} catch (err) {
return 'error: ' + err;
}
}
async logout(): Promise<string> {
return 'OK';
}
async getVehicles(): Promise<Array<Vehicle>> {
logger.info('Begin getVehicleList request');
try {
const response = await this.request(this.environment.endpoints.vehicleList, {});
const data = response.result;
if (data.vehicles === undefined) {
this.vehicles = [];
return this.vehicles;
}
data.vehicles.forEach(vehicle => {
const vehicleConfig = {
nickname: vehicle.nickName,
name: vehicle.nickName,
vin: vehicle.vin,
regDate: vehicle.enrollmentDate,
brandIndicator: vehicle.brandIndicator,
regId: vehicle.regid,
id: vehicle.vehicleId,
generation: vehicle.genType,
} as VehicleRegisterOptions;
this.vehicles.push(new CanadianVehicle(vehicleConfig, this));
});
return this.vehicles;
} catch (err) {
logger.debug(err);
return this.vehicles;
}
}
//////////////////////////////////////////////////////////////////////////////
// Internal
//////////////////////////////////////////////////////////////////////////////
// TODO: not quite sure how to type this if it's dynamic?
/* eslint-disable @typescript-eslint/no-explicit-any */
private async request(endpoint, body: any, headers: any = {}): Promise<any | null> {
logger.debug(`[${endpoint}] ${JSON.stringify(headers)} ${JSON.stringify(body)}`);
const [major,,] = process.versions.node.split('.').map(Number);
if (major >= 21) {
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
logger.debug('Node version >= 21, using fetch instead of got');
const options = {
method: 'POST',
body: JSON.stringify(body),
headers: {
from: this.environment.origin,
language: 0,
offset: this.timeOffset,
accessToken: this.session.accessToken,
Origin: 'https://kiaconnect.ca',
Referer: 'https://kiaconnect.ca/login',
'Content-Type': 'application/json',
...headers,
},
dispatcher: new Agent({
connect: {
rejectUnauthorized: false,
secureOptions: crypto.constants.SSL_OP_LEGACY_SERVER_CONNECT
}
}),
};
try {
const response = await fetch(endpoint, options);
const data = await response.json();
return data;
} catch (err) {
logger.error(err);
return;
}
}
try {
const response = await got(endpoint, {
method: 'POST',
json: true,
headers: {
from: this.environment.origin,
language: 1,
offset: this.timeOffset,
accessToken: this.session.accessToken,
...headers,
},
body: {
...body,
},
});
if (response.body.responseHeader.responseCode != 0) {
throw response.body.responseHeader.responseDesc;
}
return response.body;
} catch (err) {
throw manageBluelinkyError(err, 'CanadianController');
}
}
}