forked from kaysoky/MagStim_PyServer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
150 lines (119 loc) · 3.56 KB
/
server.py
File metadata and controls
150 lines (119 loc) · 3.56 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
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
146
147
148
149
150
import web
import Magstim.Rapid2Constants
from Magstim.MagstimInterface import Rapid2
import sys
import argparse
import time
from threading import Lock, Thread
"""
Where the TMS machine is connected to this computer
"""
SERIAL_PORT = 'COM1'
"""
"""
POWER_THRESHOLD = 80;
"""
"""
PERCENT_THRESHOLD = 110;
urls = (
'/', 'index',
'/TMS/arm', 'tms_arm',
'/TMS/disarm', 'tms_disarm',
'/TMS/fire', 'tms_fire',
'/TMS/power/(\d*)', 'tms_intensity'
)
class index:
"""
Returns a readme with how to use this API
"""
def GET(self):
with open('README.md', 'r') as f:
return f.read()
class tms_arm:
"""
Arms the TMS device
"""
def POST(self):
web.STIMULATOR_LOCK.acquire()
web.STIMULATOR.armed = True
# Wait a bit
waitTime = (Magstim.Rapid2Constants.output_intesity[web.STIMULATOR.intensity] - 1050) / 1050.0
waitTime = max(0.5, waitTime)
time.sleep(waitTime)
# Just in case
web.STIMULATOR.disable_safety()
web.STIMULATOR_LOCK.release()
# Return nothing
web.ctx.status = '204 No Content'
class tms_disarm:
"""
Disarms the TMS device
"""
def POST(self):
web.STIMULATOR_LOCK.acquire()
web.STIMULATOR.armed = False
web.STIMULATOR_LOCK.release()
# Return nothing
web.ctx.status = '204 No Content'
class tms_fire:
"""
Triggers a TMS pulse
"""
def POST(self):
web.STIMULATOR_LOCK.acquire()
web.STIMULATOR.trigger()
web.STIMULATOR_LOCK.release()
# Return nothing
web.ctx.status = '204 No Content'
# Allow Cross-Origin Resource Sharing (CORS)
# This lets a web browser call this method with no problems
web.header('Access-Control-Allow-Origin', web.ctx.env.get('HTTP_ORIGIN'))
class tms_intensity:
"""
Sets the intensity level of the TMS
"""
def POST(self, powerLevel):
web.STIMULATOR_LOCK.acquire()
web.STIMULATOR.intensity = int(powerLevel)
web.STIMULATOR_LOCK.release()
# Return nothing
web.ctx.status = '204 No Content'
class maintain_communication(Thread):
def run(self):
while True:
web.STIMULATOR_LOCK.acquire()
web.STIMULATOR.remocon = True
web.STIMULATOR_LOCK.release()
time.sleep(0.5)
# Report all errors to the client
web.internalerror = web.debugerror
def do_main():
# Take only a port as an argument
parser = argparse.ArgumentParser(
description='Opens a server to control the TMS machine on the given port')
parser.add_argument('port', type=int)
args = parser.parse_args()
# Make sure that the server only listens to localhost
# This is because we cannot allow outside computer to access the TMS
sys.argv[1] = '127.0.0.1:%d' % args.port
# Initialize the shared state between web threads
web.STIMULATOR = Rapid2(port=SERIAL_PORT)
web.STIMULATOR_LOCK = Lock()
web.STIMULATOR.remocon = True
# Start the thread to keep the TMS awake
poller = maintain_communication()
poller.daemon = True
poller.start()
# Set the power level
powerLevel = int(POWER_THRESHOLD * PERCENT_THRESHOLD / 100);
if powerLevel > 100:
powerLevel = 100
elif powerLevel < 0:
powerLevel = 0
web.STIMULATOR.intensity = powerLevel
web.STIMULATOR.disable_safety()
# Start the server
app = web.application(urls, globals())
app.run()
if __name__ == '__main__':
do_main()