/
fancoil_schedule.go
176 lines (156 loc) · 4.08 KB
/
fancoil_schedule.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
package main
import (
"context"
"flag"
"fmt"
"sort"
"time"
"github.com/golang/glog"
"github.com/gonzojive/heatpump/proto/fancoil"
"github.com/martinlindhe/unit"
rrule "github.com/teambition/rrule-go"
"golang.org/x/sync/errgroup"
"google.golang.org/grpc"
)
var (
fancoilServiceAddress = flag.String("fancoil-service", "localhost:8083", "Remote address of fancoil service.")
)
var localTimezone = func() *time.Location {
loc, err := time.LoadLocation("America/Los_Angeles")
if err != nil {
panic(err)
}
return loc
}()
type recurringCommands struct {
name string
recurrenceRule *rrule.RRule
commands []*fancoil.SetStateRequest
}
var (
daytimeTemp = &fancoil.Temperature{DegreesCelcius: float32(unit.FromFahrenheit(68).Celsius())}
nightimeTemp = &fancoil.Temperature{DegreesCelcius: float32(unit.FromFahrenheit(61).Celsius())}
)
const (
startYear = 2022
startMonth = time.January
startDayOfMonth = 1
)
var allRecurringCommands = []*recurringCommands{
{
name: "Set fan speed to low in the evenings for ease of conversation",
recurrenceRule: mustNewRRule(rrule.ROption{
Freq: rrule.DAILY,
Count: 4000,
Dtstart: time.Date(startYear, startMonth, startDayOfMonth, 17, 15, 0, 0, localTimezone),
}),
commands: []*fancoil.SetStateRequest{
{
PreferenceFanSetting: fancoil.FanSetting_FAN_SETTING_LOW,
},
},
},
{
name: "At bedtime, set target temp to 61F and fan speed to low",
recurrenceRule: mustNewRRule(rrule.ROption{
Freq: rrule.DAILY,
Count: 4000,
Dtstart: time.Date(startYear, startMonth, startDayOfMonth, 19, 30, 0, 0, localTimezone),
}),
commands: []*fancoil.SetStateRequest{
{
PreferenceFanSetting: fancoil.FanSetting_FAN_SETTING_LOW,
HeatingTargetTemperature: nightimeTemp,
},
},
},
{
name: "At 5am, start heating up the room at low fan speed",
recurrenceRule: mustNewRRule(rrule.ROption{
Freq: rrule.DAILY,
Count: 4000,
Dtstart: time.Date(startYear, startMonth, startDayOfMonth, 5, 0, 0, 0, localTimezone),
}),
commands: []*fancoil.SetStateRequest{
{
HeatingTargetTemperature: daytimeTemp,
},
},
},
{
name: "At 7:15am, increase the fan speed",
recurrenceRule: mustNewRRule(rrule.ROption{
Freq: rrule.DAILY,
Count: 4000,
Dtstart: time.Date(startYear, startMonth, startDayOfMonth, 7, 15, 0, 0, localTimezone),
}),
commands: []*fancoil.SetStateRequest{
{
PreferenceFanSetting: fancoil.FanSetting_FAN_SETTING_MEDIUM,
},
},
},
}
func main() {
flag.Parse()
if err := run(context.Background()); err != nil {
glog.Exitf("%v", err)
}
}
func run(ctx context.Context) error {
client, err := dial(ctx)
if err != nil {
return err
}
eg, ctx := errgroup.WithContext(ctx)
{
executionStart := time.Now()
sort.Slice(allRecurringCommands, func(i, j int) bool {
a := allRecurringCommands[i].recurrenceRule.After(executionStart, true)
b := allRecurringCommands[j].recurrenceRule.After(executionStart, true)
return a.Before(b)
})
}
for _, scheduleItem := range allRecurringCommands {
scheduleItem := scheduleItem
eg.Go(func() error {
for {
now := time.Now()
next := scheduleItem.recurrenceRule.After(now, true)
if next.IsZero() {
return nil
}
glog.Infof("executing next %q command in %s", scheduleItem.name, next.Sub(now))
time.Sleep(next.Sub(now))
for _, cmd := range scheduleItem.commands {
_, err := client.SetState(ctx, cmd)
if err != nil {
glog.Errorf("error setting fan coil state: %v", err)
}
}
}
})
}
if err := eg.Wait(); err != nil {
return err
}
return nil
}
func dial(ctx context.Context) (fancoil.FanCoilServiceClient, error) {
conn, err := grpc.DialContext(ctx, *fancoilServiceAddress, grpc.WithInsecure())
if err != nil {
return nil, fmt.Errorf("failed to connect to FanCoilService: %w", err)
}
return fancoil.NewFanCoilServiceClient(conn), nil
}
func mustNewRRule(arg rrule.ROption) *rrule.RRule {
value, err := rrule.NewRRule(arg)
if err != nil {
panic(err)
}
return value
}
type RRuleTimer struct {
rrule *rrule.RRule
timer *time.Timer
}