-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDuskDawnSwitcher.groovy
More file actions
235 lines (194 loc) · 6.63 KB
/
DuskDawnSwitcher.groovy
File metadata and controls
235 lines (194 loc) · 6.63 KB
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/**
* Dusk/Dawn Switcher with local computation of Sunrise/Sunset times.
*
* Author: florianz
*
* Date: 2013-12-14
*/
preferences {
section ("At sunrise...") {
input "sunriseOn", "capability.switch", title: "Turn on?", required: false, multiple: true
input "sunriseOnLevel", "number", title: "On Level?", required: false
input "sunriseOff", "capability.switch", title: "Turn off?", required: false, multiple: true
}
section ("At sunset...") {
input "sunsetOn", "capability.switch", title: "Turn on?", required: false, multiple: true
input "sunsetOnLevel", "number", title: "On Level?", required: false
input "sunsetOff", "capability.switch", title: "Turn off?", required: false, multiple: true
}
section ("Sunrise offset (optional)...") {
input "sunriseOffsetValue", "text", title: "HH:MM", required: false
input "sunriseOffsetDir", "enum", title: "Before or After", required: false, metadata: [values: ["Before","After"]]
}
section ("Sunset offset (optional)...") {
input "sunsetOffsetValue", "text", title: "HH:MM", required: false
input "sunsetOffsetDir", "enum", title: "Before or After", required: false, metadata: [values: ["Before","After"]]
}
section( "Notifications" ) {
input "sendPushMessage", "enum", title: "Send a push notification?", metadata:[values:["Yes", "No"]], required: false
input "phoneNumber", "phone", title: "Send a text message?", required: false
}
}
private mod(v, max) {
while (v < 0) { v += max }
v % max
}
private GetDayOfYear(Date date) {
def year = date.getYear() + 1900
def month = date.getMonth() + 1
def day = date.getDate()
def n1 = Math.floor(275 * month / 9)
def n2 = Math.floor((month + 9) / 12)
def n3 = (1 + Math.floor((year - 4 * Math.floor(year / 4) + 2) / 3))
n1 - (n2 * n3) + day - 30
}
private GetRisingTime(Date date, longitude) {
GetDayOfYear(date) + ((6 - (longitude / 15)) / 24)
}
private GetSettingTime(Date date, longitude) {
GetDayOfYear(date) + ((18 - (longitude / 15)) / 24)
}
private GetSunLongitude(t) {
def m = (0.9856 * t) - 3.289
def l =
m +
(1.916 * Math.sin(Math.toRadians(m))) +
(0.02 * Math.sin(Math.toRadians(2 * m))) +
282.634
mod(l, 360)
}
private GetSunRightAscension(sunLongitude) {
def l = sunLongitude
def a = Math.toDegrees(Math.atan(0.91764 * Math.tan(Math.toRadians(l))))
a = mod(a, 360)
def lquadrant = Math.floor(l / 90) * 90
def aquadrant = Math.floor(a / 90) * 90
a = a + (lquadrant - aquadrant)
a / 15
}
private GetSunHourAngle(sunLongitude, latitude) {
def l = sunLongitude
def sinDec = 0.39782 * Math.sin(Math.toRadians(l))
def cosDec = Math.cos(Math.asin(sinDec))
def latitudeRadians = Math.toRadians(latitude)
def zenithAngle = Math.toRadians(90.5)
def cosH =
(Math.cos(zenithAngle) - sinDec * Math.sin(latitudeRadians)) /
(cosDec * Math.cos(latitudeRadians))
// Sun never sets or rises at this location, on this date
if (cosH > 1 || cosH < -1) {
return null
}
Math.toDegrees(Math.acos(cosH))
}
private GetLocalMeanTime(t, ra, h) {
h + ra - (0.06571 * t) - 6.622
}
private Date GetSunsetTime(Date date, latitude, longitude) {
def t = GetSettingTime(date, longitude)
def l = GetSunLongitude(t)
def ra = GetSunRightAscension(l)
def h = GetSunHourAngle(l, latitude) / 15
def T = GetLocalMeanTime(t, ra, h)
def utc = mod(T - (longitude / 15), 24)
def hours = Math.floor(utc)
def minutes = Math.floor((utc - hours) * 60)
def seconds = Math.floor((utc - hours - (minutes / 60)) * 3600)
return new Date(Date.UTC(
date.getYear(), date.getMonth(), date.getDate(),
hours as Integer, minutes as Integer, seconds as Integer))
}
private Date GetSunriseTime(Date date, latitude, longitude) {
def t = GetRisingTime(date, longitude)
def l = GetSunLongitude(t)
def ra = GetSunRightAscension(l)
def h = (360 - GetSunHourAngle(l, latitude)) / 15
def T = GetLocalMeanTime(t, ra, h)
def utc = mod(T - (longitude / 15), 24)
def hours = Math.floor(utc)
def minutes = Math.floor((utc - hours) * 60)
def seconds = Math.floor((utc - hours - (minutes / 60)) * 3600)
return new Date(Date.UTC(
date.getYear(), date.getMonth(), date.getDate(),
hours as Integer, minutes as Integer, seconds as Integer))
}
def installed() {
initialize()
}
def updated() {
unschedule()
initialize()
}
def initialize() {
astroCheck()
schedule("0 1 * * * ?", astroCheck)
}
def astroCheck() {
def now = new Date()
def riseTime = GetSunriseTime(now, location.latitude, location.longitude)
def setTime = GetSunsetTime(now, location.latitude, location.longitude)
log.debug "nowTime: $now"
log.debug "riseTime: $riseTime"
log.debug "setTime: $setTime"
state.riseTime = riseTime.time
state.setTime = setTime.time
unschedule("sunriseHandler")
unschedule("sunsetHandler")
if (riseTime.after(now)) {
log.info "scheduling sunrise handler for $riseTime"
runOnce(riseTime, sunriseHandler)
}
if (setTime.after(now)) {
log.info "scheduling sunset handler for $setTime"
runOnce(setTime, sunsetHandler)
}
}
def sunriseHandler() {
log.info "Executing sunrise handler"
if (sunriseOn) {
if (sunriseOnLevel == null) {
sunriseOn.on()
}
else {
sunriseOn?.setLevel(sunriseOnLevel)
}
}
if (sunriseOff) {
sunriseOff.off()
}
unschedule("sunriseHandler") // Temporary work-around for scheduling bug
send("Sunrise")
}
def sunsetHandler() {
log.info "Executing sunset handler"
if (sunsetOn) {
if (sunsetOnLevel == null) {
sunsetOn.on()
}
else {
sunsetOn?.setLevel(sunsetOnLevel)
}
}
if (sunsetOff) {
sunsetOff.off()
}
unschedule("sunsetHandler") // Temporary work-around for scheduling bug
send("Sunset")
}
private send(msg) {
if ( sendPushMessage == "Yes" ) {
log.debug( "sending push message" )
sendPush( msg )
}
if ( phoneNumber ) {
log.debug( "sending text message" )
sendSms( phoneNumber, msg )
}
log.debug msg
}
private getSunriseOffset() {
sunriseOffsetValue ? (sunriseOffsetDir == "Before" ? "-$sunriseOffsetValue" : sunriseOffsetValue) : null
}
private getSunsetOffset() {
sunsetOffsetValue ? (sunsetOffsetDir == "Before" ? "-$sunsetOffsetValue" : sunsetOffsetValue) : null
}