View Javadoc
1   /*
2    * Copyright 2020-2022 The OSHI Project Contributors
3    * SPDX-License-Identifier: MIT
4    */
5   package oshi.driver.windows.wmi;
6   
7   import com.sun.jna.platform.win32.COM.WbemcliUtil.WmiQuery;
8   import com.sun.jna.platform.win32.COM.WbemcliUtil.WmiResult;
9   
10  import oshi.annotation.concurrent.ThreadSafe;
11  import oshi.util.platform.windows.WmiQueryHandler;
12  import oshi.util.platform.windows.WmiUtil;
13  
14  /**
15   * Utility to query Open Hardware Monitor WMI data for Sensors
16   */
17  @ThreadSafe
18  public final class OhmSensor {
19  
20      private static final String SENSOR = "Sensor";
21  
22      /**
23       * Sensor value property
24       */
25      public enum ValueProperty {
26          VALUE;
27      }
28  
29      private OhmSensor() {
30      }
31  
32      /**
33       * Queries the sensor value of an hardware identifier and sensor type.
34       *
35       * @param h          An instantiated {@link WmiQueryHandler}. User should have already initialized COM.
36       * @param identifier The identifier whose value to query.
37       * @param sensorType The type of sensor to query.
38       * @return The sensor value.
39       */
40      public static WmiResult<ValueProperty> querySensorValue(WmiQueryHandler h, String identifier, String sensorType) {
41          StringBuilder sb = new StringBuilder(SENSOR);
42          sb.append(" WHERE Parent = \"").append(identifier);
43          sb.append("\" AND SensorType=\"").append(sensorType).append('\"');
44          WmiQuery<ValueProperty> ohmSensorQuery = new WmiQuery<>(WmiUtil.OHM_NAMESPACE, sb.toString(),
45                  ValueProperty.class);
46          return h.queryWMI(ohmSensorQuery, false);
47      }
48  }