forked from WebControlCNC/WebControl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
412 lines (345 loc) · 14.1 KB
/
main.py
File metadata and controls
412 lines (345 loc) · 14.1 KB
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
# main.py
from app import app, socketio
from gevent import monkey
monkey.patch_all()
import schedule
import time
import threading
import json
from flask import Flask, jsonify, render_template, current_app, request, flash, Response
from flask_mobility.decorators import mobile_template
from werkzeug import secure_filename
from Background.UIProcessor import UIProcessor # do this after socketio is declared
from Background.WebMCPProcessor import WebMCPProcessor
from Background.WebMCPProcessor import ConsoleProcessor
from DataStructures.data import Data
from Connection.nonVisibleWidgets import NonVisibleWidgets
from WebPageProcessor.webPageProcessor import WebPageProcessor
from os import listdir
from os.path import isfile, join
app.data = Data()
app.nonVisibleWidgets = NonVisibleWidgets()
app.nonVisibleWidgets.setUpData(app.data)
app.data.config.computeSettings(None, None, None, True)
app.data.config.parseFirmwareVersions()
app.data.units = app.data.config.getValue("Computed Settings", "units")
app.data.tolerance = app.data.config.getValue("Computed Settings", "tolerance")
app.data.distToMove = app.data.config.getValue("Computed Settings", "distToMove")
app.data.distToMoveZ = app.data.config.getValue("Computed Settings", "distToMoveZ")
app.data.unitsZ = app.data.config.getValue("Computed Settings", "unitsZ")
app.data.comport = app.data.config.getValue("Maslow Settings", "COMport")
app.data.gcodeShift = [
float(app.data.config.getValue("Advanced Settings", "homeX")),
float(app.data.config.getValue("Advanced Settings", "homeY")),
]
app.data.firstRun = False
# app.previousPosX = 0.0
# app.previousPosY = 0.0
app.UIProcessor = UIProcessor()
app.webPageProcessor = WebPageProcessor(app.data)
## this defines the schedule for running the serial port open connection
def run_schedule():
while 1:
schedule.run_pending()
time.sleep(1)
## this runs the scheduler to check for connections
app.th = threading.Thread(target=run_schedule)
app.th.daemon = True
app.th.start()
## this runs the thread that processes messages from the controller
app.th1 = threading.Thread(target=app.data.messageProcessor.start)
app.th1.daemon = True
app.th1.start()
## this runs the thread that sends debugging messages to the terminal and webmcp (if active)
app.th2 = threading.Thread(target=app.data.consoleProcessor.start)
app.th2.daemon = True
app.th2.start()
## uithread set to None.. will be activated upon first websocket connection from browser
app.uithread = None
## uithread set to None.. will be activated upon first websocket connection from webmcp
app.mcpthread = None
@app.route("/")
@mobile_template("{mobile/}")
def index(template):
app.data.logger.resetIdler()
if template == "mobile/":
return render_template("frontpage3d_mobile.html", modalStyle="modal-lg")
else:
return render_template("frontpage3d.html", modalStyle="mw-100 w-75")
@app.route("/controls")
@mobile_template("/controls/{mobile/}")
def controls(template):
app.data.logger.resetIdler()
if template == "/controls/mobile/":
return render_template("frontpage3d_mobilecontrols.html", modalStyle="modal-lg", isControls=True)
else:
return render_template("frontpage3d.html", modalStyle="mw-100 w-75")
@app.route("/maslowSettings", methods=["POST"])
def maslowSettings():
app.data.logger.resetIdler()
if request.method == "POST":
result = request.form
app.data.config.updateSettings("Maslow Settings", result)
message = {"status": 200}
resp = jsonify(message)
resp.status_code = 200
return resp
@app.route("/advancedSettings", methods=["POST"])
def advancedSettings():
app.data.logger.resetIdler()
if request.method == "POST":
result = request.form
app.data.config.updateSettings("Advanced Settings", result)
message = {"status": 200}
resp = jsonify(message)
resp.status_code = 200
return resp
@app.route("/webControlSettings", methods=["POST"])
def webControlSettings():
app.data.logger.resetIdler()
if request.method == "POST":
result = request.form
app.data.config.updateSettings("WebControl Settings", result)
message = {"status": 200}
resp = jsonify(message)
resp.status_code = 200
return resp
@app.route("/cameraSettings", methods=["POST"])
def cameraSettings():
app.data.logger.resetIdler()
if request.method == "POST":
result = request.form
app.data.config.updateSettings("Camera Settings", result)
message = {"status": 200}
resp = jsonify(message)
resp.status_code = 200
return resp
@app.route("/uploadGCode", methods=["POST"])
def uploadGCode():
app.data.logger.resetIdler()
if request.method == "POST":
result = request.form
directory = result["selectedDirectory"]
#print(directory)
f = request.files["file"]
home = app.data.config.getHome()
app.data.config.setValue("Computed Settings", "lastSelectedDirectory", directory)
app.data.gcodeFile.filename = home+"/.WebControl/gcode/" + directory+"/"+secure_filename(f.filename)
f.save(app.data.gcodeFile.filename)
returnVal = app.data.gcodeFile.loadUpdateFile()
if returnVal:
message = {"status": 200}
resp = jsonify(message)
resp.status_code = 200
return resp
else:
message = {"status": 500}
resp = jsonify(message)
resp.status_code = 500
return resp
@app.route("/openGCode", methods=["POST"])
def openGCode():
app.data.logger.resetIdler()
if request.method == "POST":
f = request.form["selectedGCode"]
app.data.console_queue.put("selectedGcode="+str(f))
tDir = f.split("/")
app.data.config.setValue("Computed Settings","lastSelectedDirectory",tDir[0])
home = app.data.config.getHome()
app.data.gcodeFile.filename = home+"/.WebControl/gcode/" + f
app.data.config.setValue("Maslow Settings", "openFile", tDir[1])
returnVal = app.data.gcodeFile.loadUpdateFile()
if returnVal:
message = {"status": 200}
resp = jsonify(message)
resp.status_code = 200
return resp
else:
message = {"status": 500}
resp = jsonify(message)
resp.status_code = 500
return resp
@app.route("/importFile", methods=["POST"])
def importFile():
app.data.logger.resetIdler()
if request.method == "POST":
f = request.files["file"]
home = app.data.config.getHome()
secureFilename = home+"/.WebControl/imports/" + secure_filename(f.filename)
f.save(secureFilename)
returnVal = app.data.importFile.importGCini(secureFilename)
if returnVal:
message = {"status": 200}
resp = jsonify(message)
resp.status_code = 200
return resp
else:
message = {"status": 500}
resp = jsonify(message)
resp.status_code = 500
return resp
@app.route("/sendGcode", methods=["POST"])
def sendGcode():
app.data.logger.resetIdler()
#print(request.form)#["gcodeInput"])
if request.method == "POST":
returnVal = app.data.actions.sendGcode(request.form["gcodeInput"])
if returnVal:
message = {"status": 200}
resp = jsonify("success")
resp.status_code = 200
return resp
else:
message = {"status": 500}
resp = jsonify("failed")
resp.status_code = 500
return resp
@app.route("/triangularCalibration", methods=["POST"])
def triangularCalibration():
app.data.logger.resetIdler()
if request.method == "POST":
result = request.form
motorYoffsetEst, rotationRadiusEst, chainSagCorrectionEst, cut34YoffsetEst = app.data.actions.calibrate(
result
)
# print(returnVal)
if motorYoffsetEst:
message = {
"status": 200,
"data": {
"motorYoffset": motorYoffsetEst,
"rotationRadius": rotationRadiusEst,
"chainSagCorrection": chainSagCorrectionEst,
"calibrationError": cut34YoffsetEst,
},
}
resp = jsonify(message)
resp.status_code = 200
return resp
else:
message = {"status": 500}
resp = jsonify(message)
resp.status_code = 500
return resp
@app.route("/opticalCalibration", methods=["POST"])
def opticalCalibration():
app.data.logger.resetIdler()
if request.method == "POST":
result = request.form
message = {"status": 200}
resp = jsonify(message)
resp.status_code = 200
return resp
else:
message = {"status": 500}
resp = jsonify(message)
resp.status_code = 500
return resp
@app.route("/quickConfigure", methods=["POST"])
def quickConfigure():
app.data.logger.resetIdler()
if request.method == "POST":
result = request.form
app.data.config.updateQuickConfigure(result)
message = {"status": 200}
resp = jsonify(message)
resp.status_code = 200
return resp
@socketio.on("checkInRequested", namespace="/WebMCP")
def checkInRequested():
socketio.emit("checkIn", namespace="/WebMCP")
@socketio.on("connect", namespace="/WebMCP")
def watchdog_connect():
app.data.console_queue.put("watchdog connected")
app.data.console_queue.put(request.sid)
socketio.emit("connect", namespace="/WebMCP")
if app.mcpthread == None:
app.data.console_queue.put("going to start mcp thread")
app.mcpthread = socketio.start_background_task(
app.data.mcpProcessor.start, current_app._get_current_object()
)
app.data.console_queue.put("created mcp thread")
app.mcpthread.start()
app.data.console_queue.put("started mcp thread")
@socketio.on("my event", namespace="/MaslowCNC")
def my_event(msg):
app.data.console_queue.put(msg["data"])
@socketio.on("modalClosed", namespace="/MaslowCNC")
def modalClosed(msg):
app.data.logger.resetIdler()
data = json.dumps({"title": msg["data"]})
socketio.emit("message", {"command": "closeModals", "data": data, "dataFormat": "json"},
namespace="/MaslowCNC", )
@socketio.on("contentModalClosed", namespace="/MaslowCNC")
def contentModalClosed(msg):
app.data.logger.resetIdler()
data = json.dumps({"title": msg["data"]})
print(data)
socketio.emit("message", {"command": "closeContentModals", "data": data, "dataFormat": "json"},
namespace="/MaslowCNC", )
@socketio.on("actionModalClosed", namespace="/MaslowCNC")
def actionModalClosed(msg):
app.data.logger.resetIdler()
data = json.dumps({"title": msg["data"]})
socketio.emit("message", {"command": "closeActionModals", "data": data, "dataFormat": "json"},
namespace="/MaslowCNC", )
@socketio.on("requestPage", namespace="/MaslowCNC")
def requestPage(msg):
app.data.logger.resetIdler()
try:
page, title, isStatic, modalSize, modalType, resume = app.webPageProcessor.createWebPage(msg["data"]["page"],msg["data"]["isMobile"], msg["data"]["args"])
data = json.dumps({"title": title, "message": page, "isStatic": isStatic, "modalSize": modalSize, "modalType": modalType, "resume":resume})
socketio.emit("message", {"command": "activateModal", "data": data, "dataFormat": "json"},
namespace="/MaslowCNC",
)
except Exception as e:
app.data.console_queue.put(e)
@socketio.on("connect", namespace="/MaslowCNC")
def test_connect():
app.data.console_queue.put("connected")
app.data.console_queue.put(request.sid)
if app.uithread == None:
app.uithread = socketio.start_background_task(
app.UIProcessor.start, current_app._get_current_object()
)
app.uithread.start()
if not app.data.connectionStatus:
app.data.console_queue.put("Attempting to re-establish connection to controller")
app.data.serialPort.openConnection()
socketio.emit("my response", {"data": "Connected", "count": 0})
@socketio.on("disconnect", namespace="/MaslowCNC")
def test_disconnect():
app.data.console_queue.put("Client disconnected")
@socketio.on("action", namespace="/MaslowCNC")
def command(msg):
app.data.logger.resetIdler()
app.data.actions.processAction(msg)
@socketio.on("settingRequest", namespace="/MaslowCNC")
def settingRequest(msg):
app.data.logger.resetIdler()
# didn't move to actions.. this request is just to send it computed values.. keeping it here makes it faster than putting it through the UIProcessor
setting, value = app.data.actions.processSettingRequest(msg["data"]["section"], msg["data"]["setting"])
if setting is not None:
data = json.dumps({"setting": setting, "value": value})
socketio.emit("message", {"command": "requestedSetting", "data": data, "dataFormat": "json"}, namespace="/MaslowCNC",)
@socketio.on("updateSetting", namespace="/MaslowCNC")
def updateSetting(msg):
app.data.logger.resetIdler()
if not app.data.actions.updateSetting(msg["data"]["setting"], msg["data"]["value"]):
app.data.ui_queue1.put("Alert", "Alert", "Error updating setting")
@socketio.on("checkForGCodeUpdate", namespace="/MaslowCNC")
def checkForGCodeUpdate(msg):
app.data.logger.resetIdler()
# this currently doesn't check for updated gcode, it just resends it..
## the gcode file might change the active units so we need to inform the UI of the change.
app.data.ui_queue1.put("Action", "unitsUpdate", "")
app.data.ui_queue1.put("Action", "gcodeUpdate", "")
@socketio.on_error_default
def default_error_handler(e):
app.data.console_queue.put(request.event["message"]) # "my error event"
app.data.console_queue.put(request.event["args"]) # (data,)1
if __name__ == "__main__":
app.debug = False
app.config["SECRET_KEY"] = "secret!"
socketio.run(app, use_reloader=False, host="0.0.0.0")
# socketio.run(app, host='0.0.0.0')