-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxy.py
145 lines (120 loc) · 4.69 KB
/
proxy.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import socket
import time
from datetime import datetime
from enum import Enum, auto
from daqmx import NIDAQmxInstrument
class BaseInput:
def connect(self) -> None:
pass
def wait_for_trigger(self) -> bool:
pass
class TCPInput(BaseInput):
"""
Read a string-based TCP packet and trigger on the specified label.
"""
def __init__(self, tcp_ip: str, tcp_port: int, trigger_label: str):
"""
:param tcp_ip: TCP server IP
:param tcp_port: TCP server port
:param trigger_label: Case-sensitive label that will trigger a stimulation.
"""
self.tcp_ip = tcp_ip
self.tcp_port = tcp_port
self.trigger_label = trigger_label
self.socket = None
def connect(self) -> None:
"""
Establishes a TCP connection with the server.
"""
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.socket.connect((self.tcp_ip, self.tcp_port))
print(self.socket)
def wait_for_trigger(self) -> bool:
"""
Blocking function to wait for a TCP message.
:return: `True` if the message is the trigger label, otherwise it continues waiting. `False` if connection is closed.
"""
BUFFER_SIZE = 1024
while True:
data = self.socket.recv(BUFFER_SIZE)
if not data:
break
text_label = data.decode().strip()
if text_label == self.trigger_label:
return True
return False
class ConsoleInput(BaseInput):
"""
Reads console input and sends a trigger on Enter. Useful for testing.
"""
def wait_for_trigger(self) -> bool:
"""
Blocking function until user types Enter into the console.
:return: Always `True`
"""
print("\nHit Enter to trigger: ", end="")
input()
return True
class BaseTrigger:
def stimulate(self):
pass
class ConsoleTrigger(BaseTrigger):
"""
Sends a message on trigger. Useful for testing.
"""
def stimulate(self) -> None:
print("Console Stimulation")
class NiDAQTrigger(BaseTrigger):
"""
Sends a NiDAQ stimulation on trigger.
"""
def __init__(self, voltage: float, pulse_width_s: float, pause_width_s: float, n_pulses: int):
"""
:param voltage: Sets the stimulation voltage, in volts.
:param pulse_width_s: Length of a single pulse in stimulation in seconds.
:param pause_width_s: Length of a pause between pulses in seconds.
:param n_pulses: Number of pulses in a stimulation.
"""
self.voltage = voltage
self.pulse_width = pulse_width_s
self.pause_width = pause_width_s
self.n_pulses = n_pulses
self.daq = NIDAQmxInstrument()
def stimulate(self):
print("NiDAQ stimulation")
for i in range(self.n_pulses):
print("Start: ", datetime.now().strftime("%H:%M:%S.%f")[:-3], end="")
self.daq.ao0 = self.voltage
time.sleep(self.pulse_width)
print(" End: ", datetime.now().strftime("%H:%M:%S.%f")[:-3])
self.daq.ao0 = 0
time.sleep(self.pause_width)
class Proxy:
"""
Establishes a connection between input and stimulation device.
"""
def __init__(self, input_obj: BaseInput, trigger: BaseTrigger, seconds_between_stimulations: int = 5):
"""
:param input_obj: Input object that sends a message at the time of wanted stimulation.
:param trigger: Trigger object that triggers the stimulation device.
:param seconds_between_stimulations: Sliding window length in seconds in which only the first stimulation is triggered. The consecutive ones are ignored.
"""
self.input = input_obj
self.trigger = trigger
self.seconds_between_stimulations = seconds_between_stimulations
def start(self) -> None:
"""
Starts the proxy server. After the proxy server has started and connection to both input and stimulation device
are established, stimulations can be triggered.
"""
self.input.connect()
last_stimulation_time = 0
while True:
triggered = self.input.wait_for_trigger()
if triggered:
current_time = time.time()
if (current_time - last_stimulation_time) < self.seconds_between_stimulations:
print("Stimulation prevented from firing")
continue
last_stimulation_time = time.time()
self.trigger.stimulate()