Strategie implementer

multiple-captors
Nicolas FRANCO 2 years ago
parent dd7357ee49
commit 553ac6d1e9

@ -1,25 +1,11 @@
package launcher;
import javafx.application.Application;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Group;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import model.*;
import view.CapteurView;
import view.ImageCtrlView;
import view.ThermostatView;
import java.io.IOException;
import java.net.URL;
import java.util.Random;
import java.util.ResourceBundle;
import java.util.Map;
public class Launch extends Application {
public static void main(String[] args) {
@ -28,11 +14,16 @@ public class Launch extends Application {
@Override
public void start(Stage primaryStage) throws IOException, InterruptedException {
Capteur captorS = new Capteur(1,"Capteur Simple");
StrategyRandom captorS = new StrategyRandom(1,"Capteur Simple");
CapteurIntervalle captorI = new CapteurIntervalle(2,"Capteur Intervalle",0.0,10.0);
CapteurRealiste captorR = new CapteurRealiste(3,"Capteur Realiste", 5.0, 10.0);
CapteurCPU captorC = new CapteurCPU(4,"Capteur CPU");
CapteurVirtuel CapteurV = new CapteurVirtuel(5,"CapteurVirtuel");
CapteurVirtuel captorV = new CapteurVirtuel(5,"CapteurVirtuel");
captorV.addCapteur(captorS,1);
captorV.addCapteur(captorI,1);
captorV.addCapteur(captorC,1);
captorV.addCapteur(captorR,1);
// FXML NOT WORKING
/*Stage thermostatStage = new Stage();
@ -44,15 +35,16 @@ public class Launch extends Application {
Thread t = new Thread() {
public void run() {
while (true) {
captorS.genTemp();
captorI.genTemp();
captorR.genTemp();
captorC.genTemp();
captorV.genTemp();
try {
System.out.println(captorS.getNom() + ": " + captorS.getTemp().getValue() + " °C");
System.out.println(captorI.getNom() + ": " + captorI.getTemp().getValue() + " °C");
System.out.println(captorR.getNom() + ": " + captorR.getTemp().getValue() + " °C");
System.out.println(captorC.getNom() + ": " + captorC.getTemp().getValue() + " °C");
System.out.println(captorV.getNom() + ": " + captorV.getTemp().getValue() + " °C");
for (Map.Entry<CapteurAbstrait, Integer> entry : captorV.getCapteurs().entrySet()) {
CapteurAbstrait c = entry.getKey();
c.genTemp();
System.out.println(c.getNom() + ": " + c.getTemp().getValue() + " °C");
System.out.println(c.getNom() + ": " + c.getTemp().getValue() + " °C");
}
sleep(4000);
} catch (InterruptedException e) {
throw new RuntimeException(e);

@ -1,20 +0,0 @@
package model;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import java.util.Random;
public class Capteur extends CapteurAbstrait {
public Capteur(int id, String nom){
super(id,nom);
}
@Override
public void genTemp() {
Random random = new Random();
this.setTemp(random.nextInt(30));
}
}

@ -13,7 +13,7 @@ public abstract class CapteurAbstrait {
this.nom = nom;
this.temp.setValue(0.0);
}
public abstract void genTemp();
//public abstract void genTemp();
/* Getters - Setters */
public int getId() {

@ -1,3 +1,5 @@
/*
GENERATE PARAMETER STRATEGY
package model;
import javafx.beans.property.ObjectProperty;
@ -5,13 +7,13 @@ import javafx.beans.property.ObjectProperty;
import java.util.Random;
public class CapteurIntervalle extends CapteurAbstrait{
/*
Un capteur peut générer une température dont la valeur est aléatoire;
Les températures ne pouvant descendre sous un certain seuil, on veut
aussi un capteur plus réaliste qui génère des températures dont la valeur
est comprise dans un certain intervalle défini à la création (et dont le
minimum ne peut être inférieur à 0°K);
*/
public CapteurIntervalle(int id, String nom, Double minVal, Double maxVal){
super(id,nom);
// si min inférieur a zero, alors passe a 0
@ -31,3 +33,4 @@ public class CapteurIntervalle extends CapteurAbstrait{
this.setTemp(randomValue);
}
}
*/

@ -1,4 +1,4 @@
package model;
/*package model;
import javafx.beans.property.ObjectProperty;
@ -12,7 +12,7 @@ public class CapteurRealiste extends CapteurAbstrait{
t0 = T
ti+1 = ti + random(-d,+d)
*/
public CapteurRealiste(int id, String nom, double tZero, double borne) {
super(id, nom);
this.borne = borne;
@ -27,13 +27,14 @@ public class CapteurRealiste extends CapteurAbstrait{
public void genTemp() {
Random rand = new Random();
double randomValue = rand.nextDouble() * 2 * this.borne - this.borne;
double randomValue = rand.nextDouble() * 2 * this.borne - this.borne;*/
/* rand.nextDouble fait un double entre 0 et 1.
* Ensuite on fait * 2*borne pour que l'intervale soit entre 0 et 2*borne.
* On soustrait borne après comme ça on a un nombre entre -borne et borne.
*/
/*
double newTemp = this.getTemp().getValue() + randomValue; //tzero + random entre -borne et borne
this.setTemp(newTemp);
}
}
*/

@ -23,7 +23,8 @@ public class CapteurVirtuel extends CapteurAbstrait {
this.capteurs.remove(c);
}
@Override
public Map<CapteurAbstrait, Integer> getCapteurs(){return this.capteurs;}
// calcule la temperature moyenne des capteurs sa collection de capteurs
public void genTemp() {
double sum = 0;

@ -1,23 +1,18 @@
package model;
import javafx.beans.property.ObjectProperty;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
public class CapteurCPU extends CapteurAbstrait {
public CapteurCPU(int id, String nom) {
super(id, nom);
}
public class StrategyCPU implements StrategyCaptor {
@Override
public void genTemp() {
public void genTemp(CapteurAbstrait c) {
try{
String temp = new String(Files.readAllBytes(Paths.get("/sys/class/thermal/thermal_zone0/temp")));
double tempClesius = Double.parseDouble(temp)/1000.0;
this.setTemp(tempClesius);
c.setTemp(tempClesius);
} catch (IOException e) {
throw new RuntimeException(e);
}

@ -0,0 +1,5 @@
package model;
public interface StrategyCaptor {
public void genTemp(CapteurAbstrait c);
}

@ -0,0 +1,13 @@
package model;
import java.util.Random;
public class StrategyRandom implements StrategyCaptor {
@Override
public void genTemp(CapteurAbstrait c) {
Random random = new Random();
c.setTemp(random.nextInt(30));
}
}

@ -0,0 +1,10 @@
package model;
public class UnitCapteur extends CapteurAbstrait{
private StrategyCaptor strategie;
public UnitCapteur(int id, String nom,StrategyCaptor st) {
super(id, nom);
this.strategie = st;
}
}

@ -1,13 +1,8 @@
package view;
import javafx.application.Platform;
import javafx.beans.InvalidationListener;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.stage.Stage;
import model.Capteur;
import javafx.scene.input.MouseEvent;
import javafx.event.EventHandler;
import model.StrategyRandom;
import java.io.IOException;
@ -16,14 +11,14 @@ public abstract class CapteurView extends FXMLView {
@FXML
Button myBtn;
protected Capteur capteur;
protected StrategyRandom capteur;
public CapteurView(Capteur capteur,String url, String title) throws IOException {
public CapteurView(StrategyRandom capteur, String url, String title) throws IOException {
super(url,title);
this.setCapteur(capteur);
}
public void setCapteur(Capteur capteur) {
public void setCapteur(StrategyRandom capteur) {
this.capteur = capteur;
}

@ -1,16 +1,10 @@
package view;
import javafx.application.Platform;
import javafx.beans.Observable;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.stage.Stage;
import model.Capteur;
import model.StrategyRandom;
import java.io.IOException;
import java.io.InputStream;
@ -20,11 +14,11 @@ public class ImageCtrlView extends CapteurView{
Class<?> clazz = this.getClass();
InputStream input;
protected Capteur capteur;
protected StrategyRandom capteur;
@FXML
ImageView imageview;
public ImageCtrlView(Capteur capteur, String url, String title) throws IOException {
public ImageCtrlView(StrategyRandom capteur, String url, String title) throws IOException {
super(capteur,url,title);
this.capteur = capteur;
myBtn.setOnAction(e -> Platform.exit());

@ -4,7 +4,7 @@ import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.scene.control.Spinner;
import javafx.scene.control.SpinnerValueFactory;
import model.Capteur;
import model.StrategyRandom;
import java.io.IOException;
@ -14,9 +14,9 @@ public class ThermostatView extends CapteurView{
@FXML
Spinner<Double> spin;
protected Capteur capteur;
protected StrategyRandom capteur;
public ThermostatView(Capteur capteur, String url, String title) throws IOException {
public ThermostatView(StrategyRandom capteur, String url, String title) throws IOException {
super(capteur,url,title);
this.spin.setValueFactory(new SpinnerValueFactory.DoubleSpinnerValueFactory(0d,100d));
this.spin.getValueFactory().valueProperty().bindBidirectional(this.capteur.getTemp());

Loading…
Cancel
Save