-
Notifications
You must be signed in to change notification settings - Fork 43
/
webserver.py
160 lines (127 loc) · 5.78 KB
/
webserver.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
"""
Copyright 2020 LeMaRiva|Tech (Mauro Riva) info@lemariva.com
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import gc
import machine
import json
import time
import camera
from microWebSrv import MicroWebSrv
class webcam():
def __init__(self):
self.saturation = 0
self.quality = 10
self.brightness = 0
self.contrast = 0
self.vflip = 0
self.hflip = 0
self.framesize = camera.FRAME_VGA
self.routeHandlers = [
("/", "GET", self._httpHandlerIndex),
("/logo.svg", "GET", self._httpLogo),
("/stream/<d>", "GET", self._httpStream),
("/upy/<saturation>/<brightness>/<contrast>/<quality>/<vflip>/<hflip>/<framesize>", "GET", self._httpHandlerSetData),
("/upy", "GET", self._httpHandlerGetData),
("/memory/<query>", "GET", self._httpHandlerMemory)
]
def run(self, app_config):
self.led = machine.Pin(app_config['led'], machine.Pin.OUT)
# Camera resilience - if we fail to init try to deinit and init again
if app_config['camera'] == 'ESP32-CAM':
camera.init(0, format=camera.JPEG, framesize=self.framesize) #ESP32-CAM
elif app_config['camera'] == 'M5CAMERA':
camera.init(0, format=camera.JPEG, framesize=self.framesize, d0=32, d1=35, d2=34, d3=5, d4=39,
d5=18, d6=36, d7=19, href=26, vsync=25, reset=15, sioc=23, siod=22, xclk=27, pclk=21) #M5CAMERA
mws = MicroWebSrv(routeHandlers=self.routeHandlers, webPath="www/")
mws.Start(threaded=True)
gc.collect()
def _httpStream(self, httpClient, httpResponse, routeArgs):
image = camera.capture()
headers = { 'Last-Modified' : 'Fri, 1 Jan 2018 23:42:00 GMT', \
'Cache-Control' : 'no-cache, no-store, must-revalidate' }
httpResponse.WriteResponse(code=200, headers=headers,
contentType="image/jpeg",
contentCharset="UTF-8",
content=image)
def _httpLogo(self, httpClient, httpResponse):
f = open("www/logo.svg", "r")
content = f.read()
f.close()
httpResponse.WriteResponseOk(headers=None,
contentType="image/svg+xml",
contentCharset="UTF-8",
content=content)
def _httpHandlerIndex(self, httpClient, httpResponse):
f = open("www/index.html", "r")
content = f.read()
f.close()
headers = { 'Last-Modified' : 'Fri, 1 Jan 2018 23:42:00 GMT', \
'Cache-Control' : 'no-cache, no-store, must-revalidate' }
httpResponse.WriteResponseOk(headers=None,
contentType="text/html",
contentCharset="UTF-8",
content=content)
def _httpHandlerSetData(self, httpClient, httpResponse, routeArgs):
self.saturation = int(routeArgs['saturation']) - 2
self.brightness = int(routeArgs['brightness']) - 2
self.contrast = int(routeArgs['contrast']) - 2
self.quality = int(routeArgs['quality'])
self.vflip = bool(routeArgs['vflip'])
self.hflip = bool(routeArgs['hflip'])
self.framesize = int(routeArgs['framesize'])
camera.saturation(self.saturation)
camera.brightness(self.brightness)
camera.contrast(self.contrast)
camera.quality(self.quality)
camera.flip(self.vflip)
camera.mirror(self.hflip)
camera.framesize(self.framesize)
data = {
'saturation': self.saturation,
'brightness': self.brightness,
'contrast': self.contrast,
'quality': self.quality,
'vflip': self.vflip,
'hflip': self.hflip,
'framesize': self.framesize
}
self._newdata = True
httpResponse.WriteResponseOk(headers=None,
contentType="text/html",
contentCharset="UTF-8",
content=json.dumps(data))
def _httpHandlerGetData(self, httpClient, httpResponse):
data = {
'saturation': self.saturation,
'brightness': self.brightness,
'contrast': self.contrast,
'quality': self.quality,
'vflip': self.vflip,
'hflip': self.hflip,
'framesize': self.framesize
}
httpResponse.WriteResponseOk(headers=None,
contentType="application/json",
contentCharset="UTF-8",
content=json.dumps(data))
def _httpHandlerMemory(self, httpClient, httpResponse, routeArgs):
print("In Memory HTTP variable route :")
query = str(routeArgs['query'])
if 'gc' in query or 'collect' in query:
gc.collect()
content = """\
{}
""".format(gc.mem_free())
httpResponse.WriteResponseOk(headers=None,
contentType="text/html",
contentCharset="UTF-8",
content=content)