forked from diyaps/AndroidAPS
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request diyaps#15 from ottai-developer/master
feat:Receive BG values from Ottai App.
- Loading branch information
Showing
10 changed files
with
123 additions
and
0 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
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
95 changes: 95 additions & 0 deletions
95
plugins/source/src/main/kotlin/app/aaps/plugins/source/PathedOTAppPlugin.kt
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,95 @@ | ||
package app.aaps.plugins.source | ||
|
||
import android.content.Context | ||
import androidx.work.WorkerParameters | ||
import androidx.work.workDataOf | ||
import app.aaps.core.interfaces.logging.AAPSLogger | ||
import app.aaps.core.interfaces.logging.LTag | ||
import app.aaps.core.interfaces.plugin.PluginBase | ||
import app.aaps.core.interfaces.plugin.PluginDescription | ||
import app.aaps.core.interfaces.plugin.PluginType | ||
import app.aaps.core.interfaces.resources.ResourceHelper | ||
import app.aaps.core.interfaces.source.BgSource | ||
import app.aaps.core.interfaces.utils.DateUtil | ||
import app.aaps.core.main.utils.worker.LoggingWorker | ||
import app.aaps.database.entities.GlucoseValue | ||
import app.aaps.database.impl.AppRepository | ||
import app.aaps.database.impl.transactions.CgmSourceTransaction | ||
import app.aaps.database.transactions.TransactionGlucoseValue | ||
import dagger.android.HasAndroidInjector | ||
import kotlinx.coroutines.Dispatchers | ||
import org.json.JSONArray | ||
import org.json.JSONException | ||
import javax.inject.Inject | ||
import javax.inject.Singleton | ||
|
||
@Singleton | ||
class PathedOTAppPlugin @Inject constructor(injector: HasAndroidInjector, rh: ResourceHelper, aapsLogger: AAPSLogger, ) | ||
: PluginBase(PluginDescription() | ||
.mainType(PluginType.BGSOURCE) | ||
.fragmentClass(BGSourceFragment::class.java.name) | ||
.preferencesId(R.xml.pref_bgsource) | ||
.pluginIcon(app.aaps.core.ui.R.mipmap.ottai_icon) | ||
.pluginName(R.string.patched_ottai_app) | ||
.description(R.string.description_source_patched_ottai_app), | ||
aapsLogger, rh, injector), BgSource { | ||
|
||
// cannot be inner class because of needed injection | ||
class PathedOTAppWorker(context: Context, params: WorkerParameters) : LoggingWorker(context, params, Dispatchers.IO) { | ||
|
||
@Inject lateinit var mOTAppPlugin: PathedOTAppPlugin | ||
@Inject lateinit var injector: HasAndroidInjector | ||
@Inject lateinit var dateUtil: DateUtil | ||
@Inject lateinit var repository: AppRepository | ||
|
||
init { | ||
(context.applicationContext as HasAndroidInjector).androidInjector().inject(this) | ||
} | ||
|
||
override suspend fun doWorkAndLog(): Result { | ||
var ret = Result.success() | ||
if (!mOTAppPlugin.isEnabled()) return Result.success() | ||
val collection = inputData.getString("collection") ?: return Result.failure(workDataOf("Error" to "missing collection")) | ||
if (collection == "entries") { | ||
val data = inputData.getString("data") | ||
aapsLogger.debug(LTag.BGSOURCE, "Received SI App Data: $data") | ||
if (!data.isNullOrEmpty()) { | ||
try { | ||
val glucoseValues = mutableListOf<TransactionGlucoseValue>() | ||
val jsonArray = JSONArray(data) | ||
for (i in 0 until jsonArray.length()) { | ||
val jsonObject = jsonArray.getJSONObject(i) | ||
when (val type = jsonObject.getString("type")) { | ||
"sgv" ->{ | ||
glucoseValues += TransactionGlucoseValue( | ||
timestamp = jsonObject.getLong("date"), | ||
value = jsonObject.getDouble("sgv"), | ||
raw = jsonObject.getDouble("sgv"), | ||
noise = null, | ||
trendArrow = GlucoseValue.TrendArrow.fromString(jsonObject.getString("direction")), | ||
sourceSensor = GlucoseValue.SourceSensor.OTApp) | ||
} | ||
else -> aapsLogger.debug(LTag.BGSOURCE, "Unknown entries type: $type") | ||
} | ||
} | ||
repository.runTransactionForResult(CgmSourceTransaction(glucoseValues, emptyList(), null)) | ||
.doOnError { | ||
aapsLogger.error(LTag.DATABASE, "Error while saving values from Ottai App", it) | ||
ret = Result.failure(workDataOf("Error" to it.toString())) | ||
} | ||
.blockingGet() | ||
.also { savedValues -> | ||
savedValues.all().forEach { | ||
aapsLogger.debug(LTag.DATABASE, "Inserted bg $it") | ||
} | ||
} | ||
} catch (e: JSONException) { | ||
aapsLogger.error("Exception: ", e) | ||
ret = Result.failure(workDataOf("Error" to e.toString())) | ||
} | ||
} | ||
} | ||
return ret | ||
} | ||
} | ||
} |
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