/*
 * libkysdk-system's Library
 *
 * Copyright (C) 2023-2025, KylinSoft Co., Ltd.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this library.  If not, see <https://www.gnu.org/licenses/>.
 *
 * Authors: ShaoShuai Tian <tianshaoshuai@kylinos.cn>
 *
 */

/**
 * @file RunInfoMethod.java
 * @author tianshaoshuai (tianshaoshuai@kylinos.cn)
 * @brief Java获取瞬时信息
 * @version 0.1
 * @date 2023-2-17
 * 
 * @copyright Copyright (c) 2021
 * @defgroup libkysdk-system-java
 * @{
 * 
 */

package kylin.kysdk.java.method;

import kylin.kysdk.java.client.DBusClient;
import com.kylin.kysdk.runinfo;
import lombok.extern.slf4j.Slf4j;
import org.freedesktop.dbus.connections.impl.DBusConnection;
import org.freedesktop.dbus.exceptions.DBusException;

/**
 * @brief 获取瞬时信息类
 * 主要用来获取瞬时网速，cpu温度、磁盘温度、磁盘转速
 */
@Slf4j
public class RunInfoMethod implements runinfo {
    /**
     * @brief 获取瞬时网速信息
     * 
     * @param nc 网卡名称，如eno1
     * @return double 瞬时网速
     */
    @Override
    public double getNetSpeed(String nc){
        DBusConnection conn = null;
        try {
            conn = DBusClient.getConn(DBusConnection.DBusBusType.SYSTEM);
            runinfo runinfo = conn.getRemoteObject("com.kylin.kysdk.service",
                    "/com/kylin/kysdk/runinfo", com.kylin.kysdk.runinfo.class);
            return runinfo.getNetSpeed(nc);
        } catch (DBusException e) {
            if (conn != null) conn.disconnect();
            log.error("DBusException GetEUser :",e);
        }
        return -1.0;
    }

    /**
     * @brief 获取磁盘转速
     * 
     * @param disk 磁盘名称，应当是例如/dev/sda这种绝对路径
     * @return int 磁盘转速
     */
    @Override
    public int getDiskRate(String disk){
        DBusConnection conn = null;
        try {
            conn = DBusClient.getConn(DBusConnection.DBusBusType.SYSTEM);
            runinfo runinfo = conn.getRemoteObject("com.kylin.kysdk.service",
                    "/com/kylin/kysdk/runinfo", com.kylin.kysdk.runinfo.class);
            return runinfo.getDiskRate(disk);
        } catch (DBusException e) {
            if (conn != null) conn.disconnect();
            log.error("DBusException GetEUser :",e);
        }
        return -1;
    }

    /**
     * @brief 获取CPU温度
     * 
     * @return double CPU温度
     */
    @Override
    public double getCpuTemperature(){
        DBusConnection conn = null;
        try {
            conn = DBusClient.getConn(DBusConnection.DBusBusType.SYSTEM);
            runinfo obj = conn.getRemoteObject("com.kylin.kysdk.service",
                    "/com/kylin/kysdk/runinfo", runinfo.class);
            return obj.getCpuTemperature();
        } catch (DBusException e) {
            if (conn != null) conn.disconnect();
            log.error("DBusException GetEUser :",e);
        }
        return -1.0;
    }

    /**
     * @brief 获取磁盘温度
     * 
     * @param name 磁盘名称，应当是例如/dev/sda这种绝对路径
     * @return double 磁盘温度
     */
    @Override
    public double getDiskTemperature(String name){
        DBusConnection conn = null;
        try {
            conn = DBusClient.getConn(DBusConnection.DBusBusType.SYSTEM);
            runinfo obj = conn.getRemoteObject("com.kylin.kysdk.service",
                    "/com/kylin/kysdk/runinfo", runinfo.class);
            return obj.getDiskTemperature(name);
        } catch (DBusException e) {
            if (conn != null) conn.disconnect();
            log.error("DBusException GetEUser :",e);
        }
        return -1.0;
    }

    /**
     * @brief 判断是否为远程对象
     *
     * @return boolean Returns true on remote objects. Local objects implementing this interface MUST return false.
     */
    @Override
    public boolean isRemote() {
        return false;
    }

    /**
     * @brief 获取ObjectPath【未实现】
     *
     * @return String ObjectPath
     */
    @Override
    public String getObjectPath() {
        return null;
    }
}

/**
 * \example kysdk-system/src/kysdk-java/kysdk-java-ext/kysdk-java/src/test/java/kylin/kysdk/java/EventTest.java
 * 
 */
