-
Notifications
You must be signed in to change notification settings - Fork 0
/
relays.go
230 lines (202 loc) · 5.48 KB
/
relays.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package main
import (
"fmt"
"sync"
"time"
"github.com/brutella/hc/accessory"
)
// Relay controls the behavior of a particular relay in the system.
type Relay struct {
name string
pin PiPin
startTime time.Time
stopTime time.Time
accessory *accessory.Switch
enabled bool
}
// SolarValve controls two relays at the same time.
// Only one can be engaged at any one time, and will shut off after a given timeout
type SolarValve struct {
fwdRelay *Relay
revRelay *Relay
status bool // true==ON
statusLED PiPin
accessory *accessory.Switch
mtx sync.Mutex
timeout time.Duration
cid int // command id, distinguishes the calls to cleanup
}
// AccessoryInfo tells Apple HomeKit about the device
func AccessoryInfo(name string, manufacturer string) accessory.Info {
info := accessory.Info{Name: name, Manufacturer: manufacturer}
return info
}
func timeStr(t time.Time) string {
return fmt.Sprintf("%02d:%02d:%02d.%09d",
t.Hour(), t.Minute(), t.Second(), t.Nanosecond())
}
// NewRelay creates a relay for a given GPIO
func NewRelay(pin uint8, name string, manufacturer string) *Relay {
return newRelay(NewGpio(pin), name, manufacturer)
}
func newRelay(pin PiPin, name string, manufacturer string) *Relay {
relay := Relay{
name: name,
pin: pin,
startTime: time.Now(),
stopTime: time.Now(),
enabled: true,
}
if name != "" {
relay.accessory = accessory.NewSwitch(AccessoryInfo(name, manufacturer))
}
relay.TurnOff()
return &relay
}
// Accessory returns the Apple HomeKit accessory associated with the relay
func (r *Relay) Accessory() *accessory.Accessory {
if r == nil || r.accessory == nil {
return nil
}
return r.accessory.Accessory
}
// Name returns the name of the Relay
func (r *Relay) Name() string {
if r == nil {
return ""
}
return r.name
}
// String returns the state of the Relay
func (r *Relay) String() string {
if r == nil {
return ""
}
return fmt.Sprintf(
"Relay: { Name: %s, Pin: %v, StartTime: %s, StopTime: %s, Accessory: %v}",
r.Name(), r.pin, timeStr(r.startTime), timeStr(r.stopTime), r.accessory)
}
// TurnOn flips the output to HIGH voltage (>1V)
func (r *Relay) TurnOn() {
Trace("TurnOn %s", r.name)
r.pin.Output(High)
r.startTime = time.Now()
if r.accessory != nil {
r.accessory.Switch.On.SetValue(true)
}
}
// TurnOff flips the output to LOW voltage (<1V)
func (r *Relay) TurnOff() {
Trace("TurnOff %s", r.name)
r.pin.Output(Low)
r.stopTime = time.Now()
if r.accessory != nil {
r.accessory.Switch.On.SetValue(false)
}
}
func (r *Relay) isOn() bool {
if r.pin.Read() == High {
if r.accessory != nil {
r.accessory.Switch.On.SetValue(true)
}
return true
}
if r.accessory != nil {
r.accessory.Switch.On.SetValue(false)
}
return false
}
// Status returns "On" if at HIGH voltage or "Off" if at LOW voltage
func (r *Relay) Status() string {
if r.pin.Read() == High {
return "On"
}
return "Off"
}
// GetStartTime returns the time the relay was last set to HIGH voltage
func (r *Relay) GetStartTime() time.Time {
return r.startTime
}
// GetStopTime returns the time the relay was last set to LOW voltage
func (r *Relay) GetStopTime() time.Time {
return r.stopTime
}
// NewSolarValve creates a special controller for the Solar Valve operation
// When set to ON, it runs the motor forward for 15 seconds
// When set to OFF, it runs the motor in reverse for 15 seconds
func NewSolarValve(forward uint8, reverse uint8, ledPin uint8, name string, manufacturer string, timeout time.Duration) *SolarValve {
r := &SolarValve{
fwdRelay: newRelay(NewGpio(forward), "", ""),
revRelay: newRelay(NewGpio(reverse), "", ""),
statusLED: NewGpio(ledPin),
accessory: accessory.NewSwitch(AccessoryInfo(name, manufacturer)),
timeout: timeout,
}
r.TurnOff()
return r
}
// String returns a string that describes the current state of the Solar Valve
func (s *SolarValve) String() string {
return fmt.Sprintf("Forward: %s, Reverse: %s, Status: %s", s.fwdRelay.String(), s.revRelay.String(), s.Status())
}
func (s *SolarValve) cleanup() {
cid := s.cid // capture the cid before sleep
time.Sleep(s.timeout)
if s.cid == cid {
// there isn't another one running
s.fwdRelay.TurnOff()
s.revRelay.TurnOff()
}
}
func (s *SolarValve) setState(fwd bool) {
s.mtx.Lock()
defer s.mtx.Unlock()
s.cid++
go s.cleanup()
s.statusLED.Output(GpioState(fwd))
if fwd {
s.revRelay.TurnOff()
time.Sleep(time.Millisecond)
s.fwdRelay.TurnOn()
} else {
s.fwdRelay.TurnOff()
time.Sleep(time.Millisecond)
s.revRelay.TurnOn()
}
s.status = fwd
}
// TurnOn runs the motor for the valve forward for timeout seconds
func (s *SolarValve) TurnOn() {
s.setState(true)
}
// TurnOff runs the motor for the valve in reverse for timeout seconds
func (s *SolarValve) TurnOff() {
s.setState(false)
}
// Status returns "On" if at HIGH voltage or "Off" if at LOW voltage
func (s *SolarValve) Status() string {
if s.status {
return "On"
}
return "Off"
}
// Accessory returns the accessory of the SolarValve
func (s *SolarValve) Accessory() *accessory.Accessory {
return s.accessory.Accessory
}
func (s *SolarValve) isOn() bool {
if s.status {
s.accessory.Switch.On.SetValue(true)
} else {
s.accessory.Switch.On.SetValue(false)
}
return s.status
}
// GetStartTime returns the time the relay was last set to HIGH voltage
func (s *SolarValve) GetStartTime() time.Time {
return s.fwdRelay.startTime
}
// GetStopTime returns the time the relay was last set to LOW voltage
func (s *SolarValve) GetStopTime() time.Time {
return s.revRelay.stopTime
}