-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
196 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
app/src/main/java/com/eveningoutpost/dexdrip/insulin/aaps/AAPSStatusHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package com.eveningoutpost.dexdrip.insulin.aaps; | ||
|
||
import com.eveningoutpost.dexdrip.alert.Persist; | ||
import com.eveningoutpost.dexdrip.models.UserError.Log; | ||
import com.eveningoutpost.dexdrip.utilitymodels.Constants; | ||
import com.eveningoutpost.dexdrip.utilitymodels.PumpStatus; | ||
import com.google.gson.Gson; | ||
import com.google.gson.GsonBuilder; | ||
|
||
import info.nightscout.sdk.localmodel.devicestatus.NSDeviceStatus; | ||
import lombok.val; | ||
|
||
/** | ||
* JamOrHam | ||
* <p> | ||
* Handle processing of AAPS device status updates | ||
*/ | ||
|
||
public class AAPSStatusHandler { | ||
|
||
private static final String TAG = AAPSStatusHandler.class.getSimpleName(); | ||
private static final Gson gson = new GsonBuilder().create(); | ||
private static final Persist.StringTimeout store = | ||
new Persist.StringTimeout("AAPS_DEVICE_STATUS", Constants.MINUTE_IN_MS * 21); | ||
private static volatile NSDeviceStatus last; | ||
|
||
// process and store received json in to object and maintain persistent time limited cache | ||
public static void processDeviceStatus(final String json) { | ||
synchronized (AAPSStatusHandler.class) { | ||
try { | ||
last = gson.fromJson(json, NSDeviceStatus.class); | ||
Log.d(TAG, "DEBUG: got device status: " + last.toString()); | ||
if (last != null) { | ||
store.set(json); | ||
val pump = last.getPump(); | ||
if (pump != null) { | ||
val r = pump.getReservoir(); | ||
if (r != null) { | ||
PumpStatus.setReservoir(r); | ||
} | ||
val b = pump.getBattery(); | ||
if (b != null) { | ||
val pc = b.getPercent(); | ||
if (pc != null) { | ||
PumpStatus.setBattery(pc); | ||
} | ||
} | ||
} | ||
} | ||
} catch (Exception e) { | ||
Log.e(TAG, "Error processing device status: " + e); | ||
} | ||
} | ||
} | ||
|
||
// get instance either from cache or persistent store if still valid | ||
public static NSDeviceStatus get() { | ||
synchronized (AAPSStatusHandler.class) { | ||
val json = store.get(); // local copy | ||
if (json != null) { | ||
if (last == null) { | ||
// needs reconstructing | ||
try { | ||
last = gson.fromJson(json, NSDeviceStatus.class); | ||
} catch (Exception e) { | ||
Log.wtf(TAG, "Unusual problem reconstructing device status: " + e); | ||
} | ||
} | ||
return last; | ||
} | ||
return null; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
app/src/test/java/com/eveningoutpost/dexdrip/alert/PersistTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.eveningoutpost.dexdrip.alert; | ||
|
||
|
||
import com.eveningoutpost.dexdrip.RobolectricTestWithConfig; | ||
import com.eveningoutpost.dexdrip.models.JoH; | ||
import com.eveningoutpost.dexdrip.utilitymodels.Constants; | ||
import com.eveningoutpost.dexdrip.utilitymodels.PersistentStore; | ||
|
||
import static com.google.common.truth.Truth.assertWithMessage; | ||
|
||
import org.junit.Test; | ||
import org.robolectric.annotation.Config; | ||
import org.robolectric.shadows.ShadowSystemClock; | ||
|
||
import java.time.Duration; | ||
|
||
import lombok.val; | ||
|
||
@Config(instrumentedPackages = {"com.eveningoutpost.dexdrip.models.JoH"}) | ||
public class PersistTest extends RobolectricTestWithConfig { | ||
|
||
@Test | ||
public void testTimeoutString() { | ||
|
||
val PREF_NAME = "TEST_TIMEOUT_STRING"; | ||
val testString = "Hello world"; | ||
|
||
// setup | ||
PersistentStore.removeItem(PREF_NAME); | ||
ShadowSystemClock.advanceBy(Duration.ofHours(100)); | ||
|
||
val store = | ||
new Persist.StringTimeout(PREF_NAME, Constants.MINUTE_IN_MS * 21); | ||
|
||
assertWithMessage("Time not zero").that(JoH.tsl()).isGreaterThan(Constants.HOUR_IN_MS); | ||
assertWithMessage("test empty null").that(store.get()).isNull(); | ||
|
||
store.set(testString); | ||
assertWithMessage("test ok 1").that(store.get()).isEqualTo(testString); | ||
ShadowSystemClock.advanceBy(Duration.ofMinutes(1)); | ||
assertWithMessage("test ok 2").that(store.get()).isEqualTo(testString); | ||
ShadowSystemClock.advanceBy(Duration.ofMinutes(10)); | ||
assertWithMessage("test ok 3").that(store.get()).isEqualTo(testString); | ||
ShadowSystemClock.advanceBy(Duration.ofMinutes(11)); | ||
assertWithMessage("test expired 4").that(store.get()).isNull(); | ||
ShadowSystemClock.advanceBy(Duration.ofMinutes(11)); | ||
assertWithMessage("test expired 5").that(store.get()).isNull(); | ||
store.set(testString); | ||
ShadowSystemClock.advanceBy(Duration.ofMinutes(10)); | ||
assertWithMessage("test ok 4").that(store.get()).isEqualTo(testString); | ||
} | ||
|
||
} |