# -*- coding: utf-8 -*-
__author__ = "Lee.le"
from Model.tools import *
import os
from django.shortcuts import render

info = {'f4eebcd6': ['f4eebcd6', 'MB000154', '一加 6', '9'],
        '9b57691d': ['9b57691d', 'MB000155', '一加 6T', '9'],
        '2d9096f3': ['2d9096f3', 'MB000156', '小米 9', '9'],
        '3150a7eb': ['3150a7eb', 'MB000158', '小米 9', '9'],
        'e37c0280': ['e37c0280', 'MB000159', '小米 9', '9'],

        }

def performance(request):
    """
    启动脚本
    :param request:
    :return:
    """
    progressstatus = int(get_progresssnum("progressnum"))
    devicesinfo = get_workdevices()
    devicesdic = []
    for i in devicesinfo:
        devicesdic.append(info.get(i, i))
    if request.method == 'POST':
        devicesnumber = request.POST.get("devicesnumber")
        email = request.POST.get("email")  # 接受报告的邮件
        stop = request.POST.get("stop")  # 处理关闭按钮事件
        if stop == "stop":  # TODO 停止测试的请求处理
            if get_system() == 'Windows':
                for i in range(10):
                    pidcmd = 'tasklist  /fi "WINDOWTITLE eq start_auto"'  # 查看进程信息
                    result = os.popen(pidcmd).read()
                    if '运行' not in result:
                        killcmd = 'taskkill /f /fi "WINDOWTITLE eq start_auto"'  # 杀掉进程
                        os.popen(killcmd)
                    else:
                        print('已经终止了程序运行')
                        break
            else:
                path = os.path.abspath(os.path.join(os.getcwd(), "../../shell/killscript.sh"))
                os.system(f'sh {path}')

        else:
            # TODO 设备列表
            if devicesnumber != '' and devicesnumber != None:
                deviceslist = devicesinfo[0:(int(devicesnumber))]
                rewrite_config('devicesList', deviceslist)
            else:
                deviceslist = devicesinfo[0:1]
                rewrite_config('devicesList', deviceslist)
            # TODO 实现email的重写
            if email != '':
                rewrite_config('email', email)
            # TODO 实现testcase=uwacase,uwatype=1（是uwa测试）,testtype=True(单步测试)，performancetype=False（性能测试关掉）的重写
            rewrite_config('testCase', 'uwacase')
            rewrite_config('uwatype', 1)
            rewrite_config('testtype', 'True')
            rewrite_config('performancetype', 'False')
            #  TODO 实现脚本开始
            if get_system() == 'Windows':
                for i in range(10):
                    pidcmd = 'tasklist  /fi "WINDOWTITLE eq start_auto"'  # 查看进程信息
                    result = os.popen(pidcmd).read()
                    if '运行' not in result:
                        killcmd = 'taskkill /f /fi "WINDOWTITLE eq start_auto"'  # 杀掉进程
                        os.popen(killcmd)
                    else:
                        print('终止上个程序运行')
                        break
                path = os.path.abspath(os.path.join(os.getcwd(), "../start.bat"))
                os.popen(f'start {path}')  # 重新打开cmd执行
                # os.popen(f'{path}')  # 当前cmd执行
            else:
                path = os.path.abspath(os.path.join(os.getcwd(), "../../shell/killscript.sh"))
                os.system(f'sh {path}')
                path = os.path.abspath(os.path.join(os.getcwd(), "../multi_processframe/start.py"))
                os.system(f'python {path}')

    return render(request, "custom.html", {'addr': get_addr(), 'count': len(devicesinfo), 'devicesdic': devicesdic, 'progressstatus': progressstatus})


if __name__ == "__main__":
    start = time.localtime()