1ba991379Sopenharmony_ci#-*- coding:utf-8 -*-
2ba991379Sopenharmony_ciimport uuid
3ba991379Sopenharmony_ciimport sys
4ba991379Sopenharmony_ciimport subprocess
5ba991379Sopenharmony_ci
6ba991379Sopenharmony_cifrom core.base import BaseApp, dec_stepmsg
7ba991379Sopenharmony_cifrom util.log_info import logger
8ba991379Sopenharmony_cifrom aw.Download.Download import *
9ba991379Sopenharmony_cifrom aw.Common.Constant import CONSTANT
10ba991379Sopenharmony_cifrom aw.Telnet.TelnetClient import TelConnect
11ba991379Sopenharmony_ci
12ba991379Sopenharmony_cilock_suffix = CONSTANT.File.LOCK_SUFFIX
13ba991379Sopenharmony_cisuc_file = CONSTANT.File.SUC_FILE
14ba991379Sopenharmony_cifailed_file = CONSTANT.File.FAILED_FILE
15ba991379Sopenharmony_ciREAD_TIMEOUT = 15
16ba991379Sopenharmony_ci
17ba991379Sopenharmony_ciclass presetup(BaseApp):
18ba991379Sopenharmony_ci    '''
19ba991379Sopenharmony_ci    @author: zwx877058
20ba991379Sopenharmony_ci    '''
21ba991379Sopenharmony_ci
22ba991379Sopenharmony_ci    def __init__(self, param_file):
23ba991379Sopenharmony_ci        super().__init__(param_file)
24ba991379Sopenharmony_ci        self.param_List = ["Telnet_IP",
25ba991379Sopenharmony_ci                        "Telnet_Port"]
26ba991379Sopenharmony_ci
27ba991379Sopenharmony_ci    @dec_stepmsg("presetup device")
28ba991379Sopenharmony_ci    def excute(self):
29ba991379Sopenharmony_ci        '''
30ba991379Sopenharmony_ci        #===================================================================================
31ba991379Sopenharmony_ci        #   @Method:        excute(self)
32ba991379Sopenharmony_ci        #   @Precondition:  none
33ba991379Sopenharmony_ci        #   @Func:          初始化预置执行入口
34ba991379Sopenharmony_ci        #   @PostStatus:    none
35ba991379Sopenharmony_ci        #   @eg:            excute()
36ba991379Sopenharmony_ci        #   @return:        True or Flase
37ba991379Sopenharmony_ci        #===================================================================================
38ba991379Sopenharmony_ci        '''
39ba991379Sopenharmony_ci        step_index = self.params_dict.get("step_list").index("presetup_app")
40ba991379Sopenharmony_ci
41ba991379Sopenharmony_ci        # 执行初始化
42ba991379Sopenharmony_ci        try:
43ba991379Sopenharmony_ci            chip_type = self.params_dict.get("chip_type")
44ba991379Sopenharmony_ci            if not self.defaultInitL1():
45ba991379Sopenharmony_ci                CONSTANT.ENVERRMESSAGE = "presetup fail"
46ba991379Sopenharmony_ci                logger.printLog(CONSTANT.ENVERRMESSAGE)
47ba991379Sopenharmony_ci                return False
48ba991379Sopenharmony_ci            return True
49ba991379Sopenharmony_ci        except Exception as e:
50ba991379Sopenharmony_ci            logger.error(e)
51ba991379Sopenharmony_ci            raise e
52ba991379Sopenharmony_ci
53ba991379Sopenharmony_ci    def defaultInitL1(self):
54ba991379Sopenharmony_ci        '''
55ba991379Sopenharmony_ci        #===================================================================================
56ba991379Sopenharmony_ci        #   @Method:        defaultInitL1(self)
57ba991379Sopenharmony_ci        #   @Precondition:  none
58ba991379Sopenharmony_ci        #   @Func:          具体执行初始化预置的步骤和内容
59ba991379Sopenharmony_ci        #   @PostStatus:    none
60ba991379Sopenharmony_ci        #   @eg:            defaultInitL1()
61ba991379Sopenharmony_ci        #   @return:        True or Flase
62ba991379Sopenharmony_ci        #===================================================================================
63ba991379Sopenharmony_ci        '''
64ba991379Sopenharmony_ci        cmd_finish = ' #'
65ba991379Sopenharmony_ci        tel_IP = self.params_dict.get("Telnet_IP")
66ba991379Sopenharmony_ci        tel_port = self.params_dict.get("Telnet_Port")
67ba991379Sopenharmony_ci        device_ip = self.params_dict.get("Device_IP")
68ba991379Sopenharmony_ci        device_netmask = self.params_dict.get("Device_Netmask")
69ba991379Sopenharmony_ci        device_gatewayip = self.params_dict.get("Device_GatewayIP")
70ba991379Sopenharmony_ci        flash_type = self.params_dict.get("flash_type")
71ba991379Sopenharmony_ci
72ba991379Sopenharmony_ci        if not tel_IP or not tel_port:
73ba991379Sopenharmony_ci            logger.error("Telnet_IP or Telnet_Port is NULL !!")
74ba991379Sopenharmony_ci            return False
75ba991379Sopenharmony_ci        if not device_netmask:
76ba991379Sopenharmony_ci            device_netmask = "255.255.252.0"
77ba991379Sopenharmony_ci
78ba991379Sopenharmony_ci        #设置单板大网IP
79ba991379Sopenharmony_ci        telc = TelConnect(tel_IP, tel_port)
80ba991379Sopenharmony_ci        board_type = telc.getBoardType(cmd_finish, READ_TIMEOUT)
81ba991379Sopenharmony_ci        if board_type == "uboot":
82ba991379Sopenharmony_ci            reset_cmd = "reset"
83ba991379Sopenharmony_ci            if not telc.sendCmdAndCheckResult(reset_cmd.encode('utf-8'), cmd_finish, READ_TIMEOUT):
84ba991379Sopenharmony_ci                logger.error("board current type not command, and set %s fail!!" % reset_cmd)
85ba991379Sopenharmony_ci                return False
86ba991379Sopenharmony_ci        elif board_type == "bootrom":
87ba991379Sopenharmony_ci            logger.error("board current type not support command !!")
88ba991379Sopenharmony_ci            return False
89ba991379Sopenharmony_ci        ret = telc.sendCmd(b'ifconfig\n', cmd_finish, READ_TIMEOUT)
90ba991379Sopenharmony_ci        gateway = "eth0"
91ba991379Sopenharmony_ci        if "et1" in ret:
92ba991379Sopenharmony_ci            gateway = "et1"
93ba991379Sopenharmony_ci        init_cmd = "ifconfig %s %s netmask %s gateway %s" % (gateway,device_ip, device_netmask, device_gatewayip)
94ba991379Sopenharmony_ci        if not telc.sendCmdAndCheckResult(init_cmd.encode('utf-8'), cmd_finish, READ_TIMEOUT):
95ba991379Sopenharmony_ci            logger.error("set ip failed!!!")
96ba991379Sopenharmony_ci            return False
97ba991379Sopenharmony_ci        ret = telc.sendCmd(b'ifconfig\n', cmd_finish, READ_TIMEOUT)
98ba991379Sopenharmony_ci        logger.info("set ip result is: %s" % ret)
99ba991379Sopenharmony_ci        return True
100ba991379Sopenharmony_ci
101ba991379Sopenharmony_ciif __name__ == "__main__":
102ba991379Sopenharmony_ci    param_file = sys.argv[1]
103ba991379Sopenharmony_ci    if not param_file:
104ba991379Sopenharmony_ci        logger.printLog("Missing params file")
105ba991379Sopenharmony_ci        sys.exit(-1)
106ba991379Sopenharmony_ci    try:
107ba991379Sopenharmony_ci        uphandle = presetup(param_file)
108ba991379Sopenharmony_ci        uphandle._excuteApp()
109ba991379Sopenharmony_ci    except Exception as e:
110ba991379Sopenharmony_ci        logger.printLog(e)
111ba991379Sopenharmony_ci        sys.exit(-1)