package cn.reghao.autodop.common.machine;

import cn.reghao.jdkutil.converter.DateTimeConverter;
import cn.reghao.jdkutil.machine.OsDetail;
import cn.reghao.jdkutil.text.TextFile;
import lombok.extern.slf4j.Slf4j;

import java.time.LocalDateTime;

/**
 * @author reghao
 * @date 2020-10-20 23:17:30
 */
@Slf4j
public class Os {
    private final TextFile textFile;

    public Os(TextFile textFile) {
        this.textFile = textFile;
    }

    public OsDetail detail() {
        OsDetail osDetail = new OsDetail();
        osDetail.setBootTime(bootTime());
        return osDetail;
    }

    private LocalDateTime bootTime() {
        String cpuStat = textFile.readFile(LinuxProc.CPU_STAT);
        for (String line : cpuStat.split(System.lineSeparator())) {
            if (line.startsWith("btime")) {
                long btime = Long.parseLong(line.split("\\s+")[1]);
                return DateTimeConverter.localDateTime(btime*1000);
            }
        }
        return null;
    }
}
