forked from WebControlCNC/WebControl
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.py
784 lines (689 loc) · 27.3 KB
/
main.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
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
# main.py
from app import app, socketio
from gevent import monkey
import webbrowser
import socket
import math
import os
import sys
monkey.patch_all()
import schedule
import time
import threading
import json
from flask import Flask, jsonify, render_template, current_app, request, flash, Response, send_file, send_from_directory
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.LogStreamer import LogStreamer # 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")),
]
version = sys.version_info
if version[:2] > (3, 5):
app.data.pythonVersion35 = False
print("Using routines for Python > 3.5")
else:
app.data.pythonVersion35 = True
print("Using routines for Python == 3.5")
app.data.firstRun = False
# app.previousPosX = 0.0
# app.previousPosY = 0.0
app.UIProcessor = UIProcessor()
app.webPageProcessor = WebPageProcessor(app.data)
app.LogStreamer = LogStreamer()
## 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
## logstreamerthread set to None.. will be activated upon first websocket connection from log streamer browser
app.logstreamerthread = None
@app.route("/")
@mobile_template("{mobile/}")
def index(template):
app.data.logger.resetIdler()
macro1Title = (app.data.config.getValue("Maslow Settings", "macro1_title"))[:6]
macro2Title = (app.data.config.getValue("Maslow Settings", "macro2_title"))[:6]
if template == "mobile/":
return render_template("frontpage3d_mobile.html", modalStyle="modal-lg", macro1_title=macro1Title, macro2_title=macro2Title)
else:
return render_template("frontpage3d.html", modalStyle="mw-100 w-75", macro1_title=macro1Title, macro2_title=macro2Title)
@app.route("/controls")
@mobile_template("/controls/{mobile/}")
def controls(template):
app.data.logger.resetIdler()
macro1Title = (app.data.config.getValue("Maslow Settings", "macro1_title"))[:6]
macro2Title = (app.data.config.getValue("Maslow Settings", "macro2_title"))[:6]
if template == "/controls/mobile/":
return render_template("frontpage3d_mobilecontrols.html", modalStyle="modal-lg", isControls=True, macro1_title=macro1Title, macro2_title=macro2Title)
else:
return render_template("frontpage3d.html", modalStyle="mw-100 w-75", macro1_title=macro1Title, macro2_title=macro2Title)
@app.route("/text")
@mobile_template("/text/{mobile/}")
def text(template):
app.data.logger.resetIdler()
macro1Title = (app.data.config.getValue("Maslow Settings", "macro1_title"))[:6]
macro2Title = (app.data.config.getValue("Maslow Settings", "macro2_title"))[:6]
if template == "/text/mobile":
return render_template("frontpageText_mobile.html", modalStyle="modal-lg", isControls=True, macro1_title=macro1Title, macro2_title=macro2Title)
else:
return render_template("frontpageText.html", modalStyle="mw-100 w-75", macro1_title=macro1Title, macro2_title=macro2Title)
@app.route("/logs")
@mobile_template("/logs/{mobile/}")
def logs(template):
print("here")
app.data.logger.resetIdler()
if template == "/logs/mobile/":
return render_template("logs.html")
else:
return render_template("logs.html")
@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("/gpioSettings", methods=["POST"])
def gpioSettings():
app.data.logger.resetIdler()
if request.method == "POST":
result = request.form
app.data.config.updateSettings("GPIO 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.getlist("file[]")
print(f)
home = app.data.config.getHome()
app.data.config.setValue("Computed Settings", "lastSelectedDirectory", directory)
if len(f) > 0:
firstFile = f[0]
for file in f:
app.data.gcodeFile.filename = home + "/.WebControl/gcode/" + directory + "/" + secure_filename(
file.filename)
file.save(app.data.gcodeFile.filename)
app.data.gcodeFile.filename = home + "/.WebControl/gcode/" + directory + "/" + secure_filename(firstFile.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
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("/saveGCode", methods=["POST"])
def saveGCode():
app.data.logger.resetIdler()
if request.method == "POST":
print(request.form)
f = request.form["fileName"]
d = request.form["selectedDirectory"]
app.data.console_queue.put("selectedGcode="+f)
app.data.config.setValue("Computed Settings", "lastSelectedDirectory",d)
home = app.data.config.getHome()
returnVal = app.data.gcodeFile.saveFile(f, home+"/.WebControl/gcode/"+d)
'''
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:
app.data.config.setValue("Maslow Settings", "openFile", f)
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("/openBoard", methods=["POST"])
def openBoard():
app.data.logger.resetIdler()
if request.method == "POST":
f = request.form["selectedBoard"]
app.data.console_queue.put("selectedBoard="+str(f))
tDir = f.split("/")
app.data.config.setValue("Computed Settings","lastSelectedBoardDirectory",tDir[0])
home = app.data.config.getHome()
app.data.gcodeFile.filename = home+"/.WebControl/boards/" + f
app.data.config.setValue("Maslow Settings", "openBoardFile", tDir[1])
returnVal = app.data.boardManager.loadBoard(home+"/.WebControl/boards/"+f)
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("/saveBoard", methods=["POST"])
def saveBoard():
app.data.logger.resetIdler()
if request.method == "POST":
print(request.form)
f = request.form["fileName"]
d = request.form["selectedDirectory"]
app.data.console_queue.put("selectedBoard="+f)
app.data.config.setValue("Computed Settings", "lastSelectedBoardDirectory",d)
home = app.data.config.getHome()
returnVal = app.data.boardManager.saveBoard(f, home+"/.WebControl/boards/"+d)
app.data.config.setValue("Maslow Settings", "openBoardFile", f)
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("/importFileWCJSON", methods=["POST"])
def importFileJSON():
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.importWebControlJSON(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("/importRestoreWebControl", methods=["POST"])
def importRestoreWebControl():
app.data.logger.resetIdler()
if request.method == "POST":
f = request.files["file"]
home = app.data.config.getHome()
secureFilename = home + "/" + secure_filename(f.filename)
f.save(secureFilename)
returnVal = app.data.actions.restoreWebControl(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["gcode"].rstrip())
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("/holeyCalibration", methods=["POST"])
def holeyCalibration():
app.data.logger.resetIdler()
if request.method == "POST":
result = request.form
motorYoffsetEst, distanceBetweenMotors, leftChainTolerance, rightChainTolerance, calibrationError = app.data.actions.holeyCalibrate(
result
)
# print(returnVal)
if motorYoffsetEst:
message = {
"status": 200,
"data": {
"motorYoffset": motorYoffsetEst,
"distanceBetweenMotors": distanceBetweenMotors,
"leftChainTolerance": leftChainTolerance,
"rightChainTolerance": rightChainTolerance,
"calibrationError": calibrationError
},
}
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
@app.route("/editGCode", methods=["POST"])
def editGCode():
app.data.logger.resetIdler()
#print(request.form["gcode"])
if request.method == "POST":
returnVal = app.data.actions.updateGCode(request.form["gcode"].rstrip())
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("/downloadDiagnostics", methods=["GET"])
def downloadDiagnostics():
app.data.logger.resetIdler()
if request.method == "GET":
returnVal = app.data.actions.downloadDiagnostics()
if returnVal != False:
print(returnVal)
return send_file(returnVal, as_attachment=True)
else:
resp = jsonify("failed")
resp.status_code = 500
return resp
@app.route("/backupWebControl", methods=["GET"])
def backupWebControl():
app.data.logger.resetIdler()
if request.method == "GET":
returnVal = app.data.actions.backupWebControl()
if returnVal != False:
print(returnVal)
return send_file(returnVal)
else:
resp = jsonify("failed")
resp.status_code = 500
return resp
@app.route("/editBoard", methods=["POST"])
def editBoard():
app.data.logger.resetIdler()
if request.method == "POST":
returnVal = app.data.boardManager.editBoard(request.form)
if returnVal:
resp = jsonify("success")
resp.status_code = 200
return resp
else:
resp = jsonify("failed")
resp.status_code = 500
return resp
@app.route("/trimBoard", methods=["POST"])
def trimBoard():
app.data.logger.resetIdler()
if request.method == "POST":
returnVal = app.data.boardManager.trimBoard(request.form)
if returnVal:
resp = jsonify("success")
resp.status_code = 200
return resp
else:
resp = jsonify("failed")
resp.status_code = 500
return resp
@app.route("/assets/<path:path>")
def sendDocs(path):
print(path)
return send_from_directory('docs/assets/', path)
@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):
#Note, this shouldn't be called anymore
#todo: cleanup
app.data.logger.resetIdler()
data = json.dumps({"title": msg["data"]})
print(data)
#socketio.emit("message", {"command": "closeContentModals", "data": data, "dataFormat": "json"},
# namespace="/MaslowCNC", )
'''
todo: cleanup
not used
@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("alertModalClosed", namespace="/MaslowCNC")
def alertModalClosed(msg):
app.data.logger.resetIdler()
data = json.dumps({"title": msg["data"]})
socketio.emit("message", {"command": "closeAlertModals", "data": data, "dataFormat": "json"},
namespace="/MaslowCNC", )
@socketio.on("requestPage", namespace="/MaslowCNC")
def requestPage(msg):
app.data.logger.resetIdler()
app.data.console_queue.put(request.sid)
client = request.sid
try:
page, title, isStatic, modalSize, modalType, resume = app.webPageProcessor.createWebPage(msg["data"]["page"],msg["data"]["isMobile"], msg["data"]["args"])
#if msg["data"]["page"] != "help":
# client = "all"
data = json.dumps({"title": title, "message": page, "isStatic": isStatic, "modalSize": modalSize, "modalType": modalType, "resume":resume, "client":client})
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})
address = app.data.hostAddress
data = json.dumps({"hostAddress": address})
print(data)
socketio.emit("message", {"command": "hostAddress", "data": data, "dataFormat":"json"}, namespace="/MaslowCNC",)
if app.data.pyInstallUpdateAvailable:
app.data.ui_queue1.put("Action", "pyinstallUpdate", "on")
@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()
retval = app.data.actions.processAction(msg)
if retval == "Shutdown":
print("Shutting Down")
socketio.stop()
print("Shutdown")
if (retval == "TurnOffRPI"):
print("Turning off RPI")
os.system('sudo poweroff')
@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("checkForBoardUpdate", namespace="/MaslowCNC")
def checkForBoardUpdate(msg):
app.data.logger.resetIdler()
# this currently doesn't check for updated board, it just resends it..
app.data.ui_queue1.put("Action", "boardUpdate", "")
@socketio.on("connect", namespace="/MaslowCNCLogs")
def log_connect():
app.data.console_queue.put("connected to log")
app.data.console_queue.put(request.sid)
if app.logstreamerthread == None:
app.logstreamerthread = socketio.start_background_task(
app.LogStreamer.start, current_app._get_current_object()
)
app.logstreamerthread.start()
socketio.emit("my response", {"data": "Connected", "count": 0}, namespace="/MaslowCNCLog")
@socketio.on("disconnect", namespace="/MaslowCNCLogs")
def log_disconnect():
app.data.console_queue.put("Client disconnected")
@app.template_filter('isnumber')
def isnumber(s):
try:
float(s)
return True
except ValueError:
return False
#def shutdown():
# print("Shutdown")
if __name__ == "__main__":
app.debug = False
app.config["SECRET_KEY"] = "secret!"
#look for touched file
app.data.config.checkForTouchedPort()
webPort = app.data.config.getValue("WebControl Settings", "webPort")
webPortInt = 5000
try:
webPortInt = int(webPort)
if webPortInt < 0 or webPortInt > 65535:
webPortInt = 5000
except Exception as e:
app.data.console_queue.put(e)
app.data.console_queue.put("Invalid port assignment found in webcontrol.json")
print("-$$$$$-")
print(os.path.abspath(__file__))
app.data.releaseManager.processAbsolutePath(os.path.abspath(__file__))
print("-$$$$$-")
print("opening browser")
webPortStr = str(webPortInt)
webbrowser.open_new_tab("//localhost:"+webPortStr)
host_name = socket.gethostname()
host_ip = socket.gethostbyname(host_name)
app.data.hostAddress = host_ip + ":" + webPortStr
#app.data.shutdown = shutdown
socketio.run(app, use_reloader=False, host="0.0.0.0", port=webPortInt)
# socketio.run(app, host='0.0.0.0')