improved code style

This commit is contained in:
paul-loedige 2021-01-08 03:03:01 +01:00
parent ffe2e6dcd6
commit 4d9edac29a

View File

@ -1,17 +1,12 @@
package com.example.aped.ui.plots; package com.example.aped.ui.plots;
import android.graphics.Color; import android.graphics.Color;
import android.graphics.ColorSpace;
import android.net.sip.SipSession;
import android.content.DialogInterface;
import android.os.Bundle; import android.os.Bundle;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import android.util.Log; import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
import com.example.aped.MainActivity; import com.example.aped.MainActivity;
import com.example.aped.R; import com.example.aped.R;
import com.example.aped.communication.Communicator; import com.example.aped.communication.Communicator;
@ -21,7 +16,6 @@ import com.jjoe64.graphview.LegendRenderer;
import com.jjoe64.graphview.series.DataPoint; import com.jjoe64.graphview.series.DataPoint;
import com.jjoe64.graphview.series.LineGraphSeries; import com.jjoe64.graphview.series.LineGraphSeries;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import java.util.ArrayList; import java.util.ArrayList;
@ -34,17 +28,17 @@ public class PlotFragment extends Fragment {
private MainActivity mainActivity; private MainActivity mainActivity;
/** List of all the devices with buffers. **/ /** List of all the devices with buffers. **/
private List<String> bufferedDevices; private List<String> bufferedDevices;
/** Line Chart View that is displayed **/ /** Line Chart View that is displayed. **/
private GraphView graphView; private GraphView graphView;
/** maximum value on X axis. **/ /** maximum value on X axis. **/
private int maxX = 0; private int maxX = 0;
/** /**
* paul * Override method for the on create view event.
* @param inflater * @param inflater the default {@link LayoutInflater}
* @param container * @param container the {@link ViewGroup} the fragment is in
* @param savedInstanceState * @param savedInstanceState
* @return * @return the created view
*/ */
@Override @Override
public View onCreateView(final LayoutInflater inflater, public View onCreateView(final LayoutInflater inflater,
@ -64,6 +58,11 @@ public class PlotFragment extends Fragment {
return view; return view;
} }
/**
* Method to create a list of all the devices with buffers.
* @param xml the {@link IXML} to pull the information about the devices
* @return a list of all the device names
*/
private List<String> getBufferedDevices(final IXML xml) { private List<String> getBufferedDevices(final IXML xml) {
List<String> returnList = new ArrayList<>(); List<String> returnList = new ArrayList<>();
for (String deviceName : xml.getDeviceNames()) { for (String deviceName : xml.getDeviceNames()) {
@ -74,11 +73,18 @@ public class PlotFragment extends Fragment {
return returnList; return returnList;
} }
private void getBufferedValues(final Communicator communicator, final IXML xml) { /**
* Method to request and display all the buffered data.
* @param communicator the {@link Communicator} to request the data
* @param xml the {@link IXML} to get information about the data
*/
private void getBufferedValues(final Communicator communicator,
final IXML xml) {
Random random = new Random(); Random random = new Random();
for (String deviceName : this.bufferedDevices) { for (String deviceName : this.bufferedDevices) {
int factor; int factor;
if(xml.getPort(deviceName).get("protocol").equals("PWM")) { //normalize PWM and digital data
if (xml.getPort(deviceName).get("protocol").equals("PWM")) {
factor = 1; factor = 1;
} else { } else {
factor = 100; factor = 100;
@ -86,10 +92,13 @@ public class PlotFragment extends Fragment {
communicator.readBuffer(deviceName, response -> { communicator.readBuffer(deviceName, response -> {
try { try {
JSONObject buffer = response.getJSONObject("buffer"); JSONObject buffer = response.getJSONObject("buffer");
for (String pin : (List<String>) (xml.getPort(deviceName)).get("pins")) { for (String pin : (List<String>) (xml.getPort(deviceName))
.get("pins")) {
String valueString = buffer.getString(pin); String valueString = buffer.getString(pin);
valueString = valueString.substring(2, valueString.length() - 2); valueString = valueString
String[] valueStringArray = valueString.split("', '"); .substring(2, valueString.length() - 2);
String[] valueStringArray = valueString
.split("', '");
int[] values = new int[valueStringArray.length]; int[] values = new int[valueStringArray.length];
for (int i = 0; i < valueStringArray.length; i++) { for (int i = 0; i < valueStringArray.length; i++) {
values[i] = Integer.parseInt(valueStringArray[i]); values[i] = Integer.parseInt(valueStringArray[i]);
@ -97,12 +106,17 @@ public class PlotFragment extends Fragment {
//display data //display data
DataPoint[] dataPoints = new DataPoint[values.length]; DataPoint[] dataPoints = new DataPoint[values.length];
for (int i = 0; i < values.length; i++){ for (int i = 0; i < values.length; i++) {
dataPoints[i] = new DataPoint(i, values[i] * factor); dataPoints[i] = new DataPoint(i,
values[i] * factor);
} }
LineGraphSeries<DataPoint> lineGraphSeries = new LineGraphSeries<>(dataPoints); LineGraphSeries<DataPoint> lineGraphSeries =
lineGraphSeries.setTitle(deviceName +":"+ pin); new LineGraphSeries<>(dataPoints);
lineGraphSeries.setColor(Color.rgb(random.nextInt(256), random.nextInt(256), random.nextInt(256))); lineGraphSeries.setTitle(deviceName + ":" + pin);
lineGraphSeries.setColor(Color.rgb(
random.nextInt(256),
random.nextInt(256),
random.nextInt(256)));
graphView.addSeries(lineGraphSeries); graphView.addSeries(lineGraphSeries);
if (maxX < values.length) { if (maxX < values.length) {
maxX = values.length; maxX = values.length;
@ -113,10 +127,14 @@ public class PlotFragment extends Fragment {
graphView.getViewport().setXAxisBoundsManual(true); graphView.getViewport().setXAxisBoundsManual(true);
graphView.getViewport().setYAxisBoundsManual(true); graphView.getViewport().setYAxisBoundsManual(true);
graphView.getLegendRenderer().setVisible(true); graphView.getLegendRenderer().setVisible(true);
graphView.getLegendRenderer().setBackgroundColor(Color.rgb(200,200,200)); graphView.getLegendRenderer().setBackgroundColor(Color.rgb(
graphView.getLegendRenderer().setAlign(LegendRenderer.LegendAlign.TOP); 200, 200, 200));
graphView.getLegendRenderer()
.setAlign(LegendRenderer.LegendAlign.TOP);
} catch (JSONException e) { } catch (JSONException e) {
Log.e("PlotFragment", "error while parsing JSON on READ BUFFER: " + e.getMessage()); Log.e("PlotFragment",
"error while parsing JSON on READ BUFFER: "
+ e.getMessage());
} }
}); });
} }