-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
76 lines (58 loc) · 2.07 KB
/
__init__.py
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
import socket
import usbhelpers
from header import Header
#from server import NeuroServer
#from client import NeuroClientEEG,NeuroClientDisp
DEFAULT_PORT = 8336
DEFAULT_HOST = "localhost"
class NeuroError(Exception):
pass
class NeuroTimeout(NeuroError):
pass
class NeuroDeviceError(NeuroError):
pass
class NeuroDevice:
def __init__(self, header):
self.header = header
def getHeader(self):
return self.header
def getData(self):
raise Exception("Not implemented.")
class NeuroDeviceUSB(NeuroDevice):
def __init__(self, idVendor, idProduct, header):
NeuroDevice.__init__(self, header=header)
# find USB device and input endpoint
self.endpoint = usbhelpers.findUSBEndpoints(idVendor, idProduct, usbhelpers.ENDPOINT_IN)
if self.endpoint is None:
raise NeuroDeviceError("No USB devices or endpoints has been found.")
class Neuro:
def __init__(self, address, device):
#if device is not None and not isinstance(device, NeuroDevice):
# raise NeuroError("Parameter 'device' must be instance of 'NeuroDevice'.")
self.address = address
self.device = device
self.socket = None
def send(self, data, sock = None):
if sock is None:
sock = self.socket
if sock is None:
raise NeuroError("Socket is not open.")
try:
return sock.send(data+'\r\n')
except socket.timeout:
raise NeuroTimeout("Timeout writting to socket.")
except socket.error as e:
raise NeuroError("Error writting to socket: {0}".format(e))
def recv(self, sock = None):
if sock is None:
sock = self.socket
if sock is None:
raise NeuroError("Socket is not open.")
try:
return sock.recv(4096)
except socket.timeout:
raise NeuroTimeout("Timeout reading socket.")
except socket.error as e:
raise NeuroError("Error reading socket: {0}".format(e))
def run(self):
self.open()