Skip to content

Commit

Permalink
Merge pull request #51 from LuisValentim1/plants_adequated_to_crop
Browse files Browse the repository at this point in the history
Plants adequated to crop
  • Loading branch information
renanaferreira authored Jan 4, 2021
2 parents 7187527 + 536f363 commit 84e5d1f
Show file tree
Hide file tree
Showing 6 changed files with 188 additions and 203 deletions.
70 changes: 0 additions & 70 deletions blossom/src/main/java/com/ies/blossom/Testing.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public String getParcel(Model model, @PathVariable(value="id") Long parcelId, Au
model.addAttribute("notOwned", true);
return "parcel.html";
}

// ir buscar todos as ultimas medidas relativas aos sensores de ph
if (!parcel.getPhSensors().isEmpty()) {
Map<PhSensor, PhMeasure> retPh = new HashMap<PhSensor, PhMeasure>();
Expand Down Expand Up @@ -88,8 +88,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<Plant> plants = this.plantRepository.findAll();
Expand All @@ -98,6 +97,8 @@ public String getParcel(Model model, @PathVariable(value="id") Long parcelId, Au

model.addAttribute("parcel", parcel);



return "parcel.html";
}

Expand Down
191 changes: 135 additions & 56 deletions blossom/src/main/java/com/ies/blossom/entitys/Parcel.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand All @@ -18,6 +18,8 @@
@Entity
@Table(name = "parcels")
public class Parcel {

private static double acceptablePercentage = 60.00;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
Expand All @@ -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;
}
Expand Down Expand Up @@ -99,6 +108,26 @@ public void setHumSensors(Set<HumSensor> 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);
}
Expand All @@ -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<Plant> bestPlants(List<Plant> 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);
}
Expand All @@ -124,34 +158,94 @@ public boolean noHumMeasure() {
}

public Map<Sensor, Measure> getPhSensorTable(){
return this.getSensorTable(false);
return this.getSensorTable(false, null);
}

public Map<Sensor, Measure> getHumSensorTable(){
return this.getSensorTable(true);
return this.getSensorTable(true, null);
}

public Double generalHumMeasurePercentage() {
return this.generalMeasurePercentage(true);
//Private Methods
//
//

//Consider both variables
private List<Plant> bestPlantsFor(List<Plant> plants, boolean usesPh, boolean usesHum, Set<Sensor> sensores) {
List<Plant> goodPlants = new ArrayList<Plant>();
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<Sensor> 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<Sensor> sensores) {
sensores = this.getSensores(isHumidity, sensores);
if(this.noMeasure(isHumidity, sensores)) {
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<Sensor> sensores = this.getSensores(isHumidity);
private Boolean plantIsGoodForParcelMeasure(boolean isHumidity, Plant plant, Set<Sensor> 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<Sensor> 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++;
}
Expand All @@ -160,9 +254,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<Sensor> sensores) {

Set<Sensor> sensores = this.getSensores(isHumidity);
sensores = this.getSensores(isHumidity, sensores);
if (this.noMeasure(isHumidity, sensores)) {
return null;
}
Expand All @@ -185,9 +279,7 @@ private boolean noMeasure(boolean isHumidity, Set<Sensor> sensores) {
return true;
}

if(sensores == null) {
sensores = this.getSensores(isHumidity);
}
sensores = this.getSensores(isHumidity, sensores);

boolean noMeasure = true;
for (Sensor sensor : sensores) {
Expand All @@ -199,6 +291,24 @@ private boolean noMeasure(boolean isHumidity, Set<Sensor> sensores) {
return noMeasure;
}

private Map<Sensor, Measure> getSensorTable(boolean isHumidity, Set<Sensor> sensores){
if(this.isEmpty(isHumidity)) {
return null;
}

Map<Sensor, Measure> mapa = new HashMap<Sensor, Measure>();

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();
Expand All @@ -207,7 +317,10 @@ private boolean isEmpty(boolean isHumidity) {
}
}

private Set<Sensor> getSensores(boolean isHumidity) {
private Set<Sensor> getSensores(boolean isHumidity, Set<Sensor> sensores) {
if (sensores != null) {
return sensores;
}
Set<Sensor> set = new HashSet<Sensor>();
if(isHumidity) {
for (HumSensor sensor : this.humSensors) {
Expand All @@ -220,38 +333,4 @@ private Set<Sensor> getSensores(boolean isHumidity) {
}
return set;
}

private Map<Sensor, Measure> getSensorTable(boolean isHumidity){
if(this.isEmpty(isHumidity)) {
return null;
}

Map<Sensor, Measure> mapa = new HashMap<Sensor, Measure>();
Set<Sensor> 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;
}

}
Loading

0 comments on commit 84e5d1f

Please sign in to comment.