# -*- coding:utf-8 -*-
"""
Function: set_communication_mode.py moudle. This moudle mainly involves the
Set communication mode function.
Copyright Information: xFusion Digital Technologies Co.,
 Ltd. All Rights Reserved © 2022-2022
"""
import os
try:
    from redfish import getbmctoken
except ImportError as e:
    from redfish.Linux import getbmctoken
from scripts import common_function


INVALID_VALUE = None


def setcommunicationmode_init(parser, parser_dict):
    """
    Function Description:initializing the Command for
    Set communication mode
    Parameter:parser object: action object
    parser_dict dict: dictionary format of subcommand and ArgumentParser
    """
    sub_parser = parser.add_parser('setcommunicationmode',
                                   help='''set usb''')
    sub_parser.add_argument('-C', dest='communicationMode', required=False,
                            help='''set communication mode''')
    parser_dict['setcommunicationmode'] = sub_parser
    return 'setcommunicationmode'


def setcommunicationmode(client, args):
    """
    Function Description:Set communication mode to USB
    Parameter:client refishClient: class object
    """
    # IPMI切换BMC通道
    communication_mode = args.communicationMode
    request_cmd = ["0x01", "0x08", "0xc0", "0x00", "0x93", "0xdb", "0x07", "0x00", "0x51"]
    if communication_mode == "veth":
        request_cmd.append("0x00")
    elif communication_mode == "cdev":
        request_cmd.append("0x01")
    else:
        request_cmd.append("0x02")
    try:
        getbmctoken.get_info_by_ipmi((common_function.format_byte_to_hex_stream(request_cmd)))
    except common_function.CustomError as error:
        common_function.print_info("The command failed to run")
    finally:
        pass
    common_function.print_info("Success: The command succeed to run")
    os.system("service iBMA restart")
    return INVALID_VALUE

