<commit_before>/**
 * Oshi (https://github.com/dblock/oshi)
 *
 * Copyright (c) 2010 - 2016 The Oshi Project Team
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Maintainers:
 * dblock[at]dblock[dot]org
 * widdis[at]gmail[dot]com
 *
 * Contributors:
 * https://github.com/dblock/oshi/graphs/contributors
 */
package oshi.hardware.platform.windows;

import oshi.hardware.common.AbstractSensors;
import oshi.util.platform.windows.WmiUtil;

public class WindowsSensors extends AbstractSensors {

    // If null, haven't attempted OHM.
    private String tempIdentifierStr = null;
    // Successful (?) WMI path and property
    private String wmiTempPath = null;
    private String wmiTempProperty = null;

    // If false, can't get from WMI
    private boolean fanSpeedWMI = true;

    // If null, haven't attempted OHM.
    private String voltIdentifierStr = null;
    // Successful (?) WMI path and property
    private String wmiVoltPath = null;
    private String wmiVoltProperty = null;

    /**
     * {@inheritDoc}
     */
    @Override
    public double getCpuTemperature() {
        // Initialize
        double tempC = 0d;
        // If Open Hardware Monitor identifier is set, we couldn't get through
        // normal WMI, and got ID from OHM at least once so go directly to OHM
        if (this.tempIdentifierStr != null) {
            double[] vals = WmiUtil.wmiGetDoubleValuesForKeys("/namespace:\\\\root\\OpenHardwareMonitor PATH Sensor",
                    this.tempIdentifierStr, "Temperature", "Parent,SensorType,Value");
            if (vals.length > 0) {
                double sum = 0;
                for (double val : vals) {
                    sum += val;
                }
                tempC = sum / vals.length;
            }
            return tempC;
        }
        // This branch is used the first time and all subsequent times if
        // successful (tempIdenifierStr == null)
        // Try to get value using initial or updated successful values
        int tempK = 0;
        if (this.wmiTempPath == null) {
            this.wmiTempPath = "Temperature";
            this.wmiTempProperty = "CurrentReading";
            tempK = WmiUtil.getIntValue(this.wmiTempPath, this.wmiTempProperty);
            if (tempK < 0) {
                this.wmiTempPath = "/namespace:\\\\root\\cimv2 PATH Win32_TemperatureProbe";
                tempK = WmiUtil.getIntValue(this.wmiTempPath, this.wmiTempProperty);
            }
            if (tempK < 0) {
                this.wmiTempPath = "/namespace:\\\\root\\wmi PATH MSAcpi_ThermalZoneTemperature";
                this.wmiTempProperty = "CurrentTemperature";
                tempK = WmiUtil.getIntValue(this.wmiTempPath, this.wmiTempProperty);
            }
        } else {
            // We've successfully read a previous time, or failed both here and
            // with OHM
            tempK = WmiUtil.getIntValue(this.wmiTempPath, this.wmiTempProperty);
        }
        // Convert K to C and return result
        if (tempK > 0) {
            tempC = tempK / 10d - 273.15;
        }
        if (tempC <= 0d) {
            // Unable to get temperature via WMI. Future attempts will be
            // attempted via Open Hardware Monitor WMI if successful
            String[] cpuIdentifiers = WmiUtil.getStrValuesForKey(
                    "/namespace:\\\\root\\OpenHardwareMonitor PATH Hardware", "CPU", "HardwareType,Identifier");
            if (cpuIdentifiers.length > 0) {
                this.tempIdentifierStr = cpuIdentifiers[0];
            }
            // If not null, recurse and get value via OHM
            if (this.tempIdentifierStr != null) {
                return getCpuTemperature();
            }
        }
        return tempC;
    }

    /**
     * {@inheritDoc}<commit_msg>Add another Windows WMI temperature option<commit_after>/**
 * Oshi (https://github.com/dblock/oshi)
 *
 * Copyright (c) 2010 - 2016 The Oshi Project Team
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Maintainers:
 * dblock[at]dblock[dot]org
 * widdis[at]gmail[dot]com
 *
 * Contributors:
 * https://github.com/dblock/oshi/graphs/contributors
 */
package oshi.hardware.platform.windows;

import oshi.hardware.common.AbstractSensors;
import oshi.util.platform.windows.WmiUtil;

public class WindowsSensors extends AbstractSensors {

