# 파일 경로: ~/services/hanmocnn/update_status.py
import json
import os
import time

def get_temp(name_keyword):
    """hwmon 폴더에서 이름 키워드로 온도를 찾아 반환 (milli-degree to degree)"""
    try:
        base_path = "/sys/class/hwmon"
        for folder in os.listdir(base_path):
            with open(os.path.join(base_path, folder, "name"), "r") as f:
                if name_keyword in f.read():
                    with open(os.path.join(base_path, folder, "temp1_input"), "r") as t:
                        return int(t.read()) // 1000
    except:
        return "N/A"
    return "N/A"

def get_hw_info():
    # 1. CPU 온도 (가장 부하가 큰 bigcore0 기준)
    cpu_temp = get_temp("bigcore0_thermal")
    
    # 2. NVMe 온도
    nvme_temp = get_temp("nvme")

    # 3. 가동 시간 (Uptime)
    with open("/proc/uptime", "r") as f:
        uptime_days = int(float(f.readline().split()[0]) // 86400)

    return {
        "cpu_temp": str(cpu_temp),
        "nvme_temp": str(nvme_temp),
        "uptime_days": uptime_days,
        "last_update": time.strftime("%H:%M:%S")
    }

if __name__ == "__main__":
    status = get_hw_info()
    # 저장 경로: ~/services/hanmocnn/html/status.json
    base_dir = os.path.dirname(os.path.abspath(__file__))
    with open(os.path.join(base_dir, "html", "status.json"), "w") as f:
        json.dump(status, f)