-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathserver.py
executable file
·499 lines (406 loc) · 15.9 KB
/
server.py
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
#!/usr/bin/env python
"""The Server Which Runs The Cloud
This is the python server that sits in between TouchOSC and the cloud hardware.
@author: Ed, Samson, April
Please use logging in key="value" format for statistics and debugging. Ed wants
to Splunk the cloud.
"""
import cPickle
import glob
import logging
import math
import os
import platform
import socket
import subprocess
import sys
import time
import struct
import pygame
import effects
import liblo
def OnPi():
uname_m = subprocess.check_output('uname -m', shell=True).strip()
# Assume that an ARM processor means we're on the Pi
return uname_m == 'armv6l'
# XXX: Hardcoding this for now. Using gethostbyname doesn't
# necessarily work, since we need to be specific about the
# network interface in question. Loopback broadcast won't
# work, for example.
BROADCAST_IP = '10.10.10.255'
CONSOLE_LOG_LEVEL = logging.INFO
FILE_LOG_LEVEL = logging.INFO
LOG_FILE = 'amcpserver.log'
MEDIA_DIRECTORY = 'media'
# Pins - Which GPIO pins correspond to what?
RAIN_PIN = 16 # = GPIO 23
MIST_PIN = 18 # = GPIO 24
SPARE_PIN = 22 # = GPIO 25
PUMP_PIN = 15 # GPIO 22
# Sound
RAIN_FILENAME = 'rain.wav'
# Setup all our logging. Timestamps will be in localtime.
# TODO(ed): Figure out how to get the timezone offset in the log, or use UTC
logger = logging.getLogger('amcpserver')
logger.setLevel(logging.DEBUG)
fh = logging.FileHandler('amcpserver.log')
fh.setLevel(FILE_LOG_LEVEL)
ch = logging.StreamHandler()
ch.setLevel(CONSOLE_LOG_LEVEL)
formatter = logging.Formatter(
'%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh.setFormatter(formatter)
ch.setFormatter(formatter)
logger.addHandler(fh)
logger.addHandler(ch)
class AMCPServer(liblo.Server):
def __init__(self, port, client_ip, client_port):
liblo.Server.__init__(self, port)
self.client = liblo.Address(client_ip, client_port)
logger.info('action="init_server", port="%s", client_port="%s"',
port, client_port)
# Allow broadcast to clients
self.socket = socket.fromfd(self.fileno(), socket.AF_INET, socket.SOCK_DGRAM)
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, True)
self.sound_effects = SoundEffects()
self.water = Water()
self.light = Lighting()
self.systems = {
'sound': {
'sync': self.sound_effects.sync,
'volume': self.sound_effects.volume,
'thunder': self.sound_effects.thunder,
'rain_volume': self.sound_effects.rain_volume,
'its_raining_men': self.sound_effects.its_raining_men,
'silence': self.sound_effects.silence
},
'light': {
'sync': self.light.sync,
'lightning': self.light.strobe,
'cloud_z': self.light.cloud_z,
'cloud_xy': self.light.cloud_xy,
},
'light2': {
'sync': self.light.sync,
'brightness': self.light.brightness,
'contrast': self.light.contrast,
'detail': self.light.detail,
'color_top': self.light.color_top,
'color_bottom': self.light.color_bottom,
'turbulence': self.light.turbulence,
'speed': self.light.speed,
'heading': self.light.heading,
'rotation': self.light.rotation,
},
'light3': {
'loadsave': self.light.loadsave,
},
'smb': {
'sync': self.sound_effects.sync,
'smb_effects': self.sound_effects.smb_sounds,
},
'water': {
'sync': self.water.sync,
'rain': self.water.rain,
'mist': self.water.mist,
'spare': self.water.spare,
'pump': self.water.pump,
'all_rain_off': self.water.all_rain_off,
}
}
@liblo.make_method(None, None)
def catch_all(self, path, args):
p = path.split("/")
system = p[1]
try:
action = p[2]
except IndexError: # No action, must be a page change
logger.debug('action="active_page", page="%s"' % system)
# Pages aren't strictly delineated by subsystem, and we don't have much data
# to send. Sync everything.
return self.sync_systems()
if system == 'smb' or system == 'light3':
x = p[3]
y = p[4]
self.systems[system][action](x=x, y=y, press=args[0])
try:
self.systems[system][action](*args)
except KeyError:
logger.error(
'action="catch_all", path="%s", error="not found" args="%s", '
'system="%s", action=%s'
% (path, args, system, action))
if system == 'water':
# Be extra vigilant in keeping the water state sync'ed-
# We should get a positive ACK from the server every time something
# changes. (Users can see the RX light blink as a confirmation).
# This also takes care of the 'all rain off' state.
self.systems[system]['sync'](self.client)
def sync_systems(self):
for sys in self.systems:
try:
self.systems[sys]['sync'](self.client)
except KeyError:
logger.warn('action="sync_systems", system="%s", '
'error="no sync method defined', sys)
def mainLoop(self):
while True:
# Drain all pending messages without blocking
while self.recv(0):
pass
# Frame rate limiting and rendering
server.light.controller.runFrame()
class Water():
"""Controls rain, mist, etc"""
def __init__(self):
self.system = 'water'
self.pi = PiGPIO()
self.toggles = {
'rain': 0.0,
'mist': 0.0,
'spare': 0.0,
'pump': 0.0,
}
def sync(self, client):
logger.debug(
'system="%s", action="sync", client=%r, toggles=%s',
self.system, client, self.toggles)
for t in self.toggles:
liblo.send(client, ("/%s/%s" % (self.system, t)), self.toggles[t])
def toggle_state(self, action, pin, toggle):
self.pi.send(pin, toggle and 1 or 0)
def rain(self, toggle):
self.toggle_state('rain', RAIN_PIN, toggle)
self.toggles['rain'] = toggle
def mist(self, toggle):
self.toggle_state('mist', MIST_PIN, toggle)
self.toggles['mist'] = toggle
def spare(self, toggle):
self.toggle_state('spare', SPARE_PIN, toggle)
self.toggles['spare'] = toggle
def pump(self, toggle):
self.toggle_state('pump', PUMP_PIN, toggle)
self.toggles['pump'] = toggle
def all_rain_off(self, press):
if press:
self.rain(False)
self.mist(False)
self.spare(False)
self.pump(False)
class Lighting():
"""High-level interface to the lighting effects subsystem.
Rendering is handled by effects.LightController().
"""
lightningProbabilityScale = 0.4
brightnessScale = 1.5
contrastScale = 10.0
detailScale = 3.0
turbulenceScale = 0.4
windSpeedScale = 0.8
def __init__(self):
self.system = 'light'
self.controller = effects.LightController()
self.lightningProbability = 0
def sync(self, client):
logger.debug('system="%s", action="sync", client="%r"',
self.system, client)
liblo.send(client, liblo.Bundle(
# Gross, this needs refactoring...
liblo.Message("/light/cloud_z", self.controller.params.lightning_new / self.lightningProbabilityScale),
liblo.Message("/light2/brightness", self.controller.params.brightness / self.brightnessScale),
liblo.Message("/light2/contrast", self.controller.params.contrast / self.contrastScale),
liblo.Message("/light2/detail", self.controller.params.detail / self.detailScale),
liblo.Message("/light2/color_top", self.controller.params.color_top),
liblo.Message("/light2/color_bottom", self.controller.params.color_bottom),
liblo.Message("/light2/turbulence", self.controller.params.turbulence / self.turbulenceScale),
liblo.Message("/light2/speed", self.controller.params.wind_speed / self.windSpeedScale),
# XXX: This doesn't work- TouchOSC seems to spam these events
# at both XY pads for some reason. Bug in TouchOSC? Using
# liblo incorrectly?
#
# liblo.Message("/light2/heading",
# -math.cos(self.controller.params.wind_heading),
# math.sin(self.controller.params.wind_heading)),
#
# liblo.Message("/light2/rotation",
# math.sin(-self.controller.params.rotation),
# -math.cos(-self.controller.params.rotation))
))
def strobe(self, press):
""" Light up cloud for as long as button is held. """
if press:
self.controller.params.lightning_new = 1.0
else:
self.controller.params.lightning_new = self.lightningProbability
def cloud_xy(self, x, y):
""" Light up cloud at given XY coordinate. """
self.controller.makeLightningBolt(x, -y)
def cloud_z(self, z):
""" Change the new lighting percentage value.
Wants to be non-linear curve, but this will suffice for now.
"""
self.lightningProbability = z * self.lightningProbabilityScale
self.controller.params.lightning_new = self.lightningProbability
def brightness(self, bright):
self.controller.params.brightness = bright * self.brightnessScale
def contrast(self, contrast):
self.controller.params.contrast = contrast * self.contrastScale
def detail(self, detail):
self.controller.params.detail = detail * self.detailScale
def color_top(self, color_top):
self.controller.params.color_top = color_top
def color_bottom(self, color_bottom):
self.controller.params.color_bottom = color_bottom
def turbulence(self, turbulence):
self.controller.params.turbulence = turbulence * self.turbulenceScale
def speed(self, speed):
self.controller.params.wind_speed = speed * self.windSpeedScale
def heading(self, x, y):
self.controller.params.wind_heading = math.atan2(y, -x)
def rotation(self, x, y):
self.controller.params.rotation = -math.atan2(x, -y)
def loadsave(self, x=None, y=None, press=None):
if press:
if int(float(x)) % 2 == 0:
self.save(int(float(y)))
else:
self.load(int(float(y)))
def save(self, slot):
logger.info('Saving to slot %d', slot)
cPickle.dump(self.controller.params,
open('/home/pi/presets/preset%d.pickle' % (slot,), 'w'))
def load(self, slot):
logger.info('Loading from slot %d', slot)
self.controller.params = cPickle.load(
open('/home/pi/presets/preset%d.pickle' % (slot,), 'r'))
class SoundEffects():
"""Play different sound effects.
Probably want to index these sounds somehow? Config file?"""
def __init__(self):
self.system = 'sound'
#self.smb_sound_list = os.listdir(os.path.join(MEDIA_DIRECTORY, 'smb'))
self.smb_sound_list = glob.glob(
os.path.join(MEDIA_DIRECTORY, 'smb', 'smb*'))
self.so = SoundOut()
self.so.initRain(os.path.join(MEDIA_DIRECTORY, RAIN_FILENAME))
self.values = {}
self.volume(0.3)
self.rain_volume(0)
def sync(self, client):
logger.debug('system="%s", action="sync", client="%r"',
self.system, client)
for t in self.values:
liblo.send(client, ("/%s/%s" % (self.system, t)), self.values[t])
def rain_volume(self, volume):
self.values['rain_volume'] = volume
self.so.setRainVolume(volume)
def volume(self, volume):
self.values['volume'] = volume
self.so.setVolume(volume)
def press_play(self, sound_file):
self.so.play(sound_file)
def silence(self, press):
if press:
self.so.stop()
def thunder(self, press):
sound_file = os.path.join(MEDIA_DIRECTORY, 'thunder_hd.wav')
if press:
self.press_play(sound_file)
def its_raining_men(self, press):
sound_file = os.path.join(MEDIA_DIRECTORY, 'its_raining_men.wav')
if press:
self.press_play(sound_file)
def smb_sounds(self, x=None, y=None, press=None):
if press:
id = int(y) * 5 + int(x)
sound_file = self.smb_sound_list[id]
self.press_play(sound_file)
class SoundOut():
"""mplayer to RPi audio out"""
def __init__(self, defaultVolume=20):
# Set volume to 0db gain. Airplay and sfx both have their own
# separate volume controls, but the system mixer should be neutral.
if OnPi():
print "Init mixer"
os.system("amixer sset PCM 0")
self.sounds = []
pygame.mixer.init(44100)
self.setVolume(defaultVolume)
def initRain(self, rain_filename):
self.rain = pygame.mixer.Sound(rain_filename)
self.rain_channel = self.rain.play(loops=-1, fade_ms=2000)
def setRainVolume(self, volume):
self.rain.set_volume(volume)
def setVolume(self, volume):
self.volume = volume
self.prune_sounds()
for (s, ch) in self.sounds:
s.set_volume(volume)
def play(self, soundfile):
logger.debug('action="play", soundfile="%s"' % soundfile)
s = pygame.mixer.Sound(soundfile)
ch = s.play()
s.set_volume(self.volume)
self.sounds.append((s, ch))
return (s, ch)
def stop(self):
self.prune_sounds()
for (s, ch) in self.sounds:
s.fadeout(2000)
def prune_sounds(self):
for idx, (s, ch) in enumerate(self.sounds[:]):
if not ch.get_busy():
self.sounds.remove((s,ch))
class PiGPIO():
"""Controls water (pumps and valves)"""
def __init__(self):
if OnPi():
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setup(RAIN_PIN, GPIO.OUT)
GPIO.setup(MIST_PIN, GPIO.OUT)
GPIO.setup(SPARE_PIN, GPIO.OUT)
GPIO.setup(PUMP_PIN, GPIO.OUT)
self.output = GPIO.output
else:
def fake_gpio(pin, value):
print "SETTING GPIO PIN %s TO %d" % (pin, value)
self.output = fake_gpio
def send(self, pin_num, value):
"""Send value (1/0) to pin_num"""
logger.debug('action="send_rpi_gpio", pin_number="%i", value="%i"'
% (pin_num, value))
self.output(pin_num, value)
if __name__ == "__main__":
try:
server = AMCPServer(port=8000, client_ip=BROADCAST_IP, client_port=9000)
except liblo.ServerError, err:
print str(err)
sys.exit()
if platform.system() == "Darwin":
service = None
else:
# Avahi announce so it's findable on the controller by name
from avahi_announce import ZeroconfService
service = ZeroconfService(
name="AMCP TouchOSC Server", port=8000, stype="_osc._udp")
service.publish()
# Main thread runs both our LED effects and our OSC server,
# draining all queued OSC events between frames. Runs until killed.
try:
server.mainLoop()
except KeyboardInterrupt:
# Cleanup
if service:
service.unpublish()
if OnPi():
import RPi.GPIO as GPIO
GPIO.cleanup()
finally:
logger.info('action="server_shutdown"')
# Cleanup
if service:
service.unpublish()
if OnPi():
import RPi.GPIO as GPIO
GPIO.cleanup()