    // If null, haven't attempted OHM.
    private String tempIdentifierStr = null;
    // Successful (?) WMI path and property
    private String wmiTempPath = null;
    private String wmiTempProperty = null;

    // If false, can't get from WMI
    private boolean fanSpeedWMI = true;

    // If null, haven't attempted OHM.
    private String voltIdentifierStr = null;
    // Successful (?) WMI path and property
    private String wmiVoltPath = null;
    private String wmiVoltProperty = null;

    /**
     * {@inheritDoc}
     */
    @Override
    public double getCpuTemperature() {
        // Initialize
        double tempC = 0d;
        // If Open Hardware Monitor identifier is set, we couldn't get through
        // normal WMI, and got ID from OHM at least once so go directly to OHM
        if (this.tempIdentifierStr != null) {
            double[] vals = WmiUtil.wmiGetDoubleValuesForKeys("/namespace:\\\\root\\OpenHardwareMonitor PATH Sensor",
                    this.tempIdentifierStr, "Temperature", "Parent,SensorType,Value");
            if (vals.length > 0) {
                double sum = 0;
                for (double val : vals) {
                    sum += val;
                }
                tempC = sum / vals.length;
            }
            return tempC;
        }
        // This branch is used the first time and all subsequent times if
        // successful (tempIdenifierStr == null)
        // Try to get value using initial or updated successful values
        int tempK = 0;
        if (this.wmiTempPath == null) {
            this.wmiTempPath = "Temperature";
            this.wmiTempProperty = "CurrentReading";
            tempK = WmiUtil.getIntValue(this.wmiTempPath, this.wmiTempProperty);
            if (tempK < 0) {
                this.wmiTempPath = "/namespace:\\\\root\\cimv2 PATH Win32_TemperatureProbe";
                tempK = WmiUtil.getIntValue(this.wmiTempPath, this.wmiTempProperty);
            }
            if (tempK < 0) {
                this.wmiTempPath = "/namespace:\\\\root\\wmi PATH MSAcpi_ThermalZoneTemperature";
                this.wmiTempProperty = "CurrentTemperature";
                tempK = WmiUtil.getIntValue(this.wmiTempPath, this.wmiTempProperty);
            }
            if (tempK < 0) {
                this.wmiTempPath = "/namespace:\\\\root\\cimv2 PATH Win32_PerfFormattedData_Counters_ThermalZoneInformation";
                this.wmiTempProperty = "Temperature";
                tempK = WmiUtil.getIntValue(this.wmiTempPath, this.wmiTempProperty);
            }
        } else {
            // We've successfully read a previous time, or failed both here and
            // with OHM
            tempK = WmiUtil.getIntValue(this.wmiTempPath, this.wmiTempProperty);
        }
        // Convert K to C and return result
        if (tempK > 0) {
            tempC = tempK / 10d - 273.15;
        }
        if (tempC <= 0d) {
            // Unable to get temperature via WMI. Future attempts will be
            // attempted via Open Hardware Monitor WMI if successful
            String[] cpuIdentifiers = WmiUtil.getStrValuesForKey(
                    "/namespace:\\\\root\\OpenHardwareMonitor PATH Hardware", "CPU", "HardwareType,Identifier");
            if (cpuIdentifiers.length > 0) {
                this.tempIdentifierStr = cpuIdentifiers[0];
            }
            // If not null, recurse and get value via OHM
            if (this.tempIdentifierStr != null) {
                return getCpuTemperature();
            }
        }
        return tempC;
    }

    /**
     * {@inheritDoc}
{'fraction_non_alphanumeric': 0.09628561125135232, 'fraction_numerical': 0.008174059382137276, 'mean_word_length': 2.018867924528302, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 9, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2369526', 'n_tokens_mistral': 2438, 'n_tokens_neox': 2291, 'n_words': 648}