-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathstream.py
More file actions
85 lines (68 loc) · 2.08 KB
/
stream.py
File metadata and controls
85 lines (68 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# CyKit Raw TCP Stream.
# 14 Signed Integers delimited(spaced) by period + CRLF
import socket
import gevent
import sys
#sys.path.insert(1, './Python/CyKit/')
import emotiv
if len(sys.argv) < 3 or len(sys.argv) < 2:
print("Usage: Python.exe stream.py IP Port")
sys.argv = [sys.argv[0], "localhost", "5555"]
print("Defaulting to localhost:5555")
def main():
try:
connectServer()
except exc as e:
print(e)
gevent.sleep(1)
try:
while True:
packet = headset.dequeue()
connbuffer = ""
gevent.sleep(.001)
for name in 'AF3 F7 F3 FC5 T7 P7 O1 O2 P8 T8 FC6 F4 F8 AF4'.split(' '):
v = packet.sensors[name]['value']
connbuffer += str(v) + "."
connbuffer += "\r\n"
conn.sendall(connbuffer.encode('utf-8'))
connbuffer = ""
except Exception as msg:
print("Streaming Error: ", sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2], " : ", msg)
conn.close()
#headset.kill()
finally:
print("Disconnecting . . .")
conn.close()
gevent.GreenletExit
print("Restarting . . .")
main()
def connectServer():
global conn
HOST = str(sys.argv[1])
PORT = int(sys.argv[2])
print("Listening to ", HOST, " : ", str(PORT))
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error as msg:
s = None
try:
s.bind((HOST, PORT))
s.listen(1)
except socket.error as msg:
s.close()
s = None
if s is None:
print('Could not open socket')
return
conn, addr = s.accept()
print('Listening to', addr)
try:
headset = emotiv.Emotiv(True, False)
gevent.spawn(headset.setup_windows)
main()
except Exception as e:
exc = e
print(" @ @ @ ", e)
print("Device Time Out or Disconnect . . . Reconnect to Server.")
conn.close()
main()