diff --git a/insight/src/main/java/info/nightscout/androidaps/insight/database/Converters.kt b/insight/src/main/java/info/nightscout/androidaps/insight/database/Converters.kt new file mode 100644 index 0000000000..76a60cd1e6 --- /dev/null +++ b/insight/src/main/java/info/nightscout/androidaps/insight/database/Converters.kt @@ -0,0 +1,11 @@ +package info.nightscout.androidaps.insight.database + +import androidx.room.TypeConverter + +class Converters { + @TypeConverter + fun fromEventType(evenType: InsightPumpID.EventType) = evenType.name + + @TypeConverter + fun toEventType(evenType: String?) = evenType?.let { InsightPumpID.EventType.valueOf(it) } +} \ No newline at end of file diff --git a/insight/src/main/java/info/nightscout/androidaps/insight/database/InsightBolusID.kt b/insight/src/main/java/info/nightscout/androidaps/insight/database/InsightBolusID.kt index 4546409145..99b6055c7f 100644 --- a/insight/src/main/java/info/nightscout/androidaps/insight/database/InsightBolusID.kt +++ b/insight/src/main/java/info/nightscout/androidaps/insight/database/InsightBolusID.kt @@ -8,8 +8,8 @@ import androidx.room.PrimaryKey indices = [Index("bolusID")]) data class InsightBolusID( var timestamp: Long, - var pumpSerial: String? = "None", - var bolusID: Int? = null, + val pumpSerial: String? = null, + val bolusID: Int? = null, var startID: Long? = null, var endID: Long? = null ) { diff --git a/insight/src/main/java/info/nightscout/androidaps/insight/database/InsightDatabase.kt b/insight/src/main/java/info/nightscout/androidaps/insight/database/InsightDatabase.kt index e7e45423a4..7be27eae76 100644 --- a/insight/src/main/java/info/nightscout/androidaps/insight/database/InsightDatabase.kt +++ b/insight/src/main/java/info/nightscout/androidaps/insight/database/InsightDatabase.kt @@ -4,6 +4,7 @@ import android.content.Context import androidx.room.Database import androidx.room.Room import androidx.room.RoomDatabase +import androidx.room.TypeConverters const val DATABASE_INSIGHT_BOLUS_IDS = "insightBolusIDs" const val DATABASE_INSIGHT_PUMP_IDS = "insightPumpIDs" @@ -14,6 +15,7 @@ const val DATABASE_INSIGHT_HISTORY_OFFSETS = "insightHistoryOffsets" exportSchema = true, version = InsightDatabase.VERSION ) +@TypeConverters(Converters::class) abstract class InsightDatabase : RoomDatabase() { abstract fun insightDatabaseDao(): InsightDatabaseDao diff --git a/insight/src/main/java/info/nightscout/androidaps/insight/database/InsightDatabaseDao.kt b/insight/src/main/java/info/nightscout/androidaps/insight/database/InsightDatabaseDao.kt index 7bffe694ef..e602b3bf77 100644 --- a/insight/src/main/java/info/nightscout/androidaps/insight/database/InsightDatabaseDao.kt +++ b/insight/src/main/java/info/nightscout/androidaps/insight/database/InsightDatabaseDao.kt @@ -4,6 +4,7 @@ import androidx.room.Dao import androidx.room.Insert import androidx.room.OnConflictStrategy import androidx.room.Query +import info.nightscout.androidaps.insight.database.InsightPumpID.EventType @Dao abstract class InsightDatabaseDao { @@ -20,8 +21,10 @@ abstract class InsightDatabaseDao { @Insert(onConflict = OnConflictStrategy.REPLACE) abstract fun createOrUpdate(insightHistoryOffset: InsightHistoryOffset) - @Query("SELECT * from $DATABASE_INSIGHT_PUMP_IDS WHERE pumpSerial = :pumpSerial AND (eventType = 'PumpStopped' OR eventType = 'PumpPaused') AND timestamp < :timestamp ORDER BY timestamp DESC") - abstract fun getPumpStoppedEvent(pumpSerial: String, timestamp: Long): InsightPumpID? + @Query("SELECT * from $DATABASE_INSIGHT_PUMP_IDS WHERE pumpSerial = :pumpSerial AND (eventType = :PumpStopped OR eventType = :PumpPaused) AND timestamp < :timestamp ORDER BY timestamp DESC") + abstract fun getPumpStoppedEvent(pumpSerial: String, timestamp: Long, PumpStopped: EventType, PumpPaused: EventType): InsightPumpID? + + fun getPumpStoppedEvent(pumpSerial: String, timestamp: Long): InsightPumpID? = getPumpStoppedEvent(pumpSerial, timestamp, EventType.PumpStopped, EventType.PumpPaused) @Insert(onConflict = OnConflictStrategy.REPLACE) abstract fun createOrUpdate(insightPumpID: InsightPumpID) diff --git a/insight/src/main/java/info/nightscout/androidaps/insight/database/InsightPumpID.kt b/insight/src/main/java/info/nightscout/androidaps/insight/database/InsightPumpID.kt index 901c04a2b8..71e5b3502e 100644 --- a/insight/src/main/java/info/nightscout/androidaps/insight/database/InsightPumpID.kt +++ b/insight/src/main/java/info/nightscout/androidaps/insight/database/InsightPumpID.kt @@ -8,10 +8,17 @@ import androidx.room.PrimaryKey indices = [Index("timestamp")]) data class InsightPumpID( var timestamp: Long, - var eventType: String? = null, - var pumpSerial: String? = "None", + var eventType: EventType = EventType.None, + val pumpSerial: String? = null, @PrimaryKey var eventID: Long ) { - fun getId(): Long = eventID + enum class EventType { + PumpStarted, + PumpStopped, + PumpPaused, + StartOfTBR, + EndOfTBR, + None; + } } \ No newline at end of file diff --git a/insight/src/main/java/info/nightscout/androidaps/plugins/pump/insight/LocalInsightPlugin.java b/insight/src/main/java/info/nightscout/androidaps/plugins/pump/insight/LocalInsightPlugin.java index f6f80dcb4c..f9f4fa676e 100644 --- a/insight/src/main/java/info/nightscout/androidaps/plugins/pump/insight/LocalInsightPlugin.java +++ b/insight/src/main/java/info/nightscout/androidaps/plugins/pump/insight/LocalInsightPlugin.java @@ -39,6 +39,7 @@ import info.nightscout.androidaps.insight.database.InsightBolusID; import info.nightscout.androidaps.insight.database.InsightDatabaseDao; import info.nightscout.androidaps.insight.database.InsightHistoryOffset; import info.nightscout.androidaps.insight.database.InsightPumpID; +import info.nightscout.androidaps.insight.database.InsightPumpID.EventType; import info.nightscout.androidaps.insight.R; import info.nightscout.androidaps.interfaces.CommandQueueProvider; import info.nightscout.androidaps.interfaces.Config; @@ -1198,11 +1199,11 @@ public class LocalInsightPlugin extends PumpPluginBase implements Pump, Constrai for (InsightPumpID pumpID : pumpStartedEvents) { InsightPumpID stoppedEvent = insightDatabaseDao.getPumpStoppedEvent(pumpID.getPumpSerial(), pumpID.getTimestamp()); - if (stoppedEvent != null && stoppedEvent.getEventType().equals("PumpStopped")) { // Search if Stop event is after 15min of Pause + if (stoppedEvent != null && stoppedEvent.getEventType().equals(EventType.PumpStopped)) { // Search if Stop event is after 15min of Pause InsightPumpID pauseEvent = insightDatabaseDao.getPumpStoppedEvent(pumpID.getPumpSerial(), stoppedEvent.getTimestamp() - T.mins(1).msecs()); - if (pauseEvent != null && pauseEvent.getEventType().equals("PumpPaused") && (stoppedEvent.getTimestamp() - pauseEvent.getTimestamp() < T.mins(16).msecs())) { + if (pauseEvent != null && pauseEvent.getEventType().equals(EventType.PumpPaused) && (stoppedEvent.getTimestamp() - pauseEvent.getTimestamp() < T.mins(16).msecs())) { stoppedEvent = pauseEvent; - stoppedEvent.setEventType("PumpStopped"); + stoppedEvent.setEventType(EventType.PumpStopped); } } if (stoppedEvent == null || stoppedEvent.getEventType().equals("PumpPaused")) continue; @@ -1213,7 +1214,7 @@ public class LocalInsightPlugin extends PumpPluginBase implements Pump, Constrai 0, false, PumpSync.TemporaryBasalType.NORMAL, - pumpID.getId(), + pumpID.getEventID(), pumpID.getEventID()); temporaryBasals.add(temporaryBasal); } @@ -1355,25 +1356,25 @@ public class LocalInsightPlugin extends PumpPluginBase implements Pump, Constrai event.getEventHour(), event.getEventMinute(), event.getEventSecond()) + timeOffset; InsightPumpID pumpID = new InsightPumpID( timestamp, - "", + EventType.None, serial, event.getEventPosition()); switch (event.getNewValue()) { case STARTED: - pumpID.setEventType("PumpStarted"); + pumpID.setEventType(EventType.PumpStarted); pumpStartedEvents.add(pumpID); if (sp.getBoolean("insight_log_operating_mode_changes", false)) logNote(timestamp, resourceHelper.gs(R.string.pump_started)); aapsLogger.debug(LTag.PUMP, "XXXX event START Event TimeStamp: " + timestamp + " HMS: " + dateUtil.dateAndTimeAndSecondsString(timestamp)); break; case STOPPED: - pumpID.setEventType("PumpStopped"); + pumpID.setEventType(EventType.PumpStopped); if (sp.getBoolean("insight_log_operating_mode_changes", false)) logNote(timestamp, resourceHelper.gs(R.string.pump_stopped)); aapsLogger.debug(LTag.PUMP, "XXXX event STOP: " + timestamp + " HMS: " + dateUtil.dateAndTimeAndSecondsString(timestamp)); break; case PAUSED: - pumpID.setEventType("PumpPaused"); + pumpID.setEventType(EventType.PumpPaused); if (sp.getBoolean("insight_log_operating_mode_changes", false)) logNote(timestamp, resourceHelper.gs(R.string.pump_paused)); aapsLogger.debug(LTag.PUMP, "XXXX event Pause: " + timestamp + " HMS: " + dateUtil.dateAndTimeAndSecondsString(timestamp)); @@ -1387,19 +1388,19 @@ public class LocalInsightPlugin extends PumpPluginBase implements Pump, Constrai event.getEventHour(), event.getEventMinute(), event.getEventSecond()) + timeOffset; insightDatabaseDao.createOrUpdate(new InsightPumpID( timestamp, - "StartOfTBR", + EventType.StartOfTBR, serial, event.getEventPosition()) ); - TemporaryBasal temporaryBasal = new TemporaryBasal( + temporaryBasals.add(new TemporaryBasal( timestamp, T.mins(event.getDuration()).msecs(), event.getAmount(), false, PumpSync.TemporaryBasalType.NORMAL, event.getEventPosition(), - event.getEventPosition()); - temporaryBasals.add(temporaryBasal); + event.getEventPosition()) + ); } private void processEndOfTBREvent(String serial, List temporaryBasals, EndOfTBREvent event) { @@ -1407,27 +1408,27 @@ public class LocalInsightPlugin extends PumpPluginBase implements Pump, Constrai event.getEventHour(), event.getEventMinute(), event.getEventSecond()) + timeOffset; insightDatabaseDao.createOrUpdate(new InsightPumpID( timestamp - 1500L, - "EndOfTBR", + EventType.EndOfTBR, serial, event.getEventPosition()) ); - TemporaryBasal temporaryBasal = new PumpSync.PumpState.TemporaryBasal( + temporaryBasals.add(new PumpSync.PumpState.TemporaryBasal( timestamp - 1500L, 0L, 100.0, false, PumpSync.TemporaryBasalType.NORMAL, event.getEventPosition(), - event.getEventPosition()); - temporaryBasals.add(temporaryBasal); + event.getEventPosition()) + ); } private void processBolusProgrammedEvent(String serial, BolusProgrammedEvent event) { long timestamp = parseDate(event.getEventYear(), event.getEventMonth(), event.getEventDay(), event.getEventHour(), event.getEventMinute(), event.getEventSecond()) + timeOffset; InsightBolusID bolusID = insightDatabaseDao.getInsightBolusID(serial, event.getBolusID(), timestamp); - if (bolusID != null && bolusID.getEndID() != null) { // TODO() Check if test EndID is necessary + if (bolusID != null && bolusID.getEndID() != null) { bolusID.setStartID(event.getEventPosition()); insightDatabaseDao.createOrUpdate(bolusID); return; @@ -1485,7 +1486,6 @@ public class LocalInsightPlugin extends PumpPluginBase implements Pump, Constrai bolusID.setEndID(event.getEventPosition()); insightDatabaseDao.createOrUpdate(bolusID); bolusID = insightDatabaseDao.getInsightBolusID(serial, event.getBolusID(), startTimestamp); // Line added to get id - //databaseHelper.createOrUpdate(bolusID); if (event.getBolusType() == BolusType.STANDARD || event.getBolusType() == BolusType.MULTIWAVE) { pumpSync.syncBolusWithPumpId( bolusID.getTimestamp(),