-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
118 lines (105 loc) · 3.2 KB
/
index.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
const http = require('http');
const PromiseRetry = require('./lib/promiseretry');
let Service;
let Characteristic;
module.exports = homebridge => {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory('homebridge-nremo', 'NatureRemo', RemoAccessory);
};
class RemoAccessory {
constructor(log, config) {
this.log = log;
this.name = config['name'];
this.switchService = new Service.Switch(this.name);
this.informationService = new Service.AccessoryInformation();
this.config = config;
this.order_on = config.command_order[0];
this.order_off = config.command_order[1];
}
request(command, delayAfter = 0, timeout = 2000) {
const options = {
host: this.config['host'],
path: this.config['path'],
method: 'POST',
headers: {
'X-Requested-With': 'curl',
'Content-Type': 'application/json',
'Content-Length': JSON.stringify(this.config[command]).length
},
timeout: timeout
};
return new Promise((resolve, reject) => {
let data = '';
const req = http.request(options, response => {
if (response.statusCode != 200) {
reject(new Error(response.statusCode));
}
response.on('data', chunk => {
data += chunk.toString();
});
response.on('end', () => {
setTimeout(() => {
resolve({ status: response.statusCode, response: data });
}, delayAfter);
});
});
req.on('timeout', () => {
reject(new Error('Request Timeout'));
});
req.on('error', error => {
reject(error);
});
req.write(JSON.stringify(this.config[command]));
req.end();
});
}
async setState(input_switch_state, next) {
this.log(`Swtich: ${input_switch_state}`);
let command_order;
if (input_switch_state) {
command_order = 'on';
} else {
command_order = 'off';
}
const pre = 'order_' + command_order;
await new Promise(resolve =>
setTimeout(resolve, this.config.delayBefore || 0)
);
try {
for (let i = 0; i < this[pre][command_order].length; i++) {
const promise = new PromiseRetry(
this.config.retry,
this.config.retry_interval
);
const response = await promise.run(() => {
return this.request(
this[pre][command_order][i],
this.config.delayAfter,
this.config.timeout
);
});
this.log(`${this[pre][command_order][i]}: ${response.status}`);
}
next();
} catch (error) {
this.log(error.message);
next(error);
}
}
getServices() {
this.log(`start homebridge Server ${this.name}`);
this.informationService
.setCharacteristic(Characteristic.Manufacturer, 'Nature')
.setCharacteristic(Characteristic.Model, 'Remo')
.setCharacteristic(Characteristic.SerialNumber, '123-457-000');
this.switchService
.getCharacteristic(Characteristic.On)
.on('set', this.setState.bind(this));
return [this.informationService, this.switchService];
}
identify(callback) {
this.log(`called ${this.config['name']}`);
callback();
}
}