-
Notifications
You must be signed in to change notification settings - Fork 20
/
app.js
87 lines (79 loc) · 2.13 KB
/
app.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
"use strict";
const Homey = require("homey");
const MiHub = require("./lib/MiHub");
const miio = require("miio");
const { ManagerSettings } = Homey;
const CHARS = "0123456789ABCDEF";
function generateKey() {
let result = "";
for (let i = 0; i < 16; i++) {
let idx = Math.floor(Math.random() * CHARS.length);
result += CHARS[idx];
}
return result;
}
class MiHomey extends Homey.App {
onInit() {
this.log("MiHomey is running...");
this.mihub = new MiHub(this.log);
this.onSettingsChanged = this.onSettingsChanged.bind(this);
ManagerSettings.on("set", this.onSettingsChanged);
ManagerSettings.on("unset", this.onSettingsChanged);
}
onSettingsChanged(key) {
switch (key) {
case "gatewaysList":
this.mihub.updateGateways(ManagerSettings.get("gatewaysList"));
break;
default:
break;
}
}
async generate(args) {
return new Promise((resolve, reject) => {
let key = generateKey();
miio
.device({ address: args.body.ip, token: args.body.token })
.then(device => {
device
.call("miIO.info", [])
.then(value => {
device
.call("set_lumi_dpf_aes_key", [key])
.then(result => {
resolve({ status: "OK", mac: value.mac.replace(/\:/g, "").toLowerCase(), password: key });
})
.catch(error => {
reject(error);
});
})
.catch(error => {
reject(error);
});
})
.catch(error => {
reject(error);
});
});
}
async testConnection(args) {
return new Promise((resolve, reject) => {
miio
.device({ address: args.body.ip, token: args.body.token })
.then(device => {
device
.call("miIO.info", [])
.then(result => {
resolve({ status: "OK", result });
})
.catch(error => {
reject(error);
});
})
.catch(error => {
reject(error);
});
});
}
}
module.exports = MiHomey;