Replace hard coded strings for EventType by enum

This commit is contained in:
Philoul 2021-05-07 23:54:02 +02:00
parent dfd6c6e859
commit 0ea9c98469
6 changed files with 48 additions and 25 deletions

View file

@ -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) }
}

View file

@ -8,8 +8,8 @@ import androidx.room.PrimaryKey
indices = [Index("bolusID")]) indices = [Index("bolusID")])
data class InsightBolusID( data class InsightBolusID(
var timestamp: Long, var timestamp: Long,
var pumpSerial: String? = "None", val pumpSerial: String? = null,
var bolusID: Int? = null, val bolusID: Int? = null,
var startID: Long? = null, var startID: Long? = null,
var endID: Long? = null var endID: Long? = null
) { ) {

View file

@ -4,6 +4,7 @@ import android.content.Context
import androidx.room.Database import androidx.room.Database
import androidx.room.Room import androidx.room.Room
import androidx.room.RoomDatabase import androidx.room.RoomDatabase
import androidx.room.TypeConverters
const val DATABASE_INSIGHT_BOLUS_IDS = "insightBolusIDs" const val DATABASE_INSIGHT_BOLUS_IDS = "insightBolusIDs"
const val DATABASE_INSIGHT_PUMP_IDS = "insightPumpIDs" const val DATABASE_INSIGHT_PUMP_IDS = "insightPumpIDs"
@ -14,6 +15,7 @@ const val DATABASE_INSIGHT_HISTORY_OFFSETS = "insightHistoryOffsets"
exportSchema = true, exportSchema = true,
version = InsightDatabase.VERSION version = InsightDatabase.VERSION
) )
@TypeConverters(Converters::class)
abstract class InsightDatabase : RoomDatabase() { abstract class InsightDatabase : RoomDatabase() {
abstract fun insightDatabaseDao(): InsightDatabaseDao abstract fun insightDatabaseDao(): InsightDatabaseDao

View file

@ -4,6 +4,7 @@ import androidx.room.Dao
import androidx.room.Insert import androidx.room.Insert
import androidx.room.OnConflictStrategy import androidx.room.OnConflictStrategy
import androidx.room.Query import androidx.room.Query
import info.nightscout.androidaps.insight.database.InsightPumpID.EventType
@Dao @Dao
abstract class InsightDatabaseDao { abstract class InsightDatabaseDao {
@ -20,8 +21,10 @@ abstract class InsightDatabaseDao {
@Insert(onConflict = OnConflictStrategy.REPLACE) @Insert(onConflict = OnConflictStrategy.REPLACE)
abstract fun createOrUpdate(insightHistoryOffset: InsightHistoryOffset) 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") @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? 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) @Insert(onConflict = OnConflictStrategy.REPLACE)
abstract fun createOrUpdate(insightPumpID: InsightPumpID) abstract fun createOrUpdate(insightPumpID: InsightPumpID)

View file

@ -8,10 +8,17 @@ import androidx.room.PrimaryKey
indices = [Index("timestamp")]) indices = [Index("timestamp")])
data class InsightPumpID( data class InsightPumpID(
var timestamp: Long, var timestamp: Long,
var eventType: String? = null, var eventType: EventType = EventType.None,
var pumpSerial: String? = "None", val pumpSerial: String? = null,
@PrimaryKey @PrimaryKey
var eventID: Long var eventID: Long
) { ) {
fun getId(): Long = eventID enum class EventType {
PumpStarted,
PumpStopped,
PumpPaused,
StartOfTBR,
EndOfTBR,
None;
}
} }

View file

@ -39,6 +39,7 @@ import info.nightscout.androidaps.insight.database.InsightBolusID;
import info.nightscout.androidaps.insight.database.InsightDatabaseDao; import info.nightscout.androidaps.insight.database.InsightDatabaseDao;
import info.nightscout.androidaps.insight.database.InsightHistoryOffset; import info.nightscout.androidaps.insight.database.InsightHistoryOffset;
import info.nightscout.androidaps.insight.database.InsightPumpID; 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.insight.R;
import info.nightscout.androidaps.interfaces.CommandQueueProvider; import info.nightscout.androidaps.interfaces.CommandQueueProvider;
import info.nightscout.androidaps.interfaces.Config; import info.nightscout.androidaps.interfaces.Config;
@ -1198,11 +1199,11 @@ public class LocalInsightPlugin extends PumpPluginBase implements Pump, Constrai
for (InsightPumpID pumpID : pumpStartedEvents) { for (InsightPumpID pumpID : pumpStartedEvents) {
InsightPumpID stoppedEvent = insightDatabaseDao.getPumpStoppedEvent(pumpID.getPumpSerial(), pumpID.getTimestamp()); 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()); 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 = pauseEvent;
stoppedEvent.setEventType("PumpStopped"); stoppedEvent.setEventType(EventType.PumpStopped);
} }
} }
if (stoppedEvent == null || stoppedEvent.getEventType().equals("PumpPaused")) continue; if (stoppedEvent == null || stoppedEvent.getEventType().equals("PumpPaused")) continue;
@ -1213,7 +1214,7 @@ public class LocalInsightPlugin extends PumpPluginBase implements Pump, Constrai
0, 0,
false, false,
PumpSync.TemporaryBasalType.NORMAL, PumpSync.TemporaryBasalType.NORMAL,
pumpID.getId(), pumpID.getEventID(),
pumpID.getEventID()); pumpID.getEventID());
temporaryBasals.add(temporaryBasal); temporaryBasals.add(temporaryBasal);
} }
@ -1355,25 +1356,25 @@ public class LocalInsightPlugin extends PumpPluginBase implements Pump, Constrai
event.getEventHour(), event.getEventMinute(), event.getEventSecond()) + timeOffset; event.getEventHour(), event.getEventMinute(), event.getEventSecond()) + timeOffset;
InsightPumpID pumpID = new InsightPumpID( InsightPumpID pumpID = new InsightPumpID(
timestamp, timestamp,
"", EventType.None,
serial, serial,
event.getEventPosition()); event.getEventPosition());
switch (event.getNewValue()) { switch (event.getNewValue()) {
case STARTED: case STARTED:
pumpID.setEventType("PumpStarted"); pumpID.setEventType(EventType.PumpStarted);
pumpStartedEvents.add(pumpID); pumpStartedEvents.add(pumpID);
if (sp.getBoolean("insight_log_operating_mode_changes", false)) if (sp.getBoolean("insight_log_operating_mode_changes", false))
logNote(timestamp, resourceHelper.gs(R.string.pump_started)); logNote(timestamp, resourceHelper.gs(R.string.pump_started));
aapsLogger.debug(LTag.PUMP, "XXXX event START Event TimeStamp: " + timestamp + " HMS: " + dateUtil.dateAndTimeAndSecondsString(timestamp)); aapsLogger.debug(LTag.PUMP, "XXXX event START Event TimeStamp: " + timestamp + " HMS: " + dateUtil.dateAndTimeAndSecondsString(timestamp));
break; break;
case STOPPED: case STOPPED:
pumpID.setEventType("PumpStopped"); pumpID.setEventType(EventType.PumpStopped);
if (sp.getBoolean("insight_log_operating_mode_changes", false)) if (sp.getBoolean("insight_log_operating_mode_changes", false))
logNote(timestamp, resourceHelper.gs(R.string.pump_stopped)); logNote(timestamp, resourceHelper.gs(R.string.pump_stopped));
aapsLogger.debug(LTag.PUMP, "XXXX event STOP: " + timestamp + " HMS: " + dateUtil.dateAndTimeAndSecondsString(timestamp)); aapsLogger.debug(LTag.PUMP, "XXXX event STOP: " + timestamp + " HMS: " + dateUtil.dateAndTimeAndSecondsString(timestamp));
break; break;
case PAUSED: case PAUSED:
pumpID.setEventType("PumpPaused"); pumpID.setEventType(EventType.PumpPaused);
if (sp.getBoolean("insight_log_operating_mode_changes", false)) if (sp.getBoolean("insight_log_operating_mode_changes", false))
logNote(timestamp, resourceHelper.gs(R.string.pump_paused)); logNote(timestamp, resourceHelper.gs(R.string.pump_paused));
aapsLogger.debug(LTag.PUMP, "XXXX event Pause: " + timestamp + " HMS: " + dateUtil.dateAndTimeAndSecondsString(timestamp)); 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; event.getEventHour(), event.getEventMinute(), event.getEventSecond()) + timeOffset;
insightDatabaseDao.createOrUpdate(new InsightPumpID( insightDatabaseDao.createOrUpdate(new InsightPumpID(
timestamp, timestamp,
"StartOfTBR", EventType.StartOfTBR,
serial, serial,
event.getEventPosition()) event.getEventPosition())
); );
TemporaryBasal temporaryBasal = new TemporaryBasal( temporaryBasals.add(new TemporaryBasal(
timestamp, timestamp,
T.mins(event.getDuration()).msecs(), T.mins(event.getDuration()).msecs(),
event.getAmount(), event.getAmount(),
false, false,
PumpSync.TemporaryBasalType.NORMAL, PumpSync.TemporaryBasalType.NORMAL,
event.getEventPosition(), event.getEventPosition(),
event.getEventPosition()); event.getEventPosition())
temporaryBasals.add(temporaryBasal); );
} }
private void processEndOfTBREvent(String serial, List<TemporaryBasal> temporaryBasals, EndOfTBREvent event) { private void processEndOfTBREvent(String serial, List<TemporaryBasal> temporaryBasals, EndOfTBREvent event) {
@ -1407,27 +1408,27 @@ public class LocalInsightPlugin extends PumpPluginBase implements Pump, Constrai
event.getEventHour(), event.getEventMinute(), event.getEventSecond()) + timeOffset; event.getEventHour(), event.getEventMinute(), event.getEventSecond()) + timeOffset;
insightDatabaseDao.createOrUpdate(new InsightPumpID( insightDatabaseDao.createOrUpdate(new InsightPumpID(
timestamp - 1500L, timestamp - 1500L,
"EndOfTBR", EventType.EndOfTBR,
serial, serial,
event.getEventPosition()) event.getEventPosition())
); );
TemporaryBasal temporaryBasal = new PumpSync.PumpState.TemporaryBasal( temporaryBasals.add(new PumpSync.PumpState.TemporaryBasal(
timestamp - 1500L, timestamp - 1500L,
0L, 0L,
100.0, 100.0,
false, false,
PumpSync.TemporaryBasalType.NORMAL, PumpSync.TemporaryBasalType.NORMAL,
event.getEventPosition(), event.getEventPosition(),
event.getEventPosition()); event.getEventPosition())
temporaryBasals.add(temporaryBasal); );
} }
private void processBolusProgrammedEvent(String serial, BolusProgrammedEvent event) { private void processBolusProgrammedEvent(String serial, BolusProgrammedEvent event) {
long timestamp = parseDate(event.getEventYear(), event.getEventMonth(), event.getEventDay(), long timestamp = parseDate(event.getEventYear(), event.getEventMonth(), event.getEventDay(),
event.getEventHour(), event.getEventMinute(), event.getEventSecond()) + timeOffset; event.getEventHour(), event.getEventMinute(), event.getEventSecond()) + timeOffset;
InsightBolusID bolusID = insightDatabaseDao.getInsightBolusID(serial, event.getBolusID(), timestamp); 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()); bolusID.setStartID(event.getEventPosition());
insightDatabaseDao.createOrUpdate(bolusID); insightDatabaseDao.createOrUpdate(bolusID);
return; return;
@ -1485,7 +1486,6 @@ public class LocalInsightPlugin extends PumpPluginBase implements Pump, Constrai
bolusID.setEndID(event.getEventPosition()); bolusID.setEndID(event.getEventPosition());
insightDatabaseDao.createOrUpdate(bolusID); insightDatabaseDao.createOrUpdate(bolusID);
bolusID = insightDatabaseDao.getInsightBolusID(serial, event.getBolusID(), startTimestamp); // Line added to get id bolusID = insightDatabaseDao.getInsightBolusID(serial, event.getBolusID(), startTimestamp); // Line added to get id
//databaseHelper.createOrUpdate(bolusID);
if (event.getBolusType() == BolusType.STANDARD || event.getBolusType() == BolusType.MULTIWAVE) { if (event.getBolusType() == BolusType.STANDARD || event.getBolusType() == BolusType.MULTIWAVE) {
pumpSync.syncBolusWithPumpId( pumpSync.syncBolusWithPumpId(
bolusID.getTimestamp(), bolusID.getTimestamp(),