accept data 1 month back in Dexcom plugin

This commit is contained in:
Milos Kozak 2019-11-20 22:03:31 +01:00
parent 117f56e247
commit 2fe276d101

View file

@ -17,6 +17,7 @@ import info.nightscout.androidaps.logging.L
import info.nightscout.androidaps.plugins.general.nsclient.NSUpload import info.nightscout.androidaps.plugins.general.nsclient.NSUpload
import info.nightscout.androidaps.utils.DateUtil import info.nightscout.androidaps.utils.DateUtil
import info.nightscout.androidaps.utils.SP import info.nightscout.androidaps.utils.SP
import info.nightscout.androidaps.utils.T
import org.json.JSONObject import org.json.JSONObject
import org.slf4j.LoggerFactory import org.slf4j.LoggerFactory
@ -51,7 +52,7 @@ object SourceDexcomPlugin : PluginBase(PluginDescription()
} }
fun findDexcomPackageName(): String? { fun findDexcomPackageName(): String? {
val packageManager = MainApp.instance().packageManager; val packageManager = MainApp.instance().packageManager
for (packageInfo in packageManager.getInstalledPackages(0)) { for (packageInfo in packageManager.getInstalledPackages(0)) {
if (PACKAGE_NAMES.contains(packageInfo.packageName)) return packageInfo.packageName if (PACKAGE_NAMES.contains(packageInfo.packageName)) return packageInfo.packageName
} }
@ -64,43 +65,53 @@ object SourceDexcomPlugin : PluginBase(PluginDescription()
val sensorType = intent.getStringExtra("sensorType") ?: "" val sensorType = intent.getStringExtra("sensorType") ?: ""
val glucoseValues = intent.getBundleExtra("glucoseValues") val glucoseValues = intent.getBundleExtra("glucoseValues")
for (i in 0 until glucoseValues.size()) { for (i in 0 until glucoseValues.size()) {
val glucoseValue = glucoseValues.getBundle(i.toString()) glucoseValues.getBundle(i.toString())?.let { glucoseValue ->
val bgReading = BgReading() val bgReading = BgReading()
bgReading.value = glucoseValue!!.getInt("glucoseValue").toDouble() bgReading.value = glucoseValue.getInt("glucoseValue").toDouble()
bgReading.direction = glucoseValue.getString("trendArrow") bgReading.direction = glucoseValue.getString("trendArrow")
bgReading.date = glucoseValue.getLong("timestamp") * 1000 bgReading.date = glucoseValue.getLong("timestamp") * 1000
bgReading.raw = 0.0 bgReading.raw = 0.0
if (MainApp.getDbHelper().createIfNotExists(bgReading, "Dexcom$sensorType")) { if (MainApp.getDbHelper().createIfNotExists(bgReading, "Dexcom$sensorType")) {
if (SP.getBoolean(R.string.key_dexcomg5_nsupload, false)) { if (SP.getBoolean(R.string.key_dexcomg5_nsupload, false)) {
NSUpload.uploadBg(bgReading, "AndroidAPS-Dexcom$sensorType") NSUpload.uploadBg(bgReading, "AndroidAPS-Dexcom$sensorType")
} }
if (SP.getBoolean(R.string.key_dexcomg5_xdripupload, false)) { if (SP.getBoolean(R.string.key_dexcomg5_xdripupload, false)) {
NSUpload.sendToXdrip(bgReading) NSUpload.sendToXdrip(bgReading)
}
} }
} }
} }
val meters = intent.getBundleExtra("meters") val meters = intent.getBundleExtra("meters")
for (i in 0 until meters.size()) { for (i in 0 until meters.size()) {
val meter = meters.getBundle(i.toString()) val meter = meters.getBundle(i.toString())
val timestamp = meter!!.getLong("timestamp") * 1000 meter?.let {
if (MainApp.getDbHelper().getCareportalEventFromTimestamp(timestamp) != null) continue val timestamp = it.getLong("timestamp") * 1000
val jsonObject = JSONObject() val now = DateUtil.now()
jsonObject.put("enteredBy", "AndroidAPS-Dexcom$sensorType") if (timestamp > now - T.months(1).msecs() && timestamp < now)
jsonObject.put("created_at", DateUtil.toISOString(timestamp)) if (MainApp.getDbHelper().getCareportalEventFromTimestamp(timestamp) == null) {
jsonObject.put("eventType", CareportalEvent.BGCHECK) val jsonObject = JSONObject()
jsonObject.put("glucoseType", "Finger") jsonObject.put("enteredBy", "AndroidAPS-Dexcom$sensorType")
jsonObject.put("glucose", meter.getInt("meterValue")) jsonObject.put("created_at", DateUtil.toISOString(timestamp))
jsonObject.put("units", Constants.MGDL) jsonObject.put("eventType", CareportalEvent.BGCHECK)
NSUpload.uploadCareportalEntryToNS(jsonObject) jsonObject.put("glucoseType", "Finger")
jsonObject.put("glucose", meter.getInt("meterValue"))
jsonObject.put("units", Constants.MGDL)
NSUpload.uploadCareportalEntryToNS(jsonObject)
}
}
} }
if (SP.getBoolean(R.string.key_dexcom_lognssensorchange, false) && intent.hasExtra("sensorInsertionTime")) { if (SP.getBoolean(R.string.key_dexcom_lognssensorchange, false) && intent.hasExtra("sensorInsertionTime")) {
val sensorInsertionTime = intent.extras!!.getLong("sensorInsertionTime") * 1000 intent.extras?.let {
if (MainApp.getDbHelper().getCareportalEventFromTimestamp(sensorInsertionTime) == null) { val sensorInsertionTime = it.getLong("sensorInsertionTime") * 1000
val jsonObject = JSONObject() val now = DateUtil.now()
jsonObject.put("enteredBy", "AndroidAPS-Dexcom$sensorType") if (sensorInsertionTime > now - T.months(1).msecs() && sensorInsertionTime < now)
jsonObject.put("created_at", DateUtil.toISOString(sensorInsertionTime)) if (MainApp.getDbHelper().getCareportalEventFromTimestamp(sensorInsertionTime) == null) {
jsonObject.put("eventType", CareportalEvent.SENSORCHANGE) val jsonObject = JSONObject()
NSUpload.uploadCareportalEntryToNS(jsonObject) jsonObject.put("enteredBy", "AndroidAPS-Dexcom$sensorType")
jsonObject.put("created_at", DateUtil.toISOString(sensorInsertionTime))
jsonObject.put("eventType", CareportalEvent.SENSORCHANGE)
NSUpload.uploadCareportalEntryToNS(jsonObject)
}
} }
} }
} catch (e: Exception) { } catch (e: Exception) {