-
Notifications
You must be signed in to change notification settings - Fork 0
/
thermostat.go
124 lines (110 loc) · 2.32 KB
/
thermostat.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
package example
type (
Gauge interface {
CurrentTemperature() int // Current ambient temperature rounded to the nearest degree (Fahrenheit).
}
HVAC interface {
SetBlower(state bool) // Turns the blower on or off.
SetCooler(state bool) // Turns the cooler on or off.
SetHeater(state bool) // Turns the heater on or off.
IsBlowing() bool // Is the blower currently on or off?
IsCooling() bool // Is the cooler currently on or off?
IsHeating() bool // Is the heater currently on or off?
}
)
type Thermostat struct {
hvac HVAC
gauge Gauge
blowerDelay int
coolerDelay int
}
func NewThermostat(hvac HVAC, gauge Gauge) *Thermostat {
hvac.SetBlower(false)
hvac.SetCooler(false)
hvac.SetHeater(false)
return &Thermostat{
hvac: hvac,
gauge: gauge,
}
}
func (this *Thermostat) Regulate() {
this.decrementDelays()
this.regulate()
}
func (this *Thermostat) decrementDelays() {
if this.blowerDelay > 0 {
this.blowerDelay--
}
if this.coolerDelay > 0 {
this.coolerDelay--
}
}
func (this *Thermostat) regulate() {
temperature := this.gauge.CurrentTemperature()
status := this.inferStatus(temperature)
switch status {
case tooCold:
this.heat()
case tooHot:
this.cool()
default:
this.idle()
}
}
type status int
const (
comfy status = iota
tooHot
tooCold
)
func (this *Thermostat) inferStatus(temperature int) status {
if temperature < 65 {
return tooCold
} else if temperature > 75 {
return tooHot
} else {
return comfy
}
}
func (this *Thermostat) heat() {
this.engageBlower()
this.disengageCooler()
this.engageHeater()
}
func (this *Thermostat) cool() {
this.engageBlower()
this.engageCooler()
this.disengageHeater()
}
func (this *Thermostat) idle() {
this.disengageBlower()
this.disengageCooler()
this.disengageHeater()
}
func (this *Thermostat) disengageBlower() {
if this.blowerDelay > 0 {
return
}
this.hvac.SetBlower(false)
}
func (this *Thermostat) disengageCooler() {
if this.hvac.IsCooling() {
this.coolerDelay = 3
}
this.hvac.SetCooler(false)
}
func (this *Thermostat) disengageHeater() {
this.hvac.SetHeater(false)
}
func (this *Thermostat) engageBlower() {
this.hvac.SetBlower(true)
}
func (this *Thermostat) engageCooler() {
if this.coolerDelay == 0 {
this.hvac.SetCooler(true)
}
}
func (this *Thermostat) engageHeater() {
this.blowerDelay = 5 + 1
this.hvac.SetHeater(true)
}