View Javadoc
1   /*
2    * Copyright 2016-2022 The OSHI Project Contributors
3    * SPDX-License-Identifier: MIT
4    */
5   package oshi.hardware.common;
6   
7   import static oshi.util.Memoizer.defaultExpiration;
8   import static oshi.util.Memoizer.memoize;
9   
10  import java.util.Arrays;
11  import java.util.function.Supplier;
12  
13  import oshi.annotation.concurrent.ThreadSafe;
14  import oshi.hardware.Sensors;
15  
16  /**
17   * Sensors from WMI or Open Hardware Monitor
18   */
19  @ThreadSafe
20  public abstract class AbstractSensors implements Sensors {
21  
22      private final Supplier<Double> cpuTemperature = memoize(this::queryCpuTemperature, defaultExpiration());
23  
24      private final Supplier<int[]> fanSpeeds = memoize(this::queryFanSpeeds, defaultExpiration());
25  
26      private final Supplier<Double> cpuVoltage = memoize(this::queryCpuVoltage, defaultExpiration());
27  
28      @Override
29      public double getCpuTemperature() {
30          return cpuTemperature.get();
31      }
32  
33      protected abstract double queryCpuTemperature();
34  
35      @Override
36      public int[] getFanSpeeds() {
37          return fanSpeeds.get();
38      }
39  
40      protected abstract int[] queryFanSpeeds();
41  
42      @Override
43      public double getCpuVoltage() {
44          return cpuVoltage.get();
45      }
46  
47      protected abstract double queryCpuVoltage();
48  
49      @Override
50      public String toString() {
51          StringBuilder sb = new StringBuilder();
52          sb.append("CPU Temperature=").append(getCpuTemperature()).append("C, ");
53          sb.append("Fan Speeds=").append(Arrays.toString(getFanSpeeds())).append(", ");
54          sb.append("CPU Voltage=").append(getCpuVoltage());
55          return sb.toString();
56      }
57  }