|
| 1 | +package com.evsuite.abrp; |
| 2 | + |
| 3 | +import android.content.Intent; |
| 4 | +import android.os.Bundle; |
| 5 | + |
| 6 | +import org.json.JSONArray; |
| 7 | +import org.json.JSONObject; |
| 8 | + |
| 9 | +/** |
| 10 | + * Outside temperature from the head unit's own weather app. |
| 11 | + * |
| 12 | + * The car does not implement ENV_OUTSIDE_TEMPERATURE, so there is no vehicle sensor to read, |
| 13 | + * and the vendor map service that answers a weather query by position never bound on this |
| 14 | + * firmware — {@code ext_temp} was simply absent from every upload. What demonstrably works is |
| 15 | + * the head unit's status bar, which shows a temperature all the time. It gets it from a |
| 16 | + * broadcast, and so does this. |
| 17 | + * |
| 18 | + * **Everything about the payload here is unverified.** The action and the extra name come from |
| 19 | + * reading the head unit, not from a published interface, and the shape inside is read by |
| 20 | + * searching rather than by field offsets: any key whose name mentions a temperature, at any |
| 21 | + * depth. That is deliberate. A parser written against a guessed layout fails silently and |
| 22 | + * looks exactly like the bug it was meant to fix, whereas a search either finds a number or |
| 23 | + * reports what it was given — see {@link #diagnostic()}, which reaches the app's own log. |
| 24 | + * |
| 25 | + * Stays in this app rather than in EVHardware for the same reason: the shared library carries |
| 26 | + * vendor knowledge that has been confirmed on a car. Once a driver's log shows this reading a |
| 27 | + * real broadcast, it belongs there instead, next to SaicWeather. |
| 28 | + */ |
| 29 | +final class HeadUnitWeather { |
| 30 | + |
| 31 | + /** The head unit's weather app announces each update with this. */ |
| 32 | + static final String ACTION = "com.saicmotor.weather"; |
| 33 | + |
| 34 | + /** Enough of an unreadable payload to recognise its shape, short enough for a log line. */ |
| 35 | + private static final int RAW_SAMPLE_CHARS = 200; |
| 36 | + |
| 37 | + /** How deep the search descends. A weather payload is nested, not deeply nested. */ |
| 38 | + private static final int MAX_DEPTH = 6; |
| 39 | + |
| 40 | + private volatile Float temperatureC; |
| 41 | + /** The place the head unit named — what ABRP's address is checked against. */ |
| 42 | + private volatile String place = ""; |
| 43 | + /** Why the last broadcast produced nothing, for the log. Null once one produced something. */ |
| 44 | + private volatile String problem = "no broadcast yet"; |
| 45 | + private volatile long receivedMs = 0L; |
| 46 | + |
| 47 | + /** Outside temperature in °C, or null while no broadcast has carried one. */ |
| 48 | + Float temperatureC() { |
| 49 | + return temperatureC; |
| 50 | + } |
| 51 | + |
| 52 | + String place() { |
| 53 | + return place; |
| 54 | + } |
| 55 | + |
| 56 | + long receivedMs() { |
| 57 | + return receivedMs; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * One short token for the upload log: the place a reading came from, or the reason there |
| 62 | + * is none. "no broadcast yet" and "no temperature in {...}" call for different fixes and |
| 63 | + * are indistinguishable from the ABRP side. |
| 64 | + */ |
| 65 | + String diagnostic() { |
| 66 | + if (temperatureC != null) return place.isEmpty() ? "ok" : place; |
| 67 | + return problem; |
| 68 | + } |
| 69 | + |
| 70 | + /** Reads whatever the head unit just broadcast. Never throws: this runs in a receiver. */ |
| 71 | + void accept(Intent intent) { |
| 72 | + acceptPayload(payloadOf(intent)); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * The half of {@link #accept} that has no Android in it, so the search below is testable |
| 77 | + * against the payloads it might be handed rather than only against the one it expects. |
| 78 | + */ |
| 79 | + void acceptPayload(String payload) { |
| 80 | + try { |
| 81 | + if (payload == null) { |
| 82 | + problem = "broadcast carried no JSON"; |
| 83 | + return; |
| 84 | + } |
| 85 | + JSONObject json = new JSONObject(payload); |
| 86 | + Float celsius = toCelsius(findNumber(json, "temp", 0), findUnit(json, 0)); |
| 87 | + if (celsius == null) { |
| 88 | + problem = "no temperature in " + truncate(payload); |
| 89 | + return; |
| 90 | + } |
| 91 | + temperatureC = celsius; |
| 92 | + String named = findText(json, 0); |
| 93 | + place = named == null ? "" : named; |
| 94 | + receivedMs = System.currentTimeMillis(); |
| 95 | + problem = null; |
| 96 | + } catch (Exception e) { |
| 97 | + // A payload that is not the JSON this expects is a finding, not a crash. |
| 98 | + problem = e.getClass().getSimpleName() + " reading broadcast"; |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * The JSON the broadcast carries. Named extras first, then any string extra that looks |
| 104 | + * like an object — the extra's name is as unverified as the rest of the payload, and a |
| 105 | + * renamed key is not a reason to give up on a broadcast that is otherwise readable. |
| 106 | + */ |
| 107 | + private static String payloadOf(Intent intent) { |
| 108 | + Bundle extras = intent.getExtras(); |
| 109 | + if (extras == null) return null; |
| 110 | + String named = asJson(extras.get("weather")); |
| 111 | + if (named != null) return named; |
| 112 | + for (String key : extras.keySet()) { |
| 113 | + String candidate = asJson(extras.get(key)); |
| 114 | + if (candidate != null) return candidate; |
| 115 | + } |
| 116 | + return null; |
| 117 | + } |
| 118 | + |
| 119 | + private static String asJson(Object value) { |
| 120 | + if (!(value instanceof String)) return null; |
| 121 | + String text = ((String) value).trim(); |
| 122 | + return text.startsWith("{") ? text : null; |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * The first number under a key whose name mentions [needle], at any depth. |
| 127 | + * |
| 128 | + * A search rather than a path: the payload's own field names are the only thing here that |
| 129 | + * is self-describing, and they survive a layout change that a field order would not. |
| 130 | + */ |
| 131 | + private static Double findNumber(Object node, String needle, int depth) { |
| 132 | + if (depth > MAX_DEPTH) return null; |
| 133 | + if (node instanceof JSONObject) { |
| 134 | + JSONObject object = (JSONObject) node; |
| 135 | + for (java.util.Iterator<String> keys = object.keys(); keys.hasNext(); ) { |
| 136 | + String key = keys.next(); |
| 137 | + Object value = object.opt(key); |
| 138 | + if (key.toLowerCase(java.util.Locale.US).contains(needle)) { |
| 139 | + Double number = asNumber(value); |
| 140 | + if (number != null) return number; |
| 141 | + } |
| 142 | + Double nested = findNumber(value, needle, depth + 1); |
| 143 | + if (nested != null) return nested; |
| 144 | + } |
| 145 | + } else if (node instanceof JSONArray) { |
| 146 | + JSONArray array = (JSONArray) node; |
| 147 | + for (int i = 0; i < array.length(); i++) { |
| 148 | + Double nested = findNumber(array.opt(i), needle, depth + 1); |
| 149 | + if (nested != null) return nested; |
| 150 | + } |
| 151 | + } |
| 152 | + return null; |
| 153 | + } |
| 154 | + |
| 155 | + /** Numbers arrive as numbers or as strings depending on the producer; both count. */ |
| 156 | + private static Double asNumber(Object value) { |
| 157 | + if (value instanceof Number) return ((Number) value).doubleValue(); |
| 158 | + if (value instanceof String) { |
| 159 | + try { |
| 160 | + return Double.valueOf(((String) value).trim()); |
| 161 | + } catch (NumberFormatException ignored) { |
| 162 | + return null; |
| 163 | + } |
| 164 | + } |
| 165 | + return null; |
| 166 | + } |
| 167 | + |
| 168 | + /** The unit the payload names, if it names one at all. */ |
| 169 | + private static String findUnit(Object node, int depth) { |
| 170 | + if (depth > MAX_DEPTH) return null; |
| 171 | + if (node instanceof JSONObject) { |
| 172 | + JSONObject object = (JSONObject) node; |
| 173 | + for (java.util.Iterator<String> keys = object.keys(); keys.hasNext(); ) { |
| 174 | + String key = keys.next(); |
| 175 | + Object value = object.opt(key); |
| 176 | + if (key.toLowerCase(java.util.Locale.US).contains("unit") |
| 177 | + && value instanceof String) { |
| 178 | + return (String) value; |
| 179 | + } |
| 180 | + String nested = findUnit(value, depth + 1); |
| 181 | + if (nested != null) return nested; |
| 182 | + } |
| 183 | + } |
| 184 | + return null; |
| 185 | + } |
| 186 | + |
| 187 | + /** The place the reading is for — whatever the payload calls it. */ |
| 188 | + private static String findText(Object node, int depth) { |
| 189 | + if (depth > MAX_DEPTH || !(node instanceof JSONObject)) return null; |
| 190 | + JSONObject object = (JSONObject) node; |
| 191 | + for (java.util.Iterator<String> keys = object.keys(); keys.hasNext(); ) { |
| 192 | + String key = keys.next(); |
| 193 | + String lower = key.toLowerCase(java.util.Locale.US); |
| 194 | + Object value = object.opt(key); |
| 195 | + if ((lower.contains("address") || lower.contains("city") || lower.contains("poi")) |
| 196 | + && value instanceof String && !((String) value).trim().isEmpty()) { |
| 197 | + return ((String) value).trim(); |
| 198 | + } |
| 199 | + String nested = findText(value, depth + 1); |
| 200 | + if (nested != null) return nested; |
| 201 | + } |
| 202 | + return null; |
| 203 | + } |
| 204 | + |
| 205 | + /** |
| 206 | + * Celsius, or null when the payload named a unit this does not know. |
| 207 | + * |
| 208 | + * An unnamed unit is taken as Celsius: the head unit displays °C on this car, and the |
| 209 | + * value is sanity-checked by the caller anyway. A named Fahrenheit is converted, because |
| 210 | + * passing 81 °F through as 81 °C is worse than reporting nothing. |
| 211 | + */ |
| 212 | + private static Float toCelsius(Double value, String unit) { |
| 213 | + if (value == null) return null; |
| 214 | + String name = unit == null ? "" : unit.trim().toUpperCase(java.util.Locale.US); |
| 215 | + if (name.contains("F")) return (float) ((value - 32.0) * 5.0 / 9.0); |
| 216 | + return value.floatValue(); |
| 217 | + } |
| 218 | + |
| 219 | + private static String truncate(String payload) { |
| 220 | + return payload.length() <= RAW_SAMPLE_CHARS |
| 221 | + ? payload |
| 222 | + : payload.substring(0, RAW_SAMPLE_CHARS) + "…"; |
| 223 | + } |
| 224 | +} |
0 commit comments