Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Receive glucose data from Syai Tag App. #3635

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,11 @@
<action android:name="com.eveningoutpost.dexdrip.OOP2_BLUETOOTH_ENABLE_RESULT" />
</intent-filter>
</receiver>
<receiver android:name=".SyaiTagAppReceiver">
<intent-filter>
<action android:name="com.eveningoutpost.dexdrip.action.SYAI_TAG_APP" />
</intent-filter>
</receiver>
<receiver android:name=".receivers.aidex.AidexReceiver">
<intent-filter>
<action android:name="com.microtechmd.cgms.aidex.action.BgEstimate" />
Expand Down
217 changes: 217 additions & 0 deletions app/src/main/java/com/eveningoutpost/dexdrip/SyaiTagAppReceiver.java
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public enum DexCollectionType {
Follower("Follower"),
LibreAlarm("LibreAlarm"),
NSEmulator("NSEmulator"),
SyaiTag("Syai Tag"),
NSFollow("NSFollower"),
SHFollow("SHFollower"),
WebFollow("WebFollower"),
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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";
Expand Down
Binary file added app/src/main/res/drawable-xhdpi/ic_syai_tag.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions app/src/main/res/values/arrays.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<item>LibreAlarm</item>
<item>Libre (patched App)</item>
<item>640G / EverSense</item>
<item>Syai Tag</item>
<item>Medtrum A6</item>
<item>Nightscout Follower</item>
<item>Dex Share Follower</item>
Expand All @@ -38,6 +39,7 @@
<item>LibreAlarm</item>
<item>LibreReceiver</item>
<item>NSEmulator</item>
<item>Syai Tag</item>
<item>Medtrum</item>
<item>NSFollower</item>
<item>SHFollower</item>
Expand Down
Loading