-
Notifications
You must be signed in to change notification settings - Fork 1
/
cooler.go
72 lines (63 loc) · 1.16 KB
/
cooler.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
package gogadgets
import "time"
type Cooler struct {
target float64
status bool
gpio OutputDevice
lastChange *time.Time
}
func NewCooler(pin *Pin) (OutputDevice, error) {
var c *Cooler
var err error
g, err := newGPIO(pin)
if err == nil {
c = &Cooler{
gpio: g,
target: 0.0,
}
}
return c, err
}
func (c *Cooler) Commands(location, name string) *Commands {
return nil
}
func (c *Cooler) Update(msg *Message) bool {
now := time.Now()
if c.lastChange != nil && now.Sub(*c.lastChange) < 120*time.Second {
return false
}
var changed bool
temperature, ok := msg.Value.Value.(float64)
if ok && c.status {
changed = true
if temperature <= c.target {
c.gpio.Off()
c.lastChange = &now
} else if temperature > c.target {
c.gpio.On(nil)
c.lastChange = &now
}
}
return changed
}
func (c *Cooler) On(val *Value) error {
if val != nil {
target, ok := val.Value.(float64)
if ok {
c.target = target
}
}
c.status = true
c.gpio.On(nil)
return nil
}
func (c *Cooler) Off() error {
if c.status {
c.status = false
c.gpio.Off()
}
return nil
}
func (c *Cooler) Status() map[string]bool {
return c.gpio.Status()
}