]> vault307.fbx.one Git - esp32Cam.git/blob - site.py
esp32Camera webserver
[esp32Cam.git] / site.py
1
2 # The MIT License (MIT)
3 #
4 # Copyright (c) Sharil Tumin
5 #
6 # Permission is hereby granted, free of charge, to any person obtaining a copy
7 # of this software and associated documentation files (the "Software"), to deal
8 # in the Software without restriction, including without limitation the rights
9 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 # copies of the Software, and to permit persons to whom the Software is
11 # furnished to do so, subject to the following conditions:
12 #
13 # The above copyright notice and this permission notice shall be included in
14 # all copies or substantial portions of the Software.
15 #
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 # THE SOFTWARE.
23 #-----------------------------------------------------------------------------
24
25 # site.py MVC - This is the model M of MVC
26
27 from uos import urandom as ran
28 from machine import Pin
29 from html import pg, hdr
30 from help import Setting as cam
31 from help import help
32
33 # Init global variables
34 rot='0'
35 flash_light=Pin(04,Pin.OUT)
36
37 class auth: pass
38
39 server=''
40 client=''
41
42 def pwd(size=8):
43 alfa = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890'
44 return ''.join('testCam')
45
46 # These will be set by server script as site.ip and site.camera
47 ip=''
48 camera=None
49
50 app={}
51 def route(p):
52 def w(g):
53 app[p]=g
54 return w
55
56 def OK(cs):
57 p=pg['OK']
58 ln=len(p)+2
59 cs.write(b'%s\r\n\r\n%s\r\n' % (hdr['OK']%ln, p))
60
61 def ERR(cs):
62 p=pg['err']
63 ln=len(p)+2
64 cs.write(b'%s\r\n\r\n%s\r\n' % (hdr['err']%ln, p))
65
66 def NO(cs):
67 p=pg['no']
68 ln=len(p)+2
69 cs.write(b'%s\r\n\r\n%s\r\n' % (hdr['err']%ln, p))
70
71 def NOP(cs):
72 p=pg['none']
73 ln=len(p)+2
74 cs.write(b'%s\r\n\r\n%s\r\n' % (hdr['none']%ln, p))
75
76 def setting(cs,w,ok,cmd,v):
77 #print("setting:", w, ok)
78 if ok:
79 cmd(w); cam[v]=w
80 OK(cs)
81 else:
82 ERR(cs)
83
84 @route('/')
85 def root(cs,v):
86 p=help(server)
87 ln=len(p)+2
88 cs.write(b'%s\r\n\r\n%s\r\n' % (hdr['OK']%ln, p))
89 OK(cs)
90
91 @route('/login')
92 def login(cs,v):
93 if auth.on:
94 if auth.ip=='':
95 if v==auth.pwd:
96 auth.ip=client
97 OK(cs)
98
99 @route('/logout')
100 def logout(cs,v):
101 if auth.on:
102 auth.pwd=pwd()
103 auth.ip=''
104 print(f'New PWD: {auth.pwd}')
105 OK(cs)
106
107 @route('/favicon.ico')
108 def fav(cs,v):
109 p=pg['favicon']
110 ln=len(p)+2
111 cs.write(b'%s\r\n\r\n%s\r\n' % (hdr['favicon']%ln, p))
112
113 @route('/webcam')
114 def webcam(cs,v):
115 global ip,rot
116 p=pg['foto']%(f'http://{ip}/live',rot) # needed by opera, vivaldi, midori..
117 ln=len(p)+2
118 cs.write(b'%s\r\n\r\n%s\r\n' % (hdr['foto']%ln, p))
119
120 @route('/live')
121 def live(cs,v): # live stream
122 cs.write(b'%s\r\n\r\n' % hdr['stream'])
123 cs.setblocking(True)
124 pic=camera.capture
125 put=cs.write
126 hr=hdr['frame']
127 while True:
128 try:
129 put(b'%s\r\n\r\n' % hr)
130 put(pic())
131 put(b'\r\n') # send and flush the send buffer
132 except Exception as e:
133 print(e)
134 break
135
136 @route('/snap')
137 def snap(cs,v):
138 global ip,rot
139 p=pg['foto']%(f'http://{ip}/foto',rot)
140 ln=len(p)+2
141 cs.write(b'%s\r\n\r\n%s\r\n' % (hdr['foto']%ln, p))
142
143 @route('/blitz')
144 def blitz(cs,v):
145 global ip,rot
146 p=pg['foto']%(f'http://{ip}/boto',rot)
147 ln=len(p)+2
148 cs.write(b'%s\r\n\r\n%s\r\n' % (hdr['foto']%ln, p))
149
150 @route('/foto')
151 def foto(cs,v): # still photo
152 #buf=camera.capture()
153 #ln=len(buf)
154 cs.setblocking(True)
155 cs.write(b'%s\r\n\r\n' % hdr['pic'])
156 cs.write(camera.capture())
157 cs.write(b'\r\n') # send and flush the send buffer
158 #nc=cs.write(b'%s\r\n\r\n' % (hdr['pix']%ln)+buf)
159
160 @route('/boto')
161 def boto(cs,v): # still photo blitz on
162 #buf=camera.capture()
163 #ln=len(buf)
164 cs.setblocking(True)
165 cs.write(b'%s\r\n\r\n' % hdr['pic'])
166 flash_light.on()
167 cs.write(camera.capture())
168 flash_light.off()
169 cs.write(b'\r\n')
170 #nc=cs.write(b'%s\r\n\r\n' % (hdr['pix']%ln)+buf)
171
172 @route('/rot')
173 def rotate(cs,v):
174 global rot
175 rot=v
176 OK(cs)
177
178 @route('/flash')
179 def flash(cs,v):
180 if v==1: flash_light.on()
181 else: flash_light.off()
182 OK(cs)
183
184 @route('/fmt')
185 def fmt(cs,w):
186 setting(cs,w,(w>=1 and w<=9),camera.pixformat,'pixformat')
187
188 @route('/pix')
189 def pix(cs,w):
190 setting(cs,w,(w>0 and w<18),camera.framesize,'framesize')
191
192 @route('/qua')
193 def qua(cs,w):
194 setting(cs,w,(w>9 and w<64),camera.quality,'quality')
195
196 @route('/con')
197 def con(cs,w):
198 setting(cs,w,(w>-3 and w<3),camera.contrast,'contrast')
199
200 @route('/sat')
201 def sat(cs,w):
202 setting(cs,w,(w>-3 and w<3),camera.saturation,'saturation')
203
204 @route('/bri')
205 def bri(cs,w):
206 setting(cs,w,(w>-3 and w<3),camera.brightness,'brightness')
207
208 @route('/ael')
209 def ael(cs,w):
210 setting(cs,w,(w>-3 and w<3),camera.aelevels,'aelevels')
211
212 @route('/aec')
213 def aec(cs,w):
214 setting(cs,w,(w>=0 and w<=1200),camera.aecvalue,'aecvalue')
215
216 @route('/agc')
217 def agc(cs,w):
218 setting(cs,w,(w>=0 and w<=30),camera.agcgain,'agcgain')
219
220 @route('/spe')
221 def spe(cs,w):
222 setting(cs,w,(w>=0 and w<7),camera.speffect,'speffect')
223
224 @route('/wbl')
225 def wbl(cs,w):
226 setting(cs,w,(w>=0 and w<5),camera.whitebalance,'whitebalance')