Commit c0a0ee7b authored by Daniel STAN's avatar Daniel STAN

ajout de remote.py

parent f17f085e
#!/usr/bin/env python
import serial, time
import dbus
from subprocess import call
import requests
import collections
SERIALPORT = "/dev/ttyUSB0"
BAUDRATE = 9600
CONTROL='Control:'
ser = serial.Serial(SERIALPORT, BAUDRATE)
ser.bytesize = serial.EIGHTBITS #number of bits per bytes
ser.parity = serial.PARITY_NONE #set parity check: no parity
ser.stopbits = serial.STOPBITS_ONE #number of stop bits
#ser.timeout = None #block read
#ser.timeout = 0 #non-block read
ser.timeout = 2 #timeout block read
ser.xonxoff = False #disable software flow control
ser.rtscts = False #disable hardware (RTS/CTS) flow control
ser.dsrdtr = False #disable hardware (DSR/DTR) flow control
ser.writeTimeout = 0 #timeout for write
print 'Starting Up Serial Monitor'
# Spotify iface
bus = dbus.SessionBus()
proxy = bus.get_object('org.mpris.MediaPlayer2.spotify', '/org/mpris/MediaPlayer2')
#proxy = bus.get_object('org.mpris.MediaPlayer2.vlc', '/org/mpris/MediaPlayer2')
spotify = dbus.Interface(proxy, dbus_interface='org.mpris.MediaPlayer2.Player')
# Volume control
def volume(p):
call(["amixer", "-D", "pulse", "sset", "Master", p])
# Light control
def light_old(p):
x = requests.get('http://gladys.home:8000/domo/')
y = requests.post('http://gladys.home:8000/domo/enable/',
data={'num': str(p), 'csrfmiddlewaretoken': x.cookies['csrftoken']},
cookies={'csrftoken': x.cookies['csrftoken']},
)
if y.status_code != 200:
raise Exception("Bad http result %d " % y.status_code)
_states = collections.defaultdict(bool)
def light(p):
p = str(p)
ser.write("%s%s;" % ('f' if _states[p] else 'o', p))
ser.flush()
_states[p] = not _states[p]
if ser.isOpen():
try:
while True:
response = ser.readline()
if response.startswith(CONTROL):
key = response[len(CONTROL):].strip().lower()
print key
if key == 'pause':
spotify.PlayPause()
elif key == 'prev':
spotify.Previous()
elif key == 'next':
spotify.Next()
elif key == 'vol+':
volume('10%+')
elif key == 'vol-':
volume('10%-')
else:
try:
ikey = int(key)
except ValueError:
ikey = None
if ikey and ikey in xrange(1,5):
light(ikey)
ser.close()
except Exception as e:
print "error communicating...: " + str(e)
else:
print "cannot open serial port "
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment