From 40f9744e5d6be457e046c316903505523161482c Mon Sep 17 00:00:00 2001 From: SyaiDev Date: Tue, 20 Aug 2024 17:09:54 +0800 Subject: [PATCH] Receive glucose data from Syai Tag App. --- app/src/main/AndroidManifest.xml | 5 + .../dexdrip/SyaiTagAppReceiver.java | 217 ++++++++++++++++++ .../dexdrip/utilitymodels/Intents.java | 2 + .../dexdrip/utilitymodels/SourceWizard.java | 2 +- .../dexdrip/utils/DexCollectionType.java | 4 +- .../main/res/drawable-xhdpi/ic_syai_tag.png | Bin 0 -> 3268 bytes app/src/main/res/values/arrays.xml | 2 + 7 files changed, 230 insertions(+), 2 deletions(-) create mode 100755 app/src/main/java/com/eveningoutpost/dexdrip/SyaiTagAppReceiver.java create mode 100644 app/src/main/res/drawable-xhdpi/ic_syai_tag.png diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 35e672da9d..3ed6ac45a5 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -586,6 +586,11 @@ + + + + + diff --git a/app/src/main/java/com/eveningoutpost/dexdrip/SyaiTagAppReceiver.java b/app/src/main/java/com/eveningoutpost/dexdrip/SyaiTagAppReceiver.java new file mode 100755 index 0000000000..544bd80c67 --- /dev/null +++ b/app/src/main/java/com/eveningoutpost/dexdrip/SyaiTagAppReceiver.java @@ -0,0 +1,217 @@ + +package com.eveningoutpost.dexdrip; + +import static com.eveningoutpost.dexdrip.models.BgReading.bgReadingInsertFromJson; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.content.SharedPreferences; +import android.os.Bundle; +import android.os.PowerManager; +import android.preference.PreferenceManager; + +import com.eveningoutpost.dexdrip.models.BgReading; +import com.eveningoutpost.dexdrip.models.JoH; +import com.eveningoutpost.dexdrip.models.LibreOOPAlgorithm; +import com.eveningoutpost.dexdrip.models.Sensor; +import com.eveningoutpost.dexdrip.models.UserError.Log; +import com.eveningoutpost.dexdrip.utilitymodels.Intents; +import com.eveningoutpost.dexdrip.utilitymodels.Pref; +import com.eveningoutpost.dexdrip.utilitymodels.PumpStatus; +import com.eveningoutpost.dexdrip.utils.DexCollectionType; + +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + +import java.util.UUID; + +/** + * Created by jamorham on 14/11/2016. + */ + +public class SyaiTagAppReceiver extends BroadcastReceiver { + + private static final String TAG = "jamorham syai tag"; + private static final boolean debug = false; + private static final boolean d = false; + private static SharedPreferences prefs; + private static final Object lock = new Object(); + + @Override + public void onReceive(final Context context, final Intent intent) { + new Thread() { + @Override + public void run() { + PowerManager.WakeLock wl = JoH.getWakeLock("SyaiTag-receiver", 60000); + synchronized (lock) { + try { + Log.d(TAG, "SyaiTagApp onReceiver: " + intent.getAction()); + JoH.benchmark(null); + // check source + if (prefs == null) + prefs = PreferenceManager.getDefaultSharedPreferences(context); + + final Bundle bundle = intent.getExtras(); + // BundleScrubber.scrub(bundle); + final String action = intent.getAction(); + + if ((bundle != null) && (debug)) { + Log.d(TAG, "Action: " + action); + JoH.dumpBundle(bundle, TAG); + } + if (action == null) return; + switch (action) { + case Intents.XDRIP_PLUS_SYAI_TAG_APP: + // in future this could have its own data source perhaps instead of follower + if (!Home.get_follower() && DexCollectionType.getDexCollectionType() != DexCollectionType.SyaiTag && + !Pref.getBooleanDefaultFalse("external_blukon_algorithm")) { + Log.e(TAG, "Received Syai Tag data but we are not a follower or emulator receiver"); + return; + } + if (!Home.get_follower()) { + // must be syai tag here ???? Not true anymore. + if (!Sensor.isActive()) { + // warn about problems running without a sensor record + Home.toaststaticnext("Please use: Start Sensor from the menu for best results!"); + } + } + if (bundle == null) break; + Log.d(TAG, "Receiving syai tag broadcast"); + final String collection = bundle.getString("collection"); + if (collection == null) return; + switch (collection) { + case "entries": + final String data = bundle.getString("data"); + if ((data != null) && (data.length() > 0)) { + try { + final JSONArray json_array = new JSONArray(data); + // if this array is >1 in length then it is from OOP otherwise something like AAPS + if (json_array.length() > 1) { + final JSONObject json_object = json_array.getJSONObject(0); + int process_id = -1; + try { + process_id = json_object.getInt("ROW_ID"); + } catch (JSONException e) { + // Intentionly ignoring ecxeption. + } + if (process_id == -1 || process_id == android.os.Process.myPid()) { + LibreOOPAlgorithm.handleData(json_array.getString(1)); + } else { + Log.d(TAG, "Ignoring OOP result since process id is wrong " + process_id); + } + + } else { + final JSONObject json_object = json_array.getJSONObject(0); + final String type = json_object.getString("type"); + switch (type) { + case "sgv": + double slope = 0; + try { + slope = BgReading.slopefromName(json_object.getString("direction")); + } catch (JSONException e) { + // + } + bgReadingInsertFromData(json_object.getLong("date"), + json_object.getDouble("sgv"), slope, true); + + break; + default: + Log.e(TAG, "Unknown entries type: " + type); + } + } + } catch (JSONException e) { + Log.e(TAG, "Got JSON exception: " + e); + } + } + break; + case "devicestatus": + final String ddata = bundle.getString("data"); + + if ((ddata != null) && (ddata.length() > 0)) { + try { + Log.d(TAG, "Got device status data: " + ddata); + final JSONArray json_array = new JSONArray(ddata); + final JSONObject json_object = json_array.getJSONObject(0); + final JSONObject json_pump_object = json_object.getJSONObject("pump"); + + try { + final double reservoir = json_pump_object.getDouble("reservoir"); + PumpStatus.setReservoir(reservoir); + + } catch (JSONException e) { + Log.d(TAG, "Got exception when processing reservoir: " + e); + } + + try { + final JSONObject battery_object = json_pump_object.getJSONObject("battery"); + final double battery_percent = battery_object.getDouble("percent"); + PumpStatus.setBattery(battery_percent); + + } catch (JSONException e) { + Log.d(TAG, "Got exception when processing battery: " + e); + } + + try { + final JSONObject iob_object = json_pump_object.getJSONObject("iob"); + final double bolus_iob = iob_object.getDouble("bolusiob"); + PumpStatus.setBolusIoB(bolus_iob); + + } catch (JSONException e) { + Log.d(TAG, "Got exception when processing iob: " + e); + } + + } catch (JSONException e) { + Log.e(TAG, "Got JSON exception: " + e); + } catch (Exception e) { + Log.e(TAG, "Got processing exception: " + e); + } + PumpStatus.syncUpdate(); + } + break; + default: + Log.d(TAG, "Unprocessed collection: " + collection); + } + break; + default: + Log.e(TAG, "Unknown action! " + action); + break; + } + + } catch (Exception e) { + Log.e(TAG, "Caught Exception handling intent", e ); + }finally { + JoH.benchmark("syai tag process"); + JoH.releaseWakeLock(wl); + } + } // lock + } + }.start(); + } + public static BgReading bgReadingInsertFromData(long timestamp, double sgv, double slope, boolean do_notification) { + Log.d(TAG, "bgReadingInsertFromData called timestamp = " + timestamp + " bg = " + sgv + " time =" + JoH.dateTimeText(timestamp)); + final JSONObject faux_bgr = new JSONObject(); + try { + faux_bgr.put("timestamp", timestamp); + faux_bgr.put("calculated_value", sgv); + faux_bgr.put("filtered_calculated_value", sgv); + faux_bgr.put("calculated_value_slope", slope); + faux_bgr.put("source_info", "Syai Tag Follow"); + // sanity checking??? + // fake up some extra data + faux_bgr.put("raw_data", sgv); + faux_bgr.put("age_adjusted_raw_value", sgv); + faux_bgr.put("filtered_data", sgv); + + faux_bgr.put("uuid", UUID.randomUUID().toString()); + } catch (JSONException e) { + Log.e(TAG, "bgReadingInsertFromData Got JSON exception: " + e); + return null; + } + + Log.d(TAG, "Received Syai Tag SGV: " + faux_bgr); + Sensor.createDefaultIfMissing(); + return bgReadingInsertFromJson(faux_bgr.toString(), do_notification, true); // notify and force sensor + } +} diff --git a/app/src/main/java/com/eveningoutpost/dexdrip/utilitymodels/Intents.java b/app/src/main/java/com/eveningoutpost/dexdrip/utilitymodels/Intents.java index 5eda8666f0..1bc496c620 100644 --- a/app/src/main/java/com/eveningoutpost/dexdrip/utilitymodels/Intents.java +++ b/app/src/main/java/com/eveningoutpost/dexdrip/utilitymodels/Intents.java @@ -54,6 +54,8 @@ public interface Intents { String ACTION_DATABASE = "info.nightscout.client.DBACCESS"; String LIBRE_ALARM_TO_XDRIP_PLUS = "com.eveningoutpost.dexdrip.FROM_LIBRE_ALARM"; String XDRIP_PLUS_NS_EMULATOR = "com.eveningoutpost.dexdrip.NS_EMULATOR"; + String XDRIP_PLUS_SYAI_TAG_APP = "com.eveningoutpost.dexdrip.action.SYAI_TAG_APP"; + String BLUEJAY_THINJAM_API = "com.eveningoutpost.dexdrip.THINJAM_API"; String BLUEJAY_THINJAM_EMIT = "com.eveningoutpost.dexdrip.THINJAM_EMIT"; // Local Broadcasts diff --git a/app/src/main/java/com/eveningoutpost/dexdrip/utilitymodels/SourceWizard.java b/app/src/main/java/com/eveningoutpost/dexdrip/utilitymodels/SourceWizard.java index 96f13f5e15..96ea975519 100644 --- a/app/src/main/java/com/eveningoutpost/dexdrip/utilitymodels/SourceWizard.java +++ b/app/src/main/java/com/eveningoutpost/dexdrip/utilitymodels/SourceWizard.java @@ -72,7 +72,7 @@ public class SourceWizard { other.addChild(new Item("Medtrum A6 / S7", DexCollectionType.Medtrum, R.drawable.a6_icon)); other.addChild(new Item("Nightscout Follower", DexCollectionType.NSFollow, R.drawable.nsfollow_icon)); other.addChild(new Item("Dex Share Follower", DexCollectionType.SHFollow, R.drawable.nsfollow_icon)); - // + other.addChild(new Item("Syai Tag", DexCollectionType.SyaiTag, R.drawable.ic_syai_tag)); other.addChild(new Item("EverSense", DexCollectionType.NSEmulator, R.drawable.wikimedia_eversense_icon_pbroks13)); } } diff --git a/app/src/main/java/com/eveningoutpost/dexdrip/utils/DexCollectionType.java b/app/src/main/java/com/eveningoutpost/dexdrip/utils/DexCollectionType.java index 5fc20f886c..ce115d2910 100644 --- a/app/src/main/java/com/eveningoutpost/dexdrip/utils/DexCollectionType.java +++ b/app/src/main/java/com/eveningoutpost/dexdrip/utils/DexCollectionType.java @@ -42,6 +42,7 @@ public enum DexCollectionType { Follower("Follower"), LibreAlarm("LibreAlarm"), NSEmulator("NSEmulator"), + SyaiTag("Syai Tag"), NSFollow("NSFollower"), SHFollow("SHFollower"), WebFollow("WebFollower"), @@ -85,7 +86,7 @@ public enum DexCollectionType { Collections.addAll(usesXbridge, DexbridgeWixel, WifiDexBridgeWixel); Collections.addAll(usesFiltered, DexbridgeWixel, WifiDexBridgeWixel, DexcomG5, WifiWixel, Follower, Mock); // Bluetooth and Wifi+Bluetooth need dynamic mode Collections.addAll(usesLibre, LimiTTer, LibreAlarm, LimiTTerWifi, LibreWifi, LibreReceiver); - Collections.addAll(isPassive, NSEmulator, NSFollow, SHFollow, WebFollow, LibreReceiver, UiBased, CLFollow, AidexReceiver); + Collections.addAll(isPassive, NSEmulator, SyaiTag, NSFollow, SHFollow, WebFollow, LibreReceiver, UiBased, CLFollow, AidexReceiver); Collections.addAll(usesBattery, BluetoothWixel, DexbridgeWixel, WifiBlueToothWixel, WifiDexBridgeWixel, Follower, LimiTTer, LibreAlarm, LimiTTerWifi, LibreWifi); // parakeet separate Collections.addAll(usesDexcomRaw, BluetoothWixel, DexbridgeWixel, WifiWixel, WifiBlueToothWixel, DexcomG5, WifiDexBridgeWixel, Mock); Collections.addAll(usesTransmitterBattery, WifiWixel, BluetoothWixel, DexbridgeWixel, WifiBlueToothWixel, WifiDexBridgeWixel); // G4 transmitter battery @@ -263,6 +264,7 @@ public static String getBestCollectorHardwareName() { final DexCollectionType dct = getDexCollectionType(); switch (dct) { case NSEmulator: + case SyaiTag: case AidexReceiver: case LibreReceiver: return "Other App"; diff --git a/app/src/main/res/drawable-xhdpi/ic_syai_tag.png b/app/src/main/res/drawable-xhdpi/ic_syai_tag.png new file mode 100644 index 0000000000000000000000000000000000000000..8f1a189e771b905bcc68457a0b35658ef7ad4301 GIT binary patch literal 3268 zcmbVP=RX?`6Q`xMso9FQs687sTcwgvG@-R4HDilaKdY#k4Ka#Rv1!oOCW-Y!?Gb9m zE@~8w(VDIKQ|F3V-JdTV%NAUfI10-w?bV9-2gg@(w7Wc#)Tfdgk55T2)Qe>o+?lOXi`gFK8YrZwt zP&-3#H$Q7c_9Pw$&(?V@{HkpYyQpXhyRbex{agFW@#N2MtB*0kPQXl_*HFaSbXI#>X&@#KM1l@Jcon@88u zLw>{>$jr~oSjsj|R@#^kqb#PHr!IxT%$g$v?k$yrH14x53E$Fntu7>3!c66j3YZ z=_`Xbt(`DNsH&Nj2~2GFrHYfsGvcSbv*sj>_|0tZa|~AkT{&-hhgb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2k;mAmD-#g(M&IHI#5!Wq0ut; z{i;1*N`0%*id1h}{*=wON zbCMHDhj$DekgKEn!J+4BjI1@-$k$EzpN9|Qhg#MlWo7@uhTy4-f3mL*&P024SFlX& zt@4*uR!$-e9F)?M*0hDLG_+aTlJ*;1M}%;c$;!!!tZ22gI#v{BKCD`!0L<*zkthohda-?Dj|a7XYg!pvvh%y`mDs z4k~)sUT#Vu;MNu>4d6fjh^3%UdaYk=4NAPX zbffl{x4CBoES4p~=wT@jGJ8VXCEGo5SrIltl1DGDH}Nq~iqs1g&X(=RWM5tDY=CLG zygjqL?ZNB$5s?tiM>hqWF_Fo<8yoEi@n*@NrJLk+?QVa-Yv__qdKYyi6`q%n^@6I< z-2D6q1{m+X{cM_?Cu8wg@i7a^XKk+wpsv@`IQ`~5qI@cXOGX zbH#2dlqmS-%!i~yhNamy962%}b-MAlVD@`5STuAd>P0!zzTK|-PyGP8h>rfgPG^sA zC_5X-gt@o%I3-~amtn9`KBT|ga1TSDO-s?rN|8A)oQ?W|*eohXtycGXd^L4wM|EDQ zH&d3mNfN92N74gq-IP2`t|2fcgljejjuI8kokW#ck!s3?=5faiN#LibreAlarm Libre (patched App) 640G / EverSense + Syai Tag Medtrum A6 Nightscout Follower Dex Share Follower @@ -38,6 +39,7 @@ LibreAlarm LibreReceiver NSEmulator + Syai Tag Medtrum NSFollower SHFollower