mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge
synced 2024-11-03 17:02:13 +01: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:
parent
a5a653dc34
commit
236d9d9e2f
@ -276,10 +276,17 @@ public class BangleJSDeviceSupport extends AbstractBTLEDeviceSupport {
|
||||
}
|
||||
|
||||
/// Write JSON object of the form {t:taskName, err: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();
|
||||
try {
|
||||
o.put("t", taskName);
|
||||
if( id!=null)
|
||||
o.put("id", id);
|
||||
o.put("err", message);
|
||||
} catch (JSONException e) {
|
||||
GB.toast(getContext(), "uartTxJSONError: " + e.getLocalizedMessage(), Toast.LENGTH_LONG, GB.ERROR);
|
||||
@ -287,6 +294,8 @@ public class BangleJSDeviceSupport extends AbstractBTLEDeviceSupport {
|
||||
uartTxJSON(taskName, o);
|
||||
}
|
||||
|
||||
|
||||
|
||||
private void handleUartRxLine(String line) {
|
||||
LOG.info("UART RX LINE: " + line);
|
||||
|
||||
@ -412,9 +421,18 @@ public class BangleJSDeviceSupport extends AbstractBTLEDeviceSupport {
|
||||
} break;
|
||||
case "http": {
|
||||
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)) {
|
||||
RequestQueue queue = Volley.newRequestQueue(getContext());
|
||||
String url = json.getString("url");
|
||||
|
||||
String _xmlPath = "";
|
||||
try {
|
||||
_xmlPath = json.getString("xpath");
|
||||
@ -433,12 +451,14 @@ public class BangleJSDeviceSupport extends AbstractBTLEDeviceSupport {
|
||||
XPath xPath = XPathFactory.newInstance().newXPath();
|
||||
response = xPath.evaluate(xmlPath, inputXML);
|
||||
} catch (Exception error) {
|
||||
uartTxJSONError("http", error.toString());
|
||||
uartTxJSONError("http", error.toString(),id);
|
||||
return;
|
||||
}
|
||||
}
|
||||
try {
|
||||
o.put("t", "http");
|
||||
if( id!=null)
|
||||
o.put("id", id);
|
||||
o.put("resp", response);
|
||||
} catch (JSONException e) {
|
||||
GB.toast(getContext(), "HTTP: " + e.getLocalizedMessage(), Toast.LENGTH_LONG, GB.ERROR);
|
||||
@ -449,15 +469,15 @@ public class BangleJSDeviceSupport extends AbstractBTLEDeviceSupport {
|
||||
@Override
|
||||
public void onErrorResponse(VolleyError error) {
|
||||
JSONObject o = new JSONObject();
|
||||
uartTxJSONError("http", error.toString());
|
||||
uartTxJSONError("http", error.toString(),id);
|
||||
}
|
||||
});
|
||||
queue.add(stringRequest);
|
||||
} else {
|
||||
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
|
||||
uartTxJSONError("http", "Internet access not enabled in this Gadgetbridge build");
|
||||
uartTxJSONError("http", "Internet access not enabled in this Gadgetbridge build",id);
|
||||
}
|
||||
} break;
|
||||
case "intent": {
|
||||
|
Loading…
Reference in New Issue
Block a user