-
Notifications
You must be signed in to change notification settings - Fork 0
/
today.go
208 lines (181 loc) · 5.55 KB
/
today.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
package services
import (
"bufio"
"fmt"
"html/template"
"net/http"
"os"
"path/filepath"
"strings"
"time"
"github.com/estesp/onimage/pkg/util"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
type Today struct {
dateStr string
homeDir string
sunrise int64
sunset int64
weatherService *WeatherData
darkPercent float32
s3bucket string
pageTemplate *template.Template
pageTemplateName string
offlinePageTemplate string
offline bool
errChan chan error
}
type PageData struct {
Today string
Sunrise string
Sunset string
}
var (
awscpIndexCmd = []string{"aws", "s3", "cp", "SOMEFILE", "BUCKETLOCATION", "--acl", "public-read",
"--content-type", "text/html", "--metadata-directive", "REPLACE", "--expires"}
)
func NewTodayService(wdService *WeatherData, config map[string]interface{}, errChan chan error) (*Today, error) {
dateStr := util.GetDateString()
pageTmpl, err := util.GetStringFromConfig(config, "website.page_template")
if err != nil {
return nil, fmt.Errorf("can't retrieve entry 'website.page_template' from config: %w", err)
}
offlinePageTmpl, err := util.GetStringFromConfig(config, "website.offline_page")
if err != nil {
return nil, fmt.Errorf("can't retrieve entry 'website.offline_page' from config: %w", err)
}
s3bucketName, err := util.GetStringFromConfig(config, "website.bucket")
if err != nil {
return nil, fmt.Errorf("can't retrieve entry 'website.bucket' from config: %w", err)
}
homeDir, err := util.GetStringFromConfig(config, "home_dir")
if err != nil {
return nil, fmt.Errorf("can't retrieve entry 'home_dir' from config: %w", err)
}
today := &Today{
homeDir: homeDir,
dateStr: dateStr,
weatherService: wdService,
darkPercent: 100.0,
s3bucket: s3bucketName,
pageTemplateName: filepath.Base(pageTmpl),
pageTemplate: template.Must(template.ParseFiles(pageTmpl)),
offlinePageTemplate: offlinePageTmpl,
errChan: errChan,
}
awscpIndexCmd[4] = fmt.Sprintf("s3://%s/index.html", s3bucketName)
weather, err := wdService.GetCurrentWeather()
if err != nil {
errChan <- err
return nil, err
}
today.sunrise = weather.Sys.Sunrise
today.sunset = weather.Sys.Sunset
return today, nil
}
func (t *Today) GetDate() string {
return t.dateStr
}
func (t *Today) GetSunrise() int64 {
return t.sunrise
}
func (t *Today) GetSunset() int64 {
return t.sunset
}
func (t *Today) GetDarkPercent() float32 {
return t.darkPercent
}
func (t *Today) SetDarkPercent(percent float32) {
t.darkPercent = percent
}
// SetTodayPage sets up an index.html for the static site with today's date and sunrise/sunset info
func (t *Today) SetTodayPage() error {
if t.offline {
// if we're in "webcam offline" mode, don't set up the new page
return nil
}
// change the website page with today's info
// set up an expiration time for our index page one day from now
// TODO: set this up to expire at midnight, not just adding 24hr to "now"
tomorrow := time.Now().Add(24 * time.Hour)
expires := time.Date(tomorrow.Year(), tomorrow.Month(), tomorrow.Day(), 6, 0, 0, 0, time.UTC).Format(http.TimeFormat)
riseTime := time.Unix(t.GetSunrise(), 0)
setTime := time.Unix(t.GetSunset(), 0)
sunriseStr := fmt.Sprintf("%02d:%02d", riseTime.Hour(), riseTime.Minute())
sunsetStr := fmt.Sprintf("%02d:%02d", setTime.Hour(), setTime.Minute())
data := PageData{
Today: t.GetDate(),
Sunrise: sunriseStr,
Sunset: sunsetStr,
}
tmpFile, err := os.CreateTemp("/tmp", "index")
if err != nil {
t.errChan <- err
return errors.Wrap(err, "unable to create temp file for index page generation")
}
writer := bufio.NewWriter(tmpFile)
if err := t.pageTemplate.ExecuteTemplate(writer, t.pageTemplateName, data); err != nil {
t.errChan <- err
return errors.Wrap(err, "unable to execute template for index page")
}
if err := writer.Flush(); err != nil {
t.errChan <- err
return errors.Wrap(err, "unable to flush bytes to temp file")
}
if err := tmpFile.Close(); err != nil {
t.errChan <- err
return errors.Wrap(err, "unable to close temp file")
}
awscpIndexCmd[3] = tmpFile.Name()
out, err := util.RunCommand(t.homeDir, append(awscpIndexCmd, expires))
if err != nil {
t.errChan <- err
logrus.Errorf("Error calling 'aws cp' from tmp file %s to S3: %v", tmpFile.Name(), err)
logrus.Errorf("> Command: %s", strings.Join(awscpIndexCmd, " "))
logrus.Errorf("> Full output: %s", out)
}
os.Remove(tmpFile.Name())
return err
}
func (t *Today) SetOffline() {
t.offline = true
}
func (t *Today) SetOnline() error {
t.offline = false
return t.SetTodayPage()
}
// WatchDate starts a goroutine that triggers the daily update of the index page
func (t *Today) WatchDate() chan string {
dayNotifier := make(chan string)
go t.watchDate(dayNotifier)
return dayNotifier
}
func (t *Today) watchDate(notifier chan string) {
tick := time.NewTicker(15 * time.Minute)
for {
select {
case <-tick.C:
today := util.GetDateString()
if today != t.GetDate() {
t.dateStr = today
w, err := t.weatherService.GetCurrentWeather()
if err != nil {
logrus.Errorf("error retrieving sunrise/sunset for new day: %v", err)
t.errChan <- err
} else {
t.sunrise = w.Sys.Sunrise
t.sunset = w.Sys.Sunset
if err := t.SetTodayPage(); err != nil {
logrus.Errorf("unable to setup index.html for new day: %v", err)
t.errChan <- err
}
}
notifier <- t.dateStr
}
}
}
}
func (t *Today) GetHomeDirectory() string {
return t.homeDir
}