NSSgv -> kt

This commit is contained in:
Milos Kozak 2021-03-21 13:50:06 +01:00
parent 3d48636429
commit 87ab4fda0e
3 changed files with 37 additions and 77 deletions

View file

@ -0,0 +1,32 @@
package info.nightscout.androidaps.plugins.general.nsclient.data
import info.nightscout.androidaps.utils.JsonHelper
import org.json.JSONObject
/**
*
* {"mgdl":105,"mills":1455136282375,"device":"xDrip-BluetoothWixel","direction":"Flat","filtered":98272,"unfiltered":98272,"noise":1,"rssi":100}
*/
@Suppress("SpellCheckingInspection")
class NSSgv(val data: JSONObject) {
val mgdl: Int?
get() = JsonHelper.safeGetIntAllowNull(data, "mgdl")
val filtered: Int?
get() = JsonHelper.safeGetIntAllowNull(data, "filtered")
val unfiltered: Int?
get() = JsonHelper.safeGetIntAllowNull(data, "unfiltered")
val noise: Int?
get() = JsonHelper.safeGetIntAllowNull(data, "noise")
val rssi: Int?
get() = JsonHelper.safeGetIntAllowNull(data, "rssi")
val mills: Long?
get() = JsonHelper.safeGetLongAllowNull(data, "mills")
val device: String?
get() = JsonHelper.safeGetStringAllowNull(data, "device", null)
val direction: String?
get() = JsonHelper.safeGetStringAllowNull(data, "direction", null)
val id: String?
get() = JsonHelper.safeGetStringAllowNull(data, "_id", null)
}

View file

@ -97,13 +97,13 @@ class NSClientSourcePlugin @Inject constructor(
(context.applicationContext as HasAndroidInjector).androidInjector().inject(this) (context.applicationContext as HasAndroidInjector).androidInjector().inject(this)
} }
private fun toGv(jsonObject: JSONObject): CgmSourceTransaction.TransactionGlucoseValue { private fun toGv(jsonObject: JSONObject): CgmSourceTransaction.TransactionGlucoseValue? {
val sgv = NSSgv(jsonObject) val sgv = NSSgv(jsonObject)
return CgmSourceTransaction.TransactionGlucoseValue( return CgmSourceTransaction.TransactionGlucoseValue(
timestamp = sgv.mills, timestamp = sgv.mills ?: return null,
value = sgv.mgdl.toDouble(), value = sgv.mgdl?.toDouble() ?: return null,
noise = null, noise = null,
raw = if (sgv.filtered != null) sgv.filtered.toDouble() else sgv.mgdl.toDouble(), raw = sgv.filtered?.toDouble() ?: sgv.mgdl?.toDouble(),
trendArrow = GlucoseValue.TrendArrow.fromString(sgv.direction), trendArrow = GlucoseValue.TrendArrow.fromString(sgv.direction),
nightscoutId = sgv.id, nightscoutId = sgv.id,
sourceSensor = GlucoseValue.SourceSensor.fromString(sgv.device) sourceSensor = GlucoseValue.SourceSensor.fromString(sgv.device)
@ -125,7 +125,7 @@ class NSClientSourcePlugin @Inject constructor(
aapsLogger.debug(LTag.BGSOURCE, "Received NS Data: $sgvs") aapsLogger.debug(LTag.BGSOURCE, "Received NS Data: $sgvs")
val glucoseValues = mutableListOf<CgmSourceTransaction.TransactionGlucoseValue>() val glucoseValues = mutableListOf<CgmSourceTransaction.TransactionGlucoseValue>()
for (i in 0 until sgvs.length()) { for (i in 0 until sgvs.length()) {
val sgv = toGv(sgvs.getJSONObject(i)) val sgv = toGv(sgvs.getJSONObject(i)) ?: continue
if (sgv.timestamp < dateUtil._now() && sgv.timestamp > latestDateInReceivedData) latestDateInReceivedData = sgv.timestamp if (sgv.timestamp < dateUtil._now() && sgv.timestamp > latestDateInReceivedData) latestDateInReceivedData = sgv.timestamp
glucoseValues += sgv glucoseValues += sgv

View file

@ -1,72 +0,0 @@
package info.nightscout.androidaps.plugins.general.nsclient.data;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import info.nightscout.androidaps.logging.L;
import info.nightscout.androidaps.logging.LTag;
import info.nightscout.androidaps.logging.StacktraceLoggerWrapper;
/**
*
* {"mgdl":105,"mills":1455136282375,"device":"xDrip-BluetoothWixel","direction":"Flat","filtered":98272,"unfiltered":98272,"noise":1,"rssi":100}
*/
public class NSSgv {
private static final Logger log = StacktraceLoggerWrapper.getLogger(LTag.NSCLIENT);
private final JSONObject data;
public NSSgv(JSONObject obj) {
this.data = obj;
}
private String getStringOrNull(String key) {
String ret = null;
if (data.has(key)) {
try {
ret = data.getString(key);
} catch (JSONException e) {
log.error("Unhandled exception", e);
}
}
return ret;
}
private Integer getIntegerOrNull(String key) {
Integer ret = null;
if (data.has(key)) {
try {
ret = data.getInt(key);
} catch (JSONException e) {
log.error("Unhandled exception", e);
}
}
return ret;
}
private Long getLongOrNull(String key) {
Long ret = null;
if (data.has(key)) {
try {
ret = data.getLong(key);
} catch (JSONException e) {
log.error("Unhandled exception", e);
}
}
return ret;
}
public JSONObject getData () { return data; }
public Integer getMgdl () { return getIntegerOrNull("mgdl"); }
public Integer getFiltered () { return getIntegerOrNull("filtered"); }
public Integer getUnfiltered () { return getIntegerOrNull("unfiltered"); }
public Integer getNoise () { return getIntegerOrNull("noise"); }
public Integer getRssi () { return getIntegerOrNull("rssi"); }
public Long getMills () { return getLongOrNull("mills"); }
public String getDevice () { return getStringOrNull("device"); }
public String getDirection () { return getStringOrNull("direction"); }
public String getId () { return getStringOrNull("_id"); }
}