/
index.js
84 lines (71 loc) · 3.04 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
let Service, Characteristic;
let exec = require("child_process").exec;
module.exports = function(homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory("homebridge-nature-remo-ceiling-light", "NatureRemoCeilingLight", CeilingLight);
}
function CeilingLight(log, config) {
this.log = log;
this.name = config["name"];
this.access_token = config["access_token"];
this.signal_ID_on_day = config["signal_ID_on_day"];
this.signal_ID_on_night = config["signal_ID_on_night"];
this.signal_ID_off = config["signal_ID_off"];
this.start_time_day = config["start_time_day"]; // Start time for day mode in 24-hour format (e.g., "08:00")
this.start_time_night = config["start_time_night"]; // Start time for night mode in 24-hour format (e.g., "20:00")
this.state = { power: false };
this.informationService = new Service.AccessoryInformation();
this.informationService
.setCharacteristic(Characteristic.Manufacturer, "Homebridge")
.setCharacteristic(Characteristic.Model, "NatureRemoCeilingLight")
.setCharacteristic(Characteristic.SerialNumber, "NRL-" + this.name);
this.switchService = new Service.Switch(this.name);
this.switchService.getCharacteristic(Characteristic.On)
.on('set', this.setPower.bind(this));
}
CeilingLight.prototype.getServices = function() {
return [this.informationService, this.switchService];
}
CeilingLight.prototype.setPower = function(value, callback) {
if (this.state.power != value) {
this.state.power = value;
this.log('Setting switch to ' + value);
if (value) {
const currentTime = new Date();
const start_day_time_hour = this.start_time_day.split(':')[0];
const start_day_time_minute = this.start_time_day.split(':')[1];
let day_start = new Date();
day_start.setHours(start_day_time_hour);
day_start.setMinutes(start_day_time_minute);
day_start.setSeconds("00");
const start_night_time_hour = this.start_time_night.split(':')[0];
const start_night_time_minute = this.start_time_night.split(':')[1];
let night_start = new Date();
night_start.setHours(start_night_time_hour);
night_start.setMinutes(start_night_time_minute);
night_start.setSeconds("00");
const isDayTime = (currentTime >= day_start && currentTime < night_start);
const signalID = isDayTime ? this.signal_ID_on_day : this.signal_ID_on_night;
this.sendSignal(signalID, callback);
} else {
this.sendSignal(this.signal_ID_off, callback);
}
} else {
callback();
}
}
CeilingLight.prototype.sendSignal = function(signalID, callback) {
let cmd = 'curl -X POST ' +
'"https://api.nature.global/1/signals/' + signalID + '/send" ' +
'-H "accept":"application/json" ' +
'-k --header "Authorization":"Bearer ' + this.access_token + '"';
exec(cmd, function(error, stdout, stderr) {
if (error) {
this.log('Failed to set: ' + error);
callback(error);
} else {
callback();
}
}.bind(this));
}