Unix domain socket based button reader
This commit is contained in:
parent
8120b51d01
commit
cf31e35d26
|
@ -5,6 +5,7 @@ from __future__ import print_function
|
|||
|
||||
import os
|
||||
import serial
|
||||
import socket
|
||||
from PySide import QtCore
|
||||
|
||||
_inputs = {}
|
||||
|
@ -139,5 +140,51 @@ class FifoInput(BaseInput):
|
|||
|
||||
_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):
|
||||
pass
|
||||
|
|
Loading…
Reference in New Issue