-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathsite.py
227 lines (189 loc) · 5.46 KB
/
site.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
# The MIT License (MIT)
#
# Copyright (c) Sharil Tumin
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#-----------------------------------------------------------------------------
# site.py MVC - This is the model M of MVC
from uos import urandom as ran
from machine import Pin
from html import pg, hdr
from help import Setting as cam
from help import help
# Init global variables
rot='0'
flash_light=Pin(04,Pin.OUT)
class auth: pass
server=''
client=''
def pwd(size=8):
alfa = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890'
return ''.join([alfa[x] for x in [(ran(1)[0] % len(alfa)) for _ in range(size)]])
# These will be set by server script as site.ip and site.camera
ip=''
camera=None
app={}
def route(p):
def w(g):
app[p]=g
return w
def OK(cs):
p=pg['OK']
ln=len(p)+2
cs.write(b'%s\r\n\r\n%s\r\n' % (hdr['OK']%ln, p))
def ERR(cs):
p=pg['err']
ln=len(p)+2
cs.write(b'%s\r\n\r\n%s\r\n' % (hdr['err']%ln, p))
def NO(cs):
p=pg['no']
ln=len(p)+2
cs.write(b'%s\r\n\r\n%s\r\n' % (hdr['err']%ln, p))
def NOP(cs):
p=pg['none']
ln=len(p)+2
cs.write(b'%s\r\n\r\n%s\r\n' % (hdr['none']%ln, p))
def setting(cs,w,ok,cmd,v):
#print("setting:", w, ok)
if ok:
cmd(w); cam[v]=w
OK(cs)
else:
ERR(cs)
@route('/')
def root(cs,v):
p=help(server)
ln=len(p)+2
cs.write(b'%s\r\n\r\n%s\r\n' % (hdr['OK']%ln, p))
OK(cs)
@route('/login')
def login(cs,v):
if auth.on:
if auth.ip=='':
if v==auth.pwd:
auth.ip=client
OK(cs)
@route('/logout')
def logout(cs,v):
if auth.on:
auth.pwd=pwd()
auth.ip=''
print(f'New PWD: {auth.pwd}')
OK(cs)
@route('/favicon.ico')
def fav(cs,v):
p=pg['favicon']
ln=len(p)+2
cs.write(b'%s\r\n\r\n%s\r\n' % (hdr['favicon']%ln, p))
@route('/webcam')
def webcam(cs,v):
global ip,rot
p=pg['foto']%(f'http://{ip}/live',rot) # needed by opera, vivaldi, midori..
ln=len(p)+2
cs.write(b'%s\r\n\r\n%s\r\n' % (hdr['foto']%ln, p))
@route('/live')
def live(cs,v): # live stream
cs.write(b'%s\r\n\r\n' % hdr['stream'])
cs.setblocking(True)
pic=camera.capture
put=cs.write
hr=hdr['frame']
while True:
try:
put(b'%s\r\n\r\n' % hr)
put(pic())
put(b'\r\n') # send and flush the send buffer
except Exception as e:
print(e)
break
@route('/snap')
def snap(cs,v):
global ip,rot
p=pg['foto']%(f'http://{ip}/foto',rot)
ln=len(p)+2
cs.write(b'%s\r\n\r\n%s\r\n' % (hdr['foto']%ln, p))
@route('/blitz')
def blitz(cs,v):
global ip,rot
p=pg['foto']%(f'http://{ip}/boto',rot)
ln=len(p)+2
cs.write(b'%s\r\n\r\n%s\r\n' % (hdr['foto']%ln, p))
@route('/foto')
def foto(cs,v): # still photo
#buf=camera.capture()
#ln=len(buf)
cs.setblocking(True)
cs.write(b'%s\r\n\r\n' % hdr['pic'])
cs.write(camera.capture())
cs.write(b'\r\n') # send and flush the send buffer
#nc=cs.write(b'%s\r\n\r\n' % (hdr['pix']%ln)+buf)
@route('/boto')
def boto(cs,v): # still photo blitz on
#buf=camera.capture()
#ln=len(buf)
cs.setblocking(True)
cs.write(b'%s\r\n\r\n' % hdr['pic'])
flash_light.on()
cs.write(camera.capture())
flash_light.off()
cs.write(b'\r\n')
#nc=cs.write(b'%s\r\n\r\n' % (hdr['pix']%ln)+buf)
@route('/rot')
def rotate(cs,v):
global rot
rot=v
OK(cs)
@route('/flash')
def flash(cs,v):
if v==1: flash_light.on()
else: flash_light.off()
OK(cs)
@route('/fmt')
def fmt(cs,w):
setting(cs,w,(w>=0 and w<=2),camera.pixformat,'pixformat')
@route('/pix')
def pix(cs,w):
setting(cs,w,(w>0 and w<18),camera.framesize,'framesize')
@route('/qua')
def qua(cs,w):
setting(cs,w,(w>9 and w<64),camera.quality,'quality')
@route('/con')
def con(cs,w):
setting(cs,w,(w>-3 and w<3),camera.contrast,'contrast')
@route('/sat')
def sat(cs,w):
setting(cs,w,(w>-3 and w<3),camera.saturation,'saturation')
@route('/bri')
def bri(cs,w):
setting(cs,w,(w>-3 and w<3),camera.brightness,'brightness')
@route('/ael')
def ael(cs,w):
setting(cs,w,(w>-3 and w<3),camera.aelevels,'aelevels')
@route('/aec')
def aec(cs,w):
setting(cs,w,(w>=0 and w<=1200),camera.aecvalue,'aecvalue')
@route('/agc')
def agc(cs,w):
setting(cs,w,(w>=0 and w<=30),camera.agcgain,'agcgain')
@route('/spe')
def spe(cs,w):
setting(cs,w,(w>=0 and w<7),camera.speffect,'speffect')
@route('/wbl')
def wbl(cs,w):
setting(cs,w,(w>=0 and w<5),camera.whitebalance,'whitebalance')