1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-10-11 15:48:31 +02:00

Bangle.js: Add ability to filter HTTP request by xpath, and extra logging if unknown JSON

This commit is contained in:
Gordon Williams 2022-03-23 15:35:09 +00:00
parent f26f3b0a15
commit 1b412af3c7

View File

@ -43,8 +43,10 @@ import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.xml.sax.InputSource;
import java.io.IOException; import java.io.IOException;
import java.io.StringReader;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Calendar; import java.util.Calendar;
@ -85,6 +87,9 @@ import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
import static nodomain.freeyourgadget.gadgetbridge.database.DBHelper.*; import static nodomain.freeyourgadget.gadgetbridge.database.DBHelper.*;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathFactory;
public class BangleJSDeviceSupport extends AbstractBTLEDeviceSupport { public class BangleJSDeviceSupport extends AbstractBTLEDeviceSupport {
private static final Logger LOG = LoggerFactory.getLogger(BangleJSDeviceSupport.class); private static final Logger LOG = LoggerFactory.getLogger(BangleJSDeviceSupport.class);
private BluetoothGattCharacteristic rxCharacteristic = null; private BluetoothGattCharacteristic rxCharacteristic = null;
@ -168,6 +173,7 @@ public class BangleJSDeviceSupport extends AbstractBTLEDeviceSupport {
// JSON - we hope! // JSON - we hope!
try { try {
JSONObject json = new JSONObject(line); JSONObject json = new JSONObject(line);
LOG.info("UART RX JSON parsed successfully");
handleUartRxJSON(json); handleUartRxJSON(json);
} catch (JSONException e) { } catch (JSONException e) {
GB.toast(getContext(), "Malformed JSON from Bangle.js: " + e.getLocalizedMessage(), Toast.LENGTH_LONG, GB.ERROR); GB.toast(getContext(), "Malformed JSON from Bangle.js: " + e.getLocalizedMessage(), Toast.LENGTH_LONG, GB.ERROR);
@ -176,7 +182,8 @@ public class BangleJSDeviceSupport extends AbstractBTLEDeviceSupport {
} }
private void handleUartRxJSON(JSONObject json) throws JSONException { private void handleUartRxJSON(JSONObject json) throws JSONException {
switch (json.getString("t")) { String packetType = json.getString("t");
switch (packetType) {
case "info": case "info":
GB.toast(getContext(), "Bangle.js: " + json.getString("msg"), Toast.LENGTH_LONG, GB.INFO); GB.toast(getContext(), "Bangle.js: " + json.getString("msg"), Toast.LENGTH_LONG, GB.INFO);
break; break;
@ -281,12 +288,28 @@ public class BangleJSDeviceSupport extends AbstractBTLEDeviceSupport {
// FIXME: This should be behind a default-off option in Gadgetbridge settings // FIXME: This should be behind a default-off option in Gadgetbridge settings
RequestQueue queue = Volley.newRequestQueue(getContext()); RequestQueue queue = Volley.newRequestQueue(getContext());
String url = json.getString("url"); String url = json.getString("url");
String _xmlPath = "";
try { _xmlPath = json.getString("xpath"); } catch (JSONException e) {}
final String xmlPath = _xmlPath;
// Request a string response from the provided URL. // Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url, StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() { new Response.Listener<String>() {
@Override @Override
public void onResponse(String response) { public void onResponse(String response) {
JSONObject o = new JSONObject(); JSONObject o = new JSONObject();
if (xmlPath.length()!=0) {
try {
InputSource inputXML = new InputSource( new StringReader( response ) );
XPath xPath = XPathFactory.newInstance().newXPath();
response = xPath.evaluate(xmlPath, inputXML);
} catch (Exception error) {
try {
o.put("err", error.toString());
} catch (JSONException e) {
GB.toast(getContext(), "HTTP: " + e.getLocalizedMessage(), Toast.LENGTH_LONG, GB.ERROR);
}
}
}
try { try {
o.put("t", "http"); o.put("t", "http");
o.put("resp", response); o.put("resp", response);
@ -310,6 +333,9 @@ public class BangleJSDeviceSupport extends AbstractBTLEDeviceSupport {
}); });
queue.add(stringRequest); queue.add(stringRequest);
} break; } break;
default : {
LOG.info("UART RX JSON packet type '"+packetType+"' not understood.");
}
} }
} }