generated from homebridge/homebridge-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.ts
160 lines (138 loc) · 4.63 KB
/
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
import { Logger, PlatformConfig } from 'homebridge';
import axios, { AxiosRequestConfig, AxiosResponse, Method } from 'axios';
type Token = {
access_token: string;
refresh_token: string;
user_id: string;
expires_in: number;
};
export class RiseGardenAPI {
private static instance: RiseGardenAPI;
private baseUrl: string;
public readonly log: Logger;
private tokenInfo: Token;
private readonly config: PlatformConfig;
public constructor(config: PlatformConfig, log: Logger) {
this.baseUrl = 'https://prod-api.risegds.com/v2';
this.config = config;
this.log = log;
this.tokenInfo = {
access_token: '',
refresh_token: '',
user_id: '',
expires_in: 0,
};
}
public async getGardens(): Promise<AxiosResponse> {
this.log.debug('Executed getGardens');
return this.request('get', '/gardens');
}
public async getCurrentTemperature(gardenId: number): Promise<number> {
this.log.debug('Executed getCurrentTemperature with gardenId:', gardenId);
const status = await this.getDeviceStatus(gardenId);
return status.data.at;
}
public async getLightLevel(gardenId: number): Promise<number> {
this.log.debug('Executed getLightLevel with gardenId:', gardenId);
const status = await this.getDeviceStatus(gardenId);
return status.data.lamp_level;
}
public setLightLevel(gardenId: number, level: number): Promise<AxiosResponse> {
this.log.debug('Executed setLightLevel with gardenId and level:', gardenId, level);
const body = JSON.stringify({
'light_level': level,
'wait_for_response': true,
});
return this.request('put', `/gardens/${gardenId}/device/light-level`, null, body);
}
public async getDeviceStatus(gardenId: number): Promise<AxiosResponse> {
this.log.debug('Executed getDeviceStatus with gardenId:', gardenId);
return this.request('get', `/gardens/${gardenId}/device/status`);
}
private tokenIsExpired(): number|boolean {
return this.tokenInfo.expires_in && (this.tokenInfo.expires_in - 60000 < new Date().getTime());
}
private async refreshOrLogin(): Promise<boolean> {
if (this.tokenInfo.refresh_token !== '' && this.tokenIsExpired()) {
await this.refresh();
} else if (this.tokenInfo.access_token === '') {
await this.login(this.config.username, this.config.password);
}
return false;
}
private async refresh(): Promise<boolean> {
this.log.debug('Executed refresh');
const refresh_token = this.tokenInfo.refresh_token;
const data = {
'refresh_token': refresh_token,
};
const options: AxiosRequestConfig = {
method: 'POST',
baseURL: this.baseUrl,
url: '/auth/refresh_token',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + this.tokenInfo.access_token,
},
data: JSON.stringify(data),
};
const res = await axios.request(options);
if (res.status !== 200) {
return false;
}
const { token, expires_in } = res.data;
const cached_user_id = this.tokenInfo.user_id;
this.tokenInfo = {
access_token: token,
refresh_token: refresh_token,
user_id: cached_user_id,
expires_in: expires_in + new Date().getTime(),
};
return true;
}
private async login(username: string, password: string): Promise<boolean> {
this.log.debug('Executed login with username and password:', username, 'hidden');
const data = {
'email': username,
'password': password,
};
const options: AxiosRequestConfig = {
method: 'POST',
baseURL: this.baseUrl,
url: '/auth/login',
headers: { 'Content-Type': 'application/json' },
data: JSON.stringify(data),
};
const res = await axios.request(options);
if (res.status !== 200) {
return false;
}
const { token, refresh_token, expires_in } = res.data;
const user_id = res.data.user.id;
this.tokenInfo = {
access_token: token,
refresh_token: refresh_token,
user_id: user_id,
expires_in: expires_in + new Date().getTime(),
};
return true;
}
private async request(method: Method, path: string, params: string|null = null, body: string|null = null): Promise<AxiosResponse> {
if (path !== '/auth/login' && path !== '/auth/refresh_token') {
await this.refreshOrLogin();
}
const headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + this.tokenInfo.access_token,
};
const options: AxiosRequestConfig = {
method: method,
baseURL: this.baseUrl,
url: path,
headers: headers,
params: params,
data: body,
};
return axios(options);
}
}