Browse Source

Unix domain socket based button reader

Sebastian Lohff 5 years ago
parent
commit
cf31e35d26
1 changed files with 47 additions and 0 deletions
  1. 47
    0
      buttonreader.py

+ 47
- 0
buttonreader.py View File

@@ -5,6 +5,7 @@ from __future__ import print_function
5 5
 
6 6
 import os
7 7
 import serial
8
+import socket
8 9
 from PySide import QtCore
9 10
 
10 11
 _inputs = {}
@@ -139,5 +140,51 @@ class FifoInput(BaseInput):
139 140
 
140 141
 _add_input("Fifo", FifoInput)
141 142
 
143
+class UnixInput(BaseInput):
144
+	def __init__(self, app, socketPath, debug=False):
145
+		super(UnixInput, self).__init__(app)
146
+		self._path = socketPath
147
+		self._debug = debug
148
+		self._currConn = None
149
+		try:
150
+			os.unlink(self._path)
151
+		except OSError as e:
152
+			if os.path.exists(self._path):
153
+				raise InputException("Could not create domain socket '%s': %s" % (self._path, e))
154
+
155
+		self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
156
+		self.sock.bind(self._path)
157
+		print(" >> Unix domain socket initialized, using '%s'" % self._path)
158
+
159
+	def run(self):
160
+		self.sock.listen(1)
161
+		while True:
162
+			self._currConn, cli = self.sock.accept()
163
+			try:
164
+				while True:
165
+					c = self._currConn.recv(1)
166
+					if c == '':
167
+						break
168
+					if len(c) == 1 and ord(c) > ord('0') and ord(c) <= ord('9'):
169
+						self._sendButtonEvent(c)
170
+			finally:
171
+				self._currConn.close()
172
+				self._currConn = None
173
+
174
+	@QtCore.Slot(bool)
175
+	def buzzersOpen(self, isOpen):
176
+		if self._currConn:
177
+			if isOpen:
178
+				self._currConn.send("O")
179
+			else:
180
+				self._currConn.send("C")
181
+
182
+	@QtCore.Slot(int)
183
+	def playerGotQuestion(self, playerNo):
184
+		if self._currConn:
185
+			self._currConn.send("T%d" % playerNo)
186
+
187
+_add_input("Unix", UnixInput)
188
+
142 189
 class InputException(Exception):
143 190
 	pass

Loading…
Cancel
Save