|
76 | 76 | import java.util.Map;
|
77 | 77 |
|
78 | 78 | import io.reactivex.Observable;
|
| 79 | +import io.reactivex.android.schedulers.AndroidSchedulers; |
79 | 80 | import io.reactivex.functions.Consumer;
|
80 | 81 | import io.reactivex.functions.Function;
|
81 | 82 | import io.reactivex.schedulers.Schedulers;
|
| 83 | +import io.reactivex.observers.DisposableSingleObserver; |
| 84 | +import io.reactivex.disposables.Disposable; |
82 | 85 | import okhttp3.ResponseBody;
|
83 | 86 | import retrofit2.Call;
|
84 | 87 | import retrofit2.Callback;
|
85 | 88 | import retrofit2.Response;
|
| 89 | +import retrofit2.HttpException; |
86 | 90 |
|
87 | 91 | public class ApiManager {
|
88 | 92 |
|
@@ -4711,4 +4715,92 @@ public void run() {
|
4711 | 4715 | public void cancelRequestStatusPollingTask() {
|
4712 | 4716 | handler.removeCallbacks(stopRequestStatusPollingTask);
|
4713 | 4717 | }
|
| 4718 | + |
| 4719 | + public void sendCommandResponse(JsonObject requestBody, ApiResponseListener listener) { |
| 4720 | + Log.d(TAG, "Send command response"); |
| 4721 | + |
| 4722 | + apiInterface.sendCommandResponse(getBaseUrl() + AppConstants.URL_USER_NODES_CMD, |
| 4723 | + accessToken, requestBody).enqueue(new Callback<ResponseBody>() { |
| 4724 | + |
| 4725 | + @Override |
| 4726 | + public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) { |
| 4727 | + try { |
| 4728 | + if (response.isSuccessful()) { |
| 4729 | + String jsonResponse = response.body().string(); |
| 4730 | + Log.d(TAG, "Response : " + jsonResponse); |
| 4731 | + JSONObject jsonObject = new JSONObject(jsonResponse); |
| 4732 | + String requestId = jsonObject.optString(AppConstants.KEY_REQUEST_ID); |
| 4733 | + Bundle data = new Bundle(); |
| 4734 | + data.putString(AppConstants.KEY_REQUEST_ID, requestId); |
| 4735 | + listener.onSuccess(data); |
| 4736 | + } else { |
| 4737 | + String jsonErrResponse = response.errorBody().string(); |
| 4738 | + processError(jsonErrResponse, listener, "Failed to send command"); |
| 4739 | + } |
| 4740 | + } catch (Exception e) { |
| 4741 | + e.printStackTrace(); |
| 4742 | + listener.onResponseFailure(e); |
| 4743 | + } |
| 4744 | + } |
| 4745 | + |
| 4746 | + @Override |
| 4747 | + public void onFailure(Call<ResponseBody> call, Throwable t) { |
| 4748 | + t.printStackTrace(); |
| 4749 | + listener.onNetworkFailure(new Exception(t)); |
| 4750 | + } |
| 4751 | + }); |
| 4752 | + } |
| 4753 | + |
| 4754 | + public void getCommandResponseStatus(String requestId, ApiResponseListener listener) { |
| 4755 | + Log.d(TAG, "Get command response status"); |
| 4756 | + |
| 4757 | + apiInterface.getCommandResponseStatus( |
| 4758 | + getBaseUrl() + AppConstants.URL_USER_NODES_CMD, accessToken, |
| 4759 | + requestId).enqueue(new Callback<ResponseBody>() { |
| 4760 | + @Override |
| 4761 | + public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) { |
| 4762 | + try { |
| 4763 | + if (response.isSuccessful()) { |
| 4764 | + String jsonResponse = response.body().string(); |
| 4765 | + Log.d(TAG, "Response : " + jsonResponse); |
| 4766 | + JSONObject jsonObject = new JSONObject(jsonResponse); |
| 4767 | + JSONArray requests = jsonObject.getJSONArray("requests"); |
| 4768 | + |
| 4769 | + if (requests.length() > 0) { |
| 4770 | + JSONObject request = requests.getJSONObject(0); |
| 4771 | + String status = request.optString(AppConstants.KEY_STATUS); |
| 4772 | + Bundle data = new Bundle(); |
| 4773 | + data.putString(AppConstants.KEY_STATUS, status); |
| 4774 | + |
| 4775 | + // Enable button if status is not "requested" or "in_progress" |
| 4776 | + boolean enableButton = !status.equals("requested") && !status.equals("in_progress"); |
| 4777 | + data.putBoolean("enable_button", enableButton); |
| 4778 | + |
| 4779 | + if (request.has(AppConstants.KEY_RESPONSE_DATA)) { |
| 4780 | + data.putString(AppConstants.KEY_RESPONSE_DATA, request.getJSONObject(AppConstants.KEY_RESPONSE_DATA).toString()); |
| 4781 | + } |
| 4782 | + if (request.has(AppConstants.KEY_STATUS_DESCRIPTION)) { |
| 4783 | + data.putString(AppConstants.KEY_STATUS_DESCRIPTION, request.getString(AppConstants.KEY_STATUS_DESCRIPTION)); |
| 4784 | + } |
| 4785 | + listener.onSuccess(data); |
| 4786 | + } else { |
| 4787 | + listener.onResponseFailure(new Exception("No request found")); |
| 4788 | + } |
| 4789 | + } else { |
| 4790 | + String jsonErrResponse = response.errorBody().string(); |
| 4791 | + processError(jsonErrResponse, listener, "Failed to get command status"); |
| 4792 | + } |
| 4793 | + } catch (Exception e) { |
| 4794 | + e.printStackTrace(); |
| 4795 | + listener.onResponseFailure(e); |
| 4796 | + } |
| 4797 | + } |
| 4798 | + |
| 4799 | + @Override |
| 4800 | + public void onFailure(Call<ResponseBody> call, Throwable t) { |
| 4801 | + t.printStackTrace(); |
| 4802 | + listener.onNetworkFailure(new Exception(t)); |
| 4803 | + } |
| 4804 | + }); |
| 4805 | + } |
4714 | 4806 | }
|
0 commit comments