1 | #!/usr/bin/env python |
---|
2 | # -*- coding: utf-8 -*- |
---|
3 | # |
---|
4 | # Puzzlebox - Brainstorms - Network - Client - Thinkgear |
---|
5 | # |
---|
6 | # Copyright Puzzlebox Productions, LLC (2010) |
---|
7 | # |
---|
8 | # This code is released under the GNU Pulic License (GPL) version 2 |
---|
9 | # For more information please refer to http://www.gnu.org/copyleft/gpl.html |
---|
10 | # |
---|
11 | # Last Update: 2010.07.01 |
---|
12 | # |
---|
13 | ##################################################################### |
---|
14 | |
---|
15 | import os, sys |
---|
16 | import signal |
---|
17 | |
---|
18 | from PyQt4 import QtCore, QtNetwork |
---|
19 | |
---|
20 | import simplejson as json |
---|
21 | |
---|
22 | import puzzlebox_brainstorms_configuration as configuration |
---|
23 | #import puzzlebox_logger |
---|
24 | |
---|
25 | ##################################################################### |
---|
26 | # Globals |
---|
27 | ##################################################################### |
---|
28 | |
---|
29 | DEBUG = 1 |
---|
30 | |
---|
31 | SERVER_HOST = configuration.THINKGEAR_SERVER_HOST |
---|
32 | SERVER_PORT = configuration.THINKGEAR_SERVER_PORT |
---|
33 | |
---|
34 | CLIENT_NO_REPLY_WAIT = configuration.CLIENT_NO_REPLY_WAIT * 1000 |
---|
35 | |
---|
36 | DELIMITER = configuration.THINKGEAR_DELIMITER |
---|
37 | |
---|
38 | THINKGEAR_CONFIGURATION_PARAMETERS = configuration.THINKGEAR_CONFIGURATION_PARAMETERS |
---|
39 | |
---|
40 | THINKGEAR_AUTHORIZATION_ENABLED = configuration.THINKGEAR_AUTHORIZATION_ENABLED |
---|
41 | AUTHORIZATION_REQUEST = configuration.THINKGEAR_AUTHORIZATION_REQUEST |
---|
42 | |
---|
43 | ##################################################################### |
---|
44 | # Classes |
---|
45 | ##################################################################### |
---|
46 | |
---|
47 | class puzzlebox_brainstorms_network_client_thinkgear: |
---|
48 | |
---|
49 | def __init__(self, log, \ |
---|
50 | server_host=SERVER_HOST, \ |
---|
51 | server_port=SERVER_PORT, \ |
---|
52 | DEBUG=DEBUG, \ |
---|
53 | parent=None): |
---|
54 | |
---|
55 | self.log = log |
---|
56 | self.DEBUG = DEBUG |
---|
57 | self.parent=parent |
---|
58 | |
---|
59 | self.server_host = server_host |
---|
60 | self.server_port = server_port |
---|
61 | |
---|
62 | self.is_authorized = True |
---|
63 | |
---|
64 | self.configureNetwork() |
---|
65 | |
---|
66 | |
---|
67 | ################################################################## |
---|
68 | |
---|
69 | def configureNetwork(self): |
---|
70 | |
---|
71 | #self.blockSize = 0 |
---|
72 | self.socket = QtNetwork.QTcpSocket() |
---|
73 | self.socket.name = 'ThinkGear Client' |
---|
74 | |
---|
75 | self.socket.readyRead.connect(self.printReply) |
---|
76 | self.socket.error.connect(self.displayError) |
---|
77 | |
---|
78 | # Perform ThinkGear authorization if enabled |
---|
79 | if THINKGEAR_AUTHORIZATION_ENABLED: |
---|
80 | self.sendCommand(AUTHORIZATION_REQUEST) |
---|
81 | self.socket.waitForReadyRead() |
---|
82 | self.socket.disconnectFromHost() |
---|
83 | |
---|
84 | self.sendCommand(THINKGEAR_CONFIGURATION_PARAMETERS) |
---|
85 | |
---|
86 | |
---|
87 | ################################################################## |
---|
88 | |
---|
89 | def printReply(self): |
---|
90 | |
---|
91 | socket_buffer = self.socket.readAll() |
---|
92 | |
---|
93 | for packet in socket_buffer.split(DELIMITER): |
---|
94 | |
---|
95 | if packet != '': |
---|
96 | |
---|
97 | try: |
---|
98 | |
---|
99 | data = json.loads(packet.data()) |
---|
100 | |
---|
101 | |
---|
102 | except Exception, e: |
---|
103 | |
---|
104 | if self.DEBUG: |
---|
105 | print "ERROR [%s]: Exception parsing packet:" % self.socket.name, |
---|
106 | print packet.data() |
---|
107 | print "ERROR [%s]: Data packet" % self.socket.name, |
---|
108 | print e |
---|
109 | |
---|
110 | continue |
---|
111 | |
---|
112 | |
---|
113 | else: |
---|
114 | |
---|
115 | if self.DEBUG: |
---|
116 | print "<-- [%s] Received:" % self.socket.name, |
---|
117 | print data |
---|
118 | |
---|
119 | self.processPacketThinkGear(data) |
---|
120 | |
---|
121 | |
---|
122 | ################################################################## |
---|
123 | |
---|
124 | def displayError(self, socketError): |
---|
125 | |
---|
126 | if self.DEBUG: |
---|
127 | if ((socketError != QtNetwork.QAbstractSocket.RemoteHostClosedError) and \ |
---|
128 | (socketError != QtNetwork.QAbstractSocket.SocketTimeoutError)): |
---|
129 | print "ERROR [%s]:" % self.socket.name, |
---|
130 | print self.socket.errorString() |
---|
131 | |
---|
132 | |
---|
133 | if (self.parent != None): |
---|
134 | |
---|
135 | if ((socketError == QtNetwork.QAbstractSocket.RemoteHostClosedError) or \ |
---|
136 | (socketError != QtNetwork.QAbstractSocket.SocketTimeoutError)): |
---|
137 | pass |
---|
138 | |
---|
139 | elif socketError == QtNetwork.QAbstractSocket.HostNotFoundError: |
---|
140 | QtGui.QMessageBox.information(self.parent, \ |
---|
141 | self.socket.name, \ |
---|
142 | "The server host was not found. Please check the host name and " |
---|
143 | "port settings.") |
---|
144 | |
---|
145 | elif socketError == QtNetwork.QAbstractSocket.ConnectionRefusedError: |
---|
146 | QtGui.QMessageBox.information(self.parent, \ |
---|
147 | self.socket.name, |
---|
148 | "The server connection was refused by the peer. Make sure the " |
---|
149 | "server is running, and check that the host name " |
---|
150 | "and port settings are correct.") |
---|
151 | |
---|
152 | else: |
---|
153 | QtGui.QMessageBox.information(self.parent, \ |
---|
154 | self.socket.name, \ |
---|
155 | "The following error occurred: %s." % \ |
---|
156 | self.socket.errorString()) |
---|
157 | |
---|
158 | |
---|
159 | ################################################################## |
---|
160 | |
---|
161 | def sendCommand(self, command): |
---|
162 | |
---|
163 | if self.DEBUG: |
---|
164 | print "--> [%s] Sending:" % self.socket.name, |
---|
165 | print command |
---|
166 | |
---|
167 | self.socket.abort() |
---|
168 | self.socket.connectToHost(self.server_host, self.server_port) |
---|
169 | |
---|
170 | data = json.dumps(command) |
---|
171 | |
---|
172 | self.socket.waitForConnected(CLIENT_NO_REPLY_WAIT) |
---|
173 | |
---|
174 | self.socket.write(data) |
---|
175 | |
---|
176 | try: |
---|
177 | self.socket.waitForBytesWritten(CLIENT_NO_REPLY_WAIT) |
---|
178 | except Exception, e: |
---|
179 | print "ERROR [%s]: Exception:" % self.socket.name, |
---|
180 | print e |
---|
181 | |
---|
182 | |
---|
183 | ################################################################## |
---|
184 | |
---|
185 | def processPacketThinkGear(self, packet): |
---|
186 | |
---|
187 | if ('isAuthorized' in packet.keys()): |
---|
188 | |
---|
189 | self.isAuthorized = packet['isAuthorized'] |
---|
190 | |
---|
191 | |
---|
192 | if ('eSense' in packet.keys()): |
---|
193 | |
---|
194 | if ('attention' in packet['eSense'].keys()): |
---|
195 | if (self.parent != None): |
---|
196 | self.parent.progressBarConcentration.setValue(packet['eSense']['attention']) |
---|
197 | |
---|
198 | if ('meditation' in packet['eSense'].keys()): |
---|
199 | if (self.parent != None): |
---|
200 | self.parent.progressBarRelaxation.setValue(packet['eSense']['meditation']) |
---|
201 | |
---|
202 | |
---|
203 | ################################################################## |
---|
204 | |
---|
205 | def disconnectFromHost(self): |
---|
206 | |
---|
207 | self.socket.disconnectFromHost() |
---|
208 | |
---|
209 | |
---|
210 | ##################################################################### |
---|
211 | # Command line class |
---|
212 | ##################################################################### |
---|
213 | |
---|
214 | class puzzlebox_brainstorms_network_client_thinkgear_command_line( \ |
---|
215 | puzzlebox_brainstorms_network_client_thinkgear): |
---|
216 | |
---|
217 | def __init__(self, log, \ |
---|
218 | command_parameters, \ |
---|
219 | server_host=SERVER_HOST, \ |
---|
220 | server_port=SERVER_PORT, \ |
---|
221 | DEBUG=DEBUG): |
---|
222 | |
---|
223 | self.log = log |
---|
224 | self.DEBUG = DEBUG |
---|
225 | self.parent = None |
---|
226 | |
---|
227 | self.command_parameters = command_parameters |
---|
228 | self.server_host = server_host |
---|
229 | self.server_port = server_port |
---|
230 | |
---|
231 | self.configureNetwork() |
---|
232 | |
---|
233 | self.execute_command_line() |
---|
234 | |
---|
235 | |
---|
236 | ################################################################## |
---|
237 | |
---|
238 | def execute_command_line(self): |
---|
239 | |
---|
240 | (command) = self.parse_command_line(self.command_parameters) |
---|
241 | |
---|
242 | if (command != None): |
---|
243 | |
---|
244 | self.sendCommand(command) |
---|
245 | |
---|
246 | self.socket.waitForReadyRead(CLIENT_NO_REPLY_WAIT) |
---|
247 | |
---|
248 | |
---|
249 | ################################################################## |
---|
250 | |
---|
251 | def parse_command_line(self, command_parameters): |
---|
252 | |
---|
253 | try: |
---|
254 | command = command_parameters[0] |
---|
255 | except: |
---|
256 | command = None |
---|
257 | |
---|
258 | |
---|
259 | return(command) |
---|
260 | |
---|
261 | |
---|
262 | ##################################################################### |
---|
263 | # Main |
---|
264 | ##################################################################### |
---|
265 | |
---|
266 | if __name__ == '__main__': |
---|
267 | |
---|
268 | # Perform correct KeyboardInterrupt handling |
---|
269 | signal.signal(signal.SIGINT, signal.SIG_DFL) |
---|
270 | |
---|
271 | #log = puzzlebox_logger.puzzlebox_logger(logfile='client_thinkgear') |
---|
272 | log = None |
---|
273 | |
---|
274 | command_parameters = sys.argv[1:] |
---|
275 | |
---|
276 | #log.info("Command parameters: %s" % command_parameters) |
---|
277 | |
---|
278 | app = QtCore.QCoreApplication(sys.argv) |
---|
279 | |
---|
280 | client = puzzlebox_brainstorms_network_client_thinkgear_command_line(log, \ |
---|
281 | command_parameters, \ |
---|
282 | server_host=SERVER_HOST, \ |
---|
283 | server_port=SERVER_PORT, \ |
---|
284 | DEBUG=DEBUG) |
---|
285 | |
---|
286 | #while True: |
---|
287 | #while client.socket.waitForReadyRead(CLIENT_NO_REPLY_WAIT): |
---|
288 | #pass |
---|
289 | |
---|
290 | sys.exit(app.exec_()) |
---|
291 | |
---|