فهرست منبع

Savable settings

Tankernn 8 سال پیش
والد
کامیت
0bc4ea809e

+ 1 - 2
README.md

@@ -10,5 +10,4 @@ A Java-based, open-source alternative to the default control software for the NZ
 
 ##TODO
 - Make a config file to save user settings in.
-- Make it possible to control fans according to the temperatures of GPU, CPU or both.
-- Manual and profile-based fan speeds.
+- Make it possible to set weights for different temperature readings.

+ 4 - 4
pom.xml

@@ -13,11 +13,11 @@
 			<artifactId>jSerialComm</artifactId>
 			<version>1.3.11</version>
 		</dependency>
-		<!-- https://mvnrepository.com/artifact/org.snmp4j/snmp4j -->
+		<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
 		<dependency>
-			<groupId>org.snmp4j</groupId>
-			<artifactId>snmp4j</artifactId>
-			<version>2.5.5</version>
+			<groupId>com.google.code.gson</groupId>
+			<artifactId>gson</artifactId>
+			<version>2.8.0</version>
 		</dependency>
 
 	</dependencies>

+ 4 - 0
src/eu/tankernn/grid/Fan.java

@@ -164,4 +164,8 @@ public class Fan {
 	public int getIndex() {
 		return index;
 	}
+
+	public FanSpeedProfile getProfile() {
+		return profile;
+	}
 }

+ 60 - 3
src/eu/tankernn/grid/GridControl.java

@@ -2,16 +2,29 @@ package eu.tankernn.grid;
 
 import java.awt.event.WindowEvent;
 import java.awt.event.WindowListener;
