1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-07-22 14:52:25 +02:00

Bangle.js - id in http request/response (#2683)

added an optional id to identify the request.
if a request with id occurs, a response with the same id is returned.

Co-authored-by: Rarder44 <lpozzi44@gmail.com>
Reviewed-on: https://codeberg.org/Freeyourgadget/Gadgetbridge/pulls/2683
Co-authored-by: rarder44 <rarder44@noreply.codeberg.org>
Co-committed-by: rarder44 <rarder44@noreply.codeberg.org>
This commit is contained in:
rarder44 2022-05-30 13:19:19 +02:00 committed by Andreas Shimokawa
parent a5a653dc34
commit 236d9d9e2f

View File

@ -276,10 +276,17 @@ public class BangleJSDeviceSupport extends AbstractBTLEDeviceSupport {
} }
/// Write JSON object of the form {t:taskName, err:message} /// Write JSON object of the form {t:taskName, err:message}
private void uartTxJSONError(String taskName, String message) { private void uartTxJSONError(String taskName, String message) {
uartTxJSONError(taskName,message,null);
}
private void uartTxJSONError(String taskName, String message,String id) {
JSONObject o = new JSONObject(); JSONObject o = new JSONObject();
try { try {
o.put("t", taskName); o.put("t", taskName);
if( id!=null)
o.put("id", id);
o.put("err", message); o.put("err", message);
} catch (JSONException e) { } catch (JSONException e) {
GB.toast(getContext(), "uartTxJSONError: " + e.getLocalizedMessage(), Toast.LENGTH_LONG, GB.ERROR); GB.toast(getContext(), "uartTxJSONError: " + e.getLocalizedMessage(), Toast.LENGTH_LONG, GB.ERROR);
@ -287,6 +294,8 @@ public class BangleJSDeviceSupport extends AbstractBTLEDeviceSupport {
uartTxJSON(taskName, o); uartTxJSON(taskName, o);
} }
private void handleUartRxLine(String line) { private void handleUartRxLine(String line) {
LOG.info("UART RX LINE: " + line); LOG.info("UART RX LINE: " + line);
@ -412,9 +421,18 @@ public class BangleJSDeviceSupport extends AbstractBTLEDeviceSupport {
} break; } break;
case "http": { case "http": {
Prefs devicePrefs = new Prefs(GBApplication.getDeviceSpecificSharedPrefs(gbDevice.getAddress())); Prefs devicePrefs = new Prefs(GBApplication.getDeviceSpecificSharedPrefs(gbDevice.getAddress()));
String _id=null;
try {
_id = json.getString("id");
} catch (JSONException e) {
}
final String id = _id;
if (BuildConfig.INTERNET_ACCESS && devicePrefs.getBoolean(PREF_DEVICE_INTERNET_ACCESS, false)) { if (BuildConfig.INTERNET_ACCESS && devicePrefs.getBoolean(PREF_DEVICE_INTERNET_ACCESS, false)) {
RequestQueue queue = Volley.newRequestQueue(getContext()); RequestQueue queue = Volley.newRequestQueue(getContext());
String url = json.getString("url"); String url = json.getString("url");
String _xmlPath = ""; String _xmlPath = "";
try { try {
_xmlPath = json.getString("xpath"); _xmlPath = json.getString("xpath");
@ -433,12 +451,14 @@ public class BangleJSDeviceSupport extends AbstractBTLEDeviceSupport {
XPath xPath = XPathFactory.newInstance().newXPath(); XPath xPath = XPathFactory.newInstance().newXPath();
response = xPath.evaluate(xmlPath, inputXML); response = xPath.evaluate(xmlPath, inputXML);
} catch (Exception error) { } catch (Exception error) {
uartTxJSONError("http", error.toString()); uartTxJSONError("http", error.toString(),id);
return; return;
} }
} }
try { try {
o.put("t", "http"); o.put("t", "http");
if( id!=null)
o.put("id", id);
o.put("resp", response); o.put("resp", response);
} catch (JSONException e) { } catch (JSONException e) {
GB.toast(getContext(), "HTTP: " + e.getLocalizedMessage(), Toast.LENGTH_LONG, GB.ERROR); GB.toast(getContext(), "HTTP: " + e.getLocalizedMessage(), Toast.LENGTH_LONG, GB.ERROR);
@ -449,15 +469,15 @@ public class BangleJSDeviceSupport extends AbstractBTLEDeviceSupport {
@Override @Override
public void onErrorResponse(VolleyError error) { public void onErrorResponse(VolleyError error) {
JSONObject o = new JSONObject(); JSONObject o = new JSONObject();
uartTxJSONError("http", error.toString()); uartTxJSONError("http", error.toString(),id);
} }
}); });
queue.add(stringRequest); queue.add(stringRequest);
} else { } else {
if (BuildConfig.INTERNET_ACCESS) if (BuildConfig.INTERNET_ACCESS)
uartTxJSONError("http", "Internet access not enabled, check Gadgetbridge Device Settings"); uartTxJSONError("http", "Internet access not enabled, check Gadgetbridge Device Settings",id);
else else
uartTxJSONError("http", "Internet access not enabled in this Gadgetbridge build"); uartTxJSONError("http", "Internet access not enabled in this Gadgetbridge build",id);
} }
} break; } break;
case "intent": { case "intent": {