package com.rf.psychological.utils;

import org.springframework.util.DigestUtils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

public class CPUSerial {

    /**获取本机CPU信息
     */
    public static String getCPUSerial() {
        String result = "";
        try {
            File file = File.createTempFile("tmp", ".vbs");//创建临时文件，路径为C:\Documents and Settings\Administrator\Local Settings\Temp
            file.deleteOnExit();
            FileWriter fw = new java.io.FileWriter(file);
            //是有vbs脚本语言，获取CPU唯一ID
            //表示程序出现运行时错误时，会继续运行，不中断
            StringBuilder sb = new StringBuilder("On Error Resume Next \r\n\r\n");
            //表示本机
            sb.append("strComputer = \".\"  \r\n");
            //使用GetObject函数获取本机信息赋值给objWMIService
            sb.append("Set objWMIService = GetObject(\"winmgmts:\" _ \r\n");
            sb.append("    & \"{impersonationLevel=impersonate}!\\\\\" & strComputer & \"\\root\\cimv2\") \r\n");
            sb.append("Set colItems = objWMIService.ExecQuery(\"Select * from Win32_Processor\")  \r\n ");
            //使用for循环取出CPU信息
            sb.append("For Each objItem in colItems\r\n " + "    Wscript.Echo objItem.ProcessorId  \r\n ");
            sb.append("    exit for  ' do the first cpu only! \r\n");
            sb.append("Next");

            fw.write(sb.toString());
            fw.close();
            Process p = Runtime.getRuntime().exec("cscript //NoLogo //T:10 " + file.getPath());
            BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line;
            while ((line = input.readLine()) != null) {
                result += line;
            }
            input.close();
            file.delete();
        } catch (Exception e) {
            e.fillInStackTrace();
        }
        return result;
    }

    public static String getDiskdriveAndCsproductUUID() throws Exception {
        String guid = WinRegistry.valueForKey(WinRegistry.HKEY_LOCAL_MACHINE,Constant.DEFAULT_REGISTRY_PATH,Constant.DEFAULT_REGISTRY_KEY);
        guid = DigestUtils.md5DigestAsHex(guid.getBytes(StandardCharsets.UTF_8));
        Process processCsproduct = Runtime.getRuntime().exec(
                new String[]{"wmic", "csproduct", "get", "UUID"});
        processCsproduct.getOutputStream().close();
        Scanner scCsproduct = new Scanner(processCsproduct.getInputStream());
        String csproduct = "";
        while (scCsproduct.hasNextLine()){
            csproduct = csproduct + scCsproduct.nextLine();

        }
        csproduct = csproduct.replace(" ","");
        csproduct = DigestUtils.md5DigestAsHex(csproduct.getBytes(StandardCharsets.UTF_8));
        return guid+csproduct;
    }
    public static void main(String[] args) throws Exception {
        System.out.println(getDiskdriveAndCsproductUUID());
    }


}
