From 55f4998f783c47b32b0271578cbe38763b635920 Mon Sep 17 00:00:00 2001 From: paul-loedige Date: Fri, 8 Jan 2021 00:35:36 +0100 Subject: [PATCH] can read and isolate buffered data from raspberry --- .../example/aped/ui/plots/PlotFragment.java | 88 +++++++++++++------ 1 file changed, 63 insertions(+), 25 deletions(-) diff --git a/APED/app/src/main/java/com/example/aped/ui/plots/PlotFragment.java b/APED/app/src/main/java/com/example/aped/ui/plots/PlotFragment.java index 15d6239..ca621df 100644 --- a/APED/app/src/main/java/com/example/aped/ui/plots/PlotFragment.java +++ b/APED/app/src/main/java/com/example/aped/ui/plots/PlotFragment.java @@ -1,13 +1,25 @@ package com.example.aped.ui.plots; +import android.net.sip.SipSession; import android.os.Bundle; import androidx.fragment.app.Fragment; + +import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import com.example.aped.MainActivity; import com.example.aped.R; +import com.example.aped.communication.Communicator; +import com.example.aped.utils.IXML; +import com.example.aped.utils.XMLHandler; + +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + import java.util.ArrayList; +import java.util.Dictionary; import java.util.List; import lecho.lib.hellocharts.model.AxisValue; import lecho.lib.hellocharts.model.Line; @@ -19,6 +31,10 @@ public class PlotFragment extends Fragment { /** Include the MainActivity.*/ private MainActivity mainActivity; + /** List of all the devices with buffers. **/ + private List bufferedDevices; + /** Line Chart View that is displayed **/ + private LineChartView lineChartView; /** * paul @@ -32,35 +48,57 @@ public class PlotFragment extends Fragment { final ViewGroup container, final Bundle savedInstanceState) { this.mainActivity = (MainActivity) getActivity(); + this.bufferedDevices = getBufferedDevices(mainActivity.getXml()); + // Inflate the layout for this fragment View view = inflater.inflate(R.layout.fragment_plot, container, false); - LineChartView lineChartView = view.findViewById(R.id.test_plot); + this.lineChartView = view.findViewById(R.id.test_plot); - String[] xAxisData = {"15:30", "15:31", "15:32"}; - int[] yAxisData = {1, 3, 2}; - - List yAxisValues = new ArrayList(); - List xAxisValues = new ArrayList(); - - Line line = new Line(yAxisValues); - - for (int i = 0; i < xAxisData.length; i++) { - xAxisValues.add(i, new AxisValue(i).setLabel(xAxisData[i])); - } - - for (int i = 0; i < yAxisData.length; i++) { - yAxisValues.add(new PointValue(i, yAxisData[i])); - } - - List lines = new ArrayList(); - - lines.add(line); - - LineChartData lineChartData = new LineChartData(); - lineChartData.setLines(lines); - - lineChartView.setLineChartData(lineChartData); + getBufferedValues(mainActivity.getDelivery(), mainActivity.getXml()); mainActivity.getConfigurationHandler().setCurrentView(R.id.nav_plots); return view; } + + private List getBufferedDevices(IXML xml){ + List returnList = new ArrayList<>(); + for (String deviceName : xml.getDeviceNames()){ + if (xml.getBufferSize(deviceName) > 0) { + returnList.add(deviceName); + } + } + return returnList; + } + + private void getBufferedValues(Communicator communicator, IXML xml){ + for (String deviceName : this.bufferedDevices){ + communicator.readBuffer(deviceName,response -> { + try { + JSONObject buffer = response.getJSONObject("buffer"); + for(String pin : (List) (xml.getPort(deviceName)).get("pins")) { + String valueString = buffer.getString(pin); + valueString = valueString.substring(2,valueString.length()-2); + String[] valueStringArray = valueString.split("', '"); + int[] values = new int[valueString.length()]; + for (int i = 0; i < valueStringArray.length; i++){ + values[i] = Integer.parseInt(valueStringArray[i]); + } + + //display data + List yAxisValues = new ArrayList(); + for (int i = 0; i < values.length; i++){ + yAxisValues.add(values[i]); + } + Line line = new Line(yAxisValues); + List lines = new ArrayList(); + lines.add(line); + LineChartData lineChartData = new LineChartData(); + lineChartData.setLines(lines); + this.lineChartView.setLineChartData(lineChartData); + } + } catch (JSONException e) { + Log.e("PlotFragment", "error while parsing JSON on READ BUFFER: " + e.getMessage()); + } + }); + } + } }