-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.js
115 lines (90 loc) · 3.57 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
var inherits = require('util').inherits,
suncalc = require('suncalc');
let ALTITUDE_UUID = 'a8af30e7-5c8e-43bf-bb21-3c1343229260';
let AZIMUTH_UUID = 'ace1dd10-2e46-4100-a74a-cc77f13f1bab';
let UpdatePeriod = 5;
module.exports = function(homebridge) {
Accessory = homebridge.hap.Accessory;
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory('homebridge-sun-position', 'SunPosition', SunPositionAccessory);
AltitudeCharacteristic = function() {
Characteristic.call(this, 'Altitude', ALTITUDE_UUID);
this.setProps({
format: Characteristic.Formats.FLOAT,
unit: Characteristic.Units.ARC_DEGREE,
minValue: -90,
maxValue: 90,
minStep: 0.1,
perms: [Characteristic.Perms.READ, Characteristic.Perms.NOTIFY]
});
this.value = this.getDefaultValue();
}
inherits(AltitudeCharacteristic, Characteristic);
AzimuthCharacteristic = function() {
Characteristic.call(this, 'Azimuth', AZIMUTH_UUID);
this.setProps({
format: Characteristic.Formats.FLOAT,
unit: Characteristic.Units.ARC_DEGREE,
minValue: 0,
maxValue: 360,
minStep: 0.1,
perms: [Characteristic.Perms.READ, Characteristic.Perms.NOTIFY]
});
this.value = this.getDefaultValue();
}
inherits(AzimuthCharacteristic, Characteristic);
}
function SunPositionAccessory(log, config) {
this.log = log;
this.config = config;
this.name = config.name;
if (!config.location || !Number.isFinite(config.location.lat) || !Number.isFinite(config.location.long))
throw new Error("Missing or invalid location configuration");
this.location = config.location;
this.updatePeriod = config.updatePeriod || UpdatePeriod;
}
SunPositionAccessory.prototype.identify = function(callback) {
this.log("Identify");
callback();
}
SunPositionAccessory.prototype.getServices = function() {
this.informationService = new Service.AccessoryInformation();
this.informationService
.setCharacteristic(Characteristic.Manufacturer, "github.com keybuk")
.setCharacteristic(Characteristic.Model, "Sun Position")
this.service = new Service.LightSensor("Sun");
this.service.addCharacteristic(AltitudeCharacteristic);
this.service.addCharacteristic(AzimuthCharacteristic);
this.updatePosition();
return [this.informationService, this.service];
}
SunPositionAccessory.prototype.updatePosition = function() {
var now = new Date();
var times = suncalc.getTimes(now, this.location.lat, this.location.long);
// Arbitrary lux values for times.
var lux = 0.0001;
if (now >= times.sunrise && now <= times.sunriseEnd) {
lux = 400;
} else if (now > times.sunriseEnd && now <= times.goldenHourEnd) {
lux = 20000;
} else if (now >= times.goldenHour && times < times.sunsetStart) {
lux = 20000;
} else if (now >= times.sunsetStart && now <= times.sunset) {
lux = 400;
} else if (now > times.sunset && now <= times.night) {
lux = 40;
} else if (now >= times.nightEnd && now < times.sunrise) {
lux = 40;
} else if (now > times.goldenHourEnd && now < times.goldenHour) {
lux = 100000;
}
this.service.setCharacteristic(Characteristic.CurrentAmbientLightLevel, lux);
var position = suncalc.getPosition(now, this.location.lat, this.location.long);
var altitude = position.altitude * 180 / Math.PI;
var azimuth = (position.azimuth * 180 / Math.PI + 180) % 360;
this.log("Sun is " + altitude + " high at " + azimuth);
this.service.setCharacteristic(AltitudeCharacteristic, altitude);
this.service.setCharacteristic(AzimuthCharacteristic, azimuth);
setTimeout(this.updatePosition.bind(this), this.updatePeriod * 60 * 1000);
}