-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.cgi
executable file
·204 lines (154 loc) · 5.78 KB
/
index.cgi
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/usr/bin/python
import sys
import os
import signal
import cgi
import status as s
import subprocess
import datetime
try:
g_help_text = open("help", "r").read().replace("\n", "\\n")
except:
g_help_text = "No help available."
def boilerplate_start():
print("<html><head><title>FLIRA65 Capture Control</title></head>");
print('<body bgcolor="#99cc99" text="#000000" link="#2020ff" vlink="#4040cc" onload="loadTime()">')
print('<div style="width:50%; align:float-left; border:1px solid black;">')
print("<h1>FLIRA65 Capture Control</h1>")
print("<p>")
print('<button onclick="alert(\'%s\')"><b>HELP</b></button>' % str(g_help_text))
print('<a href="manage.cgi"><button>Manage data files</button></a>')
print("<script type='text/javascript'>")
print("function loadTime() {")
print("var now = new Date();")
print('var strDateTime = [[now.getFullYear(), AddZero(now.getMonth() + 1), AddZero(now.getDate()) ].join("-"), [AddZero(now.getHours()), AddZero(now.getMinutes()), AddZero(now.getSeconds())].join(":"), now.getHours() >= 12 ? "PM" : "AM"].join(" ");')
print('document.getElementById("date").value = strDateTime;}')
print('function AddZero(num) {return (num >= 0 && num < 10) ? "0" + num : num + "";}')
print("</script>")
def boilerplate_end():
print("</div>")
if os.path.isfile("latest.png"):
print('<div style="width:50%; align:float-right; border:1px solid black;">')
print("<pre>")
print("<b>Latest Image</b>")
print("<hr>")
print('<img src="latest.png?'+datetime.datetime.now().strftime("%s")+'" width="40%"/>')
print("</hr>")
print("</div>")
print("</body>")
print("</html>")
def red(text):
return '<span style="color:red;">'+str(text)+'</span>'
def green(text):
return '<span style="color:green;">'+str(text)+'</span>'
def bold(text):
return '<b>'+str(text)+'</b>'
def link(text):
return '<a href="%s">%s</a>' % (str(text), str(text))
def parse_form(form):
s.load()
if s.status != None:
try:
daemon_pid = s.status["pid"]
os.kill(daemon_pid, signal.SIGQUIT)
except:
pass
s.status = None
s.save()
return
if form.getvalue("submitted") == "SINGLE":
os.environ["LD_LIBRARY_PATH"] = os.environ.get("LD_LIBRARY_PATH", "")+":"+os.getcwd()+"/contrib/lib"
if os.path.exists("latest0.png"):
os.unlink("latest0.png")
error = subprocess.call(["./FLIRA65-Capture", "-e", "100", "-f", "1", "-p", "single", "-t", "png"])
if error != 0:
print(bold(red("Error %d performing capture" % error)))
return
dir = form.getvalue("directory")
dir = os.path.abspath(dir)
root = os.getcwd()
if dir[0:len(root)] != root or dir.count("/") <= root.count("/"):
print(bold(red("Invalid data directory; must be below root.")))
s.status = None
s.save()
return
os.system("date --set '%s'" % form.getvalue("date"))
keys = ["directory", "maxFrames", "date", "period", "bitdepth"]
s.status = {}
for k in keys:
s.status[k] = form.getvalue(k)
try:
s.status["maxFrames"] = int(s.status["maxFrames"])
except:
s.status["maxFrames"] = -1
s.save()
def reload_page(timeout):
print("<script type='text/javascript'>")
print(" var url = window.location.href;"
"var i = url.indexOf('?');"
"if (i < 0) {i = url.length;}"
"url = url.substr(0,i);"
"setTimeout(function() {window.location.href=this}.bind(url), %d);" % int(timeout))
print("</script>")
def run_daemon():
pid = os.fork()
if pid > 0:
sys.exit(0)
oldcwd = os.getcwd()
os.chdir("/")
os.setsid()
os.umask(0)
pid = os.fork()
if pid > 0:
sys.exit(0)
os.close(sys.stdin.fileno())
os.close(sys.stdout.fileno())
os.close(sys.stderr.fileno())
si = file("/dev/null", "r")
so = file(oldcwd+"/log.out", "w")
se = file(oldcwd+"/log.err", "w", 0)
os.dup2(si.fileno(), sys.stdin.fileno())
os.dup2(so.fileno(), sys.stdout.fileno())
os.dup2(se.fileno(), sys.stderr.fileno())
os.chdir(oldcwd)
sys.exit(os.system("./run.py"))
# stdout seems to often get flushed twice :S
# but this is CGI and firefox ignores everything after the first </html> so it's all good...
if __name__ == "__main__":
#sys.stderr = open("index.err", "w", 0)
print("Content-type: text/html\r\n\r\n")
form = cgi.FieldStorage()
boilerplate_start()
if "submitted" in form:
parse_form(form)
reload_page(1000)
if s.status != None:
reload_page(3000)
print('<form action="index.cgi">')
print("<pre id='status' style='display:block;'>")
print("<b>Experiment Status</b>")
sys.stdout.write("<hr>")
if s.status != None:
if "submitted" not in form:
print("Experiment is running")
s.print_status()
action = "STOP"
else:
print("Waiting to configure New Experiment\n")
print("<b>\tData directory </b> <input id='directory' name='directory' type='text' value='data/'></input>")
print("<b>\tStart Time </b> <input id='date' name='date' type='text' value='' readonly='readonly'></input>")
print("<b>\tMaximum Frames </b> <input name='maxFrames' type='number' value=''></input>")
print("<b>\tSample Period(us) </b> <input name='period' type='number' value='0'></input>")
print("<b>\tBit depth </b> <input name='bitdepth' type='radio' value='8' checked='yes'>Mono8</input> <input name='bitdepth' type='radio' value='14'>Mono14</input>")
action = "START"
print("</form>")
print("<hr>")
print("<input id='submitted' name='submitted' type='text' value='' style='display:none;'/>")
print("<input id='submit' type='submit' value='%s EXPERIMENT' onclick='document.getElementById(\"submit\").value=\"%s\"; document.getElementById(\"submitted\").value=\"%s\"; loadTime();'/>" % (action, action+"ING", action+"ING"))
if s.status == None:
print("<input id='submit' type='submit' value='SINGLE CAPTURE' onclick='loadTime(); document.getElementById(\"submitted\").value=\"SINGLE\"'/>")
print("</pre>")
boilerplate_end()
if "submitted" in form and s.status != None:
run_daemon()
sys.exit(0)