-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
180 lines (159 loc) · 5.86 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
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
'use strict';
const suncalc = require('suncalc');
var inherits = require('util').inherits;
module.exports = homebridge => {
const Characteristic = homebridge.hap.Characteristic;
const Service = homebridge.hap.Service;
const Accessory = homebridge.hap.Accessory;
const uuid = homebridge.hap.uuid;
const PeriodValue = function() {
Characteristic.call(this, 'Suncalc Period Value', '4d640a06-34fe-45d7-bf7c-736bb2cf5693');
this.setProps({
format: Characteristic.Formats.UINT8,
maxValue: 5,
minValue: 0,
minStep: 1,
perms: [Characteristic.Perms.READ, Characteristic.Perms.NOTIFY]
});
this.value = this.getDefaultValue();
};
inherits(PeriodValue, Characteristic);
const PeriodName = function() {
Characteristic.call(this, 'Suncalc Period Name', '4d640a06-34fe-45d7-bf7c-736bb2cf5694');
this.setProps({
format: Characteristic.Formats.STRING,
perms: [Characteristic.Perms.READ]
});
this.value = this.getDefaultValue();
};
inherits(PeriodName, Characteristic);
const Suncalc = function(displayName, subtype) {
Service.call(this, displayName, '61f96c67-1b62-4c2b-a300-58c242355017', subtype);
// Required Characteristics
this.addCharacteristic(PeriodValue);
this.addCharacteristic(PeriodName);
// Optional Characteristics
this.addOptionalCharacteristic(Characteristic.StatusActive);
this.addOptionalCharacteristic(Characteristic.StatusFault);
this.addOptionalCharacteristic(Characteristic.StatusTampered);
this.addOptionalCharacteristic(Characteristic.StatusLowBattery);
this.addOptionalCharacteristic(Characteristic.Name);
};
inherits(Suncalc, Service);
class SuncalcAccessory {
constructor(log, config) {
if (!config.location ||
!Number.isFinite(config.location.lat) ||
!Number.isFinite(config.location.lng)) {
throw new Error('Invalid or missing `location` configuration.');
}
this.sunriseEndOffset = 0
this.sunsetStartOffset = 0
if (!config.offset || !config.offset.sunriseEnd) {
this.sunriseEndOffset = 0
} else {
this.sunriseEndOffset = config.offset.sunriseEnd
}
if (!config.offset || !config.offset.sunsetStart) {
this.sunsetStartOffset = 0
} else {
this.sunsetStartOffset = config.offset.sunsetStart
}
this.location = config.location;
// this.service = new Service.LightSensor(config.name);
this.service = new Suncalc(config.name);
this.log = log;
this.updateAmbientLightLevel();
}
updateAmbientLightLevel() {
const nowDate = new Date();
const now = nowDate.getTime();
const sunDates = suncalc.getTimes(
nowDate,
this.location.lat,
this.location.lng
);
const times = {
nightEnd: sunDates.nightEnd.getTime(),
nauticalDawn: sunDates.nauticalDawn.getTime(),
dawn: sunDates.dawn.getTime(),
sunrise: sunDates.sunrise.getTime(),
sunriseEnd: sunDates.sunriseEnd.getTime() + (this.sunriseEndOffset * 1000 * 60),
sunsetStart: sunDates.sunsetStart.getTime() + (this.sunsetStartOffset * 1000 * 60),
sunset: sunDates.sunset.getTime(),
dusk: sunDates.dusk.getTime(),
nauticalDusk: sunDates.nauticalDusk.getTime(),
night: sunDates.night.getTime()
};
let lightRatio;
let sunCalc;
let nextUpdate;
let periodName;
if (now < times.dawn) {
// Nightime
this.log("Currently Nighttime(0). Morning Twilight begins at " + sunDates.dawn.toLocaleTimeString())
nextUpdate = times.dawn
sunCalc = 0
periodName = "Night"
} else if (now >= times.dawn && now < times.sunrise) {
// Morning Twilight
this.log("Currently Morning Twilight(1). Sunrise is at " + sunDates.sunrise.toLocaleTimeString())
if (this.sunriseEndOffset != 0) {
this.log("Sunrise time will be offset by " + this.sunriseEndOffset + " minute(s)")
}
nextUpdate = times.sunrise
sunCalc = 1
periodName = "Morning Twilight"
} else if (now >= times.sunrise && now < times.sunriseEnd) {
// Sunrise
this.log("Currently Sunrise(2). Sunrise ends at " + sunDates.sunriseEnd.toLocaleTimeString())
nextUpdate = times.sunriseEnd
sunCalc = 2
periodName = "Sunrise"
} else if (now >= times.sunriseEnd && now < times.sunsetStart) {
// Daytime
this.log("Currently Daytime(3). Sunset is at " + sunDates.sunsetStart.toLocaleTimeString())
if (this.sunsetStartOffset != 0) {
this.log("Sunset time will be offset by " + this.sunsetStartOffset + " minute(s)")
}
nextUpdate = times.sunsetStart
sunCalc = 3
periodName = "Daytime"
} else if (now >= times.sunsetStart && now < times.sunset) {
// Sunset
this.log("Currently Sunset(4). Evening Twilight begins at " + sunDates.sunset.toLocaleTimeString())
nextUpdate = times.sunset
sunCalc = 4
periodName = "Sunset"
} else if (now >= times.sunset && now < times.dusk){
// Evening Twilight
this.log("Currently Evening Twilight(5). Night begins at " + sunDates.dusk.toLocaleTimeString())
nextUpdate = times.dusk
sunCalc = 5
periodName = "Evening Twilight"
} else {
// Nighttime
sunCalc = 0
var tomorrow = new Date()
tomorrow.setDate(tomorrow.getDate() + 1)
const tomorrowsTimes = suncalc.getTimes(tomorrow,this.location.lat,this.location.lng)
this.log("Currently Nighttime(0). Morning Twilight begins at " + tomorrowsTimes.dawn.toLocaleTimeString())
nextUpdate = tomorrowsTimes.dawn.getTime()
periodName = "Night"
}
this.service.setCharacteristic(
PeriodValue,
sunCalc
);
this.service.setCharacteristic(
PeriodName,
periodName
);
setTimeout(this.updateAmbientLightLevel.bind(this), nextUpdate - now);
}
getServices() {
return [this.service];
}
}
homebridge.registerAccessory('homebridge-suncalc', 'Suncalc', SuncalcAccessory);
};