Stap 9: Code
Hier is de code. U zal moeten installeren pyomxplayer, pexpect en pyOSC. Ook, afhankelijk van uw versie van omxplayer, u wellicht wijzigen pyomxplayer vanwege de manier waarop die het probeert omxplayer de uitvoer parseren. Dit script werkt voor zowel de master en de slave op basis van de hostnaam. Hier is het basisproces:
- Wanneer alle machines opwaarts schoen, zij de relevante media kopiëren naar het ram-station en start het in omxplayer onderbroken
- De slaven dan in een lus van de opstarten een "ready" adres verzenden aan de kapitein totdat ze horen gaan terug.
- De meester gaat in een lus om te controleren of beide slaven klaar, luisteren naar een "ready" adres van elk.
- Wanneer de kapitein bepaalt zowel slaven staan klaar, het stuurt een adres "luisteren" naar de slaven en de slaven komen uit hun opstarten lus.
- Vanaf hier op uit, de meester bepaalt de slaven via OSC te hervatten/pauzeren/terugspoelen de films voor onbepaalde tijd.
- De machines of opnieuw opstarten of afsluiten als de specifieke GPIO pin wordt ingesteld op de master.
<p>#!/usr/bin/python<br> import OSC import threading, socket, shutil from pyomxplayer import OMXPlayer from time import sleep ''' Thomas Hollier, 2015. ''' hosts = {'master':'192.168.11.45', 'slave1':'192.168.11.46', 'slave2':'192.168.11.47'} movies = {'master':'/home/pi/media/movie_lf.mov', 'slave1':'/home/pi/media/movie_cn.mov', 'slave2':'/home/pi/media/movie_rt.mov'} movieLength = 60*5 hostname = socket.gethostname() print "copying %s to /var/ramdisk" % movies[hostname] shutil.copy(movies[hostname], "/var/ramdisk/") movies[hostname] = movies[hostname].replace('/home/pi/media', '/var/ramdisk') print "playing movie from %s" % movies[hostname] omx = OMXPlayer(movies[hostname]) sleep(5) omx.toggle_pause() sleep(1) omx.rewind() sleep(1) def reboot(): command = "/usr/bin/sudo /sbin/shutdown -r now" import subprocess process = subprocess.Popen(command.split(), stdout=subprocess.PIPE) output = process.communicate()[0] print output def poweroff(): command = "/usr/bin/sudo /sbin/shutdown -h now" import subprocess process = subprocess.Popen(command.split(), stdout=subprocess.PIPE) output = process.communicate()[0] print output if hostname == 'master': def gpio_check(): import RPi.GPIO as GPIO GPIO.setmode(GPIO.BCM) GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.setup(24, GPIO.IN, pull_up_down=GPIO.PUD_UP) while True: input_state = GPIO.input(23) if input_state == False: print('Detected a reboot request: Button 23 Pressed') send_reboot(slaves['slave1'][2]) send_reboot(slaves['slave2'][2]) sleep(.3) reboot() input_state = GPIO.input(24) if input_state == False: print('Detected a poweroff request: Button 24 Pressed') send_poweroff(slaves['slave1'][2]) send_poweroff(slaves['slave2'][2]) print('Button 24 Pressed') sleep(.3) poweroff() sleep(.1) gpio_thread = threading.Thread(target=gpio_check) gpio_thread.start() # send listen def send_listen(slaveServer): print "querying slave", slaveServer msg = OSC.OSCMessage() msg.setAddress("/listen") # set OSC address msg.append(" the master.\nThe master is pausing for 20 seconds.") # int slaveServer.send(msg) # send it! # send play command def send_play(slaveServer): print "sending play to", slaveServer msg = OSC.OSCMessage() msg.setAddress("/play") # set OSC address msg.append("the master") # int slaveServer.send(msg) # send it! # send toggle_pause command def send_toggle_pause(slaveServer): print "sending toggle_pause to", slaveServer msg = OSC.OSCMessage() msg.setAddress("/toggle_pause") # set OSC address msg.append("the master") # int slaveServer.send(msg) # send it! # send rewind command def send_rewind(slaveServer): print "sending rewind to", slaveServer msg = OSC.OSCMessage() msg.setAddress("/rewind") # set OSC address msg.append("the master") # int slaveServer.send(msg) # send it! # send reboot command def send_reboot(slaveServer): print "sending reboot to", slaveServer msg = OSC.OSCMessage() msg.setAddress("/reboot") # set OSC address msg.append("the master") # int slaveServer.send(msg) # send it! # send poweroff command def send_poweroff(slaveServer): print "sending poweroff to", slaveServer msg = OSC.OSCMessage() msg.setAddress("/poweroff") # set OSC address msg.append("the master") # int slaveServer.send(msg) # send it! # handler for ready address def ready_handler(addr, tags, stuff, source): if not slaves[stuff[0]][0]: slaves[stuff[0]][0] = True print "setting %s to ready" % stuff[0] # setup clients to send messages to slavesReady = False c1 = OSC.OSCClient() c2 = OSC.OSCClient() slaves = {'slave1':[False, (hosts['slave1'], 9000), c1] , 'slave2':[False, (hosts['slave2'], 9000), c2] } # set up self to receive messages receive_address = hosts['master'], 9000 s = OSC.OSCServer(receive_address) # basic s.addDefaultHandlers() s.addMsgHandler("/ready", ready_handler) # adding our function # Start OSCServer print "\nStarting OSCServer. Use ctrl-C to quit." st = threading.Thread( target = s.serve_forever ) st.start() # set up clients to send messages to slaves['slave1'][2].connect( slaves['slave1'][1] ) # set the address for all following messages slaves['slave2'][2].connect( slaves['slave2'][1] ) # set the address for all following messages ######### # establish communication print "Master is waiting to hear from the slaves." # The master waits until both slaves are ready while not slavesReady: sleep(.01) if slaves['slave1'][0] and slaves['slave2'][0]: slavesReady = True print "The master has heard from both slaves" # The master tells the slaves to listen send_listen(slaves['slave1'][2]) send_listen(slaves['slave2'][2]) print "The master has told the slaves to listen" print "Pausing for 20 seconds" # catch our breath sleep(20) ######### # media control # we go into an infinite loop where we # unpause, wait for the movie length # pause, wait, rewind, wait, unpause print "entering main loop" while True: send_toggle_pause(slaves['slave1'][2]) send_toggle_pause(slaves['slave2'][2]) omx.toggle_pause() sleep(movieLength) send_toggle_pause(slaves['slave1'][2]) send_toggle_pause(slaves['slave2'][2]) omx.toggle_pause() sleep(2) send_rewind(slaves['slave1'][2]) send_rewind(slaves['slave2'][2]) omx.rewind() sleep(2) else: thisName = hostname thisIP = hosts[hostname], 9000 masterStatus = {'awake':[False], 'play':[False]} masterAddress = hosts['master'], 9000 def send_ready(c): msg = OSC.OSCMessage() msg.setAddress("/ready") # set OSC address msg.append(thisName) # int try: c.send(msg) except: pass def listen_handler(add, tags, stuff, source): print "I was told to listen by%s" % stuff[0] masterStatus['awake'][0] = True def play_handler(add, tags, stuff, source): print "I was told to play by %s" % stuff[0] masterStatus['play'][0] = True def toggle_pause_handler(add, tags, stuff, source): print "I was told to toggle_pause by %s" % stuff[0] omx.toggle_pause() def rewind_handler(add, tags, stuff, source): print "I was told to rewind by %s" % stuff[0] omx.rewind() masterStatus['awake']=False def reboot_handler(add, tags, stuff, source): print "I was told to reboot by %s" % stuff[0] reboot() def poweroff_handler(add, tags, stuff, source): print "I was told to poweroff by %s" % stuff[0] poweroff() ########### # create a client to send messages to master c = OSC.OSCClient() c.connect( masterAddress ) ########### # listen to messages from master receive_address = thisIP s = OSC.OSCServer(receive_address) # basic # define handlers s.addDefaultHandlers() s.addMsgHandler("/listen", listen_handler) s.addMsgHandler("/play", play_handler) s.addMsgHandler("/toggle_pause", toggle_pause_handler) s.addMsgHandler("/rewind", rewind_handler) s.addMsgHandler("/reboot", reboot_handler) s.addMsgHandler("/poweroff", poweroff_handler) # Start OSCServer print "\nStarting OSCServer. Use ctrl-C to quit." st = threading.Thread( target = s.serve_forever ) st.start() print "%s connecting to master." % hostname while True: #########i## # keep sending ready signals until master sends a message # on the /listen address which gets us out of this loop while not masterStatus['awake'][0]: sleep(.01) send_ready(c) ########## # once the master has taken control, we do nothing # and let the master drive playback through handlers</p>