import java.io.InputStreamReader;
import java.util.Timer;
import java.util.TimerTask;
public class HardwareCooling {
public static void main(String[] args) {
try {
while (true) {
double temperature = getCPUTemperature();
if (temperature > 75.0) {
activateCoolingSystem();
}
Thread.sleep(5000); // Check every 5 seconds
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static double getCPUTemperature() throws Exception {
Process process = Runtime.getRuntime().exec("sensors");
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
double temperature = 0.0;
while ((line = reader.readLine()) != null) {
if (line.contains("Core 0")) {
String[] parts = line.split("\\s+");
temperature = Double.parseDouble(parts[parts.length - 1].replace("°C", ""));
break;
}
}
return temperature;
}
private static void activateCoolingSystem() {
System.out.println("Activating cooling system...");
// Code to activate the cooling system
public class CoolingSystem {
private boolean isCoolingActive;
public CoolingSystem() {
this.isCoolingActive = false;
}
public void activateCooling() {
if (!isCoolingActive) {
isCoolingActive = true;
System.out.println("Cooling system activated.");
// Additional logic to start the cooling system
} else {
System.out.println("Cooling system is already active.");
}
}
public void deactivateCooling() {
if (isCoolingActive) {
isCoolingActive = false;
System.out.println("Cooling system deactivated.");
// Additional logic to stop the cooling system
} else {
System.out.println("Cooling system is already inactive.");
}
}
public static void main(String[] args) {
CoolingSystem coolingSystem = new CoolingSystem();
coolingSystem.activateCooling();
// Example of deactivating after a certain period
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
coolingSystem.deactivateCooling();
}
}, 5000); // Deactivate after 5 seconds
}
}
}
}
No comments:
Post a Comment