parent
cf47f08d5e
commit
448006bc34
@ -0,0 +1,18 @@
|
||||
package fr.uca.iut.clfreville2.gui;
|
||||
|
||||
import javafx.fxml.FXMLLoader;
|
||||
import javafx.scene.Parent;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
|
||||
public final class FXMLUtils {
|
||||
|
||||
private FXMLUtils() {}
|
||||
|
||||
public static Parent load(URL location, Object controller) throws IOException {
|
||||
FXMLLoader loader = new FXMLLoader(location);
|
||||
loader.setController(controller);
|
||||
return loader.load();
|
||||
}
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
package fr.uca.iut.clfreville2.gui.thread;
|
||||
|
||||
import fr.uca.iut.clfreville2.model.shared.Tickable;
|
||||
import javafx.application.Platform;
|
||||
import javafx.beans.property.IntegerProperty;
|
||||
import javafx.beans.property.SimpleIntegerProperty;
|
||||
|
||||
public class Ticker extends Thread {
|
||||
|
||||
private final Tickable tickable;
|
||||
private final IntegerProperty millisPerTick = new SimpleIntegerProperty(250);
|
||||
|
||||
public Ticker(Tickable tickable) {
|
||||
this.tickable = tickable;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
while (true) {
|
||||
try {
|
||||
Thread.sleep(millisPerTick.getValue());
|
||||
Platform.runLater(tickable::tick);
|
||||
} catch (InterruptedException e) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public int getMillisPerTick() {
|
||||
return millisPerTick.get();
|
||||
}
|
||||
|
||||
public void setMillisPerTick(int millisPerTick) {
|
||||
this.millisPerTick.set(millisPerTick);
|
||||
}
|
||||
|
||||
public IntegerProperty millisPerTickProperty() {
|
||||
return millisPerTick;
|
||||
}
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
package fr.uca.iut.clfreville2.model.sensor;
|
||||
|
||||
import fr.uca.iut.clfreville2.model.sensor.auto.AutoUpdateStrategy;
|
||||
import fr.uca.iut.clfreville2.model.shared.Tickable;
|
||||
import javafx.beans.property.DoubleProperty;
|
||||
import javafx.beans.property.ReadOnlyDoubleProperty;
|
||||
import javafx.beans.property.SimpleDoubleProperty;
|
||||
|
||||
import static java.util.Objects.requireNonNull;
|
||||
|
||||
/**
|
||||
* A sensor that automatically create new temperatures.
|
||||
*/
|
||||
public class AutoSensor extends Sensor implements Tickable {
|
||||
|
||||
private final DoubleProperty temperature = new SimpleDoubleProperty();
|
||||
private AutoUpdateStrategy updateStrategy;
|
||||
|
||||
public AutoSensor(int id, String name, AutoUpdateStrategy updateStrategy) {
|
||||
super(id, name);
|
||||
this.updateStrategy = requireNonNull(updateStrategy, "update strategy");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void tick() {
|
||||
temperature.set(updateStrategy.nextValue(this));
|
||||
}
|
||||
|
||||
@Override
|
||||
public ReadOnlyDoubleProperty temperatureProperty() {
|
||||
return temperature;
|
||||
}
|
||||
|
||||
public AutoUpdateStrategy getUpdateStrategy() {
|
||||
return updateStrategy;
|
||||
}
|
||||
|
||||
public void setUpdateStrategy(AutoUpdateStrategy updateStrategy) {
|
||||
this.updateStrategy = requireNonNull(updateStrategy, "update strategy");
|
||||
}
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
package fr.uca.iut.clfreville2.model.sensor.auto;
|
||||
|
||||
import fr.uca.iut.clfreville2.model.sensor.AutoSensor;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface AutoUpdateStrategy {
|
||||
|
||||
/**
|
||||
* Computes the next value of the sensor.
|
||||
*
|
||||
* @param currentState The current state of the sensor.
|
||||
* @return The next value of the sensor.
|
||||
*/
|
||||
double nextValue(AutoSensor currentState);
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
package fr.uca.iut.clfreville2.model.sensor.auto;
|
||||
|
||||
import fr.uca.iut.clfreville2.model.sensor.AutoSensor;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
public class RandomVariationStrategy implements AutoUpdateStrategy {
|
||||
|
||||
private final Random random;
|
||||
private final int maxVariation;
|
||||
|
||||
public RandomVariationStrategy(int maxVariation) {
|
||||
this(new Random(), maxVariation);
|
||||
}
|
||||
|
||||
public RandomVariationStrategy(Random random, int maxVariation) {
|
||||
this.random = random;
|
||||
this.maxVariation = maxVariation;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double nextValue(AutoSensor currentState) {
|
||||
return currentState.getTemperature() + random.nextInt(maxVariation * 2) - maxVariation;
|
||||
}
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
package fr.uca.iut.clfreville2.model.shared;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface Tickable {
|
||||
|
||||
/**
|
||||
* Tick the current object.
|
||||
*/
|
||||
void tick();
|
||||
}
|
Loading…
Reference in new issue