/
SoundDetectionManager.groovy
267 lines (199 loc) · 9.01 KB
/
SoundDetectionManager.groovy
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package global.zombieinvasion.halloween
import groovy.util.logging.Log4j
import akka.actor.Cancellable
import global.zombieinvasion.BaseActor
import global.zombieinvasion.halloween.command.*
import global.zombieinvasion.halloween.event.*
import grails.util.Holders
import scala.concurrent.duration.Duration
import java.util.concurrent.TimeUnit
import static grails.async.Promises.task
@Log4j
class SoundDetectionManager extends BaseActor {
private Cancellable soundNotDetectedTimer = null
boolean soundNotDetectedTimerRunning = false
private Status lightOne
private Status lightTwo
private Status lightThree
private soundDetections = []
Integer soundDetectionsLastSum = 0
def akkaService = Holders.applicationContext.getBean("akkaService")
private Integer detectionBufferSize = Holders.config.app.automation.arduino.sound.detectionBuffer.size
public SoundDetectionManager(){
}
@Override
void onReceive(Object message) throws Exception{
if (message instanceof AudioDetection) {
// turn lights off if no sound detected
if (!soundNotDetectedTimerRunning)
startSoundNotDetectedTimer()
Integer val = message?.value?.toInteger()
/**
* map audio sensor values to phillips hue bridge values
*/
if (val<=Holders.config.app.automation.arduino.sound.detectionAvgDeviation.none) // no sound... 3 or less accounts for the sound of the projector fan: mic mounted here...
val = 0
else if(val<=Holders.config.app.automation.arduino.sound.detectionAvgDeviation.lite) // very light sound
val = 1
else if (val>1 && val<100)
val = val
else // lots of sound
val = 100
soundDetections.add(val)
def sum
def avg
if (soundDetections?.size()>=detectionBufferSize+1) {
sum = soundDetections?.sum()
avg = (soundDetections?.sum()/detectionBufferSize)
log.debug "==================================================="
log.debug "Sound(sum: $sum | avg: $avg)"
log.debug "==================================================="
soundDetections.clear()
soundDetectionsLastSum = sum
// set the usable value to the average of last 10 (250 milliseconds) of sound detection
val = avg
} else {
log.debug "${soundDetections.toListString()}"
// return // do not bother continuing this sub-routine if sound detection buffer isn't full
}
if (avg<1) { // sound is not detected so turn off the lights if they are on
// no audio detected turn off the lights 1 & 2 & 3
if (lightOne == Status.ON || lightOne == null) {
lightOne = Status.OFF
akkaService.deviceUriDispatcher.tell(Light1Off.newInstance(), self)
}
if (lightTwo == Status.ON || lightTwo == null) {
lightTwo = Status.OFF
akkaService.deviceUriDispatcher.tell(Light2Off.newInstance(), self)
}
if (lightThree == Status.ON || lightThree == null) {
lightThree = Status.OFF
akkaService.deviceUriDispatcher.tell(Light3Off.newInstance(), self)
}
} else { // sound is detected cancel the sound not detected timer and turn the lights on if they ar on
if (sum && avg) {
cancelSoundNotDetectedTimer()
// if the lights are off turn them on
if (lightOne == Status.OFF) {
lightOne = Status.ON
akkaService.deviceUriDispatcher.tell(Light1On.newInstance(), self)
akkaService.deviceUriDispatcher.tell(Light1Level.newInstance([value:val]), self)
}
if (lightTwo == Status.OFF) {
lightTwo = Status.ON
akkaService.deviceUriDispatcher.tell(Light2On.newInstance(), self)
akkaService.deviceUriDispatcher.tell(Light2Level.newInstance([value:val]), self)
}
if (lightThree == Status.OFF) {
lightThree = Status.ON
akkaService.deviceUriDispatcher.tell(Light3On.newInstance(), self)
akkaService.deviceUriDispatcher.tell(Light3Level.newInstance([value:val]), self)
}
getColorCommands(val,sum).each{
akkaService.deviceUriDispatcher.tell(it, self)
}
def shouldSmokeVal = Holders.config.app.automation.arduino.sound.detectionAvgDeviation.upper*2
if (sum>=shouldSmokeVal){
context?.sender()?.tell("START_SMOKE_TIMER", self)
akkaService.deviceUriDispatcher.tell(StrobeOn.newInstance(), self)
}
}
}
}
}
/**
* Maps a average of last 10 sound values to a color for lights 1 & 2
* @param val
* @return Command
*/
def getColorCommands(Integer val, Integer sum = 0){
def res = []
if (val == 0) {
// do nothing
}
else if (val <= 5) {
if (lightOne == Status.ON)
res.add(Light1ColorGreen.newInstance())
if (lightTwo == Status.ON)
res.add(Light2ColorGreen.newInstance())
if (lightThree == Status.ON)
res.add(Light3ColorGreen.newInstance())
}
else if (val <= 10) {
if (lightOne == Status.ON)
res.add(Light1ColorBlue.newInstance())
if (lightTwo == Status.ON)
res.add(Light2ColorBlue.newInstance())
if (lightThree == Status.ON)
res.add(Light3ColorBlue.newInstance())
}
else if (val <= 25) {
if (lightOne == Status.ON)
res.add(Light1ColorPurple.newInstance())
if (lightTwo == Status.ON)
res.add(Light2ColorPurple.newInstance())
if (lightThree == Status.ON)
res.add(Light3ColorPurple.newInstance())
}
else if (val <= 50) {
if (lightOne == Status.ON)
res.add(Light1ColorOrange.newInstance())
if (lightTwo == Status.ON)
res.add(Light2ColorPurple.newInstance())
if (lightThree == Status.ON) {
res.add(Light3ColorPurple.newInstance())
}
}
else if (val <= 75) {
if (lightOne == Status.ON)
res.add(Light1ColorPurple.newInstance())
if (lightTwo == Status.ON)
res.add(Light2ColorOrange.newInstance())
if (lightThree == Status.ON) {
res.add(Light3ColorPink.newInstance())
}
}
else {
if (lightOne == Status.ON)
res.add(Light1ColorPurple.newInstance())
if (lightTwo == Status.ON)
res.add(Light2ColorPurple.newInstance())
if (lightThree == Status.ON)
res.add(Light3ColorPurple.newInstance())
if (lightOne == Status.ON)
res.add(Light1ColorOrange.newInstance())
if (lightTwo == Status.ON)
res.add(Light2ColorOrange.newInstance())
if (lightThree == Status.ON)
res.add(Light3ColorOrange.newInstance())
if (lightOne == Status.ON)
res.add(Light1ColorRed.newInstance())
if (lightTwo == Status.ON)
res.add(Light2ColorRed.newInstance())
if (lightThree == Status.ON)
res.add(Light3ColorRed.newInstance())
}
return res
}
public void cancelSoundNotDetectedTimer(){
soundNotDetectedTimer?.cancel()
soundNotDetectedTimerRunning = false
}
public void startSoundNotDetectedTimer(){
soundNotDetectedTimerRunning = true
soundNotDetectedTimer = akkaService.system.scheduler().scheduleOnce(Duration.create(3, TimeUnit.SECONDS),
new Runnable() {
@Override
public void run() {
if (lightOne == Status.ON)
akkaService.deviceUriDispatcher.tell(Light1Off.newInstance(), self)
if (lightTwo == Status.ON)
akkaService.deviceUriDispatcher.tell(Light2Off.newInstance(), self)
if (lightThree == Status.ON)
akkaService.deviceUriDispatcher.tell(Light3Off.newInstance(), self)
soundNotDetectedTimerRunning = false
}
}, akkaService.system.dispatcher()
)
}
}