-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
control.js
95 lines (72 loc) 路 2.14 KB
/
control.js
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
const TuyAPI = require('tuyapi');
const c = require('./common');
function parseConfig(config, options) {
if (options.id === undefined) {
c.badArgument(options);
}
if (Number.isNaN(options.protocolVersion)) {
c.badArgument(options);
}
// Save to config
if (options.save === true) {
config.set(options.id, options.key);
}
// If key is not given but exists in config
if (options.key === undefined && config.get(options.id) !== undefined) {
if (options.ip === undefined) {
return new TuyAPI({version: options.protocolVersion, id: options.id, key: config.get(options.id)});
}
return new TuyAPI({version: options.protocolVersion, ip: options.ip, id: options.id, key: config.get(options.id)});
}
// If key is not given and does not exit in config
if (options.key === undefined && config.get(options.id) === undefined) {
c.badArgument(options);
} else {
// If both arguments are given
if (options.ip === undefined) {
return new TuyAPI({version: options.protocolVersion, id: options.id, key: options.key});
}
return new TuyAPI({version: options.protocolVersion, ip: options.ip, id: options.id, key: options.key});
}
}
async function get(config, options) {
const tuya = parseConfig(config, options);
try {
await tuya.find();
await tuya.connect();
const properties = await tuya.get({schema: true});
tuya.disconnect();
if (options.all) {
console.log(properties);
} else {
console.log(properties.dps[options.dps]);
}
} catch (error) {
console.log(error);
}
}
async function set(config, options) {
if (options.set === undefined) {
c.badArgument(options);
}
if (!options.rawValue) {
if (!Number.isNaN(options.set)) {
options.set = Number.parseInt(options.set, 10);
} else if (options.set.toLowerCase() === 'true') {
options.set = true;
} else if (options.set.toLowerCase() === 'false') {
options.set = false;
}
}
const tuya = parseConfig(config, options);
try {
await tuya.find();
await tuya.connect();
await tuya.set({set: options.set, dps: options.dps});
tuya.disconnect();
console.log('Set succeeded.');
} catch (error) {
console.log(error);
}
}
module.exports = {get, set};