from TCAction import TCActionBase
from NativeLog import NativeLog
import time
import random
import string

TEST_COUNT_ONE_ROUND = 500


class TestCase(TCActionBase.CommonTCActionBase):
    def __init__(self, test_case, test_env, timeout=45, log_path=TCActionBase.LOG_PATH):
        TCActionBase.CommonTCActionBase.__init__(self, test_case, test_env,
                                                 timeout=timeout, log_path=log_path)
        self.send_len = 1460
        self.server_port = random.randint(10000, 50000)
        self.server_port_2 = random.randint(10000, 50000)
        self.server_echo = True
        self.test_time = 12 * 60
        self.sta_number = 3
        self.send_delay = 50
        # load param from excel
        cmd_set = test_case["cmd set"]
        for i in range(1, len(cmd_set)):
            if cmd_set[i][0] != "dummy":
                cmd_string = "self." + cmd_set[i][0]
                exec cmd_string
        self.result_cntx = TCActionBase.ResultCheckContext(self, test_env, self.tc_name)
        pass

    def cleanup(self):
        checker_stings = []
        test_action_strings = []
        for i in range(self.sta_number + 2):
            checker_stings.append("R SSC%s C +RECVPRINT:1" % (i+1))
            test_action_strings.append("SSC SSC%s soc -R -o 1" % (i+1))
            fail_string = "Fail, Fail to turn on recv print"
            self.load_and_exe_one_step(checker_stings, test_action_strings, fail_string)
        pass

    def execute(self):
        TCActionBase.TCActionBase.execute(self)
        self.result_cntx.start()

        try:
            # configurable params
            send_len = self.send_len
            test_time = self.test_time * 60
            # server port
            server_port = self.server_port
            server_port_t = self.server_port_2
            # ap ip
            # ap_ip = self.ap_ip
            # server echo
            server_echo = self.server_echo
            # station number
            sta_number = self.sta_number
            # send delay
            send_delay = self.send_delay
            # configurable params
        except StandardError, e:
            NativeLog.add_trace_critical("Error configuration for TCPTransparent script, error is %s" % e)
            raise StandardError("Error configuration")

        # step0 reboot
        checker_stings = []
        test_action_string = []

        for i in range(sta_number + 2):
            checker_stings.append("P SSC%d C !!!ready!!!" % (i + 1))
            test_action_string.append("SSCC SSC%d reboot" % (i + 1))

            fail_string = "Fail, Fail to reboot"
            if self.load_and_exe_one_step(checker_stings, test_action_string, fail_string) is False:
                return

        # switch off recv print
        checker_stings = []
        test_action_strings = []
        for i in range(self.sta_number + 2):
            checker_stings.append("R SSC%s C +RECVPRINT:0" % (i+1))
            test_action_strings.append("SSC SSC%s soc -R -o 0" % (i+1))
            fail_string = "Fail, Fail to turn off recv print"
            self.load_and_exe_one_step(checker_stings, test_action_strings, fail_string)

        # step1, set ap/STA mode on all target
        for i in range(sta_number + 2):
            checker_stings = ["R SSC%d C +MODE:OK" % (i + 1)]