This repository has been archived by the owner on Feb 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
api.ts
176 lines (146 loc) · 4.58 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import superagent from 'superagent';
import {
ApiResponse, DataType, Device, DeviceApiResponse, EventType, MeasureApiResponse,
} from './api.types';
enum Api {
Connect = 'https://account.withings.com/connectionwou/account_login',
Devices = 'https://scalews.withings.com/cgi-bin/association',
Measure = 'https://scalews.withings.com/cgi-bin/v2/measure',
}
export enum Measure {
CarbonDioxide = 35,
Temperature = 12,
}
const agent = superagent.agent();
const INVALID_PARAMS_ERROR = 'Invalid Params';
const TICK_INTERVAL = 15 * 60 * 1000; // tslint:disable-line:no-magic-numbers
export class WithingsApi {
private device: Device;
private battery: number;
private carbondioxide: MeasureApiResponse['body']['series'][0]['data'][0];
private temperature: MeasureApiResponse['body']['series'][0]['data'][0];
private subscribers: Array<{ type: EventType, callback: (data: any) => void }> = [];
private timer: NodeJS.Timeout;
constructor(
private readonly email: string,
private readonly password: string,
private readonly mac: string,
) { }
async init() {
if (this.timer) {
return;
}
this.timer = setInterval(() => this.tick(), TICK_INTERVAL);
await this.tick();
}
getBatteryLevel() {
return this.battery;
}
getDeviceInfo() {
return this.device;
}
getCarbonDioxide() {
return this.carbondioxide.value;
}
getTemperature() {
return this.temperature.value;
}
on(type: DataType, callback: (data: number) => void): void;
on(type: 'error', callback: (data: { message: string, error: Error }) => void): void;
on(type: EventType, callback: (data: any) => void) {
this.subscribers.push({ type, callback });
}
private async connect() {
const query = await agent.post(Api.Connect)
.field('email', this.email).field('password', this.password)
.field('r', 'https://healthmate.withings.com/').field('is_admin', 'f')
;
const error = (query.text.replace(/[\n|\r|\t]/g, '').match(/<div class="alert alert-danger"><li>(.+?)<\/li><\/div>/) || [])[1];
if (error) {
throw new Error(`Connection failed (${error})`);
}
}
private emit<T>(type: EventType, value: T) {
this.subscribers
.filter((subscriber) => subscriber.type === type)
.forEach(({ callback }) => callback(value))
;
}
private emitError(message: string, error: any) {
this.emit<{ message: string, error: any }>('error', { message, error });
}
private async fetchDeviceInfo() {
try {
const query = await agent
.post(Api.Devices)
.field('action', 'getbyaccountid').field('enrich', 't')
;
const data = JSON.parse(query.text) as ApiResponse;
if (data.status === 0) {
this.device = (data as DeviceApiResponse).body.associations
.find(({ deviceproperties }) => deviceproperties.macaddress.toUpperCase() === this.mac)
;
if (this.device) {
return this.device;
} else {
throw new Error(`Could not find device with MAC ${this.mac}`);
}
} else {
throw data.error;
}
} catch (e) {
throw e;
}
}
private async fetchMeasure(type: Measure) {
try {
const query = await agent
.post(Api.Measure)
.field('action', 'getmeashf').field('meastype', type).field('deviceid', this.device.deviceid)
;
const data = JSON.parse(query.text) as ApiResponse;
if (data.status === 0) {
const measure = (data as MeasureApiResponse).body.series[0].data[0];
return measure;
} else {
throw data.error;
}
} catch (e) {
throw e;
}
}
private async reset() {
this.device = null;
}
// tslint:disable-next-line:cyclomatic-complexity
private async tick() {
if (!this.device) {
try {
await this.connect();
} catch (error) {
this.emitError('Could not connect', error);
return;
}
}
try {
const device = await this.fetchDeviceInfo();
this.device = device;
const battery = device.deviceproperties.batterylvl;
(!this.battery || this.battery !== battery) && this.emit('battery', battery);
this.battery = battery;
const carbondioxide = await this.fetchMeasure(Measure.CarbonDioxide);
(!this.carbondioxide || this.carbondioxide.date !== carbondioxide.date) && this.emit('carbondioxide', carbondioxide.value);
this.carbondioxide = carbondioxide;
const temperature = await this.fetchMeasure(Measure.Temperature);
(!this.temperature || this.temperature.date !== temperature.date) && this.emit('temperature', temperature.value);
this.temperature = temperature;
} catch (error) {
if (error === INVALID_PARAMS_ERROR) {
this.emitError('Session expired. Will reconnect on next tick.', error);
this.reset();
} else {
this.emitError('Could not fetch data from API', error);
}
}
}
}