forked from tschuehly/music-cards
-
Notifications
You must be signed in to change notification settings - Fork 1
/
box.py
70 lines (56 loc) · 1.33 KB
/
box.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
from mpd import MPDClient
from Reader import Reader
import sys
import time
def connectMPD():
try:
client = MPDClient() # create client object
client.timeout = 200 # network timeout in seconds (floats allowed), default: None
client.idletimeout = None
client.connect("localhost", 6600)
return client
except:
print 'Could not connect to MPD server'
def clear_and_play(client, plist):
try:
client.stop()
client.clear()
client.add(plist)
client.repeat(1)
client.play()
except:
print 'Could not play playlist %s' % plist
reader = Reader()
client = None
before_card = None
while not client:
client = connectMPD()
if not client:
time.sleep(2)
print 'Ready: place a card on top of the reader'
while True:
try:
card = reader.readCard()
print "Read card!"
client = connectMPD()
if card != '' and card != before_card:
clear_and_play(client, card)
before_card = card
elif card == before_card:
print "Same card."
if client.status()["state"] != "play":
client.play()
client.close()
reader.released_Card()
client = connectMPD()
if client.status()["state"] != "pause":
client.pause()
client.close()
except KeyboardInterrupt:
sys.exit(0)
except ValueError:
print "this card is new"
print "need to Set a playlist"
reader.released_Card()
except:
pass