diff --git a/app/src/main/java/info/nightscout/androidaps/dependencyInjection/AppModule.kt b/app/src/main/java/info/nightscout/androidaps/dependencyInjection/AppModule.kt index ed55beb8e7..d7e1ad3073 100644 --- a/app/src/main/java/info/nightscout/androidaps/dependencyInjection/AppModule.kt +++ b/app/src/main/java/info/nightscout/androidaps/dependencyInjection/AppModule.kt @@ -7,6 +7,7 @@ import dagger.Module import dagger.Provides import dagger.android.HasAndroidInjector import info.nightscout.androidaps.MainApp +import info.nightscout.androidaps.database.AppRepository import info.nightscout.androidaps.db.DatabaseHelperProvider import info.nightscout.androidaps.interfaces.* import info.nightscout.androidaps.logging.AAPSLogger @@ -14,6 +15,7 @@ import info.nightscout.androidaps.plugins.aps.loop.LoopPlugin import info.nightscout.androidaps.plugins.bus.RxBusWrapper import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin import info.nightscout.androidaps.plugins.configBuilder.PluginStore +import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctionImplementation import info.nightscout.androidaps.plugins.general.maintenance.ImportExportPrefsImpl import info.nightscout.androidaps.plugins.general.nsclient.DataSyncSelectorImplementation import info.nightscout.androidaps.plugins.general.nsclient.UploadQueue @@ -22,9 +24,11 @@ import info.nightscout.androidaps.plugins.iob.iobCobCalculator.IobCobCalculatorP import info.nightscout.androidaps.plugins.pump.PumpSyncImplementation import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin import info.nightscout.androidaps.queue.CommandQueue +import info.nightscout.androidaps.utils.DateUtil import info.nightscout.androidaps.utils.androidNotification.NotificationHolderImpl import info.nightscout.androidaps.utils.buildHelper.ConfigImpl import info.nightscout.androidaps.utils.resources.IconsProviderImplementation +import info.nightscout.androidaps.utils.resources.ResourceHelper import info.nightscout.androidaps.utils.rx.AapsSchedulers import info.nightscout.androidaps.utils.rx.DefaultAapsSchedulers import info.nightscout.androidaps.utils.sharedPreferences.SP @@ -72,6 +76,12 @@ open class AppModule { rxBus: RxBusWrapper ): UploadQueueInterface = UploadQueue(aapsLogger, databaseHelper, context, sp, rxBus) + @Provides + @Singleton + fun provideProfileFunction(aapsLogger: AAPSLogger, sp: SP, resourceHelper: ResourceHelper, activePlugin: ActivePlugin, repository: AppRepository, dateUtil: DateUtil): ProfileFunction { + return ProfileFunctionImplementation(aapsLogger, sp, resourceHelper, activePlugin, repository, dateUtil) + } + @Module interface AppBindings { @Binds fun bindContext(mainApp: MainApp): Context diff --git a/core/src/main/java/info/nightscout/androidaps/plugins/configBuilder/ProfileFunctionImplementation.kt b/app/src/main/java/info/nightscout/androidaps/plugins/configBuilder/ProfileFunctionImplementation.kt similarity index 100% rename from core/src/main/java/info/nightscout/androidaps/plugins/configBuilder/ProfileFunctionImplementation.kt rename to app/src/main/java/info/nightscout/androidaps/plugins/configBuilder/ProfileFunctionImplementation.kt diff --git a/core/src/main/java/info/nightscout/androidaps/di/CoreDataClassesModule.kt b/core/src/main/java/info/nightscout/androidaps/di/CoreDataClassesModule.kt index 0c37e3e32c..0343a3791c 100644 --- a/core/src/main/java/info/nightscout/androidaps/di/CoreDataClassesModule.kt +++ b/core/src/main/java/info/nightscout/androidaps/di/CoreDataClassesModule.kt @@ -6,15 +6,12 @@ import info.nightscout.androidaps.data.ProfileImplOld import info.nightscout.androidaps.data.PumpEnactResult import info.nightscout.androidaps.interfaces.ProfileStore import info.nightscout.androidaps.plugins.aps.loop.APSResult -import info.nightscout.androidaps.plugins.general.nsclient.data.NSMbg import info.nightscout.androidaps.plugins.iob.iobCobCalculator.data.AutosensData @Module @Suppress("unused") abstract class CoreDataClassesModule { - @ContributesAndroidInjector abstract fun nsMbgInjector(): NSMbg - @ContributesAndroidInjector abstract fun pumpEnactResultInjector(): PumpEnactResult @ContributesAndroidInjector abstract fun apsResultInjector(): APSResult @ContributesAndroidInjector abstract fun autosensDataInjector(): AutosensData diff --git a/core/src/main/java/info/nightscout/androidaps/di/CoreModule.kt b/core/src/main/java/info/nightscout/androidaps/di/CoreModule.kt index 423ae537e3..ec2b5bb1ad 100644 --- a/core/src/main/java/info/nightscout/androidaps/di/CoreModule.kt +++ b/core/src/main/java/info/nightscout/androidaps/di/CoreModule.kt @@ -4,14 +4,9 @@ import android.content.Context import android.preference.PreferenceManager import dagger.Module import dagger.Provides -import info.nightscout.androidaps.database.AppRepository -import info.nightscout.androidaps.interfaces.ActivePlugin -import info.nightscout.androidaps.interfaces.ProfileFunction import info.nightscout.androidaps.logging.AAPSLogger import info.nightscout.androidaps.logging.AAPSLoggerProduction import info.nightscout.androidaps.logging.L -import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctionImplementation -import info.nightscout.androidaps.utils.DateUtil import info.nightscout.androidaps.utils.resources.ResourceHelper import info.nightscout.androidaps.utils.resources.ResourceHelperImplementation import info.nightscout.androidaps.utils.sharedPreferences.SP @@ -25,12 +20,6 @@ import javax.inject.Singleton ]) open class CoreModule { - @Provides - @Singleton - fun provideProfileFunction(aapsLogger: AAPSLogger, sp: SP, resourceHelper: ResourceHelper, activePlugin: ActivePlugin, repository: AppRepository, dateUtil: DateUtil): ProfileFunction { - return ProfileFunctionImplementation(aapsLogger, sp, resourceHelper, activePlugin, repository, dateUtil) - } - @Provides @Singleton fun provideResources(context: Context): ResourceHelper = ResourceHelperImplementation(context) diff --git a/core/src/main/java/info/nightscout/androidaps/interfaces/PumpSync.kt b/core/src/main/java/info/nightscout/androidaps/interfaces/PumpSync.kt index b8d21730d3..ffc7c04d0a 100644 --- a/core/src/main/java/info/nightscout/androidaps/interfaces/PumpSync.kt +++ b/core/src/main/java/info/nightscout/androidaps/interfaces/PumpSync.kt @@ -344,6 +344,19 @@ interface PumpSync { **/ fun invalidateTemporaryBasal(id: Long): Boolean + /** + * Invalidate of temporary basals that failed to start + * MDT specific + * + * If exists, isValid is set false + * If db record doesn't exist data is ignored and false returned + * + * + * @param temporaryId temporary id of temporary basal + * @return true if running record is found and invalidated + **/ + fun invalidateTemporaryBasalWithTempId(temporaryId: Long): Boolean + /** * Synchronization of extended bolus * diff --git a/core/src/main/java/info/nightscout/androidaps/plugins/pump/PumpSyncImplementation.kt b/core/src/main/java/info/nightscout/androidaps/plugins/pump/PumpSyncImplementation.kt index 0c995a3a44..b313af90ef 100644 --- a/core/src/main/java/info/nightscout/androidaps/plugins/pump/PumpSyncImplementation.kt +++ b/core/src/main/java/info/nightscout/androidaps/plugins/pump/PumpSyncImplementation.kt @@ -330,6 +330,18 @@ class PumpSyncImplementation @Inject constructor( } } + override fun invalidateTemporaryBasalWithTempId(temporaryId: Long): Boolean { + repository.runTransactionForResult(InvalidateTemporaryBasalWithTempIdTransaction(temporaryId)) + .doOnError { aapsLogger.error(LTag.DATABASE, "Error while invalidating TemporaryBasal", it) } + .blockingGet() + .also { result -> + result.invalidated.forEach { + aapsLogger.debug(LTag.DATABASE, "Invalidated TemporaryBasal $it") + } + return result.invalidated.size > 0 + } + } + override fun syncExtendedBolusWithPumpId(timestamp: Long, amount: Double, duration: Long, isEmulatingTB: Boolean, pumpId: Long, pumpType: PumpType, pumpSerial: String): Boolean { if (!confirmActivePump(timestamp, pumpType, pumpSerial)) return false val extendedBolus = ExtendedBolus( diff --git a/dana/build.gradle b/dana/build.gradle index e36ef61f0d..14391efbea 100644 --- a/dana/build.gradle +++ b/dana/build.gradle @@ -12,6 +12,12 @@ android { defaultConfig { versionCode 1 versionName "1.0" + kapt { + arguments { + arg("room.incremental", "true") + arg("room.schemaLocation", "$projectDir/schemas") + } + } } } diff --git a/dana/schemas/info.nightscout.androidaps.dana.database.DanaHistoryDatabase/1.json b/dana/schemas/info.nightscout.androidaps.dana.database.DanaHistoryDatabase/1.json new file mode 100644 index 0000000000..07a894d1aa --- /dev/null +++ b/dana/schemas/info.nightscout.androidaps.dana.database.DanaHistoryDatabase/1.json @@ -0,0 +1,92 @@ +{ + "formatVersion": 1, + "database": { + "version": 1, + "identityHash": "04f2e440fae2d62ca4e4a38840b5cf2f", + "entities": [ + { + "tableName": "danaHistory", + "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`timestamp` INTEGER NOT NULL, `code` INTEGER NOT NULL, `value` REAL NOT NULL, `bolusType` TEXT NOT NULL, `stringValue` TEXT NOT NULL, `duration` INTEGER NOT NULL, `dailyBasal` REAL NOT NULL, `dailyBolus` REAL NOT NULL, `alarm` TEXT NOT NULL, PRIMARY KEY(`timestamp`))", + "fields": [ + { + "fieldPath": "timestamp", + "columnName": "timestamp", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "code", + "columnName": "code", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "value", + "columnName": "value", + "affinity": "REAL", + "notNull": true + }, + { + "fieldPath": "bolusType", + "columnName": "bolusType", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "stringValue", + "columnName": "stringValue", + "affinity": "TEXT", + "notNull": true + }, + { + "fieldPath": "duration", + "columnName": "duration", + "affinity": "INTEGER", + "notNull": true + }, + { + "fieldPath": "dailyBasal", + "columnName": "dailyBasal", + "affinity": "REAL", + "notNull": true + }, + { + "fieldPath": "dailyBolus", + "columnName": "dailyBolus", + "affinity": "REAL", + "notNull": true + }, + { + "fieldPath": "alarm", + "columnName": "alarm", + "affinity": "TEXT", + "notNull": true + } + ], + "primaryKey": { + "columnNames": [ + "timestamp" + ], + "autoGenerate": false + }, + "indices": [ + { + "name": "index_danaHistory_code_timestamp", + "unique": false, + "columnNames": [ + "code", + "timestamp" + ], + "createSql": "CREATE INDEX IF NOT EXISTS `index_danaHistory_code_timestamp` ON `${TABLE_NAME}` (`code`, `timestamp`)" + } + ], + "foreignKeys": [] + } + ], + "views": [], + "setupQueries": [ + "CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)", + "INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '04f2e440fae2d62ca4e4a38840b5cf2f')" + ] + } +} \ No newline at end of file diff --git a/database/src/main/java/info/nightscout/androidaps/database/daos/TemporaryBasalDao.kt b/database/src/main/java/info/nightscout/androidaps/database/daos/TemporaryBasalDao.kt index d03cc6d91d..a6c46075c5 100644 --- a/database/src/main/java/info/nightscout/androidaps/database/daos/TemporaryBasalDao.kt +++ b/database/src/main/java/info/nightscout/androidaps/database/daos/TemporaryBasalDao.kt @@ -18,6 +18,9 @@ internal interface TemporaryBasalDao : TraceableDao { @Query("DELETE FROM $TABLE_TEMPORARY_BASALS") override fun deleteAllEntries() + @Query("SELECT * FROM $TABLE_TEMPORARY_BASALS WHERE temporaryId = :temporaryId") + fun findByTempId(temporaryId: Long): TemporaryBasal? + @Query("SELECT * FROM $TABLE_TEMPORARY_BASALS WHERE timestamp = :timestamp AND referenceId IS NULL") fun findByTimestamp(timestamp: Long): TemporaryBasal? diff --git a/database/src/main/java/info/nightscout/androidaps/database/transactions/InvalidateTemporaryBasalWithTempIdTransaction.kt b/database/src/main/java/info/nightscout/androidaps/database/transactions/InvalidateTemporaryBasalWithTempIdTransaction.kt new file mode 100644 index 0000000000..1e1afaf4d8 --- /dev/null +++ b/database/src/main/java/info/nightscout/androidaps/database/transactions/InvalidateTemporaryBasalWithTempIdTransaction.kt @@ -0,0 +1,21 @@ +package info.nightscout.androidaps.database.transactions + +import info.nightscout.androidaps.database.entities.TemporaryBasal + +class InvalidateTemporaryBasalWithTempIdTransaction(val tempId: Long) : Transaction() { + + override fun run(): TransactionResult { + val result = TransactionResult() + val temporaryBasal = database.temporaryBasalDao.findByTempId(tempId) + ?: throw IllegalArgumentException("There is no such Temporary Basal with the specified temp ID.") + temporaryBasal.isValid = false + database.temporaryBasalDao.updateExistingEntry(temporaryBasal) + result.invalidated.add(temporaryBasal) + return result + } + + class TransactionResult { + + val invalidated = mutableListOf() + } +} \ No newline at end of file