forked from stefanix/LasaurApp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
161 lines (134 loc) · 4.97 KB
/
app.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
import time
import os.path
import wsgiref.simple_server
from bottle import *
import serial_manager, serial
# ARDUINO_PORT = '/dev/tty.usbmodem621'
# ARDUINO_PORT = '/dev/tty.usbmodemfa131'
# ARDUINO_PORT = '/dev/tty.usbmodemfd122'
ARDUINO_PORT = '/dev/tty.usbmodemfd121'
BITSPERSECOND = 57600
current_dir = os.path.dirname(os.path.abspath(__file__))
def run_with_callback(host='127.0.0.1', port=4444, timeout=0.01):
""" Start a wsgiref server instance with control over the main loop.
This is a function that I derived from the bottle.py run()
"""
handler = default_app()
server = wsgiref.simple_server.make_server(host, port, handler)
server.timeout = timeout
print "Bottle server starting up ..."
print "Serial is set to %d bps" % BITSPERSECOND
print "Point your browser to: http://%s:%d/" % (host, port)
print "Use Ctrl-C to quit."
print
while 1:
try:
serial_manager.send_queue_as_ready()
server.handle_request()
except KeyboardInterrupt:
break
print "\nShutting down..."
serial_manager.close()
@route('/hello')
def hello_handler():
return "Hello World!!"
@route('/connect')
def connect_handler():
global ARDUINO_PORT, BITSPERSECOND
try:
serial_manager.connect(ARDUINO_PORT, BITSPERSECOND)
ret = "Serial connected to %s:%d." % (ARDUINO_PORT, BITSPERSECOND) + '<br>'
serial_manager.write("\r\n\r\n")
time.sleep(1) # allow some time to receive a prompt/welcome
serial_manager.flushInput()
return serial_manager.get_responses('<br>')
except serial.SerialException:
print "Failed to connect to serial."
return ""
@route('/longtest')
def longtest_handler():
fp = open("longtest.ngc")
for line in fp:
serial_manager.queue_for_sending(line)
return "Longtest queued."
@route('/css/:path#.+#')
def static_css_handler(path):
return static_file(path, root=os.path.join(current_dir, 'css'))
@route('/js/:path#.+#')
def static_css_handler(path):
return static_file(path, root=os.path.join(current_dir, 'js'))
@route('/img/:path#.+#')
def static_css_handler(path):
return static_file(path, root=os.path.join(current_dir, 'img'))
@route('/')
@route('/index.html')
@route('/app.html')
def default_handler():
return static_file('app.html', root=current_dir)
@route('/canvas')
def default_handler():
return static_file('testCanvas.html', root=current_dir)
@route('/serial/:connect')
def serial_handler(connect):
if connect == '1' and not serial_manager.is_connected():
print 'js is asking to connect serial'
try:
global arduino_port, baudrate
serial_manager.connect(ARDUINO_PORT, BITSPERSECOND)
ret = "Serial connected to %s:%d." % (ARDUINO_PORT, BITSPERSECOND) + '<br>'
time.sleep(1.0) # allow some time to receive a prompt/welcome
resp = serial_manager.get_responses('<br>')
if resp == "": resp = ret
return resp
except serial.SerialException:
print "Failed to connect to serial."
return ""
elif connect == '0' and serial_manager.is_connected():
print 'js is asking to closer serial'
if serial_manager.close(): return "1"
else: return ""
elif connect == "2":
print 'js is asking if serial connected'
if serial_manager.is_connected(): return "1"
else: return ""
else:
print 'got neither: ' + connect
return ""
@route('/gcode/:gcode_line')
def gcode_handler(gcode_line):
if serial_manager.is_connected():
print gcode_line
serial_manager.queue_for_sending(gcode_line + '\n')
return "Queued for sending."
else:
return ""
@route('/gcode', method='POST')
def gcode_handler_submit():
gcode_program = request.forms.get('gcode_program')
if gcode_program and serial_manager.is_connected():
print gcode_program
lines = gcode_program.split('\n')
for line in lines:
serial_manager.queue_for_sending(line + '\n')
return "Queued for sending."
else:
return ""
@route('/queue_pct_done')
def queue_pct_done_handler():
return serial_manager.get_queue_percentage_done()
# @route('/svg_upload', method='POST')
# def svg_upload():
# data = request.files.get('data')
# if data.file:
# raw = data.file.read() # This is dangerous for big files
# filename = data.filename
# print "You uploaded %s (%d bytes)." % (filename, len(raw))
# return raw
# # boundarys = SVG(raw).get_boundarys()
# # gcode = write_GCODE(boundarys, 1200, 255, 0.2822222222, 0.0, 0.0)
# # # 0.2822222222 converts from px to mm (at 90dpi)
# # # this is necessary because inkscape stores everything in px units
# # return gcode
# return "You missed a field."
debug(True)
run_with_callback(host='localhost')