From 8350150817402a91c79ee0f652984dfdc97e0ada Mon Sep 17 00:00:00 2001 From: Neel Trivedi Date: Mon, 19 Aug 2019 13:33:30 +0530 Subject: [PATCH] new architecture for wifi added (#1927) --- .../io/pslab/communication/HttpAsyncTask.java | 43 +++++++++ .../io/pslab/communication/HttpHandler.java | 87 +++++++++++++++++++ .../io/pslab/communication/PacketHandler.java | 80 ++++++++++++++--- .../java/io/pslab/fragment/ESPFragment.java | 5 ++ .../io/pslab/interfaces/HttpCallback.java | 6 ++ .../io/pslab/others/ScienceLabCommon.java | 18 ++++ 6 files changed, 226 insertions(+), 13 deletions(-) create mode 100644 app/src/main/java/io/pslab/communication/HttpAsyncTask.java create mode 100644 app/src/main/java/io/pslab/communication/HttpHandler.java create mode 100644 app/src/main/java/io/pslab/interfaces/HttpCallback.java diff --git a/app/src/main/java/io/pslab/communication/HttpAsyncTask.java b/app/src/main/java/io/pslab/communication/HttpAsyncTask.java new file mode 100644 index 000000000..4aae41545 --- /dev/null +++ b/app/src/main/java/io/pslab/communication/HttpAsyncTask.java @@ -0,0 +1,43 @@ +package io.pslab.communication; + +import android.os.AsyncTask; + +import org.json.JSONException; +import org.json.JSONObject; + +import java.io.IOException; + +import io.pslab.interfaces.HttpCallback; + +public class HttpAsyncTask extends AsyncTask { + + private HttpHandler mHttpHandler; + private HttpCallback mHttpCallback; + + public HttpAsyncTask(String baseIP, HttpCallback httpCallback) { + mHttpHandler = new HttpHandler(baseIP); + mHttpCallback = httpCallback; + } + + @Override + protected Void doInBackground(byte[]... data) { + int res = 0; + try { + if (data.length != 0) { + res = mHttpHandler.write(data[0]); + + } else { + res = mHttpHandler.read(); + } + } catch (IOException | JSONException e) { + mHttpCallback.error(e); + e.printStackTrace(); + } + if (res == 1) { + mHttpCallback.success(mHttpHandler.getReceivedData()); + } else { + mHttpCallback.error(new Exception()); + } + return null; + } +} diff --git a/app/src/main/java/io/pslab/communication/HttpHandler.java b/app/src/main/java/io/pslab/communication/HttpHandler.java new file mode 100644 index 000000000..a2f7bed38 --- /dev/null +++ b/app/src/main/java/io/pslab/communication/HttpHandler.java @@ -0,0 +1,87 @@ +package io.pslab.communication; + +import android.util.Log; + +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + +import java.io.IOException; +import java.net.URL; + +import okhttp3.MediaType; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.RequestBody; +import okhttp3.Response; + +public class HttpHandler { + + private final String TAG = this.getClass().getSimpleName(); + private String baseIP; + private String sendDataEndPoint = "send"; + private String getDataEndPoint = "get"; + private String dataKeyString = "data"; + private OkHttpClient client; + private JSONObject receivedData; + + public HttpHandler(String baseIP) { + this.baseIP = baseIP; + this.client = new OkHttpClient(); + } + + /** + * Method to send data to ESP + * + * @param data data to be sent in byte array + * @return 1 if response code is "200" 0 otherwise; + */ + public int write(byte[] data) throws IOException, JSONException { + int result = 1; + URL baseURL = new URL("http://" + baseIP + "/" + sendDataEndPoint); + int written = 0; + JSONArray responseArray = new JSONArray(); + while (written < data.length) { + JSONObject jsonObject = new JSONObject(); + jsonObject.put(dataKeyString, data[written]); + RequestBody body = RequestBody.create(jsonObject.toString(), MediaType.get("application/json; charset=utf-8")); + Request request = new Request.Builder() + .url(baseURL) + .post(body) + .build(); + Response response = client.newCall(request).execute(); + responseArray.put(new JSONObject(response.body().string())); + if (response.code() != 200) { + Log.e(TAG, "Error writing byte:" + written); + return 0; + } + written++; + } + receivedData = new JSONObject(responseArray.toString()); + return result; + } + + /** + * Method to get data from ESP + * @return 1 if data was received 0 otherwise + */ + public int read() throws IOException, JSONException { + int result = 1; + URL baseURL = new URL("http://" + baseIP + "/" + getDataEndPoint); + Request request = new Request.Builder() + .url(baseURL) + .build(); + Response response = client.newCall(request).execute(); + if (response.code() != 200) { + Log.e(TAG, "Error reading data"); + return 0; + } else { + receivedData = new JSONObject(response.body().string()); + } + return result; + } + + public JSONObject getReceivedData() { + return receivedData; + } +} diff --git a/app/src/main/java/io/pslab/communication/PacketHandler.java b/app/src/main/java/io/pslab/communication/PacketHandler.java index 2cfa87b1c..49522aed9 100644 --- a/app/src/main/java/io/pslab/communication/PacketHandler.java +++ b/app/src/main/java/io/pslab/communication/PacketHandler.java @@ -2,12 +2,18 @@ import android.util.Log; +import org.json.JSONException; +import org.json.JSONObject; + import java.io.IOException; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.charset.Charset; import java.util.Arrays; +import io.pslab.interfaces.HttpCallback; +import io.pslab.others.ScienceLabCommon; + /** * Created by viveksb007 on 28/3/17. */ @@ -24,6 +30,7 @@ public class PacketHandler { private CommandsProto mCommandsProto; private int timeout = 500, VERSION_STRING_LENGTH = 15; ByteBuffer burstBuffer = ByteBuffer.allocate(2000); + private HttpAsyncTask httpAsyncTask; public PacketHandler(int timeout, CommunicationHandler communicationHandler) { this.loadBurst = false; @@ -31,11 +38,11 @@ public PacketHandler(int timeout, CommunicationHandler communicationHandler) { this.timeout = timeout; this.mCommandsProto = new CommandsProto(); this.mCommunicationHandler = communicationHandler; - connected = mCommunicationHandler.isConnected(); + connected = (mCommunicationHandler.isConnected() || ScienceLabCommon.isWifiConnected()); } public boolean isConnected() { - connected = mCommunicationHandler.isConnected(); + connected = (mCommunicationHandler.isConnected() || ScienceLabCommon.isWifiConnected()); return connected; } @@ -44,7 +51,7 @@ public String getVersion() { sendByte(mCommandsProto.COMMON); sendByte(mCommandsProto.GET_VERSION); // Read "\n" - mCommunicationHandler.read(buffer, VERSION_STRING_LENGTH + 1, timeout); + commonRead(VERSION_STRING_LENGTH + 1); version = new String(Arrays.copyOfRange(buffer, 0, VERSION_STRING_LENGTH), Charset.forName("UTF-8")); } catch (IOException e) { Log.e("Error in Communication", e.toString()); @@ -58,7 +65,7 @@ public void sendByte(int val) throws IOException { } if (!loadBurst) { try { - mCommunicationHandler.write(new byte[]{(byte) (val & 0xff)}, timeout); + commonWrite(new byte[]{(byte) (val & 0xff)}); } catch (IOException | NullPointerException e) { Log.e("Error in sending byte", e.toString()); e.printStackTrace(); @@ -74,7 +81,7 @@ public void sendInt(int val) throws IOException { } if (!loadBurst) { try { - mCommunicationHandler.write(new byte[]{(byte) (val & 0xff), (byte) ((val >> 8) & 0xff)}, timeout); + commonWrite(new byte[]{(byte) (val & 0xff), (byte) ((val >> 8) & 0xff)}); } catch (IOException e) { Log.e("Error in sending int", e.toString()); e.printStackTrace(); @@ -97,7 +104,7 @@ public int getAcknowledgement() { return 1; } else { try { - mCommunicationHandler.read(buffer, 1, timeout); + commonRead(1); return buffer[0]; } catch (IOException | NullPointerException e) { e.printStackTrace(); @@ -108,7 +115,7 @@ public int getAcknowledgement() { public byte getByte() { try { - int numByteRead = mCommunicationHandler.read(buffer, 1, timeout); + int numByteRead = commonRead(1); if (numByteRead == 1) { return buffer[0]; } else { @@ -123,7 +130,7 @@ public byte getByte() { int getVoltageSummation() { try { // Note : bytesToBeRead has to be +1 than the requirement - int numByteRead = mCommunicationHandler.read(buffer, 3, timeout); + int numByteRead = commonRead(3); if (numByteRead == 3) { return (buffer[0] & 0xff) | ((buffer[1] << 8) & 0xff00); } else { @@ -137,7 +144,7 @@ int getVoltageSummation() { public int getInt() { try { - int numByteRead = mCommunicationHandler.read(buffer, 2, timeout); + int numByteRead = commonRead(2); if (numByteRead == 2) { // LSB is read first return (buffer[0] & 0xff) | ((buffer[1] << 8) & 0xff00); @@ -152,7 +159,7 @@ public int getInt() { public long getLong() { try { - int numByteRead = mCommunicationHandler.read(buffer, 4, timeout); + int numByteRead = commonRead(4); if (numByteRead == 4) { // C++ has long of 4-bytes but in Java int has 4-bytes // refer "https://stackoverflow.com/questions/7619058/convert-a-byte-array-to-integer-in-java-and-vice-versa" for Endian @@ -171,7 +178,7 @@ public boolean waitForData() { } public int read(byte[] dest, int bytesToRead) throws IOException { - int numBytesRead = mCommunicationHandler.read(buffer, bytesToRead, timeout); + int numBytesRead = commonRead(bytesToRead); for (int i = 0; i < bytesToRead; i++) { dest[i] = buffer[i]; } @@ -185,10 +192,10 @@ public int read(byte[] dest, int bytesToRead) throws IOException { public byte[] sendBurst() { try { - mCommunicationHandler.write(burstBuffer.array(), timeout); + commonWrite(burstBuffer.array()); burstBuffer.clear(); loadBurst = false; - int bytesRead = mCommunicationHandler.read(buffer, inputQueueSize, timeout); + int bytesRead = commonRead(inputQueueSize); inputQueueSize = 0; return Arrays.copyOfRange(buffer, 0, bytesRead); } catch (IOException e) { @@ -197,4 +204,51 @@ public byte[] sendBurst() { return new byte[]{-1}; } + private int commonRead(int bytesToRead) throws IOException { + final int[] bytesRead = {0}; + if (mCommunicationHandler.isConnected()) { + bytesRead[0] = mCommunicationHandler.read(buffer, bytesToRead, timeout); + } else if (ScienceLabCommon.isWifiConnected()) { + httpAsyncTask = new HttpAsyncTask(ScienceLabCommon.getEspIP(), new HttpCallback() { + @Override + public void success(JSONObject jsonObject) { + try { + //Server will send byte array + buffer = (byte[])jsonObject.get("data"); + bytesRead[0] = buffer.length; + } catch (JSONException e) { + e.printStackTrace(); + } + } + + @Override + public void error(Exception e) { + Log.e(TAG, "Error reading data over ESP"); + } + }); + httpAsyncTask.execute(new byte[]{}); + } + return bytesRead[0]; + } + + private void commonWrite(byte[] data) throws IOException { + if (mCommunicationHandler.isConnected()) { + mCommunicationHandler.write(data, timeout); + } else if (ScienceLabCommon.isWifiConnected()) { + httpAsyncTask = new HttpAsyncTask(ScienceLabCommon.getEspIP(), new HttpCallback() { + @Override + public void success(JSONObject jsonObject) { + Log.v(TAG, "write response:" + jsonObject.toString()); + } + + @Override + public void error(Exception e) { + Log.e(TAG, "Error writing data over ESP"); + } + }); + + httpAsyncTask.execute(data); + } + + } } diff --git a/app/src/main/java/io/pslab/fragment/ESPFragment.java b/app/src/main/java/io/pslab/fragment/ESPFragment.java index 00ac88e71..255a22b7a 100644 --- a/app/src/main/java/io/pslab/fragment/ESPFragment.java +++ b/app/src/main/java/io/pslab/fragment/ESPFragment.java @@ -17,6 +17,7 @@ import java.io.IOException; import io.pslab.R; +import io.pslab.others.ScienceLabCommon; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; @@ -78,6 +79,10 @@ protected String doInBackground(Void... voids) { .url("http://" + espIPAddress) .build(); Response response = client.newCall(request).execute(); + if (response.code() == 200) { + ScienceLabCommon.setIsWifiConnected(true); + ScienceLabCommon.setEspBaseIP(espIPAddress); + } result = response.body().string(); } catch (IOException e) { e.printStackTrace(); diff --git a/app/src/main/java/io/pslab/interfaces/HttpCallback.java b/app/src/main/java/io/pslab/interfaces/HttpCallback.java new file mode 100644 index 000000000..3bcdd62e8 --- /dev/null +++ b/app/src/main/java/io/pslab/interfaces/HttpCallback.java @@ -0,0 +1,6 @@ +package io.pslab.interfaces; + +public interface HttpCallback { + void success(T t1); + void error(Exception e); +} diff --git a/app/src/main/java/io/pslab/others/ScienceLabCommon.java b/app/src/main/java/io/pslab/others/ScienceLabCommon.java index bae5a80a3..8bd20b962 100644 --- a/app/src/main/java/io/pslab/others/ScienceLabCommon.java +++ b/app/src/main/java/io/pslab/others/ScienceLabCommon.java @@ -15,6 +15,8 @@ public class ScienceLabCommon { private static ScienceLabCommon scienceLabCommon = null; public static ScienceLab scienceLab; public boolean connected = false; + public static boolean isWifiConnected = false; + private static String espBaseIP = ""; private ScienceLabCommon() { } @@ -35,4 +37,20 @@ public static ScienceLabCommon getInstance() { } return scienceLabCommon; } + + public static String getEspIP() { + return espBaseIP; + } + + public static void setEspBaseIP(String espBaseIP) { + ScienceLabCommon.espBaseIP = espBaseIP; + } + + public static boolean isWifiConnected() { + return isWifiConnected; + } + + public static void setIsWifiConnected(boolean wifiConnected) { + isWifiConnected = wifiConnected; + } }