-
Notifications
You must be signed in to change notification settings - Fork 17
/
index.ts
327 lines (284 loc) · 8.93 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
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
import TuyAPI from 'tuyapi';
export enum CleanSpeed {
STANDARD = 'Standard',
BOOST_IQ = 'Boost_IQ',
MAX = 'Max',
NO_SUCTION = 'No_suction'
}
export enum ErrorCode {
NO_ERROR = 'no_error',
STUCK_5_MIN = 'Stuck_5_min',
CRASH_BAR_STUCK = 'Crash_bar_stuck',
SENSOR_DIRTY = 'sensor_dirty',
NOT_ENOUGH_POWER = 'N_enough_pow',
WHEEL_STUCK = 'Wheel_stuck',
S_BRUSH_STUCK = 'S_brush_stuck',
FAN_STUCK = 'Fan_stuck',
R_BRUSH_STUCK = 'R_brush_stuck'
}
export enum WorkStatus {
RUNNING = 'Running', // Cleaning
STAND_BY = 'standby', // Not in the dock, paused
SLEEPING = 'Sleeping', // Not in the dock - goes into this state after being paused for a while
CHARGING = 'Charging', // In the dock, charging
COMPLETED = 'completed', // In the dock, full charged
RECHARGE_NEEDED = 'Recharge' // Going home because battery is depleted or home was pressed
}
export enum Direction {
FORWARD = 'forward',
BACKWARD = 'back',
LEFT = 'left',
RIGHT = 'right'
}
export enum WorkMode {
AUTO = 'auto',
SMALL_ROOM = 'SmallRoom',
SPOT = 'Spot',
EDGE = 'Edge',
NO_SWEEP = 'Nosweep'
}
export interface StatusResponse {
devId: string,
dps: {
"1": boolean,
"2": boolean,
"3": string,
"5": string,
"15": string,
"101": boolean,
"102": string,
"103": boolean,
"104": number,
"106": string,
}
}
export interface RoboVacConfig {
deviceId: string
localKey: string
ip?: string
port?: number
}
export class RoboVac {
api: any;
PLAY_PAUSE = '2';
DIRECTION = '3';
WORK_MODE = '5';
WORK_STATUS = '15';
GO_HOME = '101';
CLEAN_SPEED = '102';
FIND_ROBOT = '103';
BATTERY_LEVEL = '104';
ERROR_CODE = '106';
connected: boolean = false;
debugLog: boolean;
statuses: StatusResponse = null;
lastStatusUpdate: number = null;
maxStatusUpdateAge: number = 1000 * (1 * 30); //30 Seconds
timeoutDuration: number;
constructor(config: RoboVacConfig, debugLog: boolean = false, timeoutDuration = 2) {
this.debugLog = debugLog;
if (!config.deviceId) {
throw new Error('You must pass through deviceId');
}
this.api = new TuyAPI(
{
id: config.deviceId,
key: config.localKey,
ip: config.ip,
port: config.port || 6668,
version: '3.3',
issueRefreshOnConnect: true
}
);
this.timeoutDuration = timeoutDuration;
this.api.on('error', (error: any) => {
if (debugLog) {
console.error('Robovac Error', JSON.stringify(error, null, 4));
}
});
this.api.on('connected', () => {
this.connected = true;
if (debugLog) {
console.log("Connected!");
}
});
this.api.on('disconnected', () => {
this.connected = false;
if (debugLog) {
console.log('Disconnected!');
}
});
this.api.on('dp-refresh', data => {
if (debugLog) {
console.log('DP_REFRESH data from device: ', data);
console.log('Status Updated!');
}
});
this.api.on('data', (data: StatusResponse) => {
if (debugLog) {
console.log('Data from device:', data);
console.log('Status Updated!');
}
});
}
async connect() {
if (!this.connected) {
if (this.debugLog) {
console.log('Connecting...');
}
await this.api.connect();
}
}
async disconnect() {
if (this.debugLog) {
console.log('Disconnecting...');
}
await this.api.disconnect();
}
async doWork(work: () => Promise<any>): Promise<any> {
if (!this.api.device.id || !this.api.device.ip) {
if (this.debugLog) {
console.log('Looking for device...');
}
try {
await this.api.find({ timeout: this.timeoutDuration });
if (this.debugLog) {
console.log(`Found device ${this.api.device.id} at ${this.api.device.ip}`);
}
} catch (err) {
console.log(err);
}
}
await this.connect();
return await work();
}
async getStatuses(force: boolean = false): Promise<{ devId: string, dps: { [key: string]: string | boolean | number } }> {
if (force || (new Date()).getTime() - this.lastStatusUpdate > this.maxStatusUpdateAge) {
return await this.doWork(async () => {
this.statuses = await this.api.get({ schema: true });
this.lastStatusUpdate = (new Date()).getTime();
return this.statuses;
});
} else {
return this.statuses;
}
}
async getCleanSpeed(force: boolean = false): Promise<CleanSpeed> {
let statuses = await this.getStatuses(force);
return <CleanSpeed>statuses.dps[this.CLEAN_SPEED];
}
async setCleanSpeed(cleanSpeed: CleanSpeed) {
await this.doWork(async () => {
await this.set({
[this.CLEAN_SPEED]: cleanSpeed
})
});
}
async getPlayPause(force: boolean = false): Promise<boolean> {
let statuses = await this.getStatuses(force);
return <boolean>statuses.dps[this.PLAY_PAUSE];
}
async setPlayPause(state: boolean) {
await this.doWork(async () => {
await this.set({
[this.PLAY_PAUSE]: state
})
});
}
async play() {
await this.setPlayPause(true);
}
async pause() {
await this.setPlayPause(true);
}
async getWorkMode(force: boolean = false): Promise<WorkMode> {
let statuses = await this.getStatuses(force);
return <WorkMode>statuses.dps[this.WORK_MODE];
}
async setWorkMode(workMode: WorkMode) {
await this.doWork(async () => {
if (this.debugLog) {
console.log(`Setting WorkMode to ${workMode}`);
}
await this.set({
[this.WORK_MODE]: workMode
})
});
}
async startCleaning(force: boolean = false) {
if (this.debugLog) {
console.log('Starting Cleaning', JSON.stringify(await this.getStatuses(force), null, 4));
}
await this.setWorkMode(WorkMode.AUTO);
if (this.debugLog) {
console.log('Cleaning Started!');
}
}
async getWorkStatus(force: boolean = false): Promise<WorkStatus> {
let statuses = await this.getStatuses(force);
return <WorkStatus>statuses.dps[this.WORK_STATUS];
}
async setWorkStatus(workStatus: WorkStatus) {
await this.doWork(async () => {
await this.set({
[this.WORK_STATUS]: workStatus
})
});
}
async goHome() {
await this.doWork(async () => {
await this.set({
[this.GO_HOME]: true
})
});
}
async setDirection(direction: Direction) {
await this.doWork(async () => {
await this.set({
[this.DIRECTION]: direction
})
});
}
async setFindRobot(state: boolean) {
return await this.doWork(async () => {
await this.set({
[this.FIND_ROBOT]: state
})
});
}
async getFindRobot(force: boolean = false) {
let statuses = await this.getStatuses(force);
return <boolean>statuses.dps[this.FIND_ROBOT];
}
async getBatteyLevel(force: boolean = false) {
let statuses = await this.getStatuses(force);
return <number>statuses.dps[this.BATTERY_LEVEL];
}
async getErrorCode(force: boolean = false): Promise<ErrorCode> {
let statuses = await this.getStatuses(force);
return <ErrorCode>statuses.dps[this.ERROR_CODE];
}
async set(data: { [key: string]: string | number | boolean }) {
if (this.debugLog) {
console.log(`Setting: ${JSON.stringify(data, null, 4)}`);
}
return await this.api.set({
multiple: true,
data: data
});
}
formatStatus() {
console.log(`
-- Status Start --
- Play/Pause: ${(this.statuses.dps as any)[this.PLAY_PAUSE]}
- Direction: ${(this.statuses.dps as any)[this.DIRECTION]}
- Work Mode: ${(this.statuses.dps as any)[this.WORK_MODE]}
- Go Home: ${(this.statuses.dps as any)[this.GO_HOME]}
- Clean Speed: ${(this.statuses.dps as any)[this.CLEAN_SPEED]}
- Find Robot: ${(this.statuses.dps as any)[this.FIND_ROBOT]}
- Battery Level: ${(this.statuses.dps as any)[this.BATTERY_LEVEL]}
- Error Code: ${(this.statuses.dps as any)[this.ERROR_CODE]}
-- Status End --
`);
}
}