-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
215 lines (200 loc) · 4.96 KB
/
main.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
// Copyright 2021 Marc-Antoine Ruel. All rights reserved.
// Use of this source code is governed under the Apache License, Version 2.0
// that can be found in the LICENSE file.
// meetin displays a meeting.
package main
import (
"context"
"encoding/json"
"errors"
"flag"
"fmt"
"io"
"log"
"os"
"os/signal"
"strings"
"time"
)
func logJSON(context string, m interface{}) {
b, _ := json.MarshalIndent(m, " ", " ")
log.Printf("%s:\n%s", context, b)
}
func printCalendars(ctx context.Context, c *Calendar) error {
s, err := c.GetCalendars(ctx)
if err != nil {
return err
}
fmt.Printf("Calendars:\n")
for _, cc := range s.Items {
fmt.Printf("- %s\n", cc.Id)
}
return nil
}
func mainImpl() error {
q := flag.Bool("q", false, "quiet")
p := flag.Int("p", 24, "number of pixels")
hahost := flag.String("hahost", "homeassistant:8123", "Home Assistant host")
cid := flag.String("cid", "primary", "Calendar ID")
path := flag.String("path", ".", "Path that contains the config")
flag.Parse()
if flag.NArg() != 0 {
return errors.New("unexpected arguments")
}
if *q {
log.SetOutput(io.Discard)
}
if *p <= 0 || *p > 1000000000 {
return errors.New("invalid number of pixels")
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ch := make(chan os.Signal, 1)
go func() {
<-ch
cancel()
}()
signal.Notify(ch, os.Interrupt)
if *path != "." {
if err := os.Chdir(*path); err != nil {
return err
}
}
b, err := os.ReadFile("api.key")
if err != nil {
return err
}
ha, err := NewHomeAssistant(*hahost, strings.TrimSpace(string(b)))
if err != nil {
return err
}
// Initialize calendar API client.
// Inspired by https://developers.google.com/calendar/api/quickstart/go
// The file token.json stores the user's access and refresh tokens, and is
// created automatically when the authorization flow completes for the first
// time.
c, err := NewCalendar(ctx, "credentials.json", "token.json")
if err != nil {
return err
}
if err = printCalendars(ctx, c); err != nil {
return err
}
return runLoop(ctx, ha, c, *cid)
}
func runLoop(ctx context.Context, ha *HomeAssistant, c *Calendar, cid string) error {
//state, err := ha.GetState("light.meetin_ring")
//if err != nil {
// return err
//}
//logJSON("Light", state)
dataOff := map[string]interface{}{
"entity_id": "light.meetin_ring",
}
data := map[string]interface{}{
"entity_id": "light.meetin_ring",
"brightness": 128,
}
if err := ha.CallService("light/turn_off", dataOff); err != nil {
return err
}
for ctx.Err() == nil {
// Round time to the next 29min55s
now := time.Now()
// The idea is that querying takes generally less than 10 seconds. But we
// want to query as much at last minute as possible in case a last minute
// meeting was added.
nextRounded := now.Round(30 * time.Minute)
if nextRounded.Before(now) {
nextRounded = nextRounded.Add(30 * time.Minute)
}
nextEarly := nextRounded.Add(-10 * time.Second)
if d := nextEarly.Sub(now); d > 0 {
log.Printf("Sleeping for %s", d.Round(time.Second))
select {
case <-ctx.Done():
break
case <-time.After(d):
}
}
if ctx.Err() != nil {
break
}
// Only take events that start on the time sharp. We want to limit the
// number of RPCs per day to not hit any quota.
events, err := c.GetEvents(ctx, nextRounded, time.Second, cid)
if err != nil {
return err
}
if len(events) == 0 {
//log.Println(" No upcoming events found.")
continue
}
// Do one last sleep to align.
now = time.Now()
if d := nextRounded.Sub(now); d > 0 {
select {
case <-ctx.Done():
break
case <-time.After(d):
}
}
if ctx.Err() != nil {
break
}
var max time.Duration
var end time.Time
log.Println("Upcoming events:")
for _, item := range events {
d := item.end.Sub(item.start)
log.Printf("- %s for %s\n", item.start.Format("2006-01-02T15:04:05"), d)
// Events for more than 1h are not supported.
if d > max && d <= time.Hour {
max = d
end = item.end
}
}
if max == 0 {
// Just got a long event, skip. Hack to wait past the :30.
time.Sleep(11 * time.Second)
continue
}
l := "30m"
if max > 30*time.Minute {
l = "60m"
}
// If the light was already on this effect, nothing will change. So turn it
// blue then on the effect.
log.Printf("Turning light for %s", l)
if err := ha.CallService("light/turn_off", dataOff); err != nil {
return err
}
data["effect"] = l
if err := ha.CallService("light/turn_on", data); err != nil {
return err
}
// Sleep until the meeting is done.
now = time.Now()
if d := end.Sub(now) - 10*time.Second; d > 0 {
select {
case <-ctx.Done():
break
case <-time.After(d):
}
}
if ctx.Err() != nil {
break
}
}
// When the context is canceled, turn the light blue.
if err := ha.CallService("light/turn_off", dataOff); err != nil {
return err
}
return nil
}
func main() {
if err := mainImpl(); err != nil {
fmt.Fprintf(os.Stderr, "meetin: %s\n", err)
os.Exit(1)
}
}