Commit 610272f0 authored by Daniel STAN's avatar Daniel STAN

remote: serveur à base de socket

parent 66c245ab
#!/bin/bash
echo "Control: $1" | socat UNIX-CONNECT:/var/run/user/$(id -u)/remote -
......@@ -5,12 +5,15 @@ from subprocess import call
import requests
import collections
import select
import socket
import sys
import os
import time
SERIALPORT = "/dev/ttyUSB0"
BAUDRATE = 9600
CONTROL=b'Control:'
SOCK_P='/var/run/user/%d/remote' % os.getuid()
ser = serial.Serial(SERIALPORT, BAUDRATE)
ser.bytesize = serial.EIGHTBITS #number of bits per bytes
......@@ -51,7 +54,6 @@ def player_action(action):
bus = dbus.SessionBus()
proxy = bus.get_object('org.gnome.ScreenSaver', '/org/gnome/ScreenSaver')
ss = dbus.Interface(proxy, dbus_interface='org.gnome.ScreenSaver')
#ss.Lock()
# Volume control
def volume(p):
......@@ -60,19 +62,18 @@ def volume(p):
_states = collections.defaultdict(bool)
def light(p):
"""Light control"""
p = bytes(p)
p = str(p).encode()
ser.write((b'f' if _states[p] else b'o') + p + b';')
ser.flush()
_states[p] = not _states[p]
def process_line(response):
print("Received %r" % response)
#print("Received %r" % response)
#if response.startswith(b'Unkwon: 4294967295'):
# ss.Lock()
# return
if response.startswith(CONTROL):
key = response[len(CONTROL):].strip().lower()
print(key)
if key == b'pause':
player_action('PlayPause')
elif key == b'prev':
......@@ -95,17 +96,63 @@ def process_line(response):
if ikey and ikey in range(1,5):
light(ikey)
# Socket unix specific
# Make sure the socket does not already exist
try:
os.unlink(SOCK_P)
except OSError:
if os.path.exists(SOCK_P):
raise
server = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
server.bind(SOCK_P)
server.listen(1)
clients = []
buffers = {}
def generic_readline(obj):
global buffers
if obj not in clients:
yield obj.readline()
return
value = obj.recv(1024).split(b'\n')
if not value:
raise ClientClosed()
value[0] = buffers.get(obj, b"") + value[0]
buffers[obj] = value[-1]
for line in value[:-1]:
yield line + b'\n'
class ClientClosed(Exception):
pass
if ser.isOpen():
try:
while True:
todo, _, _ = select.select([ser, sys.stdin], [], [])
todo, _, _ = select.select([server, ser, sys.stdin] + clients, [], [])
for sock in todo:
process_line(sock.readline())
ser.close()
if sock is server:
connection, client_address = sock.accept()
connection.setblocking(0)
clients.append(connection)
else:
try:
for line in generic_readline(sock):
if type(line) == str:
line = line.encode()
process_line(line)
except ClientClosed:
print("Removing client")
clients.remove(sock)
except KeyboardInterrupt:
print("Interupted by user... quitting.")
except Exception as e:
print("error communicating...: " + repr(e))
raise
finally:
ser.close()
sock.close()
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