Skip to content

Commit 492df13

Browse files
neel1998CloudyPadmal
authored andcommitted
new architecture for wifi added (#1927)
1 parent ba0373b commit 492df13

File tree

6 files changed

+226
-13
lines changed

6 files changed

+226
-13
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package io.pslab.communication;
2+
3+
import android.os.AsyncTask;
4+
5+
import org.json.JSONException;
6+
import org.json.JSONObject;
7+
8+
import java.io.IOException;
9+
10+
import io.pslab.interfaces.HttpCallback;
11+
12+
public class HttpAsyncTask extends AsyncTask<byte[], Void, Void> {
13+
14+
private HttpHandler mHttpHandler;
15+
private HttpCallback<JSONObject> mHttpCallback;
16+
17+
public HttpAsyncTask(String baseIP, HttpCallback<JSONObject> httpCallback) {
18+
mHttpHandler = new HttpHandler(baseIP);
19+
mHttpCallback = httpCallback;
20+
}
21+
22+
@Override
23+
protected Void doInBackground(byte[]... data) {
24+
int res = 0;
25+
try {
26+
if (data.length != 0) {
27+
res = mHttpHandler.write(data[0]);
28+
29+
} else {
30+
res = mHttpHandler.read();
31+
}
32+
} catch (IOException | JSONException e) {
33+
mHttpCallback.error(e);
34+
e.printStackTrace();
35+
}
36+
if (res == 1) {
37+
mHttpCallback.success(mHttpHandler.getReceivedData());
38+
} else {
39+
mHttpCallback.error(new Exception());
40+
}
41+
return null;
42+
}
43+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package io.pslab.communication;
2+
3+
import android.util.Log;
4+
5+
import org.json.JSONArray;
6+
import org.json.JSONException;
7+
import org.json.JSONObject;
8+
9+
import java.io.IOException;
10+
import java.net.URL;
11+
12+
import okhttp3.MediaType;
13+
import okhttp3.OkHttpClient;
14+
import okhttp3.Request;
15+
import okhttp3.RequestBody;
16+
import okhttp3.Response;
17+
18+
public class HttpHandler {
19+
20+
private final String TAG = this.getClass().getSimpleName();
21+
private String baseIP;
22+
private String sendDataEndPoint = "send";
23+
private String getDataEndPoint = "get";
24+
private String dataKeyString = "data";
25+
private OkHttpClient client;
26+
private JSONObject receivedData;
27+
28+
public HttpHandler(String baseIP) {
29+
this.baseIP = baseIP;
30+
this.client = new OkHttpClient();
31+
}
32+
33+
/**
34+
* Method to send data to ESP
35+
*
36+
* @param data data to be sent in byte array
37+
* @return 1 if response code is "200" 0 otherwise;
38+
*/
39+
public int write(byte[] data) throws IOException, JSONException {
40+
int result = 1;
41+
URL baseURL = new URL("http://" + baseIP + "/" + sendDataEndPoint);
42+
int written = 0;
43+
JSONArray responseArray = new JSONArray();
44+
while (written < data.length) {
45+
JSONObject jsonObject = new JSONObject();
46+
jsonObject.put(dataKeyString, data[written]);
47+
RequestBody body = RequestBody.create(jsonObject.toString(), MediaType.get("application/json; charset=utf-8"));
48+
Request request = new Request.Builder()
49+
.url(baseURL)
50+
.post(body)
51+
.build();
52+
Response response = client.newCall(request).execute();
53+
responseArray.put(new JSONObject(response.body().string()));
54+
if (response.code() != 200) {
55+
Log.e(TAG, "Error writing byte:" + written);
56+
return 0;
57+
}
58+
written++;
59+
}
60+
receivedData = new JSONObject(responseArray.toString());
61+
return result;
62+
}
63+
64+
/**
65+
* Method to get data from ESP
66+
* @return 1 if data was received 0 otherwise
67+
*/
68+
public int read() throws IOException, JSONException {
69+
int result = 1;
70+
URL baseURL = new URL("http://" + baseIP + "/" + getDataEndPoint);
71+
Request request = new Request.Builder()
72+
.url(baseURL)
73+
.build();
74+
Response response = client.newCall(request).execute();
75+
if (response.code() != 200) {
76+
Log.e(TAG, "Error reading data");
77+
return 0;
78+
} else {
79+
receivedData = new JSONObject(response.body().string());
80+
}
81+
return result;
82+
}
83+
84+
public JSONObject getReceivedData() {
85+
return receivedData;
86+
}
87+
}

app/src/main/java/io/pslab/communication/PacketHandler.java

Lines changed: 67 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,18 @@
22

33
import android.util.Log;
44

5+
import org.json.JSONException;
6+
import org.json.JSONObject;
7+
58
import java.io.IOException;
69
import java.nio.ByteBuffer;
710
import java.nio.ByteOrder;
811
import java.nio.charset.Charset;
912
import java.util.Arrays;
1013

14+
import io.pslab.interfaces.HttpCallback;
15+
import io.pslab.others.ScienceLabCommon;
16+
1117
/**
1218
* Created by viveksb007 on 28/3/17.
1319
*/
@@ -24,18 +30,19 @@ public class PacketHandler {
2430
private CommandsProto mCommandsProto;
2531
private int timeout = 500, VERSION_STRING_LENGTH = 15;
2632
ByteBuffer burstBuffer = ByteBuffer.allocate(2000);
33+
private HttpAsyncTask httpAsyncTask;
2734

2835
public PacketHandler(int timeout, CommunicationHandler communicationHandler) {
2936
this.loadBurst = false;
3037
this.connected = false;
3138
this.timeout = timeout;
3239
this.mCommandsProto = new CommandsProto();
3340
this.mCommunicationHandler = communicationHandler;
34-
connected = mCommunicationHandler.isConnected();
41+
connected = (mCommunicationHandler.isConnected() || ScienceLabCommon.isWifiConnected());
3542
}
3643

3744
public boolean isConnected() {
38-
connected = mCommunicationHandler.isConnected();
45+
connected = (mCommunicationHandler.isConnected() || ScienceLabCommon.isWifiConnected());
3946
return connected;
4047
}
4148

@@ -44,7 +51,7 @@ public String getVersion() {
4451
sendByte(mCommandsProto.COMMON);
4552
sendByte(mCommandsProto.GET_VERSION);
4653
// Read "<PSLAB Version String>\n"
47-
mCommunicationHandler.read(buffer, VERSION_STRING_LENGTH + 1, timeout);
54+
commonRead(VERSION_STRING_LENGTH + 1);
4855
version = new String(Arrays.copyOfRange(buffer, 0, VERSION_STRING_LENGTH), Charset.forName("UTF-8"));
4956
} catch (IOException e) {
5057
Log.e("Error in Communication", e.toString());
@@ -58,7 +65,7 @@ public void sendByte(int val) throws IOException {
5865
}
5966
if (!loadBurst) {
6067
try {
61-
mCommunicationHandler.write(new byte[]{(byte) (val & 0xff)}, timeout);
68+
commonWrite(new byte[]{(byte) (val & 0xff)});
6269
} catch (IOException | NullPointerException e) {
6370
Log.e("Error in sending byte", e.toString());
6471
e.printStackTrace();
@@ -74,7 +81,7 @@ public void sendInt(int val) throws IOException {
7481
}
7582
if (!loadBurst) {
7683
try {
77-
mCommunicationHandler.write(new byte[]{(byte) (val & 0xff), (byte) ((val >> 8) & 0xff)}, timeout);
84+
commonWrite(new byte[]{(byte) (val & 0xff), (byte) ((val >> 8) & 0xff)});
7885
} catch (IOException e) {
7986
Log.e("Error in sending int", e.toString());
8087
e.printStackTrace();
@@ -97,7 +104,7 @@ public int getAcknowledgement() {
97104
return 1;
98105
} else {
99106
try {
100-
mCommunicationHandler.read(buffer, 1, timeout);
107+
commonRead(1);
101108
return buffer[0];
102109
} catch (IOException | NullPointerException e) {
103110
e.printStackTrace();
@@ -108,7 +115,7 @@ public int getAcknowledgement() {
108115

109116
public byte getByte() {
110117
try {
111-
int numByteRead = mCommunicationHandler.read(buffer, 1, timeout);
118+
int numByteRead = commonRead(1);
112119
if (numByteRead == 1) {
113120
return buffer[0];
114121
} else {
@@ -123,7 +130,7 @@ public byte getByte() {
123130
int getVoltageSummation() {
124131
try {
125132
// Note : bytesToBeRead has to be +1 than the requirement
126-
int numByteRead = mCommunicationHandler.read(buffer, 3, timeout);
133+
int numByteRead = commonRead(3);
127134
if (numByteRead == 3) {
128135
return (buffer[0] & 0xff) | ((buffer[1] << 8) & 0xff00);
129136
} else {
@@ -137,7 +144,7 @@ int getVoltageSummation() {
137144

138145
public int getInt() {
139146
try {
140-
int numByteRead = mCommunicationHandler.read(buffer, 2, timeout);
147+
int numByteRead = commonRead(2);
141148
if (numByteRead == 2) {
142149
// LSB is read first
143150
return (buffer[0] & 0xff) | ((buffer[1] << 8) & 0xff00);
@@ -152,7 +159,7 @@ public int getInt() {
152159

153160
public long getLong() {
154161
try {
155-
int numByteRead = mCommunicationHandler.read(buffer, 4, timeout);
162+
int numByteRead = commonRead(4);
156163
if (numByteRead == 4) {
157164
// C++ has long of 4-bytes but in Java int has 4-bytes
158165
// 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() {
171178
}
172179

173180
public int read(byte[] dest, int bytesToRead) throws IOException {
174-
int numBytesRead = mCommunicationHandler.read(buffer, bytesToRead, timeout);
181+
int numBytesRead = commonRead(bytesToRead);
175182
for (int i = 0; i < bytesToRead; i++) {
176183
dest[i] = buffer[i];
177184
}
@@ -185,10 +192,10 @@ public int read(byte[] dest, int bytesToRead) throws IOException {
185192

186193
public byte[] sendBurst() {
187194
try {
188-
mCommunicationHandler.write(burstBuffer.array(), timeout);
195+
commonWrite(burstBuffer.array());
189196
burstBuffer.clear();
190197
loadBurst = false;
191-
int bytesRead = mCommunicationHandler.read(buffer, inputQueueSize, timeout);
198+
int bytesRead = commonRead(inputQueueSize);
192199
inputQueueSize = 0;
193200
return Arrays.copyOfRange(buffer, 0, bytesRead);
194201
} catch (IOException e) {
@@ -197,4 +204,51 @@ public byte[] sendBurst() {
197204
return new byte[]{-1};
198205
}
199206

207+
private int commonRead(int bytesToRead) throws IOException {
208+
final int[] bytesRead = {0};
209+
if (mCommunicationHandler.isConnected()) {
210+
bytesRead[0] = mCommunicationHandler.read(buffer, bytesToRead, timeout);
211+
} else if (ScienceLabCommon.isWifiConnected()) {
212+
httpAsyncTask = new HttpAsyncTask(ScienceLabCommon.getEspIP(), new HttpCallback<JSONObject>() {
213+
@Override
214+
public void success(JSONObject jsonObject) {
215+
try {
216+
//Server will send byte array
217+
buffer = (byte[])jsonObject.get("data");
218+
bytesRead[0] = buffer.length;
219+
} catch (JSONException e) {
220+
e.printStackTrace();
221+
}
222+
}
223+
224+
@Override
225+
public void error(Exception e) {
226+
Log.e(TAG, "Error reading data over ESP");
227+
}
228+
});
229+
httpAsyncTask.execute(new byte[]{});
230+
}
231+
return bytesRead[0];
232+
}
233+
234+
private void commonWrite(byte[] data) throws IOException {
235+
if (mCommunicationHandler.isConnected()) {
236+
mCommunicationHandler.write(data, timeout);
237+
} else if (ScienceLabCommon.isWifiConnected()) {
238+
httpAsyncTask = new HttpAsyncTask(ScienceLabCommon.getEspIP(), new HttpCallback<JSONObject>() {
239+
@Override
240+
public void success(JSONObject jsonObject) {
241+
Log.v(TAG, "write response:" + jsonObject.toString());
242+
}
243+
244+
@Override
245+
public void error(Exception e) {
246+
Log.e(TAG, "Error writing data over ESP");
247+
}
248+
});
249+
250+
httpAsyncTask.execute(data);
251+
}
252+
253+
}
200254
}

app/src/main/java/io/pslab/fragment/ESPFragment.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import java.io.IOException;
1818

1919
import io.pslab.R;
20+
import io.pslab.others.ScienceLabCommon;
2021
import okhttp3.OkHttpClient;
2122
import okhttp3.Request;
2223
import okhttp3.Response;
@@ -78,6 +79,10 @@ protected String doInBackground(Void... voids) {
7879
.url("http://" + espIPAddress)
7980
.build();
8081
Response response = client.newCall(request).execute();
82+
if (response.code() == 200) {
83+
ScienceLabCommon.setIsWifiConnected(true);
84+
ScienceLabCommon.setEspBaseIP(espIPAddress);
85+
}
8186
result = response.body().string();
8287
} catch (IOException e) {
8388
e.printStackTrace();
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package io.pslab.interfaces;
2+
3+
public interface HttpCallback<T> {
4+
void success(T t1);
5+
void error(Exception e);
6+
}

app/src/main/java/io/pslab/others/ScienceLabCommon.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ public class ScienceLabCommon {
1515
private static ScienceLabCommon scienceLabCommon = null;
1616
public static ScienceLab scienceLab;
1717
public boolean connected = false;
18+
public static boolean isWifiConnected = false;
19+
private static String espBaseIP = "";
1820

1921
private ScienceLabCommon() {
2022
}
@@ -35,4 +37,20 @@ public static ScienceLabCommon getInstance() {
3537
}
3638
return scienceLabCommon;
3739
}
40+
41+
public static String getEspIP() {
42+
return espBaseIP;
43+
}
44+
45+
public static void setEspBaseIP(String espBaseIP) {
46+
ScienceLabCommon.espBaseIP = espBaseIP;
47+
}
48+
49+
public static boolean isWifiConnected() {
50+
return isWifiConnected;
51+
}
52+
53+
public static void setIsWifiConnected(boolean wifiConnected) {
54+
isWifiConnected = wifiConnected;
55+
}
3856
}

0 commit comments

Comments
 (0)