-
Notifications
You must be signed in to change notification settings - Fork 1
/
coordinates.go
219 lines (184 loc) · 5.8 KB
/
coordinates.go
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
package core
import (
"fmt"
"github.com/nmaupu/gotomation/model"
"sync"
"time"
"github.com/kelvins/sunrisesunset"
"github.com/nmaupu/gotomation/app"
"github.com/nmaupu/gotomation/httpclient"
"github.com/nmaupu/gotomation/logging"
"github.com/nmaupu/gotomation/routines"
"github.com/pkg/errors"
)
var (
coords coordinates
once sync.Once
onceErr error
)
// Coordinates represents GPS coordinates using latitude and longitude
type Coordinates interface {
routines.Runnable
GetSunriseSunset() (time.Time, time.Time, error)
IsDarkNow(offsetDawn, offsetDusk time.Duration) bool
GetLatitude() float64
GetLongitude() float64
}
// Coordinates represents GPS coordinates using latitude and longitude
type coordinates struct {
Latitude float64
Longitude float64
// Store previous sunrise/sunset values because it takes 10s on raspberry to compute...
sunrise time.Time
sunset time.Time
lastUpdate time.Time
// mutex ensures that only one thread at a time modifies private variables
mutex *sync.Mutex
sunriseSunsetDone chan bool
started bool
mutexStopStart sync.Mutex
}
// InitCoordinates gets the latitude and longitude of a Home Assistant zone entity
func InitCoordinates(zoneName string) error {
once.Do(
func() {
var entity model.HassEntity
entity, onceErr = httpclient.GetSimpleClient().GetEntity("zone", zoneName)
if onceErr != nil {
onceErr = errors.Wrapf(onceErr, "Unable to get latitude and longitude")
} else {
coords = coordinates{
Latitude: entity.State.Attributes["latitude"].(float64),
Longitude: entity.State.Attributes["longitude"].(float64),
mutex: &sync.Mutex{},
sunriseSunsetDone: make(chan bool, 1),
}
}
})
return onceErr
}
// Coords returns the Coordinates singleton
func Coords() Coordinates {
return &coords
}
// Stop stops the sunrise/sunset refresh goroutine
func (c *coordinates) Stop() {
c.mutexStopStart.Lock()
defer c.mutexStopStart.Unlock()
if !c.started {
return
}
c.sunriseSunsetDone <- true
c.started = false
}
func (c *coordinates) Start() error {
c.mutexStopStart.Lock()
defer c.mutexStopStart.Unlock()
if c.started {
return nil
}
// init stop channel
c.sunriseSunsetDone = make(chan bool, 1)
l := logging.NewLogger("Coordinates.Start")
// first init before ticker ticks
app.RoutinesWG.Add(1)
go func() {
defer app.RoutinesWG.Done()
c.getSunriseSunset(false)
}()
app.RoutinesWG.Add(1)
go func() { // updating sunrise / sunset dates once in a while
defer app.RoutinesWG.Done()
l.Debug().Msg("Starting sunrise/sunset refresh go routine")
ticker := time.NewTicker(6 * time.Hour)
defer ticker.Stop()
for {
select {
case <-c.sunriseSunsetDone:
l.Trace().Msg("Exiting sunrise/sunset refresh go routine")
return
case <-ticker.C:
c.getSunriseSunset(false)
}
}
}()
c.started = true
return nil
}
func (c *coordinates) IsStarted() bool {
c.mutexStopStart.Lock()
defer c.mutexStopStart.Unlock()
return c.started && c.sunriseSunsetDone != nil
}
func (c *coordinates) GetLatitude() float64 {
return c.Latitude
}
func (c *coordinates) GetLongitude() float64 {
return c.Longitude
}
func (c *coordinates) GetSunriseSunset() (time.Time, time.Time, error) {
return c.getSunriseSunset(true)
}
// GetSunriseSunset gets sunrise and sunset times
func (c *coordinates) getSunriseSunset(cache bool) (time.Time, time.Time, error) {
if c.mutex == nil {
return time.Time{}, time.Time{}, fmt.Errorf("call NewLatitudeLongitude to create Coordinates")
}
c.mutex.Lock()
defer c.mutex.Unlock()
now := time.Now().Local()
l := logging.NewLogger("GetSunriseSunset")
defer func() { // need anonymous func to have correct duration
l.Trace().Str("duration", time.Now().Sub(now).String()).Msg("Time taken to get sunrise/sunset dates")
}()
// If cache is true, return values if freshness is < 12 hours
// If func has been called too soon (< 5 secs), returning cached values if they exist even if cache is false
if !c.sunrise.IsZero() && !c.sunset.IsZero() {
durationSinceLastUpdate := now.Sub(c.lastUpdate)
if (cache && durationSinceLastUpdate < 12*time.Hour) ||
durationSinceLastUpdate < 30*time.Second {
return c.sunrise, c.sunset, nil
}
}
name, offset := now.Zone()
l.Trace().Str("zone_name", name).Int("offset", offset).Msg("Timezone information")
p := sunrisesunset.Parameters{
Latitude: c.Latitude,
Longitude: c.Longitude,
UtcOffset: float64(offset) / 3600,
Date: now,
}
sunrise, sunset, err := p.GetSunriseSunset()
if err != nil {
return time.Time{}, time.Time{}, err
}
// Using now.Location() to get correct timezone (sunrise and sunset doesn't have a Location set to return UTC time.UTC)
c.sunrise = time.Date(now.Year(), now.Month(), now.Day(), sunrise.Hour(), sunrise.Minute(), sunrise.Second(), sunrise.Nanosecond(), now.Location())
c.sunset = time.Date(now.Year(), now.Month(), now.Day(), sunset.Hour(), sunset.Minute(), sunset.Second(), sunset.Nanosecond(), now.Location())
c.lastUpdate = now
l.Info().
Time("sunrise", c.sunrise).
Time("sunset", c.sunset).
Msg("Sunrise, sunset dates has been initialized successfully")
return c.sunrise, c.sunset, nil
}
// IsDarkNow returns true if it's dark outside
func (c *coordinates) IsDarkNow(offsetDawn, offsetDusk time.Duration) bool {
l := logging.NewLogger("IsDarkNow")
if c.GetLatitude() == 0 || c.GetLongitude() == 0 {
l.Warn().Msg("Latitude or longitude is not set, cannot determine if it's dark")
return false
}
now := time.Now().Local()
sunrise, sunset, _ := c.GetSunriseSunset()
sunrise = sunrise.Add(offsetDawn)
sunset = sunset.Add(offsetDusk)
return now.Before(sunrise) || now.After(sunset)
}
// GetName returns the name of this runnable object
func (c *coordinates) GetName() string {
return "Coordinates"
}
func (c *coordinates) IsAutoStart() bool {
return true
}