-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyCameraHandlerREST.py
322 lines (294 loc) · 10.5 KB
/
MyCameraHandlerREST.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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
"""Simple HTTPServer that can serve images or temperature graphs."""
import os
import SimpleHTTPServer
import urlparse
import subprocess
import datetime
class MyHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
def ReadLabels(self, filename):
"""Read in the id:label correspondence."""
self.labelmap = {}
with open(filename, 'r') as f:
lines = f.readlines()
for line in lines:
if line:
fields = line.split(',')
assert len(fields) == 2
self.labelmap[fields[0].strip()] = fields[1].strip()
def LoadLabelMapOnce(self):
# Setup the labelmap.
try:
len(self.labelmap)
except AttributeError as e:
self.ReadLabels('devicelist.txt')
def do_GET(self):
self.LoadLabelMapOnce()
parsedParameters = urlparse.urlparse(self.path)
queryParsed = urlparse.parse_qs(parsedParameters.query)
path = parsedParameters.path
print "Path is ", path
today = datetime.datetime.now().strftime("%Y/%m/%d")
default_units = 'F'
if path == '/tempraw.html':
self.TemperatureRequest('~/templog/%s' %
queryParsed.get('date',[today])[0])
elif path == '/temp.html':
self.TemperatureGraphRequest(
queryParsed.get('date',[today])[0],
queryParsed.get('units',[default_units])[0])
elif path == '/temp.js':
self.TemperatureRequestAsJS('~/templog/%s' %
queryParsed.get('date',[today])[0],
queryParsed.get('units',[default_units])[0])
elif path == '/dailytemp.js':
self.DailyTemperatureRequestAsJS('~/templog/maxmin',
queryParsed.get('units',[default_units])[0])
elif path == '/photo.html':
width = queryParsed.get('width',['300'])[0]
height = queryParsed.get('height',['300'])[0]
timeout = queryParsed.get('timeout',['500'])[0]
filename = queryParsed.get('filename',['image.jpg'])[0]
subprocess.call(["raspistill","-n",
"--width",width,
"--height",height,
"--timeout",timeout,
"-o",filename])
self.processMyRequest(filename)
else:
SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self);
def BasicHeaders(self):
self.send_response(200)
self.send_header('Content-Type', 'text/html')
self.end_headers()
self.wfile.write("<!DOCTYPE html>\n")
self.wfile.write("<html>\n")
self.wfile.write("<body>\n")
def Footer(self):
self.wfile.write("</body>\n")
self.wfile.write("</html>\n")
def processMyRequest(self, filename):
self.BasicHeaders()
self.wfile.write("<h1>Image: " + filename + "</h1>\n")
self.wfile.write('<img src="./'+filename+'" alt="camera">\n')
self.wfile.write('<br><a href="temp.html">Temperatures</a>\n')
self.Footer()
self.wfile.close()
def TemperatureRequest(self, filename):
self.BasicHeaders()
with open(os.path.expanduser(filename)) as myfile:
content = myfile.readlines()
self.wfile.write('<br>\n'.join(content))
self.Footer()
self.wfile.close()
def TemperatureGraphRequest(self, datestring, units):
"""Returns an HTML page with two dygraphs and links.
First is daily max/min temperatures for all time recorded.
Second is minute temps for the given day (a string YYYY/MM/DD)
Temperatures are provided as javascript via separate requests,
in the requested units (a string 'C' or 'F'
"""
self.send_response(200)
self.send_header('Content-Type', 'text/html')
self.end_headers()
dt = datetime.datetime.strptime(datestring, "%Y/%m/%d")
delta = datetime.timedelta(1) # 1 Day
prevday= (dt - delta).strftime("%Y/%m/%d")
nextday= (dt + delta).strftime("%Y/%m/%d")
self.wfile.write("<!DOCTYPE html>\n")
self.wfile.write("<html>\n")
self.wfile.write("<script src='/dygraph-combined.js' ></script>\n")
self.wfile.write("<script src='/dailytemp.js?units=%s' ></script>\n" %
units)
self.wfile.write(
"<script src='/temp.js?date=%s&units=%s' ></script>\n" %
(datestring, units))
self.wfile.write(
"""
<style type="text/css">
.dygraphlegend > span.highlight { border: 2px solid rgb(128, 128, 128); }
th { background-color: rgb(255,200,200); }
</style>""")
if units[0] == 'C':
other_units = "F"
else:
other_units = 'C'
self.wfile.write("""
<body>
<h2>%s</h2>
Temperatures in %s (switch to
<a href="temp.html?date=%s&units=%s">%s</a>)<br>
""" % (dt.strftime("%A %d %B %Y"),
units[0], datestring, other_units, other_units))
# Write the dygraph bits.
self.wfile.write("""
<div id="graphdivdaily"></div>
<div class="dygraphlegend" id="dailylabeldiv"></div>
<div id="graphdiv2"></div>
<div class="dygraphlegend" id="onedaylabeldiv"></div>
<script>
new Dygraph(document.getElementById("graphdivdaily"),
dailytemperatures,
{
legend: 'always',
customBars: true,
labels: dailytemplabels,
ylabel: 'Temperature (%s)',
width: 800,
labelsDiv: "dailylabeldiv",
highlightSeriesOpts: {
strokeWidth: 3,
strokeBorderWidth: 1,
highlightCircleSize: 5,
},
});
new Dygraph(document.getElementById("graphdiv2"),
temperatures,
{
legend: 'always',
labels: templabels,
ylabel: 'Temperature (%s)',
width: 800,
labelsDiv: "onedaylabeldiv",
highlightSeriesOpts: {
strokeWidth: 3,
strokeBorderWidth: 1,
highlightCircleSize: 5,
},
});
</script>
""" % (units[0], units[0]))
self.wfile.write("""
<a href="temp.html?date=%s">%s<<</a>
<a href="temp.html?date=%s">>>%s</a> <br>
<a href="temp.html">Today</a> <br>
<a href="photo.html?width=800&height=600">Photo</a>
</body>
</html>
""" % (prevday, prevday, nextday, nextday))
self.wfile.write("""<table>""")
for l in self.labelmap:
self.wfile.write("""
<tr><td><input type="checkbox" id="check_file%s"
name="{{ f.Basename }}" checked="true"
onclick="change_one_file(this)">%s</td></tr>
""" % (self.labelmap[l], self.labelmap[l]))
self.wfile.write("""</table>""")
self.wfile.close()
@staticmethod
def CanonicalUnits(units):
if units and (units[0] == 'c' or units[0] == 'C'):
return 'C'
else:
return 'F'
@staticmethod
def ConvertTempStringFromC(temp, units):
"""Takes a string of floating point Celsius temp
and returns it as a floating point string in the
requested units."""
if MyHandler.CanonicalUnits(units) == 'C':
return temp
else:
return "%.2f" % (1.8 * float(temp) + 32.0)
@staticmethod
def ConvertTempStringListFromC(temp, units):
"""Takes a list of strings of floating point Celsius temps
and returns them as a list of floating points strings in the
requested units."""
return [MyHandler.ConvertTempStringFromC(t,units) for t in temp]
def MapLabels(self, labels):
"""Change the device_ids to location strings."""
mapped_labels = []
for rawlabel in labels:
rawlabel = rawlabel.lstrip('0')
if rawlabel in self.labelmap:
mapped_labels.append(self.labelmap[rawlabel])
else:
mapped_labels.append(rawlabel)
return mapped_labels
def TemperatureRequestAsJS(self, filename, units):
"""Send the file's data as a javascript array for dygraphs to plot"""
self.LoadLabelMapOnce()
self.send_response(200)
self.send_header('Content-Type', 'text/javascript')
self.end_headers()
self.wfile.write('temperatures = [')
with open(os.path.expanduser(filename)) as myfile:
content = myfile.readlines()
labels = []
# First build a list of all the labels.
for l in content:
a = l.strip().split(' ')
for fields in a[2:]:
parts = fields.split(':')
if not parts[0] in labels:
labels.append('%s' % parts[0])
labels= sorted(labels)
# Now build the lines with the labels in a consistent order.
for l in content:
a = l.strip().split(' ')
thedate = a[0]
thetime = a[1]
tf = thetime.split(':')
timesecs = int(tf[0]) + (int(tf[1]) * 60 + int(tf[2]))/3600.0
temps = ['NaN' for i in labels]
for fields in a[2:]:
parts = fields.split(':')
assert parts[0] in labels
temperature = parts[1]
if float(temperature) != 85.0: # 85C seems to be malfunction.
temps[labels.index(parts[0])] = self.ConvertTempStringFromC(temperature, units)
# temps.append(parts[1])
self.wfile.write(
'[new Date(\"%s %s\"), %s],\n' %
(thedate, thetime, ','.join(temps)))
mapped_labels = ['Date'] + self.MapLabels(labels)
print 'mapped labels in oneday are labels', mapped_labels
self.wfile.write('];\ntemplabels = [%s];\n' % ','.join(['"%s"' % x for x in mapped_labels]))
self.wfile.close()
def DailyTemperatureRequestAsJS(self, filename, units):
"""Send the file's data as a javascript array for dygraphs to plot.
The file is YYYY/MM/DD <device>:mean:min:max <device>...
"""
self.LoadLabelMapOnce()
self.send_response(200)
self.send_header('Content-Type', 'text/javascript')
self.end_headers()
units = self.CanonicalUnits(units)
self.wfile.write('dailytemperatures = [\n')
with open(os.path.expanduser(filename)) as myfile:
content = myfile.readlines()
print 'Labelmap is:', self.labelmap
labels = []
# First build a list of all the labels.
for l in content:
a= l.strip().split(' ')
thedate=a[0]
temps = []
for fields in a[1:]:
parts = fields.split(':')
rawlabel = parts[0]
if not rawlabel in labels:
labels.append(rawlabel)
# TODO sort the labels.
labels= sorted(labels)
# Now build a javascript array with all the data.
for l in content:
a= l.strip().split(' ')
temps = ['NaN' for i in labels]
thedate = a[0]
these_temps = {}
# Put all the temperatures into a dict.
for fields in a[1:]:
parts = fields.split(':')
assert parts[0] in labels
temps[labels.index(parts[0])] = "[%s]" % ",".join(
self.ConvertTempStringListFromC(parts[1:], units))
# Now pull the temperatures out in the order of the labels.
self.wfile.write('[ new Date(\"%s\"), %s],\n' %
(thedate, ','.join(temps)))
first = False
mapped_labels = ['Date'] + self.MapLabels(labels)
print 'mapped labels in daily are labels', mapped_labels
self.wfile.write('];\n dailytemplabels = [%s];\n' % ','.join(
['"%s"' % x for x in mapped_labels]))
self.wfile.close()