#!/usr/bin/python3

import sys
from check_3 import *

num = 1


def runPop(cmd, shell=False, encoding='utf-8'):
    args = cmd if shell else shlex.split(cmd)
    r = subprocess.Popen(args, shell=shell, stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding=encoding)
    return r


def w_disk(disk):
    """
    老化写入测试
    """
    cmd=f'sudo dd if=/dev/zero of=/dev/{disk} bs=10M oflag=direct count=1024'
    st = time.time()
    ret = runcmd(cmd, shell=True)
    if not ret[1]:
        et = time.time()
        ct = et - st
        if ct < 60:
            logs(f"{disk} 磁盘速度正常,用时{ct:.2f}s")
            return 1
        else:
            warn(1)
            logs(f"{disk} 磁盘速度异常请将设备进行归类")
            return 0


def warn(n: int):
    """
    信号灯控制
    """
    cmd = f"/opt/tool/saturn-gpio-set.sh --sys {n}"
    runcmd(cmd, shell=True)


if __name__ == '__main__':
    if sys.argv[1] == "startema":
        runcmd("systemctl stop hb")
        fail = False
        mode = model()
        disk_num = mode[0][1]
        if disk_num:
            logs("=====开始老化测试=====")
            warn(0)
            led = 'while true;do /opt/tool/saturn-gpio-set.sh --sys 0 && sleep 0.1 && /opt/tool/saturn-gpio-set.sh --sys 2 && sleep 0.2;done'
            ledrun = runPop(led, shell=True)
            for x in range(num):
                new_disks = disks()
                if new_disks and len(new_disks) == disk_num:
                    if fail:
                        break
                    for i in new_disks:
                        if not w_disk(i):
                            fail = True
                            break
                else:
                    fail = True
                    logs(f"Error:硬盘数量为{len(new_disks)},与型号标配数量{disk_num}不符！")
                    ledrun.kill()
                    warn(1)
                    break
            if not fail:
                logs("=====老化结束设备正常=====")
                logs(f"mode:{mode[1]}\n开始初始化服务...")
                if mode[1][0:2] == "A0":
                    initpool = "python3 /opt/tool/truenasInitPool.py"
                    ret = runcmd(initpool)
                    if ret[1] == 0:
                        logs("默认池已创建")
                        ledrun.kill()
                        warn(2)
                        delcmd = "rm -f /opt/tool/check_3.py && rm -f /opt/tool/emaging.py && rm -f /opt/tool/truenasInitPool.py"
                        #runcmd(delcmd, shell=True)
                    else:
                        logs("默认池创建失败")
                        ledrun.kill()
                        warn(1)
                elif mode[1][0:2] == "A1":
                    initkod = "/opt/tool/kod_install.sh kodinit"
                    ret = runcmd(initkod)
                    if ret[1] == 0:
                        ledrun.kill()
                        warn(2)
                        logs("可道云服务已启动")
                        delcmd = "rm -f /opt/tool/check_3.py && rm -f /opt/tool/emaging.py && rm -f /opt/tool/kod_install.sh"
                        #runcmd(delcmd, shell=True)
                    else:
                        ledrun.kill()
                        warn(1)
                        logs("可道云服务启动失败")
                else:
                    logs("望获系统初始化完成。")
                    
        else:
            logs("Error:序列号读取失败！", op="w")
