package cn.reghao.devops.common.machine;

import cn.reghao.devops.common.msg.event.EvtAgentHeartbeat;
import cn.reghao.devops.common.msg.event.EvtAgentStart;
import cn.reghao.devops.common.version.AppVersion;
import cn.reghao.jutil.jdk.machine.data.detail.*;
import cn.reghao.jutil.jdk.machine.id.MachineId;
import cn.reghao.jutil.jdk.machine.id.MachineIdLinux;
import oshi.SystemInfo;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author reghao
 * @date 2020-10-22 15:47:58
 */
public class Machine {
    public final static String ID;
    public final static String IPV4;
    static {
        MachineId machineId = new MachineIdLinux();
        ID = machineId.id();
        // TODO ip 发生变化时会引发数据不一致
        IPV4 = machineId.ipv4();
    }

    private final Os os;
    private final Network network;
    private final Cpu cpu;
    private final Memory memory;
    private final Disk disk;

    public Machine() {
        SystemInfo si = new SystemInfo();
        this.os = new Os(si.getOperatingSystem());
        this.network = new Network();
        this.cpu = new Cpu(si.getHardware().getProcessor());
        this.memory = new Memory(si.getHardware().getMemory());
        this.disk = new Disk(si);
    }

    public EvtAgentStart detail() {
        EvtAgentStart machineDetail = new EvtAgentStart();
        machineDetail.setMachineId(Machine.ID);
        machineDetail.setAppVersion(AppVersion.getVersion());

        machineDetail.setOsDetail(os.detail());
        List<NetworkDetail> list = network.detail().stream()
                .peek(networkDetail -> {
                    networkDetail.setPubicIpv4("0.0.0.0");
                })
                .collect(Collectors.toList());
        machineDetail.setNetworkDetails(list);

        machineDetail.setCpuDetail(cpu.detail());
        machineDetail.setMemoryDetail(memory.detail());
        machineDetail.setDiskDetails(disk.detail());
        return machineDetail;
    }

    public EvtAgentHeartbeat stat() {
        EvtAgentHeartbeat machineStat = new EvtAgentHeartbeat();
        machineStat.setMachineId(Machine.ID);
        machineStat.setMemoryDetail(memory.detail());
        machineStat.setDiskDetails(disk.detail());
        return machineStat;
    }
}
