36 lines
1.6 KiB
Java
36 lines
1.6 KiB
Java
package com.iomatex.console;
|
|
import java.io.*;
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.nio.file.Files;
|
|
import java.util.*;
|
|
|
|
/** Linux thermal-zone ABI, also consumed by the client's NativeSystemMetrics.
|
|
* Preserve sensor identity; a SoC zone is not an inferred CPU measurement.
|
|
*/
|
|
final class ThermalSensors {
|
|
static final class Reading {
|
|
final String name; final Double celsius;
|
|
Reading(String name,Double celsius){this.name=name;this.celsius=celsius;}
|
|
}
|
|
static List<Reading> read(File root){
|
|
List<Reading> result=new ArrayList<>();
|
|
File[] zones=root.listFiles(f->f.getName().matches("thermal_zone[0-9]+"));
|
|
if(zones==null)return result;
|
|
Arrays.sort(zones,Comparator.comparing(File::getName));
|
|
for(File zone:zones){
|
|
String name=text(new File(zone,"type"));if(name.isEmpty())name=zone.getName();
|
|
// Vendor test zones are not hardware telemetry. Battery temperature belongs
|
|
// to Android BatteryManager (different units), not this CPU/GPU adapter.
|
|
String kind=name.toLowerCase(Locale.ROOT);
|
|
if(kind.startsWith("test") || kind.contains("battery"))continue;
|
|
Double value=null;
|
|
try{double degrees=Double.parseDouble(text(new File(zone,"temp")))/1000.0;
|
|
if(Double.isFinite(degrees)&°rees>=-50&°rees<=150)value=Math.round(degrees*10.0)/10.0;
|
|
}catch(NumberFormatException ignored){}
|
|
result.add(new Reading(name,value));
|
|
}
|
|
return result;
|
|
}
|
|
private static String text(File file){try{if(file.length()>4096)return "";return new String(Files.readAllBytes(file.toPath()),StandardCharsets.UTF_8).trim();}catch(IOException|SecurityException e){return "";}}
|
|
}
|