-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresi_api.ts
279 lines (222 loc) · 7.82 KB
/
resi_api.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// Class for interacting with the Resi API
// Uses HTTP requests to interact with the Resi API
import got from 'got';
export class ResiApi {
// base URL for the Resi API
private readonly baseUrl = 'https://central.resi.io/api/v3';
private readonly baseUrlV2 = 'https://central.resi.io/api_v2.svc';
private readonly username: string;
private readonly password: string;
private token: string | null = null;
private tokenExpiration: number = 0;
private customerId: string | null = null;
private userId: string | null = null;
private encoderStatus: any = {};
constructor(username: string, password: string) {
this.username = username;
this.password = password;
this.token = null; // Initialize token as null
}
async getToken(): Promise<void> {
console.log('Getting token');
const response = await got.post(`${this.baseUrl}/auth/token`, {
json: {
username: this.username,
password: this.password,
grant_type: 'password_cookie'
}
});
const body = JSON.parse(response.body);
this.token = body.access_token;
this.tokenExpiration = Date.now() + body.expires_in * 1000;
}
needsToken(): boolean {
return this.token === null || this.tokenExpiration < Date.now();
}
async getMe(): Promise<any> {
if (this.needsToken()) {
console.log('Don\'t have token, getting token');
await this.getToken();
}
console.log('Getting user data');
const response = await got.get(`${this.baseUrlV2}/users/me`, {
headers: {
Authorization: `X-Bearer ${this.token}`
}
});
const body = JSON.parse(response.body);
this.customerId = body.customerId;
this.userId = body.userId;
}
needsUserData(): boolean {
return this.customerId === null || this.userId === null;
}
async getEncoderStatus(): Promise<any> {
if (!this.needsStatusRefresh()) {
console.log('No need to refresh status');
return;
}
if (this.needsToken()) {
console.log('Don\'t have token, getting token');
await this.getToken();
}
if (this.needsUserData()) {
console.log('Don\'t have user data, getting user data');
await this.getMe();
}
console.log('Getting encoder status');
const response = await got.get(`${this.baseUrl}/customers/${this.customerId}/monitors`, {
headers: {
Authorization: `X-Bearer ${this.token}`
}
});
const body = JSON.parse(response.body);
this.encoderStatus = {};
for (const encoder of body.encoderStatus) {
this.encoderStatus[encoder.encoderId] = encoder;
}
return this.encoderStatus;
}
needsStatusRefresh(): boolean {
if (Object.keys(this.encoderStatus).length === 0) {
return true;
}
const now = Date.now();
for (const encoderId in this.encoderStatus) {
// parse datetime like "2024-02-04T06:14:12Z"
if (this.encoderStatus[encoderId].lastUpdate) {
let lastUpdate = Date.parse(this.encoderStatus[encoderId].lastUpdate);
if (lastUpdate + 20000 < now) {
return true;
}
} else {
return true;
}
}
return false;
}
async getEncoderStatusById(encoderId: string): Promise<any> {
if (this.needsStatusRefresh()) {
await this.getEncoderStatus();
}
return this.encoderStatus[encoderId];
}
async getEncoders(): Promise<any> {
if (this.needsToken()) {
console.log('Don\'t have token, getting token');
await this.getToken();
}
if (this.needsUserData()) {
console.log('Don\'t have user data, getting user data');
await this.getMe();
}
console.log('Getting encoders');
const response = await got.get(`${this.baseUrlV2}/encoders?wide=true`, {
headers: {
Authorization: `X-Bearer ${this.token}`
}
});
const body = JSON.parse(response.body);
let encoders: any = {};
for (const encoder of body) {
encoders[encoder.uuid] = encoder.name;
}
return encoders;
}
async getEncoderProfiles(): Promise<any> {
if (this.needsToken()) {
console.log('Don\'t have token, getting token');
await this.getToken();
}
if (this.needsUserData()) {
console.log('Don\'t have user data, getting user data');
await this.getMe();
}
console.log('Getting encoder profiles');
const response = await got.get(`${this.baseUrl}/customers/${this.customerId}/encoderprofiles`, {
headers: {
Authorization: `X-Bearer ${this.token}`
}
});
const body = JSON.parse(response.body);
let profiles: any = {};
for (const profile of body) {
profiles[profile.uuid] = profile.name;
}
return profiles;
}
async getEventProfiles(): Promise<any> {
if (this.needsToken()) {
console.log('Don\'t have token, getting token');
await this.getToken();
}
if (this.needsUserData()) {
console.log('Don\'t have user data, getting user data');
await this.getMe();
}
console.log('Getting event profiles');
const response = await got.get(`${this.baseUrl}/customers/${this.customerId}/eventprofiles`, {
headers: {
Authorization: `X-Bearer ${this.token}`
}
});
const body = JSON.parse(response.body);
let profiles: any = {};
for (const profile of body) {
profiles[profile.uuid] = profile.name;
}
return profiles;
}
async startEncoder(encoderId: string, streamProfile: string, encoderProfile: string): Promise<any> {
if (this.needsToken()) {
await this.getToken();
}
if (this.needsUserData()) {
await this.getMe();
}
try {
const response = await got.patch(`${this.baseUrlV2}/encoders/${encoderId}`, {
headers: {
Authorization: `X-Bearer ${this.token}`
},
json: {
streamProfile: {
uuid: streamProfile
},
encoderProfile: {
uuid: encoderProfile
},
requestedStatus: 'start'
}
});
return response.statusCode;
} catch (error) {
console.error('Error starting encoder: ' + error);
return 500;
}
}
async stopEncoder(encoderId: string): Promise<any> {
if (this.needsToken()) {
await this.getToken();
}
if (this.needsUserData()) {
await this.getMe();
}
console.log('Stopping encoder ' + encoderId);
try {
const response = await got.patch(`${this.baseUrlV2}/encoders/${encoderId}`, {
headers: {
Authorization: `X-Bearer ${this.token}`
},
json: {
requestedStatus: 'stop'
}
});
console.log('Response: ' + response.statusCode + ' ' + response.body);
return response.statusCode;
} catch (error) {
console.error('Error stopping encoder: ' + error);
return 500;
}
}
}