// Path: app/src/main/java/org/emoncms/myapps/HTTPClient.java
// public class HTTPClient
// {
//     private static HTTPClient mInstance;
//     private RequestQueue mRequestQueue;
//     private static Context mCtx;
// 
//     private HTTPClient(Context context) {
//         mCtx = context;
//         mRequestQueue = getRequestQueue();
//     }
// 
//     public static synchronized HTTPClient getInstance(Context context) {
//         if (mInstance == null) {
//             mInstance = new HTTPClient(context);
//         }
//         return mInstance;
//     }
// 
//     public RequestQueue getRequestQueue() {
//         if (mRequestQueue == null) {
//             // getApplicationContext() is key, it keeps you from leaking the
//             // Activity or BroadcastReceiver if someone passes one in.
//             mRequestQueue = Volley.newRequestQueue(mCtx.getApplicationContext());
//             mRequestQueue.getCache().clear();
//         }
//         return mRequestQueue;
//     }
// 
//     public void cancellAll(String TAG) {
//         if (mRequestQueue != null) {
//             mRequestQueue.cancelAll(TAG);
//         }
//     }
// 
//     public <T> void addToRequestQueue(Request<T> req) {
//         req.setShouldCache(false);
//         req.setRetryPolicy(new DefaultRetryPolicy(2500, 0, 1f));
//         getRequestQueue().add(req);
//     }
// }


import android.content.Context;
import android.os.Handler;
import android.support.design.widget.Snackbar;
import android.util.Log;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import org.emoncms.myapps.HTTPClient;
import org.emoncms.myapps.R;
import org.json.JSONArray;
import org.json.JSONException;
import java.util.Calendar;
                        row = response.getJSONArray(i);
                        long time = Long.parseLong(row.getString(0));

                        if (lastEntry == 0) {
                            powerChart.addData(row.getString(0), row.getDouble(1));
                        } else if (time >= (lastEntry + (interval * 1000))) {
                            powerChart.removeFirstPoint();
                            powerChart.addData(row.getString(0), row.getDouble(1));
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

                powerChart.refreshChart();
                myElectricDataManager.clearMessage();
                myElectricDataManager.loadPowerNow(10000);
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                myElectricDataManager.showMessage(R.string.connection_error);
                myElectricDataManager.loadPowerHistory(5000);
            }
        });

        jsArrayRequest.setTag(myElectricDataManager.getPageTag());

        if (endTime > lastEntry + (interval * 1000)) {
// Path: app/src/main/java/org/emoncms/myapps/HTTPClient.java
// public class HTTPClient
// {
//     private static HTTPClient mInstance;
//     private RequestQueue mRequestQueue;
//     private static Context mCtx;
// 
//     private HTTPClient(Context context) {
//         mCtx = context;
//         mRequestQueue = getRequestQueue();
//     }
// 
//     public static synchronized HTTPClient getInstance(Context context) {
//         if (mInstance == null) {
//             mInstance = new HTTPClient(context);
//         }
//         return mInstance;
//     }
// 
//     public RequestQueue getRequestQueue() {
//         if (mRequestQueue == null) {
//             // getApplicationContext() is key, it keeps you from leaking the
//             // Activity or BroadcastReceiver if someone passes one in.
//             mRequestQueue = Volley.newRequestQueue(mCtx.getApplicationContext());
//             mRequestQueue.getCache().clear();
//         }
//         return mRequestQueue;
//     }
// 
//     public void cancellAll(String TAG) {
//         if (mRequestQueue != null) {
//             mRequestQueue.cancelAll(TAG);
//         }
//     }
// 
//     public <T> void addToRequestQueue(Request<T> req) {
//         req.setShouldCache(false);
//         req.setRetryPolicy(new DefaultRetryPolicy(2500, 0, 1f));
//         getRequestQueue().add(req);
//     }
// }


// Path: app/src/main/java/org/emoncms/myapps/chart/PowerChartDataLoader.java
import android.content.Context;
import android.os.Handler;
import android.support.design.widget.Snackbar;
import android.util.Log;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import org.emoncms.myapps.HTTPClient;
import org.emoncms.myapps.R;
import org.json.JSONArray;
import org.json.JSONException;
import java.util.Calendar;

                        row = response.getJSONArray(i);
                        long time = Long.parseLong(row.getString(0));

                        if (lastEntry == 0) {
                            powerChart.addData(row.getString(0), row.getDouble(1));
                        } else if (time >= (lastEntry + (interval * 1000))) {
                            powerChart.removeFirstPoint();
                            powerChart.addData(row.getString(0), row.getDouble(1));
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

                powerChart.refreshChart();
                myElectricDataManager.clearMessage();
                myElectricDataManager.loadPowerNow(10000);
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                myElectricDataManager.showMessage(R.string.connection_error);
                myElectricDataManager.loadPowerHistory(5000);
            }
        });

        jsArrayRequest.setTag(myElectricDataManager.getPageTag());

        if (endTime > lastEntry + (interval * 1000)) {
            HTTPClient.getInstance(context).addToRequestQueue(jsArrayRequest);