forked from fossasia/pslab-android
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new architecture for wifi added (fossasia#1927)
- Loading branch information
Showing
6 changed files
with
226 additions
and
13 deletions.
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
app/src/main/java/io/pslab/communication/HttpAsyncTask.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<byte[], Void, Void> { | ||
|
||
private HttpHandler mHttpHandler; | ||
private HttpCallback<JSONObject> mHttpCallback; | ||
|
||
public HttpAsyncTask(String baseIP, HttpCallback<JSONObject> 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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package io.pslab.interfaces; | ||
|
||
public interface HttpCallback<T> { | ||
void success(T t1); | ||
void error(Exception e); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters