From 95de4c14374194c0858745caa6c28098ed463757 Mon Sep 17 00:00:00 2001 From: renanaferreira Date: Sun, 3 Jan 2021 22:42:32 +0000 Subject: [PATCH 1/3] algumas mudancar --- .../main/java/com/ies/blossom/Testing.java | 10 +- .../blossom/controllers/ParcelController.java | 9 +- .../java/com/ies/blossom/entitys/Parcel.java | 190 ++++++++++++------ .../com/ies/blossom/model/GoodPlantModel.java | 73 +------ .../src/main/resources/templates/parcel.html | 28 +-- 5 files changed, 176 insertions(+), 134 deletions(-) diff --git a/blossom/src/main/java/com/ies/blossom/Testing.java b/blossom/src/main/java/com/ies/blossom/Testing.java index 419a93b..1feb311 100644 --- a/blossom/src/main/java/com/ies/blossom/Testing.java +++ b/blossom/src/main/java/com/ies/blossom/Testing.java @@ -19,8 +19,14 @@ public class Testing { - public static void makeData(Parcel parcel) { - parcel.setPlant(new Plant("Daisy", "Daisius", 2.0, 1.0, 1.0, 2.0)); + public static void makeData(Parcel parcel, List plants) { + plants = new ArrayList(); + Plant plant = new Plant("Daisy", "Daisius", 2.0, 1.0, 1.0, 2.0); + parcel.setPlant(plant); + plants.add(plant); + plants.add(new Plant("Rosa", "Rosita", 1.5, 1.0, 1.0, 2.5)); + plants.add(new Plant("Margarida", "Margaridus", 0.5, 0.3, 1.0, 2.5)); + Double[] lista = new Double[]{1.0, 1.6, 1.4}; Double[] lista2 = new Double[]{4.0, 1.6, 1.4}; try { diff --git a/blossom/src/main/java/com/ies/blossom/controllers/ParcelController.java b/blossom/src/main/java/com/ies/blossom/controllers/ParcelController.java index f330a64..e55cd68 100644 --- a/blossom/src/main/java/com/ies/blossom/controllers/ParcelController.java +++ b/blossom/src/main/java/com/ies/blossom/controllers/ParcelController.java @@ -1,5 +1,6 @@ package com.ies.blossom.controllers; +import com.ies.blossom.Testing; import com.ies.blossom.entitys.HumMeasure; import com.ies.blossom.entitys.HumSensor; import com.ies.blossom.entitys.Parcel; @@ -59,6 +60,9 @@ public String getParcel(Model model, @PathVariable(value="id") Long parcelId, Au return "parcel.html"; } + List plants2 = this.plantRepository.findAll(); + Testing.makeData(parcel, plants2); + // ir buscar todos as ultimas medidas relativas aos sensores de ph if (!parcel.getPhSensors().isEmpty()) { Map retPh = new HashMap(); @@ -88,8 +92,7 @@ public String getParcel(Model model, @PathVariable(value="id") Long parcelId, Au model.addAttribute("humSensorsLastMeasures", retHum); } - model.addAttribute("goodPlantModel", parcel.checkPlantConditions()); - + model.addAttribute("goodPlant", parcel.checkPlantConditions()); // ir buscar todas as plantas na bd // talvez seja melhor colocar noutro método, esta funcionalidade é chamada poucas vezes List plants = this.plantRepository.findAll(); @@ -98,6 +101,8 @@ public String getParcel(Model model, @PathVariable(value="id") Long parcelId, Au model.addAttribute("parcel", parcel); + + return "parcel.html"; } diff --git a/blossom/src/main/java/com/ies/blossom/entitys/Parcel.java b/blossom/src/main/java/com/ies/blossom/entitys/Parcel.java index f90ce15..7105b7c 100644 --- a/blossom/src/main/java/com/ies/blossom/entitys/Parcel.java +++ b/blossom/src/main/java/com/ies/blossom/entitys/Parcel.java @@ -2,7 +2,7 @@ import com.fasterxml.jackson.annotation.JsonBackReference; import com.fasterxml.jackson.annotation.JsonManagedReference; - +import com.ies.blossom.model.GoodPlantMeasureModel; import com.ies.blossom.model.GoodPlantModel; import javax.persistence.*; @@ -18,6 +18,8 @@ @Entity @Table(name = "parcels") public class Parcel { + + private static double acceptablePercentage = 60.00; @Id @GeneratedValue(strategy = GenerationType.AUTO) @@ -44,13 +46,20 @@ public class Parcel { @JsonBackReference private Plant plant; + //Constructors + // + // public Parcel() { super(); } + public Parcel(User owner, String location) { this.location = location; this.owner = owner; } + //Get and Set + // + // public Plant getPlant() { return plant; } @@ -99,6 +108,26 @@ public void setHumSensors(Set humSensors) { this.humSensors = humSensors; } + //General Object public Methods + // + // + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (! (obj instanceof Parcel)) { + return false; + } + + Parcel other = (Parcel) obj; + if (this.getParcelId() == other.getParcelId()) { + return true; + } + return false; + } + + //Other public methods public void addHumSensor(HumSensor sensor) { this.humSensors.add(sensor); } @@ -107,14 +136,19 @@ public void addPhSensor(PhSensor sensor) { this.phSensors.add(sensor); } - public Double PhMeasure() { - return this.measure(false); + public GoodPlantModel checkPlantConditions() { + GoodPlantMeasureModel phStatus = this.checkPlantMeasureConditions(false, this.getPlant(), null); + GoodPlantMeasureModel humStatus = this.checkPlantMeasureConditions(true, this.getPlant(), null); + return new GoodPlantModel(phStatus, humStatus); } - public Double HumMeasure() { - return this.measure(true); + public List bestPlants(List plants) { + return this.bestPlantsFor(plants, true, true, null); } + //Public methods which use same private method(PH/HUM) + // + // public boolean noPhMeasure() { return this.noMeasure(false, null); } @@ -124,34 +158,93 @@ public boolean noHumMeasure() { } public Map getPhSensorTable(){ - return this.getSensorTable(false); + return this.getSensorTable(false, null); } public Map getHumSensorTable(){ - return this.getSensorTable(true); + return this.getSensorTable(true, null); } - public Double generalHumMeasurePercentage() { - return this.generalMeasurePercentage(true); + //Private Methods + // + // + + //Consider both variables + private List bestPlantsFor(List plants, boolean usesPh, boolean usesHum, Set sensores) { + List goodPlants = new ArrayList(); + for (Plant plant : plants) { + Boolean factor = this.plantIsGoodForParcel(plant, usesPh, usesHum, sensores); + if(factor != null && factor) { + goodPlants.add(plant); + } + } + return goodPlants; } - public Double generalPhMeasurePercentage() { - return this.generalMeasurePercentage(false); + private Boolean plantIsGoodForParcel(Plant plant, boolean usesPh, boolean usesHum, Set sensores) { + Boolean goodPh; + if(usesPh) { + goodPh = this.plantIsGoodForParcelMeasure(false, plant, sensores); + } else { + goodPh = null; + } + + Boolean goodHum; + if(usesHum) { + goodHum = this.plantIsGoodForParcelMeasure(true, plant, sensores); + } else { + goodHum = null; + } + + if (goodHum == null && goodPh == null) { + return null; + } else if(goodHum == null && goodPh) { + return true; + } else if(goodHum && goodPh == null) { + return true; + } else if(goodHum && goodPh) { + return true; + } else { + return false; + } } - public GoodPlantModel checkPlantConditions() { - return new GoodPlantModel(this, 60.00); + //Private method for HUM/PH references + // + // + + private GoodPlantMeasureModel checkPlantMeasureConditions(boolean isHumidity, Plant plant, Set sensores) { + if(this.noMeasure(isHumidity, null)) { + return null; + } + Double percentage = this.generalMeasurePercentage(isHumidity, plant, sensores); + Boolean goodPh = this.plantIsGoodForParcel(plant, !isHumidity, isHumidity, sensores); + return new GoodPlantMeasureModel(percentage, goodPh); } - private Double generalMeasurePercentage(boolean isHumidity) { - Set sensores = this.getSensores(isHumidity); + private Boolean plantIsGoodForParcelMeasure(boolean isHumidity, Plant plant, Set sensores) { + Double percentage = this.generalMeasurePercentage(isHumidity, plant, sensores); + + if(percentage == null) { + return null; + } + if(percentage < Parcel.acceptablePercentage) { + return false; + } else { + return true; + } + + } + + private Double generalMeasurePercentage(boolean isHumidity, Plant plant, Set sensores) { + sensores = this.getSensores(isHumidity, sensores); if(this.noMeasure(isHumidity, sensores)){ return null; } int count = 0; for (Sensor sensor : sensores) { - Boolean isGood = sensor.isGood(this.getPlant()); + Boolean isGood = sensor.isGood(plant); if(isGood != null && isGood.booleanValue()) { count++; } @@ -160,9 +253,9 @@ private Double generalMeasurePercentage(boolean isHumidity) { return Double.valueOf(formatter.format(Double.valueOf(100*count/sensores.size()))); } - private Double measure(boolean isHumidity) { + private Double measure(boolean isHumidity, Set sensores) { - Set sensores = this.getSensores(isHumidity); + sensores = this.getSensores(isHumidity, sensores); if (this.noMeasure(isHumidity, sensores)) { return null; } @@ -185,9 +278,7 @@ private boolean noMeasure(boolean isHumidity, Set sensores) { return true; } - if(sensores == null) { - sensores = this.getSensores(isHumidity); - } + sensores = this.getSensores(isHumidity, sensores); boolean noMeasure = true; for (Sensor sensor : sensores) { @@ -199,6 +290,24 @@ private boolean noMeasure(boolean isHumidity, Set sensores) { return noMeasure; } + private Map getSensorTable(boolean isHumidity, Set sensores){ + if(this.isEmpty(isHumidity)) { + return null; + } + + Map mapa = new HashMap(); + + sensores = this.getSensores(isHumidity, sensores); + for (Sensor sensor : sensores) { + if(sensor.isEmpty()) { + mapa.put(sensor, sensor.getLatest()); + } else { + mapa.put(sensor, null); + } + } + return mapa; + } + private boolean isEmpty(boolean isHumidity) { if(isHumidity) { return this.humSensors.isEmpty(); @@ -207,7 +316,10 @@ private boolean isEmpty(boolean isHumidity) { } } - private Set getSensores(boolean isHumidity) { + private Set getSensores(boolean isHumidity, Set sensores) { + if (sensores != null) { + return sensores; + } Set set = new HashSet(); if(isHumidity) { for (HumSensor sensor : this.humSensors) { @@ -220,38 +332,4 @@ private Set getSensores(boolean isHumidity) { } return set; } - - private Map getSensorTable(boolean isHumidity){ - if(this.isEmpty(isHumidity)) { - return null; - } - - Map mapa = new HashMap(); - Set set = this.getSensores(isHumidity); - for (Sensor sensor : set) { - if(sensor.isEmpty()) { - mapa.put(sensor, sensor.getLatest()); - } else { - mapa.put(sensor, null); - } - } - return mapa; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (! (obj instanceof Parcel)) { - return false; - } - - Parcel other = (Parcel) obj; - if (this.getParcelId() == other.getParcelId()) { - return true; - } - return false; - } - } diff --git a/blossom/src/main/java/com/ies/blossom/model/GoodPlantModel.java b/blossom/src/main/java/com/ies/blossom/model/GoodPlantModel.java index c32f6e3..9d5ae58 100644 --- a/blossom/src/main/java/com/ies/blossom/model/GoodPlantModel.java +++ b/blossom/src/main/java/com/ies/blossom/model/GoodPlantModel.java @@ -5,71 +5,20 @@ public class GoodPlantModel { - public Boolean phNull; - public Boolean humNull; - public Double goodPhPercentage; - public Double goodHumPercentage; - public Boolean goodPh; - public Boolean goodHum; - public Parcel parcel; + public GoodPlantMeasureModel phStatus; + public GoodPlantMeasureModel humStatus; - public GoodPlantModel(Parcel parcel, double acceptablePercentage) { - - this.parcel = parcel; - - this.phNull = parcel.noPhMeasure(); - if (this.phNull == null) { - this.goodPhPercentage = null; - this.goodPh = null; - - } else { - this.goodPhPercentage = parcel.generalPhMeasurePercentage(); - if (this.goodPhPercentage < acceptablePercentage) { - this.goodPh = false; - } else { - this.goodPh = true; - } - } - - - - this.humNull = parcel.noHumMeasure(); - if (this.humNull == null) { - this.goodHumPercentage = null; - this.goodHum = null; - } else { - this.goodHumPercentage = parcel.generalHumMeasurePercentage(); - if (this.goodHumPercentage < acceptablePercentage) { - this.goodHum = false; - } else { - this.goodHum = true; - } - } - + public GoodPlantMeasureModel getPhStatus() { + return phStatus; } - - public Boolean isPhNull() { - return phNull; - } - public Boolean isHumNull() { - return humNull; - } - public Double getPhMeasure() { - return goodPhPercentage; - } - public Double getHumMeasure() { - return goodHumPercentage; - } - public Boolean isGoodPh() { - return goodPh; - } - public Boolean isGoodHum() { - return goodHum; + + public GoodPlantMeasureModel getHumStatus() { + return humStatus; } - public Parcel getParcel() { - return parcel; + + public GoodPlantModel(GoodPlantMeasureModel phStatus, GoodPlantMeasureModel humStatus) { + this.phStatus = phStatus; + this.humStatus = humStatus; } - - } diff --git a/blossom/src/main/resources/templates/parcel.html b/blossom/src/main/resources/templates/parcel.html index 5fea33b..5abee9e 100644 --- a/blossom/src/main/resources/templates/parcel.html +++ b/blossom/src/main/resources/templates/parcel.html @@ -52,13 +52,22 @@

Parcel info

-
PH NUll: ...
-
GOOD PH Percentage: ...
-
GOOD PH? ...
-
HUM NUll: ...
-
GOOD HUM Percentage: ...
-
GOOD HUM? ...
+
+ ... + ... +
+
+ PH é nulo, e por isso não pode-e fazer avaliação da planta para a medida. +
+
+ ... + ... +
+
+ Humidade é nula, e por isso não pode-e fazer avaliação da planta para a medida. +
+ @@ -163,12 +172,7 @@

Humidity Sensors

class="btn btn-primary">Add humidity sensor -
-

Results

-
Condições são boas
-
Condições não são - boas, ph e humidade não são adequadas a planta
-
+
From fba0a60798bec041acbe004db735fd7ca1135858 Mon Sep 17 00:00:00 2001 From: renanaferreira Date: Mon, 4 Jan 2021 09:52:16 +0000 Subject: [PATCH 2/3] dd --- .../java/com/ies/blossom/controllers/ParcelController.java | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/blossom/src/main/java/com/ies/blossom/controllers/ParcelController.java b/blossom/src/main/java/com/ies/blossom/controllers/ParcelController.java index e55cd68..15ed1b2 100644 --- a/blossom/src/main/java/com/ies/blossom/controllers/ParcelController.java +++ b/blossom/src/main/java/com/ies/blossom/controllers/ParcelController.java @@ -59,10 +59,7 @@ public String getParcel(Model model, @PathVariable(value="id") Long parcelId, Au model.addAttribute("notOwned", true); return "parcel.html"; } - - List plants2 = this.plantRepository.findAll(); - Testing.makeData(parcel, plants2); - + // ir buscar todos as ultimas medidas relativas aos sensores de ph if (!parcel.getPhSensors().isEmpty()) { Map retPh = new HashMap(); From 536f363556b4f21a8987a14c7ec426185d1cfe57 Mon Sep 17 00:00:00 2001 From: renanaferreira Date: Mon, 4 Jan 2021 10:01:22 +0000 Subject: [PATCH 3/3] remove testing code and add new files --- .../main/java/com/ies/blossom/Testing.java | 76 ------------------- .../blossom/controllers/ParcelController.java | 1 - .../java/com/ies/blossom/entitys/Parcel.java | 3 +- .../blossom/model/GoodPlantMeasureModel.java | 22 ++++++ 4 files changed, 24 insertions(+), 78 deletions(-) delete mode 100644 blossom/src/main/java/com/ies/blossom/Testing.java create mode 100644 blossom/src/main/java/com/ies/blossom/model/GoodPlantMeasureModel.java diff --git a/blossom/src/main/java/com/ies/blossom/Testing.java b/blossom/src/main/java/com/ies/blossom/Testing.java deleted file mode 100644 index 1feb311..0000000 --- a/blossom/src/main/java/com/ies/blossom/Testing.java +++ /dev/null @@ -1,76 +0,0 @@ -package com.ies.blossom; - -import java.sql.Date; -import java.sql.Timestamp; -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.Set; - -import com.ies.blossom.entitys.HumMeasure; -import com.ies.blossom.entitys.HumSensor; -import com.ies.blossom.entitys.Parcel; -import com.ies.blossom.entitys.PhMeasure; -import com.ies.blossom.entitys.PhSensor; -import com.ies.blossom.entitys.Plant; - -public class Testing { - - - public static void makeData(Parcel parcel, List plants) { - plants = new ArrayList(); - Plant plant = new Plant("Daisy", "Daisius", 2.0, 1.0, 1.0, 2.0); - parcel.setPlant(plant); - plants.add(plant); - plants.add(new Plant("Rosa", "Rosita", 1.5, 1.0, 1.0, 2.5)); - plants.add(new Plant("Margarida", "Margaridus", 0.5, 0.3, 1.0, 2.5)); - - Double[] lista = new Double[]{1.0, 1.6, 1.4}; - Double[] lista2 = new Double[]{4.0, 1.6, 1.4}; - try { - parcel.setPhSensors(makePhSensors(parcel, lista2)); - parcel.setHumSensors(makeHumSensors(parcel, lista)); - } catch (ParseException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - - } - - private static Set makeHumSensors(Parcel parcel, Double[] lista) throws ParseException{ - SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); - - Set humsensores = new HashSet(); - HumSensor humsensor; - List humeasures; - for (Double double1 : lista) { - humsensor = new HumSensor(parcel, new Date((sdf.parse("2019-01-01 00:00:00")).getTime())); - humeasures = new ArrayList(); - humeasures.add(new HumMeasure(humsensor, new Timestamp((sdf.parse("2019-01-03 00:00:00")).getTime()) , double1)); - humsensor.setMeasures(humeasures); - humsensores.add(humsensor); - } - return humsensores; - } - - private static Set makePhSensors(Parcel parcel, Double[] lista) throws ParseException{ - SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); - - Set phsensores = new HashSet(); - PhSensor phsensor; - List phmeasures; - for (Double double1 : lista) { - phsensor = new PhSensor(parcel, new Date((sdf.parse("2019-01-01 00:00:00")).getTime())); - phmeasures = new ArrayList(); - phmeasures.add(new PhMeasure(phsensor, new Timestamp((sdf.parse("2019-01-03 00:00:00")).getTime()), double1)); - phsensor.setMeasures(phmeasures); - phsensores.add(phsensor); - } - return phsensores; - } - - - -} diff --git a/blossom/src/main/java/com/ies/blossom/controllers/ParcelController.java b/blossom/src/main/java/com/ies/blossom/controllers/ParcelController.java index 15ed1b2..3adde28 100644 --- a/blossom/src/main/java/com/ies/blossom/controllers/ParcelController.java +++ b/blossom/src/main/java/com/ies/blossom/controllers/ParcelController.java @@ -1,6 +1,5 @@ package com.ies.blossom.controllers; -import com.ies.blossom.Testing; import com.ies.blossom.entitys.HumMeasure; import com.ies.blossom.entitys.HumSensor; import com.ies.blossom.entitys.Parcel; diff --git a/blossom/src/main/java/com/ies/blossom/entitys/Parcel.java b/blossom/src/main/java/com/ies/blossom/entitys/Parcel.java index 7105b7c..ddedb76 100644 --- a/blossom/src/main/java/com/ies/blossom/entitys/Parcel.java +++ b/blossom/src/main/java/com/ies/blossom/entitys/Parcel.java @@ -214,7 +214,8 @@ private Boolean plantIsGoodForParcel(Plant plant, boolean usesPh, boolean usesHu // private GoodPlantMeasureModel checkPlantMeasureConditions(boolean isHumidity, Plant plant, Set sensores) { - if(this.noMeasure(isHumidity, null)) { + sensores = this.getSensores(isHumidity, sensores); + if(this.noMeasure(isHumidity, sensores)) { return null; } Double percentage = this.generalMeasurePercentage(isHumidity, plant, sensores); diff --git a/blossom/src/main/java/com/ies/blossom/model/GoodPlantMeasureModel.java b/blossom/src/main/java/com/ies/blossom/model/GoodPlantMeasureModel.java new file mode 100644 index 0000000..0e42fe1 --- /dev/null +++ b/blossom/src/main/java/com/ies/blossom/model/GoodPlantMeasureModel.java @@ -0,0 +1,22 @@ +package com.ies.blossom.model; + +public class GoodPlantMeasureModel { + + + private double percentage; + private boolean good; + + public GoodPlantMeasureModel(double percentage, boolean good){ + this.percentage = percentage; + this.good = good; + } + + public double getPercentage() { + return percentage; + } + + public boolean isGood() { + return good; + } + +}