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