import serial
import time
import sys

# http://domoticx.com/terminal-codes-ansivt100/

bianco_su_blu = "\x1B[37;44m"
bianco_su_rosso = "\x1B[37;41m"
bianco_su_verde = "\x1B[37;42m"

verde_su_nero = "\x1B[32m"
rosso_su_nero = "\x1B[31m"
bianco_su_nero = "\x1B[0m"
blu_su_nero = "\x1B[34m"

terminal_reset = "\033c"

ser_aria = serial.Serial(
	port="/dev/ttyAMA0",
	baudrate=115200,
	timeout=0,
	parity=serial.PARITY_NONE,
	stopbits=serial.STOPBITS_ONE,
	bytesize=serial.EIGHTBITS
)

ser_esp = serial.Serial(
	port="/dev/ttyS0",
	baudrate=115200,
	timeout=0,
	parity=serial.PARITY_NONE,
	stopbits=serial.STOPBITS_ONE,
	bytesize=serial.EIGHTBITS
)

ser_aria.flushInput()
ser_esp.flushInput()

print(terminal_reset + "Banco test - Aria G25")

token="";
test_executed=0
rxchar_aria = ""
rxchar_esp = ""

while True:

	# Controlla se c sono caratteri in arrivo dalla porta
	# console di Aria

	try:
		if ser_aria.in_waiting:
			rxchar_aria = ser_aria.read(1)
			# print rxchar_aria[0]
		else:
			rxchar_aria=""
	except IOError:
		ser_aria.close()
		ser_aria.open()
		rxchar_aria=""

	# Controlla se c sono caratteri in arrivo dalla scheda
	# ESP

	try:
		if ser_esp.in_waiting:
			rxchar_esp = ser_esp.read(1)
		else:
			rxchar_esp = ""
	except IOError:
		ser_esp.close()
		ser_esp.open()
		rxchar_esp = ""

	# Echo dei caratteri ricevuti dalla porta consore di Aria
	# sul display della CM3-Panel

	if len(rxchar_aria)>0:
		if ord(rxchar_aria[0])>0 and ord(rxchar_aria[0])<127:
			sys.stdout.write(str(rxchar_aria))
			sys.stdout.flush()

			token += str(rxchar_aria[0]);

			pos = token.find("RomBOOT")
			if pos >= 0:
				token=""
				print(terminal_reset)
				print("%s\n*** RomBOOT OK%s ***" % (bianco_su_blu,bianco_su_nero))

			pos = token.find("AT91Bootstrap")
			if pos >= 0:
				token=""
				print("%s\n*** AT91Bootstrap OK ***%s" % (bianco_su_blu,bianco_su_nero))
				lan_errors=0
				ram_errors=0
				usb1_errors=0
				usb2_errors=0
				usb3_errors=0
				gpio_errors=0
				total_errors=0
				test_executed=0

			pos = token.find("buildroot login:")
			if pos >= 0:
				time.sleep(0.5)
				token=""
				print("%s\n*** Login OK ***%s" % (bianco_su_blu,bianco_su_nero))
				ser_aria.write(b"root\r")
				test_executed=0

			pos = token.find("root@buildroot:~#")
			if pos >= 0 and test_executed==0:
				time.sleep(0.5)
				token=""
				print("%s\n*** Prompt OK ***%s" % (bianco_su_blu,bianco_su_nero))
				ser_aria.write(b"python test.py\r")
				test_executed+=1

			pos = token.find("test ram test ko")
			if pos >= 0:
				token=""
				ram_errors+=1
				total_errors+=1

			pos = token.find("test lan test ko")
			if pos >= 0:
				token=""
				lan_errors+=1
				total_errors+=1

			pos = token.find("test usb1 test ko")
			if pos >= 0:
				token=""
				usb1_errors+=1
				total_errors+=1

			pos = token.find("test usb2 test ko")
			if pos >= 0:
				token=""
				usb2_errors+=1
				total_errors+=1

			pos = token.find("test usb3 test ko")
			if pos >= 0:
				token=""
				usb3_errors+=1
				total_errors+=1

			pos = token.find("test gpio test ko")
			if pos >= 0:
				token=""
				gpio_errors+=1
				total_errors+=1

			pos = token.find("test end")
			if pos >= 0:
				token=""
				if total_errors>0: 
					print bianco_su_rosso
					print "*** Test KO ***"
					print
					if ram_errors>0:
						print "   RAM"	
					if lan_errors>0:
						print "   LAN"	
					if usb1_errors>0:
						print "   USB1"	
					if usb2_errors>0:
						print "   USB2"	
					if usb3_errors>0:
						print "   USB3"	
					if gpio_errors>0:
						print "   GPIO"	
					print bianco_su_nero
				else:
					print bianco_su_verde
					print "*** Test OK ***"
					print bianco_su_nero
					
				ser_esp.write("headup")
