Unix domain socket based button reader

This commit is contained in:
Sebastian Lohff 2014-12-28 14:51:54 +01:00
parent 8120b51d01
commit cf31e35d26
1 changed files with 47 additions and 0 deletions

View File

@ -5,6 +5,7 @@ from __future__ import print_function
import os import os
import serial import serial
import socket
from PySide import QtCore from PySide import QtCore
_inputs = {} _inputs = {}
@ -139,5 +140,51 @@ class FifoInput(BaseInput):
_add_input("Fifo", FifoInput) _add_input("Fifo", FifoInput)
class UnixInput(BaseInput):
def __init__(self, app, socketPath, debug=False):
super(UnixInput, self).__init__(app)
self._path = socketPath
self._debug = debug
self._currConn = None
try:
os.unlink(self._path)
except OSError as e:
if os.path.exists(self._path):
raise InputException("Could not create domain socket '%s': %s" % (self._path, e))
self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
self.sock.bind(self._path)
print(" >> Unix domain socket initialized, using '%s'" % self._path)
def run(self):
self.sock.listen(1)
while True:
self._currConn, cli = self.sock.accept()
try:
while True:
c = self._currConn.recv(1)
if c == '':
break
if len(c) == 1 and ord(c) > ord('0') and ord(c) <= ord('9'):
self._sendButtonEvent(c)
finally:
self._currConn.close()
self._currConn = None
@QtCore.Slot(bool)
def buzzersOpen(self, isOpen):
if self._currConn:
if isOpen:
self._currConn.send("O")
else:
self._currConn.send("C")
@QtCore.Slot(int)
def playerGotQuestion(self, playerNo):
if self._currConn:
self._currConn.send("T%d" % playerNo)
_add_input("Unix", UnixInput)
class InputException(Exception): class InputException(Exception):
pass pass