-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgobot.go
213 lines (193 loc) · 5.23 KB
/
gobot.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"log"
"net/http"
"sync"
"time"
"github.com/kelvins/sunrisesunset"
"gobot.io/x/gobot"
"gobot.io/x/gobot/api"
"gobot.io/x/gobot/drivers/gpio"
"gobot.io/x/gobot/platforms/raspi"
)
// GetRobot returns configured raspi robot - PIR sensor + relay + API command
func GetRobot(
pirPin,
relayPin string,
delay int,
longitude float64,
latitude float64,
lightOnState bool,
port string,
pirSensorOn bool,
remoteRelayIP string,
halloweenDivider int,
halloweenLoop int,
) *gobot.Master {
r := raspi.NewAdaptor()
sensor := gpio.NewPIRMotionDriver(r, pirPin)
relay := gpio.NewRelayDriver(r, relayPin)
relay.Inverted = !lightOnState
relay.Off()
mutex := sync.Mutex{}
cnt := 0
// Set sun clock
t := time.Now()
_, offset := t.Zone()
year, month, day := t.Date()
p := sunrisesunset.Parameters{
Latitude: latitude,
Longitude: longitude,
UtcOffset: float64(offset) / 3600,
Date: time.Date(year, month, day, 0, 0, 0, 0, time.UTC),
}
sunrise, sunset, err := p.GetSunriseSunset()
log.Println("Sunrise:", sunrise.Format("15:04:05"), sunrise) // Sunrise: 06:11:44
log.Println("Sunset:", sunset.Format("15:04:05"), sunset) // Sunset: 18:14:27
if err != nil {
return nil
}
// Light on and off function with mutex protection
lightSwitch := func(lightOnDuration time.Duration) {
// Protect counter
mutex.Lock()
relay.On()
cnt++
timer := time.NewTimer(lightOnDuration)
mutex.Unlock()
// Light off after configured period of time
go func() {
<-timer.C
// Protect counter
mutex.Lock()
if cnt > 0 {
cnt--
// Light off only for last call
if cnt == 0 {
relay.Off()
}
}
mutex.Unlock()
}()
}
// Function called by API command and pir sensor, checks wheather there is a dark outside
lightOn := func(duration int) {
t = time.Now()
year, month, day = t.Date()
today := time.Date(year, month, day, 0, 0, 0, 0, time.UTC)
if !p.Date.Equal(today) {
p.Date = today
_, offset = t.Zone()
p.UtcOffset = float64(offset) / 3600
sunrise, sunset, err = p.GetSunriseSunset()
}
startTime := t.Hour()*60 + t.Minute()
endTime := startTime + duration/60
sunriseTime := sunrise.Hour()*60 + sunrise.Minute()
sunsetTime := sunset.Hour()*60 + sunset.Minute()
// Light On request out of the darkness window
if err != nil || ((sunriseTime <= startTime) && (sunsetTime >= endTime)) {
return
}
// Light Off time adjustment for sunrise - endtime after sunrise
if (startTime < sunriseTime) && (sunriseTime < endTime) && (startTime != endTime) {
duration = (sunriseTime - startTime) * 60
}
// Light On time adjustment for sunset - start time before sunset
if (startTime < sunsetTime) && (sunsetTime < endTime) && (startTime != endTime) {
sunsetTimer := time.NewTimer(time.Duration(sunsetTime-startTime) * time.Minute)
go func() {
<-sunsetTimer.C
lightSwitch(time.Duration(endTime-sunsetTime) * time.Minute)
}()
return
}
lightSwitch(time.Duration(duration) * time.Second)
}
halloweenLight := func(divider int, loop int) {
go func() {
if remoteRelayIP != "" {
log.Println("Halloween child start")
postBody, _ := json.Marshal(map[string]int{
"divider": divider,
"loop": loop,
})
reqBody := bytes.NewBuffer(postBody)
resp, err := http.Post("http://"+remoteRelayIP+":"+port+"/api/robots/pilight/commands/halloween", "application/json", reqBody)
if err != nil {
log.Println(err)
} else if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
log.Println("Halloween child end")
}
}()
log.Printf("Halloween main start: %d, %d\n", divider, loop)
d := time.Duration(1) * time.Second / time.Duration(divider)
for i := 0; i < loop; i++ {
relay.Toggle()
time.Sleep(d)
relay.Toggle()
time.Sleep(d)
}
log.Println("Halloween main end")
}
work := func() {
if pirSensorOn {
sensor.On(gpio.MotionDetected, func(data interface{}) {
log.Println(gpio.MotionDetected)
lightOn(delay)
if remoteRelayIP != "" {
resp, err := http.Get("http://" + remoteRelayIP + ":" + port + "/api/robots/pilight/commands/light_on")
if err != nil {
log.Println(err)
} else if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
}
})
}
}
robot := gobot.NewRobot("pilight",
[]gobot.Connection{r},
[]gobot.Device{sensor, relay},
work,
)
robot.AddCommand("light_on",
func(params map[string]interface{}) interface{} {
duration := delay
if paramDuration, ok := params["duration"]; ok {
if val, ok := paramDuration.(float64); ok {
duration = int(val)
}
}
lightOn(duration)
return fmt.Sprintf("Light On - %d", duration)
})
robot.AddCommand("halloween",
func(params map[string]interface{}) interface{} {
divider := halloweenDivider
if paramDivider, ok := params["divider"]; ok {
if val, ok := paramDivider.(float64); ok {
divider = int(val)
}
}
loop := halloweenLoop
if paramLoop, ok := params["loop"]; ok {
if val, ok := paramLoop.(float64); ok {
loop = int(val)
}
}
halloweenLight(divider, loop)
return "Halloween"
})
mbot := gobot.NewMaster()
mbot.AddRobot(robot)
server := api.NewAPI(mbot)
server.Port = port
server.Start()
return mbot
}