+import java.io.FileNotFoundException;
+import java.io.FileReader;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.Reader;
+import java.io.Writer;
 import java.util.Arrays;
 
 import javax.swing.JFrame;
 
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+
 import eu.tankernn.grid.frame.GridControlPanel;
 import eu.tankernn.grid.model.ComputerModel;
 
 public class GridControl implements WindowListener, Runnable {
+
+	private static final String PROFILE_PATH = "profiles.json";
+	private static final String SETTINGS_PATH = "settings.json";
+
 	private Thread t;
-	
+
 	private int pollingSpeed = 500;
 
 	private ComputerModel model = new ComputerModel();
@@ -19,6 +32,8 @@ public class GridControl implements WindowListener, Runnable {
 	private GridControlPanel frame;
 
 	public GridControl(boolean gui) {
+		readSettings();
+		
 		if (gui) {
 			frame = new GridControlPanel(this, model);
 			frame.setResizable(true);
@@ -32,6 +47,47 @@ public class GridControl implements WindowListener, Runnable {
 		t.setDaemon(true);
 		t.start();
 	}
+	
+	public void readSettings() {
+		Gson gson = new GsonBuilder().create();
+		try (Reader reader = new FileReader(PROFILE_PATH)) {
+			Arrays.stream(gson.fromJson(reader, FanSpeedProfile[].class)).forEach(model::addProfile);
+		} catch (FileNotFoundException e) {
+			e.printStackTrace();
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
+		try (Reader reader = new FileReader(SETTINGS_PATH)) {
+			Settings settings = gson.fromJson(reader, Settings.class);
+			model.getGrid().getCommunicator().connect(settings.portname);
+			for (int i = 0; i < 6; i++)
+				model.getGrid().getFan(i).setProfile(model.getProfile(settings.fanProfiles[i]));
+			pollingSpeed = settings.pollingRate;
+			model.setMinSpeed(settings.minSpeed);
+		} catch (FileNotFoundException e) {
+			e.printStackTrace();
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
+	}
+
+	public void saveSettings() {
+		Gson gson = new GsonBuilder().create();
+		// Save profiles
+		try (Writer writer = new FileWriter(PROFILE_PATH)) {
+			gson.toJson(model.getCustomProfiles(), writer);
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
+		// Save misc. settings
+		try (Writer writer = new FileWriter(SETTINGS_PATH)) {
+			gson.toJson(new Settings(model.getGrid().getCommunicator().getPortName(),
+					model.getGrid().fanStream().map(f -> f.getProfile().name).toArray(String[]::new), pollingSpeed,
+					model.getMinSpeed()), writer);
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
+	}
 
 	@Override
 	public void run() {
@@ -41,7 +97,7 @@ public class GridControl implements WindowListener, Runnable {
 
 			if (frame != null)
 				frame.updateProperties();
-			
+
 			try {
 				Thread.sleep(pollingSpeed);
 			} catch (InterruptedException ex) {
@@ -54,7 +110,8 @@ public class GridControl implements WindowListener, Runnable {
 
 	/**
 	 * 
-	 * @param args the command line arguments
+	 * @param args
+	 *            the command line arguments
 	 */
 	public static void main(String[] args) {
 		new GridControl(!Arrays.asList(args).contains("nogui"));

+ 17 - 0
src/eu/tankernn/grid/Settings.java

@@ -0,0 +1,17 @@
+package eu.tankernn.grid;
+
+public class Settings {
+	public final String portname;
+	/**
+	 * Names of the profiles of each fan.
+	 */
+	public final String[] fanProfiles;
+	public final int pollingRate, minSpeed;
+	
+	public Settings(String portname, String[] fanProfiles, int pollingRate, int minSpeed) {
+		this.portname = portname;
+		this.fanProfiles = fanProfiles;
+		this.pollingRate = pollingRate;
+		this.minSpeed = minSpeed;
+	}
+}

+ 5 - 5
src/eu/tankernn/grid/frame/GridControlPanel.java

@@ -73,7 +73,7 @@ public class GridControlPanel extends JFrame {
 		
 		menuBar.add(fileMenu);
 		fileMenu.add(saveSettings);
-		saveSettings.addActionListener(e -> model.saveSettings());
+		saveSettings.addActionListener(e -> control.saveSettings());
 		menuBar.add(profileMenu);
 		profileMenu.add(addProfile);
 		addProfile.addActionListener(e -> {
@@ -157,11 +157,11 @@ public class GridControlPanel extends JFrame {
 	public void updateProperties() {
 		DecimalFormat df = new DecimalFormat("#.##");
 
-		CPULabel.setText("CPU: " + df.format(getModel().getSensor().getCPUTemp()) + " °C");
+		CPULabel.setText("CPU: " + df.format(getModel().getSensor().getCPUTemp()) + " �C");
 		PowerLabel.setText("Total power: " + df.format(getModel().getGrid().getTotalWattage()) + " W");
-		CPULabelMax.setText("CPU: " + df.format(getModel().getSensor().getCpuMax()) + " °C Max");
-		GPULabel.setText("GPU: " + df.format(getModel().getSensor().getGPUTemp()) + " °C");
-		GPULabelMax.setText("GPU: " + df.format(getModel().getSensor().getGpuMax()) + " °C Max");
+		CPULabelMax.setText("CPU: " + df.format(getModel().getSensor().getCpuMax()) + " �C Max");
+		GPULabel.setText("GPU: " + df.format(getModel().getSensor().getGPUTemp()) + " �C");
+		GPULabelMax.setText("GPU: " + df.format(getModel().getSensor().getGpuMax()) + " �C Max");
 
 		for (FanPanel p : fanPanels)
 			p.update();

+ 4 - 0
src/eu/tankernn/grid/model/Communicator.java

@@ -204,5 +204,9 @@ public class Communicator {
 	public boolean isConnected() {
 		return serialPort != null && serialPort.isOpen();
 	}
+	
+	public String getPortName() {
+		return serialPort.getSystemPortName();
+	}
 
 }

+ 24 - 16
src/eu/tankernn/grid/model/ComputerModel.java

@@ -6,6 +6,7 @@ import java.util.logging.Level;
 import java.util.logging.Logger;
 import java.util.stream.Collectors;
 import java.util.stream.IntStream;
+import java.util.stream.Stream;
 
 import eu.tankernn.grid.FanSpeedProfile;
 
@@ -19,7 +20,7 @@ import eu.tankernn.grid.FanSpeedProfile;
  * @author Roel
  */
 public class ComputerModel {
-	
+
 	private Sensor sensor;
 	private GRID grid;
 
@@ -29,7 +30,7 @@ public class ComputerModel {
 	 */
 	private int minSpeed = 30;
 
-	private List<FanSpeedProfile> profiles;
+	private List<FanSpeedProfile> defaultProfiles, customProfiles = new ArrayList<>();
 
 	/**
 	 *
@@ -37,7 +38,7 @@ public class ComputerModel {
 	 */
 	public ComputerModel() {
 		grid = new GRID();
-		profiles = generateProfiles();
+		defaultProfiles = generateProfiles();
 
 		try {
 			sensor = new Sensor();
@@ -65,9 +66,11 @@ public class ComputerModel {
 			ex.printStackTrace();
 		}
 	}
-	
+
 	private List<FanSpeedProfile> generateProfiles() {
-		return IntStream.range(30 / 5, 100 / 5 + 1).map(i -> i * 5).mapToObj(i -> new FanSpeedProfile(i + "%", new int[] { i })).collect(Collectors.toCollection(ArrayList::new));
+		return IntStream.range(30 / 5, 100 / 5 + 1).map(i -> i * 5)
+				.mapToObj(i -> new FanSpeedProfile(i + "%", new int[] { i }))
+				.collect(Collectors.toCollection(ArrayList::new));
 	}
 
 	/**
@@ -104,20 +107,21 @@ public class ComputerModel {
 	/**
 	 * Connects to the GRID on the port specified.
 	 *
-	 * @param selectedPort The COM port the GRID controller is located at
+	 * @param selectedPort
+	 *            The COM port the GRID controller is located at
 	 */
 	public void setGrid(String selectedPort) {
 		grid.getCommunicator().connect(selectedPort);
 	}
 
-	public double getMinSpeed() {
+	public int getMinSpeed() {
 		return minSpeed;
 	}
-	
+
 	public void setMinSpeed(int minSpeed) {
 		this.minSpeed = minSpeed;
 	}
-	
+
 	/**
 	 * 
 	 * @return The temperature used to calculate fan speeds.
@@ -127,19 +131,23 @@ public class ComputerModel {
 		return (sensor.getCPUTemp() + sensor.getGPUTemp()) / 2;
 	}
 
-	public void saveSettings() {
-		// TODO Implement
-	}
-
 	public List<FanSpeedProfile> getProfiles() {
-		return profiles;
+		return Stream.concat(defaultProfiles.stream(), customProfiles.stream()).collect(Collectors.toList());
+	}
+	
+	public List<FanSpeedProfile> getCustomProfiles() {
+		return customProfiles;
 	}
 
 	public void setProfiles(List<FanSpeedProfile> profiles) {
-		this.profiles = profiles;
+		this.defaultProfiles = profiles;
 	}
 
 	public void addProfile(FanSpeedProfile profile) {
-		profiles.add(profile);
+		customProfiles.add(profile);
+	}
+
+	public FanSpeedProfile getProfile(String string) {
+		return getProfiles().stream().filter(p -> p.name.equals(string)).findFirst().get();
 	}
 }