-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbslamp.js
191 lines (130 loc) · 6.51 KB
/
bslamp.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
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
#! /usr/bin/env node
const { HttpClient, IPDiscovery } = require('hap-controller');
const fs = require('fs');
let font = {
bright: "\x1b[1m%s\x1b[0m",
red: "\x1b[31m%s\x1b[0m",
green: "\x1b[32m%s\x1b[0m",
yellow: "\x1b[33m%s\x1b[0m"
};
let args = process.argv.slice(2);
if (args[0] == "pair") {
if (args[1] && args[2]) {
let ip = args[1];
let pin = args[2];
let discoveryTime = 0;
let stopSearch = false;
const discovery = new IPDiscovery();
process.stdout.write("Search Device");
discovery.on('serviceUp', async (service) => {
if (service.address == ip) {
console.log("");
console.log(font.green, "Found " + service.name);
discoveryTime = 30;
stopSearch = true;
if (service.availableToPair) {
console.log("Start pairing");
try {
const pairMethod = await discovery.getPairMethod(service);
const client = new HttpClient(service.id, service.address, service.port);
await client.pairSetup(pin, pairMethod);
console.log(service.name + " successfully paired!");
const pairingData = client.getLongTermData();
const data = {
accessory: "MiBedsideLamp2",
name: "Mi Bedside Lamp 2",
id: service.id,
address: service.address,
port: service.port,
pairingData
}
console.log("");
console.log("Paste the following into your homebridge config");
console.log("'name' is what you see in homebridge/homekit and can be changed");
console.log(font.green, "===========COPY START===========");
console.log(JSON.stringify(data, undefined, 4));
console.log(font.green, "============COPY END============");
let ipAddress = data.address;
let fileName = "bslamp-" + ipAddress.replace(/\./g, '-');
fs.writeFileSync(__dirname + "/" + fileName, JSON.stringify(data, undefined, 4));
} catch (e) {
console.log(font.red, service.name + " pairing failed");
console.log(font.red, "===========ERROR START===========");
console.log(e);
console.log(font.red, "============ERROR END============");
}
} else {
console.log(font.red, service.name + " not available for pairing.");
console.log(font.yellow, "Please reset the device if you haven't paired it already.");
console.log(" - Hold the power button and the mode button for 5 seconds, the lamp will start flashing. After 3 seconds, it will restart and show white color, which means the lamp has been reset to default.");
console.log(" - Setup the wifi of the device with the yeelight/xiaomi home app and afterwards you can try the pairing again.");
}
}
});
discovery.start();
const discoveryInterval = setInterval(() => {
if (!stopSearch) {
process.stdout.write(".");
}
if (discoveryTime == 30) {
clearInterval(discoveryInterval);
discovery.stop();
if (!stopSearch) {
console.log("");
console.log(font.red, "No device found");
console.log("- Make sure the device is powered on and the wifi connection is working");
console.log("- If the device is already powered on and wifi is working, try unplugging it for a few seconds and then rerun the command immediately after plugging it in");
}
}
discoveryTime++;
}, 1000);
} else {
console.log(font.red, "IP address or pairing code missing");
}
} else if (args[0] == "unpair") {
if (args[1] && args[1].includes("bslamp")) {
let file = __dirname + "/" + args[1];
let device;
try {
if (fs.existsSync(file)) {
const data = fs.readFileSync(file, 'utf8');
device = JSON.parse(data);
const client = new HttpClient(device.id, device.address, device.port, device.pairingData);
client.removePairing(client.pairingProtocol.iOSDevicePairingID)
.then(function () {
console.log(device.accessory + " with IP " + device.address + " successfully unpaired");
fs.unlinkSync(file);
})
.catch(function (e) {
console.log(device.name + font.red, "===========ERROR START===========");
console.log(e);
console.log(font.red, "============ERROR END============");
process.exit();
});
} else {
console.log(font.red, "No file under the given name found");
}
} catch (e) {
console.log(font.red, "===========ERROR START===========");
console.log(e);
console.log(font.red, "============ERROR END============");
}
} else {
console.log(font.red, "Filename wrong or missing");
process.stdout.write("Usage: ");
console.log(font.bright, "bslamp-unpair [filename]");
console.log(" - [filename] is 'bslamp-' and the IP address of the device with '-' instead of '.' (e.g. bslamp-192-168-0-123)");
console.log(" - e.g. 'bslamp-192-168-0-123'");
}
} else {
console.log(font.bright, "bslamp help");
console.log("=================================");
process.stdout.write("Usage pair: ");
console.log(font.bright, "bslamp pair [ip address] [pairing code]");
console.log(" - e.g. 'bslamp pair 192.168.0.123 012-34-567'");
console.log("");
process.stdout.write("Usage unpair: ");
console.log(font.bright, "bslamp unpair [filename]");
console.log(" - [filename] is 'bslamp-' and the IP address of the device with '-' instead of '.'");
console.log(" - e.g. 'bslamp unpair bslamp-192-168-0-123'");
}