Skip to content

Instantly share code, notes, and snippets.

@utkarshns

utkarshns/.java Secret

Created August 25, 2016 08:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save utkarshns/e1723dcc57022fcd392bc3b127b6c898 to your computer and use it in GitHub Desktop.
Save utkarshns/e1723dcc57022fcd392bc3b127b6c898 to your computer and use it in GitHub Desktop.
package faclon.sensorremote;
/**
* Created by Utkarsh on 18-Feb-16.
*/
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.AxisValueFormatter;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
import com.github.mikephil.charting.utils.ColorTemplate;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;
public class FirstFragment extends Fragment {
protected String sensorUID;
protected String tankname;
protected String SCALE_M;
private LineChart mChart;
private float prev;
protected String SCALE_C;
protected String DPs;
protected String UNITs;
protected String accessToken;
protected String tHeight;
View v;
int numVar = 10;
float m = 1;
String[] prsdData;
float c = 0;
int len = 0;
public static FirstFragment newInstance(String text) {
FirstFragment f = new FirstFragment();
return f;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
setRetainInstance(true);
v = inflater.inflate(R.layout.first_frag, container, false);
Bundle bundle = getArguments();
if (bundle != null) {
sensorUID = getArguments().getString("senID");
tankname = getArguments().getString("tNAME");
SCALE_C = (getArguments().getString("tSCALEC"));
SCALE_M = (getArguments().getString("tSCALEM"));
DPs = (getArguments().getString("tDP"));
UNITs = (getArguments().getString("tUNIT"));
numVar = getArguments().getInt("numVar");
accessToken = getArguments().getString("accessToken");
tHeight = getArguments().getString("tHeight");
}
mChart = (LineChart) v.findViewById(R.id.chart1);
mChart.setPinchZoom(true);
m = Float.parseFloat(SCALE_M);
c = Float.parseFloat(SCALE_C);
setData();
return v;
}
private ArrayList<LineDataSet> getDataSet() {
ArrayList<LineDataSet> dataSets = null;
ArrayList<Entry> valueSet1 = new ArrayList<>();
int z = 0;
for (int i = 1; i < (len / 2) + 1; i++) {
valueSet1.add(new Entry((Float.parseFloat(prsdData[i * 2]) * m + c), z++));
}
LineDataSet lDataSet1 = new LineDataSet(valueSet1, "Readings");
lDataSet1.setColor(Color.rgb(0, 0, 0));
lDataSet1.setValueTextSize(8f);
lDataSet1.setDrawValues(false);
lDataSet1.getCircleColor(5);
dataSets = new ArrayList<>();
dataSets.add(lDataSet1);
return dataSets;
}
private void setData() {
new getChartData().execute();
}
private class getChartData extends AsyncTask<Void, Void, String[]> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
protected String[] doInBackground(Void... params) {
return getData();
}
@Override
protected void onPostExecute(String[] s) {
Log.d("printing", "Doing + " + s);
int len = s.length;
ArrayList<Entry> values1 = new ArrayList<Entry>();
for (int i = 1; i < (len / 2) + 1; i++) {
int x = Integer.parseInt(prsdData[i * 2 - 1]);
float y = (Float.parseFloat((prsdData[i * 2])) * m + c);
// Log.d("valuesareforreal", Float.toString(x) + " " + Float.toString(y));
System.out.println("Value : " + y + " at " + x);
// if (prev != x)
{
values1.add(new Entry(x, y));
}
// This is a little weird
// // If not keep that if condition then the graph is very weird. Check For Yourself.
prev = x;
}
// create a dataset and give it a type
LineDataSet set1 = new LineDataSet(values1, "DataSet 1");
set1.setAxisDependency(YAxis.AxisDependency.LEFT);
set1.setColor(ColorTemplate.getHoloBlue());
set1.setValueTextColor(ColorTemplate.getHoloBlue());
set1.setLineWidth(1.5f);
set1.setDrawCircles(false);
set1.setDrawValues(false);
set1.setFillAlpha(65);
set1.setFillColor(ColorTemplate.getHoloBlue());
set1.setHighLightColor(Color.rgb(244, 117, 117));
set1.setDrawCircleHole(false);
// create a data object with the datasets
LineData data = new LineData(set1);
data.setValueTextColor(Color.WHITE);
data.setValueTextSize(5f);
// set data
try {
MyMarkerView mv = new MyMarkerView(getContext(), R.layout.custom_marker_view);
mChart.setMarkerView(mv);
} catch (Exception e) {
System.out.println(e);
}
mChart.setData(data);
List<ILineDataSet> sets = mChart.getData()
.getDataSets();
for (ILineDataSet iSet : sets) {
LineDataSet set = (LineDataSet) iSet;
set.setDrawFilled(true);
}
mChart.invalidate();
}
protected String[] getData() {
String decodedString = "";
String returnMsg = "";
String request = "http://x.x.x.x:4000/getLast?device=" + sensorUID + "&sensor=arduino&lim=" + numVar;
URL url;
HttpURLConnection connection = null;
try {
url = new URL(request);
connection = (HttpURLConnection) url.openConnection();
connection.addRequestProperty("Content-Type", "application/json");
connection.addRequestProperty("Authorization", "Bearer ");
connection.setRequestMethod("GET");
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
while ((decodedString = in.readLine()) != null) {
returnMsg += decodedString;
JSONObject myJson = new JSONObject(returnMsg);
String str = myJson.optString("data");
str = str.substring(1, str.length() - 1);
str = str.replace(",", "");
str = str.replace(":", "");
prsdData = str.split("[\"]");
}
in.close();
connection.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
return prsdData;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment