use profile store units as fallback
This commit is contained in:
parent
77904cd1fd
commit
8af8d584eb
4 changed files with 201 additions and 140 deletions
|
@ -1,6 +1,7 @@
|
||||||
package info.nightscout.androidaps.data
|
package info.nightscout.androidaps.data
|
||||||
|
|
||||||
import androidx.collection.ArrayMap
|
import androidx.collection.ArrayMap
|
||||||
|
import info.nightscout.androidaps.utils.JsonHelper
|
||||||
import org.json.JSONException
|
import org.json.JSONException
|
||||||
import org.json.JSONObject
|
import org.json.JSONObject
|
||||||
import org.slf4j.LoggerFactory
|
import org.slf4j.LoggerFactory
|
||||||
|
@ -40,21 +41,19 @@ class ProfileStore(val data: JSONObject) {
|
||||||
|
|
||||||
fun getSpecificProfile(profileName: String): Profile? {
|
fun getSpecificProfile(profileName: String): Profile? {
|
||||||
var profile: Profile? = null
|
var profile: Profile? = null
|
||||||
try {
|
getStore()?.let { store ->
|
||||||
getStore()?.let { store ->
|
if (store.has(profileName)) {
|
||||||
if (store.has(profileName)) {
|
profile = cachedObjects[profileName]
|
||||||
profile = cachedObjects[profileName]
|
if (profile == null) {
|
||||||
if (profile == null) {
|
JsonHelper.safeGetJSONObject(store, profileName, null)?.let { profileObject ->
|
||||||
val profileObject = store.getJSONObject(profileName)
|
// take units from profile and if N/A from store
|
||||||
if (profileObject != null && profileObject.has("units")) {
|
JsonHelper.safeGetStringAllowNull(profileObject, "units", JsonHelper.safeGetString(store, "units"))?.let { units ->
|
||||||
profile = Profile(profileObject, profileObject.getString("units"))
|
profile = Profile(profileObject, units)
|
||||||
cachedObjects[profileName] = profile
|
cachedObjects[profileName] = profile
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (e: JSONException) {
|
|
||||||
log.error("Unhandled exception", e)
|
|
||||||
}
|
}
|
||||||
return profile
|
return profile
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,126 +0,0 @@
|
||||||
package info.nightscout.androidaps.utils;
|
|
||||||
|
|
||||||
import androidx.annotation.Nullable;
|
|
||||||
|
|
||||||
import org.json.JSONException;
|
|
||||||
import org.json.JSONObject;
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* JSonHelper is a Helper class which contains several methods to safely get data from the ggiven JSONObject.
|
|
||||||
*
|
|
||||||
* Created by triplem on 04.01.18.
|
|
||||||
*/
|
|
||||||
|
|
||||||
public class JsonHelper {
|
|
||||||
|
|
||||||
private static final Logger log = LoggerFactory.getLogger(JsonHelper.class);
|
|
||||||
|
|
||||||
private JsonHelper() {}
|
|
||||||
|
|
||||||
public static Object safeGetObject(JSONObject json, String fieldName, Object defaultValue) {
|
|
||||||
Object result = defaultValue;
|
|
||||||
|
|
||||||
if (json != null && json.has(fieldName)) {
|
|
||||||
try {
|
|
||||||
result = json.get(fieldName);
|
|
||||||
} catch (JSONException ignored) {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Nullable
|
|
||||||
public static String safeGetString(JSONObject json, String fieldName) {
|
|
||||||
String result = null;
|
|
||||||
|
|
||||||
if (json != null && json.has(fieldName)) {
|
|
||||||
try {
|
|
||||||
result = json.getString(fieldName);
|
|
||||||
} catch (JSONException ignored) {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String safeGetString(JSONObject json, String fieldName, String defaultValue) {
|
|
||||||
String result = defaultValue;
|
|
||||||
|
|
||||||
if (json != null && json.has(fieldName)) {
|
|
||||||
try {
|
|
||||||
result = json.getString(fieldName);
|
|
||||||
} catch (JSONException ignored) {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static double safeGetDouble(JSONObject json, String fieldName) {
|
|
||||||
double result = 0d;
|
|
||||||
|
|
||||||
if (json != null && json.has(fieldName)) {
|
|
||||||
try {
|
|
||||||
result = json.getDouble(fieldName);
|
|
||||||
} catch (JSONException ignored) {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static double safeGetDouble(JSONObject json, String fieldName, double defaultValue) {
|
|
||||||
double result = defaultValue;
|
|
||||||
|
|
||||||
if (json != null && json.has(fieldName)) {
|
|
||||||
try {
|
|
||||||
result = json.getDouble(fieldName);
|
|
||||||
} catch (JSONException ignored) {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static int safeGetInt(JSONObject json, String fieldName) {
|
|
||||||
int result = 0;
|
|
||||||
|
|
||||||
if (json != null && json.has(fieldName)) {
|
|
||||||
try {
|
|
||||||
result = json.getInt(fieldName);
|
|
||||||
} catch (JSONException ignored) {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static long safeGetLong(JSONObject json, String fieldName) {
|
|
||||||
long result = 0;
|
|
||||||
|
|
||||||
if (json != null && json.has(fieldName)) {
|
|
||||||
try {
|
|
||||||
result = json.getLong(fieldName);
|
|
||||||
} catch (JSONException e) {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static boolean safeGetBoolean(JSONObject json, String fieldName) {
|
|
||||||
boolean result = false;
|
|
||||||
|
|
||||||
if (json != null && json.has(fieldName)) {
|
|
||||||
try {
|
|
||||||
result = json.getBoolean(fieldName);
|
|
||||||
} catch (JSONException e) {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
}
|
|
126
app/src/main/java/info/nightscout/androidaps/utils/JsonHelper.kt
Normal file
126
app/src/main/java/info/nightscout/androidaps/utils/JsonHelper.kt
Normal file
|
@ -0,0 +1,126 @@
|
||||||
|
package info.nightscout.androidaps.utils
|
||||||
|
|
||||||
|
import org.json.JSONException
|
||||||
|
import org.json.JSONObject
|
||||||
|
|
||||||
|
object JsonHelper {
|
||||||
|
@JvmStatic
|
||||||
|
fun safeGetObject(json: JSONObject?, fieldName: String, defaultValue: Any): Any {
|
||||||
|
var result = defaultValue
|
||||||
|
if (json != null && json.has(fieldName)) {
|
||||||
|
try {
|
||||||
|
result = json[fieldName]
|
||||||
|
} catch (ignored: JSONException) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
@JvmStatic
|
||||||
|
fun safeGetJSONObject(json: JSONObject?, fieldName: String, defaultValue: JSONObject?): JSONObject? {
|
||||||
|
var result = defaultValue
|
||||||
|
if (json != null && json.has(fieldName)) {
|
||||||
|
try {
|
||||||
|
result = json.getJSONObject(fieldName)
|
||||||
|
} catch (ignored: JSONException) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
@JvmStatic
|
||||||
|
fun safeGetString(json: JSONObject?, fieldName: String): String? {
|
||||||
|
var result: String? = null
|
||||||
|
if (json != null && json.has(fieldName)) {
|
||||||
|
try {
|
||||||
|
result = json.getString(fieldName)
|
||||||
|
} catch (ignored: JSONException) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
@JvmStatic
|
||||||
|
fun safeGetString(json: JSONObject?, fieldName: String, defaultValue: String): String {
|
||||||
|
var result = defaultValue
|
||||||
|
if (json != null && json.has(fieldName)) {
|
||||||
|
try {
|
||||||
|
result = json.getString(fieldName)
|
||||||
|
} catch (ignored: JSONException) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
@JvmStatic
|
||||||
|
fun safeGetStringAllowNull(json: JSONObject?, fieldName: String, defaultValue: String?): String? {
|
||||||
|
var result = defaultValue
|
||||||
|
if (json != null && json.has(fieldName)) {
|
||||||
|
try {
|
||||||
|
result = json.getString(fieldName)
|
||||||
|
} catch (ignored: JSONException) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
@JvmStatic
|
||||||
|
fun safeGetDouble(json: JSONObject?, fieldName: String): Double {
|
||||||
|
var result = 0.0
|
||||||
|
if (json != null && json.has(fieldName)) {
|
||||||
|
try {
|
||||||
|
result = json.getDouble(fieldName)
|
||||||
|
} catch (ignored: JSONException) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
@JvmStatic
|
||||||
|
fun safeGetDouble(json: JSONObject?, fieldName: String, defaultValue: Double): Double {
|
||||||
|
var result = defaultValue
|
||||||
|
if (json != null && json.has(fieldName)) {
|
||||||
|
try {
|
||||||
|
result = json.getDouble(fieldName)
|
||||||
|
} catch (ignored: JSONException) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
@JvmStatic
|
||||||
|
fun safeGetInt(json: JSONObject?, fieldName: String): Int {
|
||||||
|
var result = 0
|
||||||
|
if (json != null && json.has(fieldName)) {
|
||||||
|
try {
|
||||||
|
result = json.getInt(fieldName)
|
||||||
|
} catch (ignored: JSONException) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
@JvmStatic
|
||||||
|
fun safeGetLong(json: JSONObject?, fieldName: String): Long {
|
||||||
|
var result: Long = 0
|
||||||
|
if (json != null && json.has(fieldName)) {
|
||||||
|
try {
|
||||||
|
result = json.getLong(fieldName)
|
||||||
|
} catch (ignored: JSONException) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
@JvmStatic
|
||||||
|
fun safeGetBoolean(json: JSONObject?, fieldName: String): Boolean {
|
||||||
|
var result = false
|
||||||
|
if (json != null && json.has(fieldName)) {
|
||||||
|
try {
|
||||||
|
result = json.getBoolean(fieldName)
|
||||||
|
} catch (ignored: JSONException) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
}
|
|
@ -4,7 +4,11 @@ import org.json.JSONException;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertFalse;
|
||||||
|
import static org.junit.Assert.assertNotEquals;
|
||||||
|
import static org.junit.Assert.assertNull;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by mike on 12.03.2018.
|
* Created by mike on 12.03.2018.
|
||||||
|
@ -12,20 +16,78 @@ import static org.junit.Assert.*;
|
||||||
|
|
||||||
public class JsonHelperTest {
|
public class JsonHelperTest {
|
||||||
|
|
||||||
String jsonString = "{\"d\":\"3.0\",\"i\":\"4\",\"s\":\"5\"}";
|
private String jsonString = "{\"d\":\"3.0\",\"i\":\"4\",\"s\":\"5\",\"b\":\"true\",\"j\":{\"a\": \"1\"}}";
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void runTest() throws JSONException {
|
public void safeGetObjectTest() throws JSONException {
|
||||||
JSONObject object = new JSONObject(jsonString);
|
JSONObject object = new JSONObject(jsonString);
|
||||||
assertEquals(null, JsonHelper.safeGetString(object, "notexisting"));
|
Object o = new Object();
|
||||||
|
assertEquals(o, JsonHelper.safeGetObject(null, "x", o));
|
||||||
|
assertEquals(o, JsonHelper.safeGetObject(object, "x", o));
|
||||||
|
assertNotEquals(o, JsonHelper.safeGetObject(object, "d", o));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void safeGetJSONObjectTest() throws JSONException {
|
||||||
|
JSONObject object = new JSONObject(jsonString);
|
||||||
|
JSONObject o = new JSONObject();
|
||||||
|
assertEquals(o, JsonHelper.safeGetJSONObject(null, "x", o));
|
||||||
|
assertTrue(JsonHelper.safeGetJSONObject(object, "j", o).has("a"));
|
||||||
|
assertEquals(o, JsonHelper.safeGetJSONObject(object, "d", o));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void safeGetStringTest() throws JSONException {
|
||||||
|
JSONObject object = new JSONObject(jsonString);
|
||||||
|
Object o = new Object();
|
||||||
|
assertNull(JsonHelper.safeGetString(null, "s"));
|
||||||
|
assertNull(JsonHelper.safeGetString(object, "notexisting"));
|
||||||
assertEquals("5", JsonHelper.safeGetString(object, "s"));
|
assertEquals("5", JsonHelper.safeGetString(object, "s"));
|
||||||
|
|
||||||
|
assertEquals("default", JsonHelper.safeGetString(null, "notexisting", "default"));
|
||||||
assertEquals("default", JsonHelper.safeGetString(object, "notexisting", "default"));
|
assertEquals("default", JsonHelper.safeGetString(object, "notexisting", "default"));
|
||||||
assertEquals("5", JsonHelper.safeGetString(object, "s", "default"));
|
assertEquals("5", JsonHelper.safeGetString(object, "s", "default"));
|
||||||
|
|
||||||
|
assertEquals("default", JsonHelper.safeGetStringAllowNull(null, "notexisting", "default"));
|
||||||
|
assertEquals("default", JsonHelper.safeGetStringAllowNull(object, "notexisting", "default"));
|
||||||
|
assertNull(JsonHelper.safeGetStringAllowNull(object, "notexisting", null));
|
||||||
|
assertEquals("5", JsonHelper.safeGetStringAllowNull(object, "s", "default"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void safeGetDoubleTest() throws JSONException {
|
||||||
|
JSONObject object = new JSONObject(jsonString);
|
||||||
|
|
||||||
assertEquals(0.0d, JsonHelper.safeGetDouble(object, "notexisting"), 0.0d);
|
assertEquals(0.0d, JsonHelper.safeGetDouble(object, "notexisting"), 0.0d);
|
||||||
|
assertEquals(0.0d, JsonHelper.safeGetDouble(null, "notexisting"), 0.0d);
|
||||||
assertEquals(3.0d, JsonHelper.safeGetDouble(object, "d"), 0.000001d);
|
assertEquals(3.0d, JsonHelper.safeGetDouble(object, "d"), 0.000001d);
|
||||||
|
|
||||||
|
assertEquals(6d, JsonHelper.safeGetDouble(null, "notexisting", 6d), 0.0d);
|
||||||
|
assertEquals(6d, JsonHelper.safeGetDouble(object, "notexisting", 6d), 0.0d);
|
||||||
|
assertEquals(3d, JsonHelper.safeGetDouble(object, "d", 6d), 0.0d);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void safeGetLntTest() throws JSONException {
|
||||||
|
JSONObject object = new JSONObject(jsonString);
|
||||||
|
assertEquals(0, JsonHelper.safeGetInt(null, "notexisting"));
|
||||||
assertEquals(0, JsonHelper.safeGetInt(object, "notexisting"));
|
assertEquals(0, JsonHelper.safeGetInt(object, "notexisting"));
|
||||||
assertEquals(4, JsonHelper.safeGetInt(object, "i"));
|
assertEquals(4, JsonHelper.safeGetInt(object, "i"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void safeGetLongTest() throws JSONException {
|
||||||
|
JSONObject object = new JSONObject(jsonString);
|
||||||
|
assertEquals(0, JsonHelper.safeGetInt(null, "notexisting"));
|
||||||
|
assertEquals(0, JsonHelper.safeGetInt(object, "notexisting"));
|
||||||
|
assertEquals(4, JsonHelper.safeGetInt(object, "i"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void safeGetBooleanTest() throws JSONException {
|
||||||
|
JSONObject object = new JSONObject(jsonString);
|
||||||
|
assertFalse(JsonHelper.safeGetBoolean(null, "notexisting"));
|
||||||
|
assertFalse(JsonHelper.safeGetBoolean(object, "notexisting"));
|
||||||
|
assertTrue(JsonHelper.safeGetBoolean(object, "b"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue