def tester_ble(com, results, ws):
	# BLEモードへ切り替え
	utils.command_send(com, 'm003\r', None)

	# BLEスキャン
	ret = commands.getstatusoutput('./get_rssi.sh %s' % config.TESTER_SUFFIX)
	if ret[0] == 0:
		rssi = ret[1].split()[1]
		msg = '{"tester":"BLE","result":true,"RSSI":%s}' % rssi
	else:
		msg = '{"tester":"BLE","result":false}'
	utils.websocket_send(ws, msg, results)

	# 選択モードへ切り替え
	utils.command_send(com, 'm000\r', None)
def tester_rtc(com, logger, results, ws):
	start_time = datetime.datetime(2015, 1, 1, 0, 0, 0)
	line = utils.command_send(com, 'r000\r', logger)
	res = json.loads(line)
	if res:
		now_time = datetime.datetime(res['year'], res['month'], res['day'], res['hour'], res['minute'], res['second'])
		delta = now_time - start_time
		if delta.seconds > 1:
			msg = '{"tester":"RTC","result":true,"seconds":%d}' % delta.seconds
		else:
			msg = '{"tester":"RTC","result":false,"seconds":%d}' % delta.seconds
		utils.websocket_send(ws, msg, results)
	else:
		msg = '{"tester":"RTC","result":false}'
		utils.websocket_send(ws, msg, results)
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 tester_usb(com, results, ws):
	# USBモードへ切り替え
	utils.command_send(com, 'm002\r', None)

	# USB認識チェック
	for i in range(0, 10):
		ret = commands.getstatusoutput('lsusb -d 0930:1703')
		if len(ret[1]) > 0:
			msg = '{"tester":"USB","result":true}'
			utils.websocket_send(ws, msg, results)
			break;
	else:
		msg = '{"tester":"USB","result":false}'
		utils.websocket_send(ws, msg, results)

	# 選択モードへ切り替え
	utils.command_send(com, 'm000\r', None)
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 start():
	global serial_no
	ws = request.environ['wsgi.websocket'] 
	msg = ''
	if not ws:
		abort(400)

	serial_no = ws.receive()
	if serial_no.startswith('TZ1'):
		msg = '{"tester":"Start","result":true, "serial_no":"%s"}' % serial_no
	else:
		serial_no = 'invalid_serial'
		msg = '{"tester":"Start","result":false, "serial_no":"%s"}' % serial_no
	
	results = utils.logger_init('%s/%s.json' % (RESULTS_PATH, serial_no), 'wb')
	utils.websocket_send(ws, msg, results)
	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'
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 check(com, ws, logger, results):
	#USB電源OFF
	utils.command_send(com, 'U001\r', None)
	time.sleep(0.5)
	#USB電源ON
	utils.command_send(com, 'U000\r', None)
	
	#TZ1電源SW ON
	line = utils.command_send(com, "P001\r", None)
	if not line:
		utils.websocket_send(ws, '{"tester":"Current","result":false}', results)
		return False
	time.sleep(1)
	
	#過電流検出チェック
	line = utils.command_send(com, "C000\r", logger)
	res = json.loads(line)
	if res == None:
		utils.websocket_send(ws, '{"tester":"Current","result":false}', results)
		return False
	if res['current'] == 1:
		utils.websocket_send(ws, '{"tester":"Current","result":false}', results)
		return False
	utils.websocket_send(ws, '{"tester":"Current","result":true}', results)

	cnt = 0
	while True:
		#バッテリー充電Enable Hi
		utils.command_send(com, "B001\r", None)
		time.sleep(0.5)
		#バッテリー充電Enable Lo
		utils.command_send(com, "B000\r", None)
		time.sleep(0.5)
		if check_CHG(com, logger):
			break
		cnt = cnt + 1
		if cnt > 10:
			utils.websocket_send(ws, '{"tester":"Voltage","result":false}', results)
			return False
		time.sleep(1)

	#TZ_VSYS(1/2)計測
	if check_VSYS(com, logger) == False:
		utils.websocket_send(ws, '{"tester":"Voltage","result":false}', results)
		return False
	
	#TZ_D3V3(1/2)計測
	if check_3V3(com, logger) == False:
		utils.websocket_send(ws, '{"tester":"Voltage","result":false}', results)
		return False

	utils.websocket_send(ws, '{"tester":"Voltage","result":true}', results)
	
	#TZ1電源SW OFF
	utils.command_send(com, "P000\r", None)

	return True
def tester_io(com, logger, results, ws):

	#IOモードへ切り替え
	utils.command_send(com, 'm001\r', None)
	time.sleep(0.5)

	#DIチェック
	di_res = True
	##治具のDOを設定 (101010....)
	io_pattern = {'7':1,'8':0,'9':1,'16':0,'17':1,'18':0,'19':1,'20':0,'21':1,'22':0,'23':1}
	for key in io_pattern.iterkeys():
		cmd = ""
		if io_pattern[key] == 0:
			cmd = "OL"	
		else:
			cmd = "OH"
		utils.command_send(com, "%s%s\r" % (cmd, key.zfill(2)), None)
	##TZ1のDIを読む
	line = utils.command_send(com, 'g000\r', logger)	
	res = json.loads(line)
	di_res = di_res and (res['gpio'] == io_pattern)

	##治具のDOを設定 (010101....)
	io_pattern = {'7':0,'8':1,'9':0,'16':1,'17':0,'18':1,'19':0,'20':1,'21':0,'22':1,'23':0}
	for key in io_pattern.iterkeys():
		cmd = ""
		if io_pattern[key] == 0:
			cmd = "OL"	
		else:
			cmd = "OH"
		utils.command_send(com, "%s%s\r" % (cmd, key.zfill(2)), None)
	##TZ1のDIを読む
	line = utils.command_send(com, 'g000\r', logger)	
	res = json.loads(line)
	di_res = di_res and (res['gpio'] == io_pattern)

	utils.websocket_send(ws, '{"tester":"DI","result":true}', results)
	if di_res:
		utils.websocket_send(ws, '{"tester":"DI","result":true}', results)
	else:
		utils.websocket_send(ws, '{"tester":"DI","result":false}', results)

	#ADCチェック
	adc_res = True
	adc_val = [0, 0, 0, 0, 0]
	try:
		##ADCC12 Ch0
		line = utils.command_send(com, 'a000\r', logger)
		line = line[line.find('{'):]
		res = json.loads(line)
		if res:
			adc_val[0] = res['adc']
		##ADCC12 Ch1
		line = utils.command_send(com, 'a001\r', logger)
		line = line[line.find('{'):]
		res = json.loads(line)
		if res:
			adc_val[1] = res['adc']
		##ADCC12 Ch2
		line = utils.command_send(com, 'a002\r', logger)
		line = line[line.find('{'):]
		res = json.loads(line)
		if res:
			adc_val[2] = res['adc']
		##ADCC12 Ch3
		line = utils.command_send(com, 'a003\r', logger)
		line = line[line.find('{'):]
		res = json.loads(line)
		if res:
			adc_val[3] = res['adc']
		##ADCC24 Ch2
		line = utils.command_send(com, 'a102\r', logger)
		line = line[line.find('{'):]
		res = json.loads(line)
		if res:
			adc_val[4] = res['adc']
	
		## 判定
		### ADCC12 Ch0
		adc_res = adc_res and (adc_val[0] < 64000) and (adc_val[0] > 48000)
		### ADCC12 Ch1
		adc_res = adc_res and (adc_val[1] < adc_val[0])
		### ADCC12 Ch2
		adc_res = adc_res and (adc_val[2] < adc_val[1])
		### ADCC12 Ch3
		adc_res = adc_res and (adc_val[3] < adc_val[2]) and (adc_val[3] > 16000)
		### ADCC24 Ch2
		adc_res = adc_res and (adc_val[4] < 1800000) and (adc_val[4] > 800000)
		### 判定結果通知
		if adc_res:
			utils.websocket_send(ws, '{"tester":"ADC","result":true}', results)
		else:
			utils.websocket_send(ws, '{"tester":"ADC","result":false}', results)
	except:
		utils.websocket_send(ws, '{"tester":"ADC","result":false}', results)

	#ADC電源OFF
	utils.command_send(com, 'A000\r', None)

	## UART Echo
	time.sleep(0.1)
	echo_msg = 'The quick brown fox jumps over the lazy dog\n'
	com_echo = serial.Serial('/dev/ttyACM0', 9600)
	com_echo.timeout = 5
	com_echo.flushInput()
	com_echo.write(echo_msg)
	line = com_echo.readline()
	logger.write('{"cmd":"UART","send":"%s","recv":"%s"}' % (echo_msg.strip(), line.strip()))
	if line == echo_msg:
		utils.websocket_send(ws, '{"tester":"UART","result":true}', results)
	else:
		utils.websocket_send(ws, '{"tester":"UART","result":false}', results)
	com_echo.close()

	## I2C PingPong
	line = utils.command_send(com, 'i000\r', logger)
	try:
		line = line[line.find('{'):]
		res = json.loads(line)
		if res:
			if res['recv'] == 'PONG':
				utils.websocket_send(ws, '{"tester":"I2C","result":true}', results)
			else:
				utils.websocket_send(ws, '{"tester":"I2C","result":false}', results)
		else:
			utils.websocket_send(ws, '{"tester":"I2C","result":false}', results)
	except:
		utils.websocket_send(ws, '{"tester":"I2C","result":false}', results)
		
	## 9軸センサ
	sens_9axis_res = True
	line = utils.command_send(com, '9000\r', logger)
	try:
		line = line.strip()
		print(line)
		res = json.loads(line)
		if res:
			##ジャイロ
			sens_9axis_res = sens_9axis_res and (res['gyro'][0] > -100) and (res['gyro'][0] < 100)
			sens_9axis_res = sens_9axis_res and (res['gyro'][1] > -100) and (res['gyro'][1] < 100)
			sens_9axis_res = sens_9axis_res and (res['gyro'][2] > -100) and (res['gyro'][2] < 100)
			##加速度
			sens_9axis_res = sens_9axis_res and (res['accel'][0] > -200) and (res['accel'][0] < 200)
			sens_9axis_res = sens_9axis_res and (res['accel'][1] > -200) and (res['accel'][1] < 200)
			sens_9axis_res = sens_9axis_res and (res['accel'][2] > 1843) and (res['accel'][2] < 2253)	#1.0G +-10%
			##地磁気
			sens_9axis_res = sens_9axis_res and not ((res['magnetometer'][0] == 0) and (res['magnetometer'][1] == 0) and (res['magnetometer'][2] == 0))
			##判定結果通知
			if sens_9axis_res:
				utils.websocket_send(ws, '{"tester":"9-Axis","result":true}', results)
			else:
				utils.websocket_send(ws, '{"tester":"9-Axis","result":false}', results)
		else:
			utils.websocket_send(ws, '{"tester":"9-Axis","result":false}', results)
	except:
		print "Exception: ", sys.exc_info()[0]
		
	# 気圧センサー
	sens_ap_res = True
	line = utils.command_send(com, 'p000\r', logger)
	line = line[line.find('{'):]
	res = json.loads(line)
	if res:
		sens_ap_res = ((res['airpressure'] / 256) > 80000) and ((res['airpressure'] / 256) < 120000)
		if sens_ap_res:
			utils.websocket_send(ws, '{"tester":"Airpressure","result":true}', results)
		else:
			utils.websocket_send(ws, '{"tester":"Airpressure","result":false}', results)
	else:
		utils.websocket_send(ws, '{"tester":"Airpressure","result":false}', results)
		
	# 充電ICステータス
	line = utils.command_send(com, 'c000\r', logger)
	line = line[line.find('{'):]
	res = json.loads(line)
	if res:
		if res['reg'][0] == 0x10:
			#充電中ステータスでFailなし
			utils.websocket_send(ws, '{"tester":"Charger","result":true}', results)
		else:
			utils.websocket_send(ws, '{"tester":"Charger","result":false}', results)
	else:
		utils.websocket_send(ws, '{"tester":"Charger","result":false}', results)

	#選択モードへ切り替え
	utils.command_send(com, 'm000\r', None)
def tester_sw(com, results, ws):
	com.timeout = 5
	#電源SW ON
	utils.command_send(com, 'P001\r', None)
	#ADC電源ON
	utils.command_send(com, 'A001\r', None)
	#起動待ち
	line = com.readline()
	print(line)
	if line.find('TZ1 TEST PROGRAM') == -1:
		msg = '{"tester":"RUN","result":false}'
		utils.websocket_send(ws, msg, results)
		
		return False
	utils.websocket_send(ws, '{"tester":"RUN","result":true}', results)

	#電源SW OFF
	utils.command_send(com, 'P000\r', None)

	#SW1(GPIO_1)チェック
	com.timeout = 20
	line = com.readline()
	print(line)
	if line.find('POWER INIT') == -1:
		utils.websocket_send(ws, '{"tester":"SW1","result":false}', results)
		return False
	utils.websocket_send(ws, '{"tester":"SW1","result":true}', results)

	#SW2(GPIO_6)チェック
	com.timeout = 20
	line = com.readline()
	print(line)
	if line.find("RUNNING") == -1:
		utils.websocket_send(ws, '{"tester":"SW2","result":false}', results)
		return False

	#Testerファーム起動完了
	utils.websocket_send(ws, '{"tester":"SW2","result":true}', results)
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'