1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-16 06:59:29 +01:00

Bangle.js: HTTP request XPath can now return Arrays.

More robost toJSON that deals with unicode and null
Log messages when converting words to images so we can debug when words are being converted that shouldn't be
This commit is contained in:
Gordon Williams 2023-06-30 11:34:53 +01:00
parent 690d97b881
commit 88e0b9543d
2 changed files with 28 additions and 3 deletions

View File

@ -17,6 +17,7 @@
* Bangle.js: Set default value for GPS event interval to 1 second * Bangle.js: Set default value for GPS event interval to 1 second
* Bangle.js: Support navigation instructions * Bangle.js: Support navigation instructions
* Bangle.js: Escape characters that fall in the Unicode codepoint area (for Espruino ~2v18.20 and later) * Bangle.js: Escape characters that fall in the Unicode codepoint area (for Espruino ~2v18.20 and later)
* Bangle.js: HTTP request XPath can now return Arrays
* Fossil/Skagen Hybrids: Add support for ultraviolet index and rain probability * Fossil/Skagen Hybrids: Add support for ultraviolet index and rain probability
* Fossil/Skagen Hybrids: Add UV index and chance of rain widgets * Fossil/Skagen Hybrids: Add UV index and chance of rain widgets
* Fossil/Skagen Hybrids: Allow launching the calibration activity on any Gadgetbridge variant * Fossil/Skagen Hybrids: Allow launching the calibration activity on any Gadgetbridge variant

View File

@ -58,6 +58,7 @@ 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.w3c.dom.NodeList;
import org.xml.sax.InputSource; import org.xml.sax.InputSource;
import java.io.BufferedWriter; import java.io.BufferedWriter;
@ -79,6 +80,7 @@ import java.util.Map;
import java.util.SimpleTimeZone; import java.util.SimpleTimeZone;
import javax.xml.xpath.XPath; import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory; import javax.xml.xpath.XPathFactory;
import de.greenrobot.dao.query.QueryBuilder; import de.greenrobot.dao.query.QueryBuilder;
@ -395,6 +397,8 @@ public class BangleJSDeviceSupport extends AbstractBTLEDeviceSupport {
else if (ch<32 || ch==127 || ch==173 || else if (ch<32 || ch==127 || ch==173 ||
((ch>=0xC2) && (ch<=0xF4))) // unicode start char range ((ch>=0xC2) && (ch<=0xF4))) // unicode start char range
json += "\\x"+Integer.toHexString((ch&255)|256).substring(1); json += "\\x"+Integer.toHexString((ch&255)|256).substring(1);
else if (ch>255)
json += "\\u"+Integer.toHexString((ch&65535)|65536).substring(1);
else json += s.charAt(i); else json += s.charAt(i);
} }
// if it was less characters to send base64, do that! // if it was less characters to send base64, do that!
@ -435,7 +439,10 @@ public class BangleJSDeviceSupport extends AbstractBTLEDeviceSupport {
if (iter.hasNext()) json+=","; if (iter.hasNext()) json+=",";
} }
return json+"}"; return json+"}";
} // else int/double/null } else if (v==null) {
// else int/double/null
return "null";
}
return v.toString(); return v.toString();
} }
@ -666,11 +673,14 @@ public class BangleJSDeviceSupport extends AbstractBTLEDeviceSupport {
final Map<String,String> headers = _headers; final Map<String,String> headers = _headers;
String _xmlPath = ""; String _xmlPath = "";
String _xmlReturn = "";
try { try {
_xmlPath = json.getString("xpath"); _xmlPath = json.getString("xpath");
_xmlReturn = json.getString("return");
} catch (JSONException e) { } catch (JSONException e) {
} }
final String xmlPath = _xmlPath; final String xmlPath = _xmlPath;
final String xmlReturn = _xmlReturn;
// Request a string response from the provided URL. // Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(method, url, StringRequest stringRequest = new StringRequest(method, url,
new Response.Listener<String>() { new Response.Listener<String>() {
@ -681,7 +691,18 @@ public class BangleJSDeviceSupport extends AbstractBTLEDeviceSupport {
try { try {
InputSource inputXML = new InputSource(new StringReader(response)); InputSource inputXML = new InputSource(new StringReader(response));
XPath xPath = XPathFactory.newInstance().newXPath(); XPath xPath = XPathFactory.newInstance().newXPath();
if (xmlReturn.equals("array")) {
NodeList result = (NodeList) xPath.evaluate(xmlPath, inputXML, XPathConstants.NODESET);
response = null; // don't add it below
JSONArray arr = new JSONArray();
if (result != null) {
for (int i = 0; i < result.getLength(); i++)
arr.put(result.item(i).getTextContent());
}
o.put("resp", arr);
} else {
response = xPath.evaluate(xmlPath, inputXML); response = xPath.evaluate(xmlPath, inputXML);
}
} catch (Exception error) { } catch (Exception error) {
uartTxJSONError("http", error.toString(), id); uartTxJSONError("http", error.toString(), id);
return; return;
@ -691,6 +712,7 @@ public class BangleJSDeviceSupport extends AbstractBTLEDeviceSupport {
o.put("t", "http"); o.put("t", "http");
if( id!=null) if( id!=null)
o.put("id", id); o.put("id", id);
if (response!=null)
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);
@ -1055,6 +1077,7 @@ public class BangleJSDeviceSupport extends AbstractBTLEDeviceSupport {
if (" -_/:.,?!'\"&*()".indexOf(ch)>=0) { if (" -_/:.,?!'\"&*()".indexOf(ch)>=0) {
// word split // word split
if (needsTranslate) { // convert word if (needsTranslate) { // convert word
LOG.info("renderUnicodeAsImage converting " + word);
result += renderUnicodeWordAsImage(word)+ch; result += renderUnicodeWordAsImage(word)+ch;
} else { // or just copy across } else { // or just copy across
result += word+ch; result += word+ch;
@ -1068,6 +1091,7 @@ public class BangleJSDeviceSupport extends AbstractBTLEDeviceSupport {
} }
} }
if (needsTranslate) { // convert word if (needsTranslate) { // convert word
LOG.info("renderUnicodeAsImage converting " + word);
result += renderUnicodeWordAsImage(word); result += renderUnicodeWordAsImage(word);
} else { // or just copy across } else { // or just copy across
result += word; result += word;