def io():
	global serial_no
	ws = request.environ['wsgi.websocket'] 
	if not ws:
		abort(400)
	logger = utils.logger_init('%s/%s.json' % (LOGS_PATH, serial_no))
	results = utils.logger_init('%s/%s.json' % (RESULTS_PATH, serial_no))
	if UI_DEBUG:
		time.sleep(0.1)
		utils.websocket_send(ws, '{"tester":"DI","result":true}', results)
		time.sleep(0.1)
		utils.websocket_send(ws, '{"tester":"ADC","result":true}', results)
		time.sleep(0.1)
		utils.websocket_send(ws, '{"tester":"UART","result":true}', results)
		time.sleep(0.1)
		utils.websocket_send(ws, '{"tester":"I2C","result":true}', results)
		time.sleep(0.1)
		utils.websocket_send(ws, '{"tester":"9-Axis","result":true}', results)
		time.sleep(0.1)
		utils.websocket_send(ws, '{"tester":"Airpressure","result":true}', results)
		time.sleep(0.1)
		utils.websocket_send(ws, '{"tester":"Charger","result":true}', results)
	else:
		com = utils.command_open()
		tester.tester_io(com, logger, results, ws)
		utils.command_close(com)
	utils.logger_term(logger)
	utils.logger_term(results)
	return 'OK'
def rtc():
	ws = request.environ['wsgi.websocket'] 
	if not ws:
		abort(400)
	logger = utils.logger_init('%s/%s.json' % (LOGS_PATH, serial_no))
	results = utils.logger_init('%s/%s.json' % (RESULTS_PATH, serial_no))
	if UI_DEBUG:
		utils.websocket_send(ws, '{"tester":"RTC","result":true,"seconds":120}', results)
	else:
		com = utils.command_open()
		tester.tester_rtc(com, logger, results, ws)
		utils.command_close(com)
	utils.logger_term(results)
	utils.logger_term(logger)
	return 'OK'
def ble():
	global serial_no
	ws = request.environ['wsgi.websocket'] 
	if not ws:
		abort(400)
	results = utils.logger_init('%s/%s.json' % (RESULTS_PATH, serial_no))
	if UI_DEBUG:
		time.sleep(1)
		utils.websocket_send(ws, '{"tester":"BLE","result":true,"RSSI":-50}', results)
	else:
		com = utils.command_open()
		tester.tester_ble(com, results, ws)
		utils.command_close(com)
	utils.logger_term(results)
	return 'OK'
def term():
	ws = request.environ['wsgi.websocket'] 
	if not ws:
		abort(400)
	results = utils.logger_init('%s/%s.json' % (RESULTS_PATH, serial_no))
	if UI_DEBUG:
		pass
	else:
		com = utils.command_open()
		tester.tester_terminate(com)	#ファームウェア停止
		firm_writer.erase_tester(com)	#ファームウェア消去
		tz_power.off(com)		#USB電源OFF
		utils.command_close(com)
	utils.websocket_send(ws, '{"tester":"Terminated","result":true}', results)
	utils.logger_term(results)

	return 'OK'
def tz1_firm():
	global serial_no
	ws = request.environ['wsgi.websocket'] 
	if not ws:
		abort(400)
	results = utils.logger_init('%s/%s.json' % (RESULTS_PATH, serial_no))
	if UI_DEBUG:
		utils.websocket_send(ws, '{"tester":"TZ1Firm","result":true}', results)
	else:
		com = utils.command_open()

		if firm_writer.write_tester(com):
			utils.websocket_send(ws, '{"tester":"TZ1Firm","result":true}', results)
		else:
			utils.websocket_send(ws, '{"tester":"TZ1Firm","result":false}', results)
		utils.command_close(com)
	utils.logger_term(results)
	return 'OK'
def power():
	global serial_no
	ws = request.environ['wsgi.websocket'] 
	if not ws:
		abort(400)
	ws.receive()
	logger = utils.logger_init('%s/%s.json' % (LOGS_PATH, serial_no), 'wb')
	results = utils.logger_init('%s/%s.json' % (RESULTS_PATH, serial_no))
	if UI_DEBUG:
		time.sleep(0.5)
		utils.websocket_send(ws, '{"tester":"Current","result":true}', results)
		time.sleep(0.5)
		utils.websocket_send(ws, '{"tester":"Voltage","result":true}', results)
	else:
		com = utils.command_open()
		if com:
			tz_power.check(com, ws, logger, results)
			utils.command_close(com)
		else:
			utils.websocket_send(ws, '{"tester":"Current","result":false}', results)
			utils.websocket_send(ws, '{"tester":"Voltage","result":false}', results)
	utils.logger_term(logger)
	utils.logger_term(results)
	return 'OK'