Add more functions
This commit is contained in:
8
src/main/java/SBC/CpuInfo.java
Normal file
8
src/main/java/SBC/CpuInfo.java
Normal file
@@ -0,0 +1,8 @@
|
||||
package SBC;
|
||||
|
||||
public class CpuInfo {
|
||||
public int processorCount;
|
||||
public String revision;
|
||||
public String serial;
|
||||
public String model;
|
||||
}
|
||||
@@ -6,7 +6,6 @@ import org.tinylog.Logger;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public class GPIO {
|
||||
|
||||
private static final Path gpioPath = Path.of("/sys/class/gpio");
|
||||
@@ -14,19 +13,15 @@ public class GPIO {
|
||||
private static final Path gpioUnexportPath = Path.of("/sys/class/gpio/unexport");
|
||||
|
||||
|
||||
public static boolean IsRaspberry64(){
|
||||
public static boolean HaveGPIO(){
|
||||
if (Platform.isLinux()){
|
||||
if (Platform.isARM()){
|
||||
if (Platform.is64Bit()){
|
||||
if (gpioPath.toFile().isDirectory()){
|
||||
if (gpioExportPath.toFile().isFile()){
|
||||
if (gpioUnexportPath.toFile().isFile()){
|
||||
return true;
|
||||
} else Logger.error("GPIO unexport path is not found");
|
||||
} else Logger.error("GPIO export path is not found");
|
||||
} else Logger.error("GPIO path is not found");
|
||||
} else Logger.info("Device is not 64 bit");
|
||||
} else Logger.info("Device is not ARM");
|
||||
if (gpioPath.toFile().isDirectory()){
|
||||
if (gpioExportPath.toFile().isFile()){
|
||||
if (gpioUnexportPath.toFile().isFile()){
|
||||
return true;
|
||||
} else Logger.error("GPIO unexport path is not found");
|
||||
} else Logger.error("GPIO export path is not found");
|
||||
} else Logger.error("GPIO path is not found");
|
||||
} else Logger.info("OS is not Linux");
|
||||
return false;
|
||||
}
|
||||
|
||||
84
src/main/java/SBC/NetworkTransmitReceiveInfo.java
Normal file
84
src/main/java/SBC/NetworkTransmitReceiveInfo.java
Normal file
@@ -0,0 +1,84 @@
|
||||
package SBC;
|
||||
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
import static Other.SomeCodes.ValidString;
|
||||
|
||||
public class NetworkTransmitReceiveInfo {
|
||||
public String name;
|
||||
public long bytesReceived;
|
||||
public long packetsReceived;
|
||||
public long errorsReceived;
|
||||
public long droppedReceived;
|
||||
public long fifoReceived;
|
||||
public long frameReceived;
|
||||
public long compressedReceived;
|
||||
public long multicastReceived;
|
||||
public long bytesTransmitted;
|
||||
public long packetsTransmitted;
|
||||
public long errorsTransmitted;
|
||||
public long droppedTransmitted;
|
||||
public long fifoTransmitted;
|
||||
public long collsTransmitted;
|
||||
public long carrierTransmitted;
|
||||
public long compressedTransmitted;
|
||||
public long timetick;
|
||||
|
||||
/**
|
||||
* Calculate the download speed
|
||||
* @param prev Previous NetworkTransmitReceiveInfo
|
||||
* @param unit Speed unit (KB, MB, GB)
|
||||
* @return Download speed in {unit}/second
|
||||
*/
|
||||
public double RxSpeed(NetworkTransmitReceiveInfo prev, String unit){
|
||||
if (prev!=null){
|
||||
if (Objects.equals(prev.name, name)){
|
||||
long timeDiff = timetick - prev.timetick;
|
||||
if (timeDiff>0){
|
||||
long bytesDiff = bytesReceived - prev.bytesReceived;
|
||||
if (ValidString(unit)) unit = unit.toUpperCase();
|
||||
Double speed = ConvertToUnit(unit, timeDiff, bytesDiff);
|
||||
if (speed != null) return speed;
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private Double ConvertToUnit(String unit, long timeDiff, long bytesDiff) {
|
||||
if (bytesDiff>0){
|
||||
double speed = ((double) bytesDiff / timeDiff) * 1000;
|
||||
return switch (unit) {
|
||||
case "KB" -> speed / 1024;
|
||||
case "MB" -> speed / 1024 / 1024;
|
||||
case "GB" -> speed / 1024 / 1024 / 1024;
|
||||
default -> speed;
|
||||
};
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate the upload speed
|
||||
* @param prev Previous NetworkTransmitReceiveInfo
|
||||
* @param unit Speed unit (KB, MB, GB)
|
||||
* @return Upload speed in {unit}/second
|
||||
*/
|
||||
public double TxSpeed(NetworkTransmitReceiveInfo prev, String unit){
|
||||
if (prev!=null){
|
||||
if (Objects.equals(prev.name, name)){
|
||||
long timeDiff = timetick - prev.timetick;
|
||||
if (timeDiff>0){
|
||||
long bytesDiff = bytesTransmitted - prev.bytesTransmitted;
|
||||
if (ValidString(unit)) unit = unit.toUpperCase();
|
||||
Double speed = ConvertToUnit(unit, timeDiff, bytesDiff);
|
||||
if (speed != null) return speed;
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
45
src/main/java/SBC/ProcessorStatus.java
Normal file
45
src/main/java/SBC/ProcessorStatus.java
Normal file
@@ -0,0 +1,45 @@
|
||||
package SBC;
|
||||
|
||||
public class ProcessorStatus {
|
||||
public String name;
|
||||
public int user;
|
||||
public int nice;
|
||||
public int system;
|
||||
public int idle;
|
||||
public int iowait;
|
||||
public int irq;
|
||||
public int softirq;
|
||||
public int steal;
|
||||
public int guest;
|
||||
public int guest_nice;
|
||||
|
||||
/**
|
||||
* Calculate total CPU time
|
||||
* @return Total CPU time
|
||||
*/
|
||||
public int total_time(){
|
||||
return user + nice + system + idle + iowait + irq + softirq + steal + guest + guest_nice;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate idle CPU time
|
||||
* @return Idle CPU time
|
||||
*/
|
||||
public int idle_time(){
|
||||
return idle + iowait;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate CPU usage percentage
|
||||
* @param prev Previous CPU information
|
||||
* @return CPU usage percentage 0 - 100
|
||||
*/
|
||||
public int cpu_usage(ProcessorStatus prev){
|
||||
if (prev!=null){
|
||||
int total_diff = total_time() - prev.total_time();
|
||||
int idle_diff = idle_time() - prev.idle_time();
|
||||
return (int)(100.0 * (total_diff - idle_diff) / total_diff);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
12
src/main/java/SBC/RamInformation.java
Normal file
12
src/main/java/SBC/RamInformation.java
Normal file
@@ -0,0 +1,12 @@
|
||||
package SBC;
|
||||
|
||||
public class RamInformation {
|
||||
public int totalKB;
|
||||
public int usedKB;
|
||||
public int availableKB;
|
||||
public int swapTotalKB;
|
||||
public int swapFreeKB;
|
||||
public double RamUsagePercentage(){
|
||||
return (double) usedKB / totalKB * 100;
|
||||
}
|
||||
}
|
||||
179
src/main/java/SBC/SystemInformation.java
Normal file
179
src/main/java/SBC/SystemInformation.java
Normal file
@@ -0,0 +1,179 @@
|
||||
package SBC;
|
||||
|
||||
import com.sun.jna.Platform;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.file.Files;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class SystemInformation {
|
||||
|
||||
|
||||
|
||||
public static int getCPUTemperature() {
|
||||
if (Platform.isLinux()){
|
||||
File ff = new File("/sys/class/thermal/thermal_zone0/temp");
|
||||
if (ff.isFile() && ff.canRead()){
|
||||
try{
|
||||
String value = Files.readString(ff.toPath()).trim();
|
||||
return Integer.parseInt(value) / 1000;
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public static NetworkTransmitReceiveInfo[] getNetworkTransmitReceiveInfo(){
|
||||
if (Platform.isLinux()){
|
||||
File ff = new File("/proc/net/dev");
|
||||
if (ff.isFile() && ff.canRead()){
|
||||
List<NetworkTransmitReceiveInfo> result = new ArrayList<>();
|
||||
final Pattern pattern = Pattern.compile("\\s+(.*):\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)");
|
||||
try{
|
||||
String[] lines = Files.readString(ff.toPath()).split("\n");
|
||||
for (String line : lines){
|
||||
Matcher m = pattern.matcher(line);
|
||||
if (m.find()){
|
||||
NetworkTransmitReceiveInfo info = new NetworkTransmitReceiveInfo();
|
||||
info.name = m.group(1).trim();
|
||||
info.bytesReceived = Long.parseLong(m.group(2));
|
||||
info.packetsReceived = Long.parseLong(m.group(3));
|
||||
info.errorsReceived = Long.parseLong(m.group(4));
|
||||
info.droppedReceived = Long.parseLong(m.group(5));
|
||||
info.fifoReceived = Long.parseLong(m.group(6));
|
||||
info.frameReceived = Long.parseLong(m.group(7));
|
||||
info.compressedReceived = Long.parseLong(m.group(8));
|
||||
info.multicastReceived = Long.parseLong(m.group(9));
|
||||
info.bytesTransmitted = Long.parseLong(m.group(10));
|
||||
info.packetsTransmitted = Long.parseLong(m.group(11));
|
||||
info.errorsTransmitted = Long.parseLong(m.group(12));
|
||||
info.droppedTransmitted = Long.parseLong(m.group(13));
|
||||
info.fifoTransmitted = Long.parseLong(m.group(14));
|
||||
info.collsTransmitted = Long.parseLong(m.group(15));
|
||||
info.carrierTransmitted = Long.parseLong(m.group(16));
|
||||
info.compressedTransmitted = Long.parseLong(m.group(17));
|
||||
info.timetick = System.currentTimeMillis();
|
||||
result.add(info);
|
||||
}
|
||||
}
|
||||
} catch (Exception ignored) {
|
||||
|
||||
}
|
||||
return result.toArray(new NetworkTransmitReceiveInfo[0]);
|
||||
}
|
||||
}
|
||||
return new NetworkTransmitReceiveInfo[0];
|
||||
}
|
||||
|
||||
public static CpuInfo getCPUInfo(){
|
||||
CpuInfo result = new CpuInfo();
|
||||
if (Platform.isLinux()){
|
||||
File ff = new File("/proc/cpuinfo");
|
||||
if (ff.isFile() && ff.canRead()){
|
||||
final Pattern pattern = Pattern.compile( "\\s*(.*):\\s*(.*)");
|
||||
try{
|
||||
String[] lines = Files.readString(ff.toPath()).split("\n");
|
||||
for (String line : lines){
|
||||
Matcher m = pattern.matcher(line);
|
||||
if (m.find()){
|
||||
String key = m.group(1).trim();
|
||||
String value = m.group(2).trim();
|
||||
switch (key){
|
||||
case "processor":
|
||||
result.processorCount++;
|
||||
break;
|
||||
case "Revision":
|
||||
result.revision = value;
|
||||
break;
|
||||
case "Serial":
|
||||
result.serial = value;
|
||||
break;
|
||||
case "Model":
|
||||
result.model = value;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static ProcessorStatus[] getProcStat(){
|
||||
if (Platform.isLinux()){
|
||||
File ff = new File("/proc/stat");
|
||||
if (ff.isFile() && ff.canRead()){
|
||||
final Pattern pattern = Pattern.compile( "(cpu\\d?)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)");
|
||||
List<ProcessorStatus> result = new ArrayList<>();
|
||||
try{
|
||||
String[] lines = Files.readString(ff.toPath()).split("\n");
|
||||
for (String line : lines){
|
||||
Matcher m = pattern.matcher(line);
|
||||
if (m.find()){
|
||||
ProcessorStatus info = new ProcessorStatus();
|
||||
info.name = m.group(1).trim();
|
||||
info.user = Integer.parseInt(m.group(2));
|
||||
info.nice = Integer.parseInt(m.group(3));
|
||||
info.system = Integer.parseInt(m.group(4));
|
||||
info.idle = Integer.parseInt(m.group(5));
|
||||
info.iowait = Integer.parseInt(m.group(6));
|
||||
info.irq = Integer.parseInt(m.group(7));
|
||||
info.softirq = Integer.parseInt(m.group(8));
|
||||
info.steal = Integer.parseInt(m.group(9));
|
||||
info.guest = Integer.parseInt(m.group(10));
|
||||
info.guest_nice = Integer.parseInt(m.group(11));
|
||||
result.add(info);
|
||||
}
|
||||
}
|
||||
return result.toArray(new ProcessorStatus[0]);
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
}
|
||||
}
|
||||
return new ProcessorStatus[0];
|
||||
}
|
||||
|
||||
public static RamInformation getRAMInformation(){
|
||||
|
||||
RamInformation result = new RamInformation();
|
||||
if (Platform.isLinux()){
|
||||
File ff = new File("/proc/meminfo");
|
||||
if (ff.isFile() && ff.canRead()){
|
||||
final Pattern pattern = Pattern.compile("(.*):\\s+(\\d+).kB");
|
||||
try{
|
||||
String[] lines = Files.readString(ff.toPath()).split("\n");
|
||||
for (String line : lines) {
|
||||
Matcher m = pattern.matcher(line);
|
||||
if (m.find()){
|
||||
String key = m.group(1);
|
||||
int value = Integer.parseInt(m.group(2));
|
||||
switch (key){
|
||||
case "MemTotal":
|
||||
result.totalKB = value;
|
||||
break;
|
||||
case "MemAvailable":
|
||||
result.availableKB = value;
|
||||
break;
|
||||
case "SwapTotal":
|
||||
result.swapTotalKB = value;
|
||||
break;
|
||||
case "SwapFree":
|
||||
result.swapFreeKB = value;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
result.usedKB = result.totalKB - result.availableKB;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user