def serial_handler(connect): if connect == '1': # print 'js is asking to connect serial' if not SerialManager.is_connected(): try: global SERIAL_PORT, BITSPERSECOND, GUESS_PREFIX if not SERIAL_PORT: SERIAL_PORT = SerialManager.match_device( GUESS_PREFIX, BITSPERSECOND) SerialManager.connect(SERIAL_PORT, BITSPERSECOND) ret = "Serial connected to %s:%d." % (SERIAL_PORT, BITSPERSECOND) + '<br>' time.sleep(1.0) # allow some time to receive a prompt/welcome SerialManager.flush_input() SerialManager.flush_output() return ret except serial.SerialException: SERIAL_PORT = None print "Failed to connect to serial." return "" elif connect == '0': # print 'js is asking to close serial' if SerialManager.is_connected(): if SerialManager.close(): return "1" else: return "" elif connect == "2": # print 'js is asking if serial connected' if SerialManager.is_connected(): return "1" else: return "" else: print 'ambigious connect request from js: ' + connect return ""
def serial_handler(connect): if connect == '1': # print 'js is asking to connect serial' if not SerialManager.is_connected(): try: global SERIAL_PORT, BITSPERSECOND, GUESS_PREFIX if not SERIAL_PORT: SERIAL_PORT = SerialManager.match_device(GUESS_PREFIX, BITSPERSECOND) SerialManager.connect(SERIAL_PORT, BITSPERSECOND) ret = "Serial connected to %s:%d." % (SERIAL_PORT, BITSPERSECOND) + '<br>' time.sleep(1.0) # allow some time to receive a prompt/welcome SerialManager.flush_input() SerialManager.flush_output() return ret except serial.SerialException: SERIAL_PORT = None print "Failed to connect to serial." return "" elif connect == '0': # print 'js is asking to close serial' if SerialManager.is_connected(): if SerialManager.close(): return "1" else: return "" elif connect == "2": # print 'js is asking if serial connected' if SerialManager.is_connected(): return "1" else: return "" else: print 'ambigious connect request from js: ' + connect return ""
def flash_firmware_handler(): if SerialManager.is_connected(): SerialManager.close() global SERIAL_PORT, GUESS_PREFIX if not SERIAL_PORT: SERIAL_PORT = SerialManager.match_device(GUESS_PREFIX) flash_upload(SERIAL_PORT, resources_dir()) return '<h2>flashing finished!</h2> Check Log window for possible errors.<br><a href="/">return</a>'
def flash_firmware_handler(firmware_file=FIRMWARE): global SERIAL_PORT, GUESS_PREFIX return_code = 1 if SerialManager.is_connected(): SerialManager.close() # get serial port by url argument # e.g: /flash_firmware?port=COM3 if 'port' in request.GET.keys(): serial_port = request.GET['port'] if serial_port[:3] == "COM" or serial_port[:4] == "tty.": SERIAL_PORT = serial_port # get serial port by enumeration method # currenty this works on windows only for updating the firmware if not SERIAL_PORT: SERIAL_PORT = SerialManager.match_device(GUESS_PREFIX, BITSPERSECOND) # resort to brute force methode # find available com ports and try them all if not SERIAL_PORT: comport_list = SerialManager.list_devices(BITSPERSECOND) for port in comport_list: print "Trying com port: " + port return_code = flash_upload(port, resources_dir(), firmware_file, HARDWARE) if return_code == 0: print "Success with com port: " + port SERIAL_PORT = port break else: return_code = flash_upload(SERIAL_PORT, resources_dir(), firmware_file, HARDWARE) ret = [] ret.append('Using com port: %s<br>' % (SERIAL_PORT)) ret.append('Using firmware: %s<br>' % (firmware_file)) if return_code == 0: print "SUCCESS: Arduino appears to be flashed." ret.append('<h2>Successfully Flashed!</h2><br>') ret.append('<a href="/">return</a>') return ''.join(ret) else: print "ERROR: Failed to flash Arduino." ret.append( '<h2>Flashing Failed!</h2> Check terminal window for possible errors. ' ) ret.append( 'Most likely LasaurApp could not find the right serial port.') ret.append('<br><a href="/flash_firmware/' + firmware_file + '">try again</a> or <a href="/">return</a><br><br>') if os.name != 'posix': ret.append( 'If you know the COM ports the Arduino is connected to you can specifically select it here:' ) for i in range(1, 13): ret.append( '<br><a href="/flash_firmware?port=COM%s">COM%s</a>' % (i, i)) return ''.join(ret)
def run_with_callback(host, port, rfidreader, logger, powertimer): """ Start a wsgiref server instance with control over the main loop. This is a function that I derived from the bottle.py run() """ debug(True) handler = default_app() handler.catchall = False handler.rfidreader = rfidreader handler.logger = logger handler.powertimer = powertimer server = make_server(host, port, handler, handler_class=HackedWSGIRequestHandler) server.timeout = 0.01 #server.quiet = True print "Persistent storage root is: " + storage_dir() print "-----------------------------------------------------------------------------" print "Bottle server starting up ..." print "Serial is set to %d bps" % BITSPERSECOND print "Point your browser to: " print "http://%s:%d/ (local)" % ('127.0.0.1', port) # if host == '': # try: # print "http://%s:%d/ (public)" % (socket.gethostbyname(socket.gethostname()), port) # except socket.gaierror: # # print "http://beaglebone.local:4444/ (public)" # pass print "Use Ctrl-C to quit." print "-----------------------------------------------------------------------------" print # auto-connect on startup global SERIAL_PORT if not SERIAL_PORT: SERIAL_PORT = SerialManager.match_device(GUESS_PREFIX, BITSPERSECOND) SerialManager.connect(SERIAL_PORT, BITSPERSECOND) # open web-browser try: webbrowser.open_new_tab('http://127.0.0.1:'+str(port)) pass except webbrowser.Error: print "Cannot open Webbrowser, please do so manually." sys.stdout.flush() # make sure everything gets flushed server.timeout = 0 while 1: try: SerialManager.send_queue_as_ready() server.handle_request() time.sleep(0.0004) except KeyboardInterrupt: break print "\nShutting down..." SerialManager.close()
def flash_firmware_handler(firmware_file=FIRMWARE): global user_approved global user_admin if not user_approved and user_admin: return 'Access denied' logger.log(current_user, 'Flashing ATMEGA') return_code = 1 if SerialManager.is_connected(): SerialManager.close() # get serial port by url argument # e.g: /flash_firmware?port=COM3 if 'port' in request.GET.keys(): serial_port = request.GET['port'] if serial_port[:3] == "COM" or serial_port[:4] == "tty.": SERIAL_PORT = serial_port # get serial port by enumeration method # currenty this works on windows only for updating the firmware if not SERIAL_PORT: SERIAL_PORT = SerialManager.match_device(GUESS_PREFIX, BITSPERSECOND) # resort to brute force methode # find available com ports and try them all if not SERIAL_PORT: comport_list = SerialManager.list_devices(BITSPERSECOND) for port in comport_list: print "Trying com port: " + port return_code = flash_upload(port, resources_dir(), firmware_file, HARDWARE) if return_code == 0: print "Success with com port: " + port SERIAL_PORT = port break else: return_code = flash_upload(SERIAL_PORT, resources_dir(), firmware_file, HARDWARE) ret = [] ret.append('Using com port: %s<br>' % (SERIAL_PORT)) ret.append('Using firmware: %s<br>' % (firmware_file)) if return_code == 0: print "SUCCESS: Arduino appears to be flashed." ret.append('<h2>Successfully Flashed!</h2><br>') ret.append('<a href="/">return</a>') return ''.join(ret) else: print "ERROR: Failed to flash Arduino." ret.append('<h2>Flashing Failed!</h2> Check terminal window for possible errors. ') ret.append('Most likely LasaurApp could not find the right serial port.') ret.append('<br><a href="/flash_firmware/'+firmware_file+'">try again</a> or <a href="/">return</a><br><br>') if os.name != 'posix': ret. append('If you know the COM ports the Arduino is connected to you can specifically select it here:') for i in range(1,13): ret. append('<br><a href="/flash_firmware?port=COM%s">COM%s</a>' % (i, i)) return ''.join(ret)
def run_with_callback(host, port): """ 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 = make_server(host, port, handler, handler_class=HackedWSGIRequestHandler) server.timeout = 0.01 server.quiet = True print "Persistent storage root is: " + storage_dir() print "-----------------------------------------------------------------------------" print "Bottle server starting up ..." print "Serial is set to %d bps" % BITSPERSECOND print "Point your browser to: " print "http://%s:%d/ (local)" % ('127.0.0.1', port) # if host == '': # try: # print "http://%s:%d/ (public)" % (socket.gethostbyname(socket.gethostname()), port) # except socket.gaierror: # # print "http://beaglebone.local:4444/ (public)" # pass print "Use Ctrl-C to quit." print "-----------------------------------------------------------------------------" print # auto-connect on startup global SERIAL_PORT if not SERIAL_PORT: SERIAL_PORT = SerialManager.match_device(GUESS_PREFIX, BITSPERSECOND) SerialManager.connect(SERIAL_PORT, BITSPERSECOND) # open web-browser try: webbrowser.open_new_tab('http://127.0.0.1:' + str(port)) pass except webbrowser.Error: print "Cannot open Webbrowser, please do so manually." sys.stdout.flush() # make sure everything gets flushed server.timeout = 0 while 1: try: SerialManager.send_queue_as_ready() server.handle_request() time.sleep(0.0004) except KeyboardInterrupt: break print "\nShutting down..." SerialManager.close()
def flash_firmware_handler(): global SERIAL_PORT, GUESS_PREFIX return_code = 1 if SerialManager.is_connected(): SerialManager.close() # get serial port by url argument # e.g: /flash_firmware?port=COM3 if "port" in request.GET.keys(): serial_port = request.GET["port"] if serial_port[:3] == "COM" or serial_port[:4] == "tty.": SERIAL_PORT = serial_port # get serial port by enumeration method # currenty this works on windows only for updating the firmware if not SERIAL_PORT: SERIAL_PORT = SerialManager.match_device(GUESS_PREFIX, BITSPERSECOND) # resort to brute force methode # find available com ports and try them all if not SERIAL_PORT: comport_list = SerialManager.list_devices(BITSPERSECOND) for port in comport_list: print "Trying com port: " + port return_code = flash_upload(port, resources_dir()) if return_code == 0: print "Success with com port: " + port SERIAL_PORT = port break else: return_code = flash_upload(SERIAL_PORT, resources_dir()) ret = [] ret.append("Using com port: %s<br>" % (SERIAL_PORT)) if return_code == 0: print "SUCCESS: Arduino appears to be flashed." ret.append("<h2>Successfully Flashed!</h2><br>") ret.append('<a href="/">return</a>') return "".join(ret) else: SERIAL_PORT = None print "ERROR: Failed to flash Arduino." ret.append("<h2>Flashing Failed!</h2> Check Log window for possible errors. ") ret.append('Most likely LasaurApp could not find the right serial port.<br><a href="/">return</a><br><br>') if os.name != "posix": ret.append("If you know the COM ports the Arduino is connected to you can specifically select it here:") for i in range(1, 13): ret.append('<br><a href="/flash_firmware?port=COM%s">COM%s</a>' % (i, i)) return "".join(ret)
# (1) get the serial device from the argument list SERIAL_PORT = args.port print "Using serial device '" + SERIAL_PORT + "' from command line." else: # (2) get the serial device from the config file if os.path.isfile(CONFIG_FILE): fp = open(CONFIG_FILE) line = fp.readline().strip() if len(line) > 3: SERIAL_PORT = line print "Using serial device '" + SERIAL_PORT + "' from '" + CONFIG_FILE + "'." if not SERIAL_PORT: if args.match: GUESS_PREFIX = args.match SERIAL_PORT = SerialManager.match_device(GUESS_PREFIX, BITSPERSECOND) if SERIAL_PORT: print "Using serial device '" + str(SERIAL_PORT) if os.name == 'posix': # not for windows for now print "(first device to match: " + args.match + ")" else: SERIAL_PORT = SerialManager.match_device(GUESS_PREFIX, BITSPERSECOND) if SERIAL_PORT: print "Using serial device '" + str( SERIAL_PORT) + "' by best guess." if not SERIAL_PORT: print "-----------------------------------------------------------------------------" print "WARNING: LasaurApp doesn't know what serial device to connect to!"
# (1) get the serial device from the argument list SERIAL_PORT = args.port print "Using serial device '"+ SERIAL_PORT +"' from command line." else: # (2) get the serial device from the config file if os.path.isfile(CONFIG_FILE): fp = open(CONFIG_FILE) line = fp.readline().strip() if len(line) > 3: SERIAL_PORT = line print "Using serial device '"+ SERIAL_PORT +"' from '" + CONFIG_FILE + "'." if not SERIAL_PORT: if args.match: GUESS_PREFIX = args.match SERIAL_PORT = SerialManager.match_device(GUESS_PREFIX, BITSPERSECOND) if SERIAL_PORT: print "Using serial device '"+ str(SERIAL_PORT) if os.name == 'posix': # not for windows for now print "(first device to match: " + args.match + ")" else: SERIAL_PORT = SerialManager.match_device(GUESS_PREFIX, BITSPERSECOND) if SERIAL_PORT: print "Using serial device '"+ str(SERIAL_PORT) +"' by best guess." if not SERIAL_PORT: print "-----------------------------------------------------------------------------" print "WARNING: LasaurApp doesn't know what serial device to connect to!" print "Make sure the Lasersaur hardware is connectd to the USB interface." if os.name == 'nt':
def run_with_callback(host, port): """ 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 = make_server(host, port, handler, handler_class=HackedWSGIRequestHandler) if CERTSDIR: server.socket = ssl.wrap_socket(server.socket, keyfile=KEYFILE, certfile=CERTSFILE, server_side=True, cert_reqs=ssl.CERT_REQUIRED, ca_certs=CA_CERTSFILE) loadAccounts() server.verify_request = verify_request server.timeout = 0.01 server.quiet = True print "Persistent storage root is: " + storage_dir() print "-----------------------------------------------------------------------------" print "Bottle server starting up ..." print "Serial is set to %d bps" % BITSPERSECOND print "Point your browser to: " if CERTSDIR: print "https://%s:%d/" % (COMMON_NAME, port) elif COMMON_NAME: print "http://%s:%d/" % (COMMON_NAME, port) else: print "http://%s:%d/" % ('127.0.0.1', port) print "Use Ctrl-C to quit." print "-----------------------------------------------------------------------------" print # auto-connect on startup global SERIAL_PORT if not SERIAL_PORT: SERIAL_PORT = SerialManager.match_device(GUESS_PREFIX, BITSPERSECOND) SerialManager.connect(SERIAL_PORT, BITSPERSECOND) # I:Mega Start time.sleep(1.0) # I:Mega End # # open web-browser # try: # webbrowser.open_new_tab('http://127.0.0.1:'+str(port)) # pass # except webbrowser.Error: # print "Cannot open Webbrowser, please do so manually." sys.stdout.flush() # make sure everything gets flushed server.timeout = 0 lastPowerStatus = 0 powerStateChange = 0 while 1: try: serial_handler('1') SerialManager.send_queue_as_ready() server.handle_request() if HARDWARE == 'raspberrypi': powerStatus = RPiPowerControl.interval_check() if powerStatus != lastPowerStatus: powerStateChange = 1 lastPowerStatus = powerStatus if powerStateChange: powerStateChange = checkStatus() time.sleep(0.0004) except KeyboardInterrupt: if HARDWARE == 'raspberrypi': RPiPowerControl.gpio_cleanup() break except: import traceback traceback.print_exc() break print "\nShutting down..." if redirect_pid: os.kill(redirect_pid, signal.SIGTERM) SerialManager.close()
# (1) get the serial device from the argument list SERIAL_PORT = args.port print "Using serial device '"+ SERIAL_PORT +"' from command line." else: # (2) get the serial device from the config file if os.path.isfile(CONFIG_FILE): fp = open(CONFIG_FILE) line = fp.readline().strip() if len(line) > 3: SERIAL_PORT = line print "Using serial device '"+ SERIAL_PORT +"' from '" + CONFIG_FILE + "'." if not SERIAL_PORT: if args.match: GUESS_PREFIX = args.match SERIAL_PORT = SerialManager.match_device(GUESS_PREFIX) if SERIAL_PORT: print "Using serial device '"+ str(SERIAL_PORT) print "(first device to match: " + args.match + ")" else: SERIAL_PORT = SerialManager.match_device(GUESS_PREFIX) if SERIAL_PORT: print "Using serial device '"+ str(SERIAL_PORT) +"' by best guess." if not SERIAL_PORT: print "-----------------------------------------------------------------------------" print "WARNING: LasaurApp doesn't know what serial device to connect to!" print "Make sure the Lasersaur hardware is connectd to the USB interface." print "-----------------------------------------------------------------------------" # run