Commit e65638c1 authored by Ramide Dantas's avatar Ramide Dantas
Browse files

Demo changes: IrrigRec, Map View, Tab View, etc.

parent 0264d65c
Pipeline #16645 failed with stages
......@@ -8,9 +8,10 @@ import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import com.swamp.swampapp.R;
import com.swamp.swampapp.model.Irrigation;
import com.swamp.swampapp.model.IrrigationRecommendation;
import com.swamp.swampapp.model.WaterLevelMeter;
import com.swamp.swampapp.model.WaterNeed;
import com.swamp.swampapp.model.WeatherForecast;
......@@ -41,7 +42,7 @@ public class HomeTabFarmFragment extends Fragment {
private Double waterNeedValue = 0.0;
private Integer waterPrecipitationValue = 0;
private Integer waterSupplyValue = 0;
private Integer waterIrrigationValue = 0;
private Double waterIrrigationValue = 0.0;
private Double waterDeficitValue = 0.0;
private SwampApiService swampApiService;
......@@ -126,22 +127,29 @@ public class HomeTabFarmFragment extends Fragment {
}
private void loadIrrigation() {
Call<List<Irrigation>> call = swampApiService.getIrrigations();
// Call<List<Irrigation>> call = swampApiService.getIrrigations();
Call<List<IrrigationRecommendation>> call = swampApiService.getIrrigationsPOG();
call.enqueue(new Callback<List<Irrigation>>() {
call.enqueue(new Callback<List<IrrigationRecommendation>>() {
@Override
public void onResponse(Call<List<Irrigation>> call, Response<List<Irrigation>> response) {
if (response.body() != null) {
Irrigation irrigation = response.body().get(0);
waterIrrigationValue = FunctionsUtils.sumList(irrigation.getWaterVolume().get(0));
txtIrrigation.setText(waterIrrigationValue.toString() + " mm");
updateWaterResult();
public void onResponse(Call<List<IrrigationRecommendation>> call, Response<List<IrrigationRecommendation>> response) {
if (response.body() != null && !response.body().isEmpty()) {
try {
IrrigationRecommendation irrigation = response.body().get(0);
waterIrrigationValue = FunctionsUtils.sumDoubleList(irrigation.getWaterVolume().get(0));
txtIrrigation.setText(waterIrrigationValue.toString() + " mm");
updateWaterResult();
} catch (Exception e) {
Toast.makeText(getContext(), "ERROR: Failed to load IR: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(getContext(), "WARNING: No irrigation recommendations found.", Toast.LENGTH_LONG).show();
}
}
@Override
public void onFailure(Call<List<Irrigation>> call, Throwable t) {
public void onFailure(Call<List<IrrigationRecommendation>> call, Throwable t) {
}
});
......
......@@ -13,10 +13,11 @@ import android.view.View;
import android.view.ViewGroup;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.Toast;
import com.swamp.swampapp.R;
import com.swamp.swampapp.model.FieldTable;
import com.swamp.swampapp.model.Irrigation;
import com.swamp.swampapp.model.IrrigationRecommendation;
import com.swamp.swampapp.model.IrrigationStatus;
import com.swamp.swampapp.model.PostEntity;
import com.swamp.swampapp.model.Probe;
......@@ -30,7 +31,6 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
......@@ -50,9 +50,9 @@ public class IrrigationTabTableFragment extends BaseTabTableFragment {
private String currentStatus;
private List<FieldTable> fieldsTableList;
private List<FieldTable> listfieldSoilByDay;
private static List<Irrigation> IRs;
private static List<IrrigationRecommendation> IRs;
private Integer selectedIrrigation;
private Map<Integer, List<List<Integer>>> mapUpdatedValues;
private Map<Integer, List<List<Double>>> mapUpdatedValues;
public IrrigationTabTableFragment() {
// Required empty public constructor
......@@ -82,13 +82,13 @@ public class IrrigationTabTableFragment extends BaseTabTableFragment {
create(SwampApiService.class);
mapUpdatedValues.size();
for (Integer key: mapUpdatedValues.keySet()) {
Irrigation irrigation = IRs.get(key);
IrrigationRecommendation irrigation = IRs.get(key);
PostEntity postEntity = new PostEntity();
postEntity.setEntity(irrigation.getEntityId());
postEntity.setAttr("waterVolume2");
postEntity.setType("StructuredValue");
List<Integer> values = FunctionsUtils.transformTo1D(irrigation.getWaterVolume());
List<Double> values = FunctionsUtils.transformTo1D(irrigation.getWaterVolume());
postEntity.setValue(values.toString());
Call<String> call = swampApiService.updateEntityyy(postEntity);
......@@ -199,7 +199,7 @@ public class IrrigationTabTableFragment extends BaseTabTableFragment {
private void updateField() {
for (Integer key : mapUpdatedValues.keySet()) {
Irrigation editedIrrigation = IRs.get(key);
IrrigationRecommendation editedIrrigation = IRs.get(key);
editedIrrigation.setWaterVolume(mapUpdatedValues.get(key));
IRs.set(key, editedIrrigation);
}
......@@ -270,12 +270,12 @@ public class IrrigationTabTableFragment extends BaseTabTableFragment {
private void requestIrrigationStatus() {
SwampApiService swampApiService = SwampClient.getRetrofitInstance().
create(SwampApiService.class);
// TODO Fix this to a better entitiy (IrrigationPlan)
Call<IrrigationStatus> call = swampApiService.getIrrigationStatus();
call.enqueue(new Callback<IrrigationStatus>() {
@Override
public void onResponse(Call<IrrigationStatus> call, Response<IrrigationStatus> response) {
if (response != null) {
if (response != null && response.body() != null) {
IrrigationStatus irrigationStatus = response.body();
currentStatus = irrigationStatus.getStatus();
changeIrrigationStatusIcon(currentStatus);
......@@ -296,17 +296,18 @@ public class IrrigationTabTableFragment extends BaseTabTableFragment {
SwampApiService swampApiService = SwampClient.getRetrofitInstance().
create(SwampApiService.class);
Call<List<Irrigation>> callIR = swampApiService.getIrrigations();
// Call<List<IrrigationRecommendation>> callIR = swampApiService.getIrrigations();
Call<List<IrrigationRecommendation>> callIR = swampApiService.getIrrigationsPOG();
callIR.enqueue(new Callback<List<Irrigation>>() {
callIR.enqueue(new Callback<List<IrrigationRecommendation>>() {
@Override
public void onResponse(Call<List<Irrigation>> call, Response<List<Irrigation>> response) {
public void onResponse(Call<List<IrrigationRecommendation>> call, Response<List<IrrigationRecommendation>> response) {
if (response.body() != null) {
IRs = response.body();
Collections.sort(IRs, new Comparator<Irrigation>() {
Collections.sort(IRs, new Comparator<IrrigationRecommendation>() {
@Override
public int compare(Irrigation irrigation, Irrigation t1) {
public int compare(IrrigationRecommendation irrigation, IrrigationRecommendation t1) {
return irrigation.getEntityId().compareTo(t1.getEntityId());
}
});
......@@ -318,29 +319,37 @@ public class IrrigationTabTableFragment extends BaseTabTableFragment {
List<String> dates = new ArrayList<>();
for (Irrigation irr : IRs) {
for (IrrigationRecommendation irr : IRs) {
FieldTable fieldTable = new FieldTable();
fieldTable.setFieldName(irr.getEntityId().replaceAll("urn:ngsi-ld:",""));
List<Integer> values = new ArrayList<>();
for (int i=0; i < irr.getWaterVolume().size(); i++) {
int value = (int) FunctionsUtils.sumList(irr.getWaterVolume().get(i));
// TODO Work wih double
for (int i = 0; i < irr.getWaterVolume().size(); i++) {
int value = (int)Math.round(FunctionsUtils.sumDoubleList(irr.getWaterVolume().get(i)));
values.add(value);
}
fieldTable.setValues(values);
// TODO deprecated, using timeReference
// if (irr.getDatesTo().size() > dates.size()) {
// dates = new ArrayList<>();
//// for (Date date : irr.getDatesTo()) {
// for (int i=0; i<4; i++) {
// Date date = irr.getDatesFrom().get(0);
// SimpleDateFormat df = new SimpleDateFormat("dd/MM");
// String dateString = df.format(date);
// dates.add(dateString);
// }
// }
if (irr.getDatesTo().size() > dates.size()) {
dates = new ArrayList<>();
// for (Date date : irr.getDatesTo()) {
for (int i=0; i<4; i++) {
Date date = irr.getDatesFrom().get(0);
SimpleDateFormat df = new SimpleDateFormat("dd/MM");
String dateString = df.format(date);
dates.add(dateString);
}
dates = new ArrayList<>();
for (Date date: irr.getTimeReferences()) {
SimpleDateFormat df = new SimpleDateFormat("dd/MM");
String dateString = df.format(date);
dates.add(dateString);
}
fieldsTableList.add(fieldTable);
......@@ -355,10 +364,12 @@ public class IrrigationTabTableFragment extends BaseTabTableFragment {
}
@Override
public void onFailure(Call<List<Irrigation>> call, Throwable t) {
public void onFailure(Call<List<IrrigationRecommendation>> call, Throwable t) {
List<FieldTable> fieldList = createFields();
tableLayout = createColumns(tableLayout, "Irrigation");
tableLayout = fillTable(fieldList, tableLayout);
Toast.makeText(getActivity(), "WARNING: failed to load IRs. Showing sample data.", Toast.LENGTH_LONG).show();
}
});
......@@ -449,8 +460,8 @@ public class IrrigationTabTableFragment extends BaseTabTableFragment {
}
public static ArrayList<FieldTable> createFieldsByHours(int index) {
Irrigation irrigation = IRs.get(index);
List<List<Integer>> waterVolume = irrigation.getWaterVolume();
IrrigationRecommendation irrigation = IRs.get(index);
List<List<Double>> waterVolume = irrigation.getWaterVolume();
ArrayList<FieldTable> fieldList = new ArrayList<>();
......@@ -465,7 +476,7 @@ public class IrrigationTabTableFragment extends BaseTabTableFragment {
field.setFieldName(r + ":00");
for (int c=0; c < colsLength; c++) {
Integer value = waterVolume.get(c).get(r);
Integer value = (int)Math.round(waterVolume.get(c).get(r));
rowsValues.add(value);
}
......
......@@ -10,7 +10,6 @@ import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Region;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.DisplayMetrics;
......@@ -25,7 +24,6 @@ import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CircleOptions;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
......@@ -35,7 +33,7 @@ import com.swamp.swampapp.R;
import com.swamp.swampapp.adapters.InfoWindowCustomAdapter;
import com.swamp.swampapp.model.DroneFlight;
import com.swamp.swampapp.model.Field;
import com.swamp.swampapp.model.Irrigation;
import com.swamp.swampapp.model.IrrigationRecommendation;
import com.swamp.swampapp.model.IrrigationZone;
import com.swamp.swampapp.model.ManagementZone;
import com.swamp.swampapp.model.SoilProbe;
......@@ -49,7 +47,6 @@ import static android.support.v4.graphics.ColorUtils.HSLToColor;
import java.util.List;
import java.util.Random;
import io.reactivex.internal.operators.flowable.FlowableElementAt;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
......@@ -460,110 +457,95 @@ public class MapFragment extends Fragment implements OnMapReadyCallback {
}
public void showIrrigationZones(List<IrrigationZone> irrigationZones,
List<IrrigationRecommendation> irrigationRecs) {
public void loadIrrigationZones() {
// Call<List<IrrigationZone>> call = swampApiService.getIrrigationZones();
Call<List<IrrigationZone>> call = swampApiService.getIrrigationZonesPOG();
call.enqueue(new Callback<List<IrrigationZone>>() {
@Override
public void onResponse(Call<List<IrrigationZone>> call, Response<List<IrrigationZone>> response) {
if (response.body() != null) {
List<IrrigationZone> irrigationZones = response.body();
if (irrigationZones == null) return;
for (IrrigationZone irrigationZone : irrigationZones) {
// Skipping bad response
if ( irrigationZone == null ||
irrigationZone.getLocation() == null ||
irrigationZone.getLocation().get(0) == null
) {
continue;
}
// Call<List<Irrigation>> callIrrigationRec = swampApiService.getIrrigationById(irrigationZone.getEntityId());
// Call<List<Irrigation>> callIrrigationRec = swampApiService.getIrrigations();
Call<List<Irrigation>> callIrrigationRec = swampApiService.getIrrigationsPOG();
for (IrrigationZone irrigationZone : irrigationZones) {
// Skipping bad response
if (irrigationZone == null ||
irrigationZone.getLocation() == null ||
irrigationZone.getLocation().get(0) == null
) {
continue;
}
callIrrigationRec.enqueue(new Callback<List<Irrigation>>() {
@Override
public void onResponse(Call<List<Irrigation>> call, Response<List<Irrigation>> response) {
Irrigation irrigation = null;
// for (Irrigation irr : response.body()) {
// if (irr != null)
// irrigation = irr;
// }
IrrigationRecommendation irrigation = null;
if (irrigationRecs != null) {
for (IrrigationRecommendation irr : irrigationRecs) {
if (irr != null && irr.getRefIrrigationZone() != null && irr.getRefIrrigationZone().equals(irrigationZone.getEntityId())) {
irrigation = irr;
break;
}
}
}
for (Irrigation irr : response.body()) {
if (irr != null && irr.getRefIrrigationZone() != null && irr.getRefIrrigationZone().equals(irrigationZone.getEntityId())) {
irrigation = irr;
break;
}
}
double irrigationValue = 5;
int irrigationValue = 5;
if (irrigation != null && irrigation.getWaterVolume() != null) {
irrigationValue = FunctionsUtils.sumDoubleList(irrigation.getWaterVolume().get(0));
}
if (irrigation != null && irrigation.getWaterVolume() != null) {
irrigationValue = FunctionsUtils.sumList(irrigation.getWaterVolume().get(0));
}
PolygonOptions polygonOptions = new PolygonOptions();
polygonOptions.addAll(irrigationZone.getLocation());
PolygonOptions polygonOptions = new PolygonOptions();
polygonOptions.addAll(irrigationZone.getLocation());
// float lightness = 1 - ((float) irrigationValue / 100);
float lightness = (float)(Math.min(irrigationValue, 25) / 25);
lightness = 1 - (float)Math.pow(1 - lightness, 2);
// float[] colors = new float[]{240.f, 1f, lightness};
float[] colors = new float[]{240.f, 1f, lightness};
// float[] colors = new float[]{lightness*360, 1f, lightness};
int color = HSLToColor(colors);
float lightness = 1 - ((float)irrigationValue / 100);
// color = setAlphaColor(color, 0.5f);
color = setAlphaColor(color, lightness);
float[] colors = new float[]{240.f, 1f, lightness};
int color = HSLToColor(colors);
polygonOptions.fillColor(color);
polygonOptions.strokeColor(color);
polygonOptions.getStrokeWidth();
color = setAlphaColor(color, 0.5f);
if (showLabels) {
LatLng latLngCenter = getLatLngCenter(polygonOptions.getPoints(), false);
String irrigationName = irrigationZone.getEntityId().replace("urn:ngsi-ld:IrrigationZone:", "");
irrigationName = "IZ-" + irrigationName;
Marker marker = addText(getActivity(), mMap, latLngCenter,
irrigationName, 1, 12);
}
polygonOptions.fillColor(color);
polygonOptions.strokeColor(color);
polygonOptions.getStrokeWidth();
mMap.addPolygon(polygonOptions);
}
}
if (showLabels) {
LatLng latLngCenter = getLatLngCenter(polygonOptions.getPoints(), false);
String irrigationName = irrigationZone.getEntityId().replace("urn:ngsi-ld:IrrigationZone:", "");
irrigationName = "IZ-" + irrigationName;
Marker marker = addText(getActivity(), mMap, latLngCenter,
irrigationName, 1, 12);
}
mMap.addPolygon(polygonOptions);
}
public void loadIrrigationZones() {
Call<List<IrrigationZone>> call = swampApiService.getIrrigationZonesPOG();
call.enqueue(new Callback<List<IrrigationZone>>() {
@Override
public void onResponse(Call<List<IrrigationZone>> call, Response<List<IrrigationZone>> response) {
if (response.body() != null && !response.body().isEmpty()) {
List<IrrigationZone> irrigationZones = response.body();
Call<List<IrrigationRecommendation>> callIrrigationRec = swampApiService.getIrrigationsPOG();
callIrrigationRec.enqueue(new Callback<List<IrrigationRecommendation>>() {
@Override
public void onResponse(Call<List<IrrigationRecommendation>> call, Response<List<IrrigationRecommendation>> response) {
List<IrrigationRecommendation> irrigationRecs = response.body();
@Override
public void onFailure(Call<List<Irrigation>> call, Throwable t) {
showIrrigationZones(irrigationZones, irrigationRecs);
}
@Override
public void onFailure(Call<List<IrrigationRecommendation>> call, Throwable t) {
}
});
// PolygonOptions polygonOptions = new PolygonOptions();
// polygonOptions.addAll(irrigationZone.getLocation());
//
// float[] colors = new float[]{240.f, 1f, .9f};
// int color = HSLToColor(colors);
//
// color = setAlphaColor(color, 0.5f);
//
// polygonOptions.fillColor(color);
// polygonOptions.strokeColor(color);
// polygonOptions.getStrokeWidth();
//
// if (showLabels) {
// LatLng latLngCenter = getLatLngCenter(polygonOptions.getPoints(), false);
// String irrigationName = irrigationZone.getEntityId().replace("urn:ngsi-ld:IrrigationZone:", "");
// irrigationName = "IZ-" + irrigationName;
// Marker marker = addText(getActivity(), mMap, latLngCenter,
// irrigationName, 1, 12);
// }
//
// mMap.addPolygon(polygonOptions);
}
showIrrigationZones(irrigationZones, null);
}
});
}
}
@Override
public void onFailure(Call<List<IrrigationZone>> call, Throwable t) {
}
});
}
......
......@@ -47,7 +47,7 @@ public class TableFragment extends Fragment {
private CustomTabAdapter tabAdapter;
private CustomTabAdapter tabAdapterAux;
private SwampApiService swampApiService;
private Map<Integer, List<List<Integer>>> mapUpdatedValues;
private Map<Integer, List<List<Double>>> mapUpdatedValues;
private ArrayList<String> managementZonesIds = new ArrayList<>();
......@@ -115,25 +115,25 @@ public class TableFragment extends Fragment {
return view;
}
public List<List<Integer>> getWaterVolumeEdited(TableLayout tableLayoutOverview) {
List<List<Integer>> waterVolume = new ArrayList<>();
public List<List<Double>> getWaterVolumeEdited(TableLayout tableLayoutOverview) {
List<List<Double>> waterVolume = new ArrayList<>();
for(int i=1; i < tableLayoutOverview.getChildCount(); i++) {
TableRow row = (TableRow) tableLayoutOverview.getChildAt(i);
List<Integer> values = new ArrayList<>();
List<Double> values = new ArrayList<>();
for (int j=1; j < row.getChildCount(); j++) {
Integer value = Integer.parseInt(((EditText)row.getChildAt(j)).getText().toString());
Double value = Double.parseDouble(((EditText)row.getChildAt(j)).getText().toString());
values.add(value);
}
waterVolume.add(values);
}
List<List<Integer>> table = new ArrayList<List<Integer>>();
List<List<Double>> table = new ArrayList<List<Double>>();
for(int i=0; i<waterVolume.get(0).size(); i++) {
List<Integer> rows = new ArrayList<>();
List<Double> rows = new ArrayList<>();
for (int j=0; j< waterVolume.size(); j++) {
Integer value = waterVolume.get(j).get(i);
Double value = waterVolume.get(j).get(i);
rows.add(value);
}
table.add(rows);
......@@ -158,7 +158,7 @@ public class TableFragment extends Fragment {
// tab.select();
List<List<Integer>> waterVolumeEdited = getWaterVolumeEdited(tableLayoutOverview);
List<List<Double>> waterVolumeEdited = getWaterVolumeEdited(tableLayoutOverview);
tabAdapter = tabAdapterAux;
......
......@@ -3,22 +3,23 @@ package com.swamp.swampapp.model;
import java.util.Date;
import java.util.List;
public class Irrigation {
public class IrrigationRecommendation {
private String entityId;
private String refIrrigationZone;
private List<List<Integer>> waterVolume;
private List<List<Double>> waterVolume;
private List<Date> datesFrom;
private List<Date> datesTo;
private List<Date> timeReferences;
public Irrigation() {
public IrrigationRecommendation() {
}
public List<List<Integer>> getWaterVolume() {
public List<List<Double>> getWaterVolume() {
return waterVolume;
}
public void setWaterVolume(List<List<Integer>> waterVolume) {
public void setWaterVolume(List<List<Double>> waterVolume) {
this.waterVolume = waterVolume;
}
......@@ -53,4 +54,12 @@ public class Irrigation {
public void setDatesTo(List<Date> datesTo) {
this.datesTo = datesTo;
}
public void setTimeReferences(List<Date> timeReferences) {
this.timeReferences = timeReferences;
}
public List<Date> getTimeReferences() {
return this.timeReferences;
}
}
......@@ -8,7 +8,7 @@ import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.reflect.TypeToken;
import com.swamp.swampapp.model.Irrigation;
import com.swamp.swampapp.model.IrrigationRecommendation;
import com.swamp.swampapp.utils.FunctionsUtils;
import com.swamp.swampapp.utils.ParserUtils;
......@@ -20,10 +20,10 @@ import java.util.Collections;
import java.util.Date;
import java.util.List;
public class IrrigationDeserializer implements JsonDeserializer<Irrigation> {
public class IrrigationDeserializer implements JsonDeserializer<IrrigationRecommendation> {