-
Notifications
You must be signed in to change notification settings - Fork 1
/
webUser.py
387 lines (336 loc) · 12.7 KB
/
webUser.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
# -*- coding: utf-8 -*-
"""
This file is part of egg-force-one.
egg-force-one is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Foobar is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with egg-force-one. If not, see <http://www.gnu.org/licenses/>. 2
############################################################################
Created on Mon Sep 25 16:24:39 2017
@author: CASAL Guillaume
"""
import os
import sys
import signal
import shutil
from threading import Thread
import git
import logging
from flask import Flask, Response, request
#from flask_socketio import SocketIO
from werkzeug.utils import secure_filename
#from camera import Camera
os.chdir(os.path.dirname(os.path.realpath(__file__))) # nous place dans le dossier de l'executable
#print(os.path.dirname(os.path.realpath(__file__)))
class WebUser(Thread):
def __init__(self, sysVar):
self.sysVar = sysVar
Thread.__init__(self)
pass
def msgTerminal(self, msg=""):
try:
#self.socketio.emit('MsgTerm', msg, broadcast=True)
pass
except:
print("WEB[ERROR] msgTerm not work")
pass
pass
def html(self):
tmp = open("html.html", "r", encoding="utf-8")
data = tmp.read()
tmp.close()
return data
def script(self):
tmp = open("script.js", "r", encoding="utf-8")
data = tmp.read()
tmp.close()
return data
def css(self):
tmp = open("css.css", "r")
data = tmp.read()
tmp.close()
return data
def font(self,name):
tmp = open("font/" + name, "rb") # lecture byte a byte
data = tmp.read()
tmp.close()
return data
def initData(self):
#fichier = open("socket.io.js", "r")
#self.socketIO = fichier.read()
#fichier.close()
fichier = open("jquery.min.js", "r")
self.jquery = fichier.read()
fichier.close()
pass
def shutdown_server(self):
func = request.environ.get('werkzeug.server.shutdown')
if func is None:
raise RuntimeError('Not running with the Werkzeug Server')
func()
def init(self):
if sys.platform.startswith('win'):
self.pathCut = "\\"
pass
else:
self.pathCut = "/"
pass
app = Flask(__name__)
#socketio = SocketIO(app)
log = logging.getLogger('werkzeug')
log.setLevel(logging.ERROR)
self.app = app
self.log = log
#self.socketio = socketio
@app.route('/shutdown')
def shutdown():
self.shutdown_server()
return 'Server shutting down...'
@app.route('/')
def home():
return Response(self.html(), content_type='charset=utf-8')
@app.route('/css.css')
def css():
return Response(self.css(), mimetype='text/css')
@app.route('/font/<name>')
def font(name):
#print("name : " + str(name))
return Response(self.font(name), mimetype='application/woff2')
#@app.route('/socket.io.js')
#def routeSocketIO():
# return self.socketIO
@app.route('/jquery.min.js')
def routeJquery():
return self.jquery
@app.route('/script.js')
def routeScript():
return Response(self.script(), mimetype='application/javascript')
@app.route('/upload', methods=['POST'])
def upload():
print('web[EVENT] upload')
f = request.files['uploadFile']
try:
f.save(self.sysVar.FolderPrint + request.form['groupe'] + self.pathCut + secure_filename(f.filename))
pass
except:
try:
os.mkdir(self.sysVar.FolderPrint + request.form['groupe'])
print("web[EVENT] dir create")
except:
print("web[ERROR] can not create dir for upload file")
return "BUG UPLOAD 1"
try:
f.save(self.sysVar.FolderPrint + request.form['groupe'] + self.pathCut + secure_filename(f.filename))
pass
except:
print("web[ERROR] can not upload file")
return "BUG UPLOAD 2"
else:
return "OK"
else:
return "OK"
pass
@app.route('/dirPrint', methods=['POST'])
def dirPrint():
try:
path = str(request.form['path'])
path = path[1:]
#print("chemin : " + path)
pass
except:
path = ''
pass
listElements = os.listdir(self.sysVar.FolderPrint + path) #list tout les element du dossier
nbelement = len(listElements)
a = 0
anser = ""
while (a < nbelement):
if a != 0:
anser += ";"
pass
anser += listElements[a] + '|' + str(os.path.isdir(self.sysVar.FolderPrint + path + listElements[a]))
a += 1
pass
try:
return anser
except:
return "TEMP"
pass
@app.route('/update', methods=['POST'])
def update():
try:
tmp = git.Git().pull()
if (tmp == "Already up-to-date."):
return "0" # deja a jour
else:
return "1" # mise a jour effectuer
except:
return "2" # mise a jour impossible
pass
return "bug /update"
@app.route('/printSrc', methods=['POST'])
def printSrc():
try:
path = str(request.form['path'])
print("PrintSrc : " + self.sysVar.FolderPrint + " ::: "+ str(path))
if (path[0] == "\\" or path[0] == "/"):
path = path[1:]
print("test path : " + self.sysVar.FolderPrint + " :and: " + path)
pass
if sys.platform.startswith('win'):
pass
if (self.sysVar.printStatut == 0 or self.sysVar.printStatut == 2 or self.sysVar.printStatut == 4):
print("test")
self.sysVar.threadControl.initPrint(path)
return "1" # impression en cour
else:
return "2" # impossible impression en cour
pass
except Exception as e:
print(e)
return "bugPrintSrc"
pass
@app.route('/paramChange', methods=['POST'])
def paramChange():
dictTemp = self.sysVar.allVarDict
#print("test : " + str(request.form['test']))
try:
for test in dictTemp:
if test[0] != '_':
#print(str(test) + " : " + str(dictTemp[test]))
try:
if (request.form[test]):
try:
dictTemp[test] = int(request.form[test])
#print("type int")
pass
except:
dictTemp[test] = request.form[test]
pass
print(test + " : change by : " + str(request.form[test]))
pass
pass
except:
#print(test + " : not found")
pass
#print(test + " : " + str(dictTemp[test]))
pass
pass
pass
except:
pass
return "end"
@app.route('/paramGetAll', methods=['GET','POST'])
def paramGetAll():
self.sysVar.threadUsb.recherche()
#print(self.sysVar.usbAllPort)
dataReturn = ""
dictTemp = self.sysVar.allVarDict
for tmp in dictTemp:
if tmp[0] != '_' and tmp != "allVarDict":
tmp2 = type(dictTemp[tmp])
if tmp == "usbAllPort":
dataReturn += tmp + ":" + "["
for element in self.sysVar.usbAllPort:
dataReturn += element[0] + ","
pass
dataReturn = dataReturn[:-1]
dataReturn += "]"
dataReturn += "&"
pass
elif tmp2 is str:
#print(type(dictTemp[tmp]))
dataReturn += tmp + ":" + dictTemp[tmp] + "&"
pass
elif tmp2 is bool or tmp2 is list or tmp2 is int:
#print(type(dictTemp[tmp]))
dataReturn += tmp + ":" + str(dictTemp[tmp]) + "&"
pass
else:
#print("no add : " + str(type(dictTemp[tmp])))
pass
pass
pass
return dataReturn[:len(dataReturn)-1]
@app.route('/createDir',methods=['POST'])
def createDir():
path = request.form["path"]
name = request.form["name"]
os.mkdir(self.sysVar.FolderPrint + path + name)
return "end"
@app.route('/deletePathDir', methods=['POST'])
def deletePathDir():
path = request.form["path"]
shutil.rmtree(self.sysVar.FolderPrint + path)
return "end"
@app.route('/deletePathFolder', methods=['POST'])
def deletePathFolder():
path = request.form["path"]
os.remove(self.sysVar.FolderPrint + path)
return "end"
@app.route('/renameDirFolder', methods=['POST'])
def renameDirFolder():
path = request.form["path"]
pathRename = request.form["pathRename"]
os.rename(self.sysVar.FolderPrint + path, self.sysVar.FolderPrint + pathRename)
return "end"
@app.route('/changeUsb', methods=['POST'])
def changeUsb():
port = request.form["port"]
bauderate = request.form["bauderate"]
if (port == "auto"):
port = False
pass
try:
self.sysVar.usbSerial.close()
pass
except:
print("serial not close")
pass
self.sysVar.usbPort = port
if (bauderate == "auto"):
self.sysVar.autoBaud = True
pass
else:
self.sysVar.usbBauderate = int(bauderate)
pass
self.sysVar.usbBug = False
self.sysVar.usbRun = False
self.sysVar.usbConnect = False
return "ok"
def gen(camera):
"""Video streaming generator function."""
while True:
frame = camera.get_frame()
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + bytearray(frame) + b'\r\n')
@app.route('/video_feed')
def video_feed():
"""Video streaming route. Put this in the src attribute of an img tag."""
return "off"
#return Response(gen(Camera()),
# mimetype='multipart/x-mixed-replace; boundary=frame')
def startWeb(self):
self.app.run(host = self.sysVar.webHost, port = self.sysVar.webPort, threaded=True)
#self.socketio.run(self.app, host = self.sysVar.webHost, port = self.sysVar.webPort)
pass
def run(self):
self.initData()
self.init()
self.startWeb()
pass
pass
if __name__ == "__main__":
import sysVar
threadWebUser = WebUser(sysVar)
sysVar.threadWebUser = threadWebUser
threadWebUser.setDaemon(True)
threadWebUser.setName("web user egg force one")
threadWebUser.start()
pass