From f61cac20873cb183e721a316a4f185e9e575e0db Mon Sep 17 00:00:00 2001 From: miyeongkim Date: Fri, 29 Apr 2022 14:33:54 +0900 Subject: [PATCH] Added pump logs upload setting to diacon platform --- .../androidaps/diaconn/DiaconnG8Pump.kt | 8 + .../diaconn/api/DiaconnApiResponse.kt | 25 + .../diaconn/api/DiaconnApiService.kt | 24 + .../diaconn/api/DiaconnLogUploader.kt | 33 + .../androidaps/diaconn/di/DiaconnG8Module.kt | 3 +- .../diaconn/di/DiaconnLogUploaderModule.kt | 12 + .../packet/BigLogInquireResponsePacket.kt | 1227 +++++++++-------- .../diaconn/service/DiaconnG8Service.kt | 122 +- .../src/main/res/values-ko-rKR/strings.xml | 2 + diaconn/src/main/res/values/strings.xml | 5 +- diaconn/src/main/res/xml/pref_diaconn.xml | 6 + 11 files changed, 849 insertions(+), 618 deletions(-) create mode 100644 diaconn/src/main/java/info/nightscout/androidaps/diaconn/api/DiaconnApiResponse.kt create mode 100644 diaconn/src/main/java/info/nightscout/androidaps/diaconn/api/DiaconnApiService.kt create mode 100644 diaconn/src/main/java/info/nightscout/androidaps/diaconn/api/DiaconnLogUploader.kt create mode 100644 diaconn/src/main/java/info/nightscout/androidaps/diaconn/di/DiaconnLogUploaderModule.kt diff --git a/diaconn/src/main/java/info/nightscout/androidaps/diaconn/DiaconnG8Pump.kt b/diaconn/src/main/java/info/nightscout/androidaps/diaconn/DiaconnG8Pump.kt index 61d15fc0a6..fcb383fa8c 100644 --- a/diaconn/src/main/java/info/nightscout/androidaps/diaconn/DiaconnG8Pump.kt +++ b/diaconn/src/main/java/info/nightscout/androidaps/diaconn/DiaconnG8Pump.kt @@ -187,6 +187,13 @@ class DiaconnG8Pump @Inject constructor( var lgsTime:Int = 0 // LGS Setting time (0~255 min) var lgsElapsedTime:Int = 0 // LGS Passed Time (0~255 min) + val pumpUid: String + get() = "$country-$productType-$makeYear-${makeMonth.toString().padStart(2,'0')}-${makeDay.toString().padStart(2, '0')}-${lotNo.toString().padStart(3,'0')}-${serialNo.toString().padStart(5,'0')}" + + val pumpVersion: String + get() = "$majorVersion.$minorVersion" + + fun buildDiaconnG8ProfileRecord(nsProfile: Profile): Array { val record = Array(24) { 0.0 } for (hour in 0..23) { @@ -247,6 +254,7 @@ class DiaconnG8Pump @Inject constructor( var apslastLogNum = 0 // 앱에서 처리한 마지막 로그 번호. var apsWrappingCount = 0 // 앱에서 처리한 마지막 로그 번호. var isProgressPumpLogSync = false // 로그 동기화 진행 여부 + var isPlatformUploadStarted = false // 플랫폼 로그 동기화 진행 여부 // 6. bolus speed status. var speed = 0 // 주입 속도(1 ~ 8) diff --git a/diaconn/src/main/java/info/nightscout/androidaps/diaconn/api/DiaconnApiResponse.kt b/diaconn/src/main/java/info/nightscout/androidaps/diaconn/api/DiaconnApiResponse.kt new file mode 100644 index 0000000000..2f75a27951 --- /dev/null +++ b/diaconn/src/main/java/info/nightscout/androidaps/diaconn/api/DiaconnApiResponse.kt @@ -0,0 +1,25 @@ +package info.nightscout.androidaps.diaconn.api + +import com.google.gson.annotations.SerializedName + +data class LastNoResponse(val ok: Boolean, val info:Info ) + +data class Info(val pumplog_no: Long) + +data class ApiResponse(val ok: Boolean) + +data class PumpLogDto( + @SerializedName("app_uid") val app_uid: String, + @SerializedName("app_version") val app_version: String, + @SerializedName("pump_uid") val pump_uid: String, + @SerializedName("pump_version") val pump_version: String, + @SerializedName("incarnation_num") val incarnation_num: Int, + @SerializedName("pumplog_info") val pumplog_info: List +) + +data class PumpLog( + @SerializedName("pumplog_no") val pumplog_no: Long, + @SerializedName("pumplog_wrapping_count") val pumplog_wrapping_count: Int, + @SerializedName("pumplog_data") val pumplog_data: String, + @SerializedName("act_type") val act_type: String +) \ No newline at end of file diff --git a/diaconn/src/main/java/info/nightscout/androidaps/diaconn/api/DiaconnApiService.kt b/diaconn/src/main/java/info/nightscout/androidaps/diaconn/api/DiaconnApiService.kt new file mode 100644 index 0000000000..7c83cfdf10 --- /dev/null +++ b/diaconn/src/main/java/info/nightscout/androidaps/diaconn/api/DiaconnApiService.kt @@ -0,0 +1,24 @@ +package info.nightscout.androidaps.diaconn.api + +import retrofit2.Call +import retrofit2.http.Body +import retrofit2.http.GET +import retrofit2.http.Headers +import retrofit2.http.POST +import retrofit2.http.Query + +interface DiaconnApiService { + + @Headers("api-key: ${DiaconnLogUploader.UPLOAD_API_KEY}") + @GET("v1/pumplog/last_no") + fun getPumpLastNo( + @Query("pump_uid") pump_uid: String, + @Query("pump_version") pump_version: String, + @Query("incarnation_num") incarnation_num: Int): Call + + @Headers("api-key: ${DiaconnLogUploader.UPLOAD_API_KEY}") + @POST("v1/pumplog/save") + fun uploadPumpLogs(@Body pumpLogDto: PumpLogDto): Call + + +} \ No newline at end of file diff --git a/diaconn/src/main/java/info/nightscout/androidaps/diaconn/api/DiaconnLogUploader.kt b/diaconn/src/main/java/info/nightscout/androidaps/diaconn/api/DiaconnLogUploader.kt new file mode 100644 index 0000000000..e4b26986dd --- /dev/null +++ b/diaconn/src/main/java/info/nightscout/androidaps/diaconn/api/DiaconnLogUploader.kt @@ -0,0 +1,33 @@ +package info.nightscout.androidaps.diaconn.api + +import android.content.Context +import info.nightscout.androidaps.plugins.bus.RxBus +import info.nightscout.shared.logging.AAPSLogger +import info.nightscout.shared.logging.LTag +import retrofit2.Retrofit +import retrofit2.converter.gson.GsonConverterFactory +import javax.inject.Inject +import javax.inject.Singleton + +@Singleton +class DiaconnLogUploader @Inject constructor ( + private val aapsLogger: AAPSLogger, +) { + companion object { + private const val BASE_URL = "https://api.diaconn.com/aaps/" + const val UPLOAD_API_KEY = "D7B3DA9FA8229D5253F3D75E1E2B1BA4" + } + + private var retrofit: Retrofit? = null + + fun getRetrofitInstance(): Retrofit? { + //aapsLogger.debug(LTag.PUMPCOMM, "Diaconn pump logs upload BASE_URL : $BASE_URL") + if (retrofit == null) { + retrofit = Retrofit.Builder() + .baseUrl(BASE_URL) + .addConverterFactory(GsonConverterFactory.create()) + .build() + } + return retrofit + } +} \ No newline at end of file diff --git a/diaconn/src/main/java/info/nightscout/androidaps/diaconn/di/DiaconnG8Module.kt b/diaconn/src/main/java/info/nightscout/androidaps/diaconn/di/DiaconnG8Module.kt index 870d460bf0..89a795be77 100644 --- a/diaconn/src/main/java/info/nightscout/androidaps/diaconn/di/DiaconnG8Module.kt +++ b/diaconn/src/main/java/info/nightscout/androidaps/diaconn/di/DiaconnG8Module.kt @@ -6,7 +6,8 @@ import dagger.Module DiaconnG8ActivitiesModule::class, DiaconnG8ServiceModule::class, DiaconnG8PacketModule::class, - DiaconnHistoryModule::class + DiaconnHistoryModule::class, + DiaconnLogUploaderModule::class ]) open class DiaconnG8Module \ No newline at end of file diff --git a/diaconn/src/main/java/info/nightscout/androidaps/diaconn/di/DiaconnLogUploaderModule.kt b/diaconn/src/main/java/info/nightscout/androidaps/diaconn/di/DiaconnLogUploaderModule.kt new file mode 100644 index 0000000000..3a5f18c52a --- /dev/null +++ b/diaconn/src/main/java/info/nightscout/androidaps/diaconn/di/DiaconnLogUploaderModule.kt @@ -0,0 +1,12 @@ +package info.nightscout.androidaps.diaconn.di + +import dagger.Module +import dagger.android.ContributesAndroidInjector +import info.nightscout.androidaps.diaconn.api.DiaconnLogUploader +import info.nightscout.androidaps.diaconn.service.DiaconnG8Service + +@Module +@Suppress("unused") +abstract class DiaconnLogUploaderModule { + @ContributesAndroidInjector abstract fun contributesDiaconnLogUploader(): DiaconnLogUploader +} \ No newline at end of file diff --git a/diaconn/src/main/java/info/nightscout/androidaps/diaconn/packet/BigLogInquireResponsePacket.kt b/diaconn/src/main/java/info/nightscout/androidaps/diaconn/packet/BigLogInquireResponsePacket.kt index 5ca09ea8e7..099a562435 100644 --- a/diaconn/src/main/java/info/nightscout/androidaps/diaconn/packet/BigLogInquireResponsePacket.kt +++ b/diaconn/src/main/java/info/nightscout/androidaps/diaconn/packet/BigLogInquireResponsePacket.kt @@ -1,9 +1,12 @@ package info.nightscout.androidaps.diaconn.packet +import android.app.Application +import android.content.Context import dagger.android.HasAndroidInjector import info.nightscout.androidaps.data.DetailedBolusInfo import info.nightscout.androidaps.diaconn.DiaconnG8Pump import info.nightscout.androidaps.diaconn.R +import info.nightscout.androidaps.diaconn.api.* import info.nightscout.androidaps.diaconn.common.RecordTypes import info.nightscout.androidaps.diaconn.database.DiaconnHistoryRecord import info.nightscout.androidaps.diaconn.database.DiaconnHistoryRecordDao @@ -22,6 +25,10 @@ import info.nightscout.androidaps.utils.resources.ResourceHelper import info.nightscout.shared.sharedPreferences.SP import org.apache.commons.lang3.time.DateUtils import org.joda.time.DateTime +import retrofit2.Call +import retrofit2.Response +import java.lang.Exception +import java.util.* import javax.inject.Inject /** @@ -30,7 +37,6 @@ import javax.inject.Inject class BigLogInquireResponsePacket( injector: HasAndroidInjector ) : DiaconnG8Packet(injector) { - @Inject lateinit var rxBus: RxBus @Inject lateinit var rh: ResourceHelper @Inject lateinit var activePlugin: ActivePlugin @@ -40,6 +46,9 @@ class BigLogInquireResponsePacket( @Inject lateinit var sp: SP @Inject lateinit var pumpSync: PumpSync @Inject lateinit var diaconnHistoryRecordDao: DiaconnHistoryRecordDao + @Inject lateinit var diaconnLogUploader: DiaconnLogUploader + @Inject lateinit var context: Context + var result = 0// 조회결과 private var pumpDesc = PumpDescription(PumpType.DIACONN_G8) init { @@ -66,6 +75,7 @@ class BigLogInquireResponsePacket( // initalize val dailyMaxvalInfo = mutableMapOf>() dailyMaxvalInfo[""] = mutableMapOf() + val pumpLogs : MutableList = mutableListOf() // 15 byte를 로그갯수만큼 돌기. for(i in 0 until logLength) { @@ -86,637 +96,693 @@ class BigLogInquireResponsePacket( PumplogUtil.getByte(bufferData), PumplogUtil.getByte(bufferData) ) - - diaconnG8Pump.apsWrappingCount = wrapingCount - diaconnG8Pump.apslastLogNum = logNum - sp.putInt(rh.gs(R.string.apslastLogNum), logNum) - sp.putInt(rh.gs(R.string.apsWrappingCount), wrapingCount) - // process Log to DB val logDataToHexString = toNarrowHex(logdata) val pumplogKind: Byte = PumplogUtil.getKind(logDataToHexString) var status: String val diaconnG8HistoryRecord = DiaconnHistoryRecord(0) - when(pumplogKind) { - LOG_INJECT_MEAL_SUCCESS.LOG_KIND -> { - val logItem = LOG_INJECT_MEAL_SUCCESS.parse(logDataToHexString) - aapsLogger.debug(LTag.PUMPCOMM, "$logItem ") - val logStartDate = DateUtils.parseDate(logItem.dttm, "yyyy-MM-dd HH:mm:ss") - val logDateTime = logStartDate.time - val detailedBolusInfo = detailedBolusInfoStorage.findDetailedBolusInfo(logDateTime, logItem.injectAmount / 100.0) - val newRecord = pumpSync.syncBolusWithPumpId( - timestamp = logDateTime, - amount = logItem.injectAmount / 100.0, - type = detailedBolusInfo?.bolusType, - pumpId = logDateTime, - pumpType = PumpType.DIACONN_G8, - pumpSerial = diaconnG8Pump.serialNo.toString()) - aapsLogger.debug(LTag.PUMPCOMM, (if (newRecord) "**NEW** " else "") + "EVENT MEALBOLUS (" + pumplogKind + ") " + dateUtil.dateAndTimeString(logDateTime) + " (" + logDateTime + ")" + " Bolus: " + logItem.injectAmount / 100.0 + "U ") - diaconnG8HistoryRecord.code = RecordTypes.RECORD_TYPE_BOLUS - diaconnG8HistoryRecord.timestamp = logDateTime - diaconnG8HistoryRecord.value = logItem.injectAmount / 100.0 - diaconnG8HistoryRecord.duration = logItem.getInjectTime() - diaconnG8HistoryRecord.bolusType = "M" // meal bolus - diaconnG8HistoryRecord.stringValue = rh.gs(R.string.diaconn_g8_logmealsuccess) - diaconnHistoryRecordDao.createOrUpdate(diaconnG8HistoryRecord) - if (!newRecord && detailedBolusInfo != null) { - // detailedInfo can be from another similar record. Reinsert - detailedBolusInfoStorage.add(detailedBolusInfo) - } - status = "MEALBOLUSSUCCESS" + dateUtil.timeString(logDateTime) - } + if(diaconnG8Pump.isPlatformUploadStarted){ + // Diaconn Platform upload start + aapsLogger.debug(LTag.PUMPCOMM, "make api upload parameter") + val pumpLog = PumpLog( + pumplog_no = logNum.toLong(), + pumplog_wrapping_count = wrapingCount, + pumplog_data = logDataToHexString, + act_type = "1" + ) + pumpLogs.add(pumpLog) + continue - LOG_INJECT_MEAL_FAIL.LOG_KIND -> { - val logItem = LOG_INJECT_MEAL_FAIL.parse(logDataToHexString) - aapsLogger.debug(LTag.PUMPCOMM, "$logItem ") - val logStartDate = DateUtils.parseDate(logItem.dttm, "yyyy-MM-dd HH:mm:ss") - val logDateTime = logStartDate.time - val detailedBolusInfo = detailedBolusInfoStorage.findDetailedBolusInfo(logDateTime, logItem.injectAmount / 100.0) - val newRecord = pumpSync.syncBolusWithPumpId( - timestamp = logDateTime, - amount = logItem.injectAmount / 100.0, - type = detailedBolusInfo?.bolusType, - pumpId = logDateTime, - pumpType = PumpType.DIACONN_G8, - pumpSerial = diaconnG8Pump.serialNo.toString()) - aapsLogger.debug(LTag.PUMPCOMM, (if (newRecord) "**NEW** " else "") + "EVENT MEALBOLUS (" + pumplogKind + ") " + dateUtil.dateAndTimeString(logDateTime) + " (" + logDateTime + ")" + " Bolus: " + logItem.injectAmount / 100.0 + "U ") - diaconnG8HistoryRecord.code = RecordTypes.RECORD_TYPE_BOLUS - diaconnG8HistoryRecord.timestamp = logDateTime - diaconnG8HistoryRecord.value = if ((logItem.injectAmount / 100.0) < 0) 0.0 else (logItem.injectAmount / 100.0) - diaconnG8HistoryRecord.duration = logItem.getInjectTime() - diaconnG8HistoryRecord.bolusType = "M" // Meal bolus - diaconnG8HistoryRecord.stringValue = rh.gs(R.string.diaconn_g8_logmealfail) - diaconnHistoryRecordDao.createOrUpdate(diaconnG8HistoryRecord) - if (!newRecord && detailedBolusInfo != null) { - // detailedInfo can be from another similar record. Reinsert - detailedBolusInfoStorage.add(detailedBolusInfo) - } - status = "MEALBOLUSFAIL " + dateUtil.timeString(logDateTime) - } + } else { + // APS Local history sync start + diaconnG8Pump.apsWrappingCount = wrapingCount + diaconnG8Pump.apslastLogNum = logNum + sp.putInt(rh.gs(R.string.apslastLogNum), logNum) + sp.putInt(rh.gs(R.string.apsWrappingCount), wrapingCount) - LOG_INJECT_NORMAL_SUCCESS.LOG_KIND -> { - val logItem = LOG_INJECT_NORMAL_SUCCESS.parse(logDataToHexString) - aapsLogger.debug(LTag.PUMPCOMM, "$logItem ") + when(pumplogKind) { - val logStartDate = DateUtils.parseDate(logItem.dttm, "yyyy-MM-dd HH:mm:ss") - val logDateTime = logStartDate.time - val detailedBolusInfo = detailedBolusInfoStorage.findDetailedBolusInfo(logDateTime, logItem.injectAmount / 100.0) - val newRecord = pumpSync.syncBolusWithPumpId( - timestamp = logDateTime, - amount = logItem.injectAmount / 100.0, - type = detailedBolusInfo?.bolusType, - pumpId = logDateTime, - pumpType = PumpType.DIACONN_G8, - pumpSerial = diaconnG8Pump.serialNo.toString()) - aapsLogger.debug(LTag.PUMPCOMM, (if (newRecord) "**NEW** " else "") + "EVENT BOLUS (" + pumplogKind + ") " + dateUtil.dateAndTimeString(logDateTime) + " (" + logDateTime + ")" + " Bolus: " + logItem.injectAmount / 100.0 + "U ") - diaconnG8HistoryRecord.code = RecordTypes.RECORD_TYPE_BOLUS - diaconnG8HistoryRecord.timestamp = logDateTime - diaconnG8HistoryRecord.value = logItem.injectAmount / 100.0 - diaconnG8HistoryRecord.duration = logItem.getInjectTime() - diaconnG8HistoryRecord.bolusType = "B" // bolus - diaconnG8HistoryRecord.stringValue = rh.gs(R.string.diaconn_g8_logsuccess) - diaconnHistoryRecordDao.createOrUpdate(diaconnG8HistoryRecord) - if (!newRecord && detailedBolusInfo != null) { - // detailedInfo can be from another similar record. Reinsert - detailedBolusInfoStorage.add(detailedBolusInfo) - } - status = "BOLUSSUCCESS" + dateUtil.timeString(logDateTime) - } - - LOG_INJECT_NORMAL_FAIL.LOG_KIND -> { - val logItem = LOG_INJECT_NORMAL_FAIL.parse(logDataToHexString) - aapsLogger.debug(LTag.PUMPCOMM, "$logItem ") - val logStartDate = DateUtils.parseDate(logItem.dttm, "yyyy-MM-dd HH:mm:ss") - val logDateTime = logStartDate.time - - // APS DB process - val detailedBolusInfo = detailedBolusInfoStorage.findDetailedBolusInfo(logDateTime, logItem.injectAmount / 100.0) - val newRecord = pumpSync.syncBolusWithPumpId( - timestamp = logDateTime, - amount = logItem.injectAmount / 100.0, - type = detailedBolusInfo?.bolusType, - pumpId = logDateTime, - pumpType = PumpType.DIACONN_G8, - pumpSerial = diaconnG8Pump.serialNo.toString()) - aapsLogger.debug(LTag.PUMPCOMM, (if (newRecord) "**NEW** " else "") + "EVENT BOLUS (" + pumplogKind + ") " + dateUtil.dateAndTimeString(logDateTime) + " (" + logDateTime + ")" + " Bolus: " + logItem.injectAmount / 100.0 + "U ") - // Diaconn History Process - diaconnG8HistoryRecord.code = RecordTypes.RECORD_TYPE_BOLUS - diaconnG8HistoryRecord.timestamp = logDateTime - diaconnG8HistoryRecord.value = if ((logItem.injectAmount / 100.0) < 0) 0.0 else (logItem.injectAmount / 100.0) - diaconnG8HistoryRecord.duration = logItem.getInjectTime() - diaconnG8HistoryRecord.bolusType = "B" // bolus - diaconnG8HistoryRecord.stringValue = getReasonName(pumplogKind, logItem.reason) - diaconnHistoryRecordDao.createOrUpdate(diaconnG8HistoryRecord) - if (!newRecord && detailedBolusInfo != null) { - // detailedInfo can be from another similar record. Reinsert - detailedBolusInfoStorage.add(detailedBolusInfo) - } - status = "BOLUSFAIL " + dateUtil.timeString(logDateTime) - } - - LOG_SET_SQUARE_INJECTION.LOG_KIND -> { - val logItem = LOG_SET_SQUARE_INJECTION.parse(logDataToHexString) - aapsLogger.debug(LTag.PUMPCOMM, "$logItem ") - val logStartDate = DateUtils.parseDate(logItem.dttm, "yyyy-MM-dd HH:mm:ss") - val logDateTime = logStartDate.time - val newRecord = pumpSync.syncExtendedBolusWithPumpId( - timestamp = logDateTime, - amount = logItem.setAmount / 100.0, - duration = T.mins((logItem.getInjectTime() * 10).toLong()).msecs(), - isEmulatingTB = false, - pumpId = logDateTime, - pumpType = PumpType.DIACONN_G8, - pumpSerial = diaconnG8Pump.serialNo.toString()) - aapsLogger.debug(LTag.PUMPCOMM, (if (newRecord) "**NEW** " else "") + "EVENT EXTENDEDSTART (" + pumplogKind + ") " + dateUtil.dateAndTimeString(logDateTime) + " (" + logDateTime + ")" + " Amount: " + logItem.setAmount / 100.0 + "U Duration: " + logItem.getInjectTime() * 10 + "min") - diaconnG8HistoryRecord.code = RecordTypes.RECORD_TYPE_BOLUS - diaconnG8HistoryRecord.timestamp = logDateTime - diaconnG8HistoryRecord.value = logItem.setAmount / 100.0 - diaconnG8HistoryRecord.duration = logItem.getInjectTime() - diaconnG8HistoryRecord.stringValue = rh.gs(R.string.diaconn_g8_logsquarestart) - diaconnG8HistoryRecord.bolusType = "E" // Extended - diaconnHistoryRecordDao.createOrUpdate(diaconnG8HistoryRecord) - status = "EXTENDEDBOLUSSTART " + dateUtil.timeString(logDateTime) - } - - LOG_INJECT_SQUARE_SUCCESS.LOG_KIND -> { - val logItem = LOG_INJECT_SQUARE_SUCCESS.parse(logDataToHexString) - aapsLogger.debug(LTag.PUMPCOMM, "$logItem ") - val logStartDate = DateUtils.parseDate(logItem.dttm, "yyyy-MM-dd HH:mm:ss") - val logDateTime = logStartDate.time - diaconnG8HistoryRecord.code = RecordTypes.RECORD_TYPE_BOLUS - diaconnG8HistoryRecord.timestamp = logDateTime - diaconnG8HistoryRecord.duration = logItem.getInjectTime() - diaconnG8HistoryRecord.stringValue = rh.gs(R.string.diaconn_g8_logsquaresuccess) - diaconnG8HistoryRecord.bolusType = "E" // Extended - diaconnHistoryRecordDao.createOrUpdate(diaconnG8HistoryRecord) - status = "EXTENDEDBOLUSEND " + dateUtil.timeString(logDateTime) - } - - LOG_INJECT_SQUARE_FAIL.LOG_KIND -> { - val logItem = LOG_INJECT_SQUARE_FAIL.parse(logDataToHexString) - aapsLogger.debug(LTag.PUMPCOMM, "$logItem ") - val logStartDate = DateUtils.parseDate(logItem.dttm, "yyyy-MM-dd HH:mm:ss") - val logDateTime = logStartDate.time - val newRecord = pumpSync.syncStopExtendedBolusWithPumpId( - timestamp = logDateTime, - endPumpId = logDateTime, - pumpType = PumpType.DIACONN_G8, - pumpSerial = diaconnG8Pump.serialNo.toString()) - aapsLogger.debug(LTag.PUMPCOMM, (if (newRecord) "**NEW** " else "") + "EVENT EXTENDEDSTOP (" + pumplogKind + ") " + dateUtil.dateAndTimeString(logDateTime) + " (" + logDateTime + ")" + " Delivered: " + logItem.injectAmount / 100.0 + "U RealDuration: " + logItem.getInjectTime() + "min") - diaconnG8HistoryRecord.code = RecordTypes.RECORD_TYPE_BOLUS - diaconnG8HistoryRecord.timestamp = logDateTime - diaconnG8HistoryRecord.value = logItem.injectAmount / 100.0 - diaconnG8HistoryRecord.duration = logItem.getInjectTime() - diaconnG8HistoryRecord.stringValue = getReasonName(pumplogKind, logItem.reason) - diaconnG8HistoryRecord.bolusType = "E" - diaconnHistoryRecordDao.createOrUpdate(diaconnG8HistoryRecord) - status = "EXTENDEDBOLUSFAIL " + dateUtil.timeString(logDateTime) - } - - LOG_SET_DUAL_INJECTION.LOG_KIND -> { - val logItem = LOG_SET_DUAL_INJECTION.parse(logDataToHexString) - aapsLogger.debug(LTag.PUMPCOMM, "$logItem ") - val logStartDate = DateUtils.parseDate(logItem.dttm, "yyyy-MM-dd HH:mm:ss") - val logDateTime = logStartDate.time - - // dual square 처리. - val newRecord = pumpSync.syncExtendedBolusWithPumpId( - timestamp = logDateTime, - amount = logItem.setSquareAmount / 100.0, - duration = T.mins((logItem.getInjectTime() * 10).toLong()).msecs(), - isEmulatingTB = false, - pumpId = logDateTime, - pumpType = PumpType.DIACONN_G8, - pumpSerial = diaconnG8Pump.serialNo.toString()) - aapsLogger.debug(LTag.PUMPCOMM, (if (newRecord) "**NEW** " else "") + "EVENT EXTENDEDSTART (" + pumplogKind + ") " + dateUtil.dateAndTimeString(logDateTime) + " (" + logDateTime + ")" + " Amount: " + logItem.setSquareAmount / 100.0 + "U Duration: " + logItem.getInjectTime() * 10 + "min") - diaconnG8HistoryRecord.code = RecordTypes.RECORD_TYPE_BOLUS - diaconnG8HistoryRecord.timestamp = logDateTime - diaconnG8HistoryRecord.value = logItem.setSquareAmount / 100.0 - diaconnG8HistoryRecord.duration = logItem.getInjectTime() * 10 // (1~30) 1:10min 30:300min - diaconnG8HistoryRecord.stringValue = rh.gs(R.string.diaconn_g8_logdualsquarestart) - diaconnG8HistoryRecord.bolusType = "D" // Extended - diaconnHistoryRecordDao.createOrUpdate(diaconnG8HistoryRecord) - - status = "DUALEXTENTEDSTART " + dateUtil.timeString(logDateTime) - } - - LOG_INJECTION_DUAL_NORMAL.LOG_KIND -> { - val logItem = LOG_INJECTION_DUAL_NORMAL.parse(logDataToHexString) - aapsLogger.debug(LTag.PUMPCOMM, "$logItem ") - val logStartDate = DateUtils.parseDate(logItem.dttm, "yyyy-MM-dd HH:mm:ss") - val logDateTime = logStartDate.time - val detailedBolusInfo = detailedBolusInfoStorage.findDetailedBolusInfo(logDateTime, logItem.injectAmount / 100.0) - val newRecord = pumpSync.syncBolusWithPumpId( - timestamp = logDateTime, - amount = logItem.injectAmount / 100.0, - type = detailedBolusInfo?.bolusType, - pumpId = logDateTime, - pumpType = PumpType.DIACONN_G8, - pumpSerial = diaconnG8Pump.serialNo.toString()) - aapsLogger.debug(LTag.PUMPCOMM, (if (newRecord) "**NEW** " else "") + "EVENT DUALBOLUS (" + pumplogKind + ") " + dateUtil.dateAndTimeString(logDateTime) + " (" + logDateTime + ")" + " Bolus: " + logItem.injectAmount / 100.0 + "U Duration: " + logItem.getInjectTime() + "min") - - diaconnG8Pump.lastBolusAmount = logItem.injectAmount / 100.0 - diaconnG8Pump.lastBolusTime = logDateTime - - //Diaconn History - diaconnG8HistoryRecord.code = RecordTypes.RECORD_TYPE_BOLUS - diaconnG8HistoryRecord.timestamp = logDateTime - diaconnG8HistoryRecord.value = logItem.injectAmount / 100.0 - diaconnG8HistoryRecord.duration = logItem.getInjectTime() - diaconnG8HistoryRecord.bolusType = "D" // bolus - diaconnG8HistoryRecord.stringValue = rh.gs(R.string.diaconn_g8_logdualnormalsuccess) - diaconnHistoryRecordDao.createOrUpdate(diaconnG8HistoryRecord) - if (!newRecord && detailedBolusInfo != null) { - // detailedInfo can be from another similar record. Reinsert - detailedBolusInfoStorage.add(detailedBolusInfo) - } - status = "DUALBOLUS" + dateUtil.timeString(logDateTime) - } - - LOG_INJECT_DUAL_SUCCESS.LOG_KIND -> { - val logItem = LOG_INJECT_DUAL_SUCCESS.parse(logDataToHexString) - aapsLogger.debug(LTag.PUMPCOMM, "$logItem ") - val logStartDate = DateUtils.parseDate(logItem.dttm, "yyyy-MM-dd HH:mm:ss") - val logDateTime = logStartDate.time - - diaconnG8HistoryRecord.code = RecordTypes.RECORD_TYPE_BOLUS - diaconnG8HistoryRecord.timestamp = logDateTime - diaconnG8HistoryRecord.value = logItem.injectSquareAmount / 100.0 - diaconnG8HistoryRecord.duration = logItem.getInjectTime() - diaconnG8HistoryRecord.bolusType = "D" - diaconnG8HistoryRecord.stringValue = rh.gs(R.string.diaconn_g8_logdualsquaresuccess) - diaconnHistoryRecordDao.createOrUpdate(diaconnG8HistoryRecord) - status = "DUALBOLUS SQUARESUCCESS " + dateUtil.timeString(logDateTime) - } - - LOG_INJECT_DUAL_FAIL.LOG_KIND -> { - val logItem = LOG_INJECT_DUAL_FAIL.parse(logDataToHexString) - aapsLogger.debug(LTag.PUMPCOMM, "$logItem ") - val logStartDate = DateUtils.parseDate(logItem.dttm, "yyyy-MM-dd HH:mm:ss") - val logDateTime = logStartDate.time - val newRecord = pumpSync.syncStopExtendedBolusWithPumpId( - timestamp = logDateTime, - endPumpId = logDateTime, - pumpType = PumpType.DIACONN_G8, - pumpSerial = diaconnG8Pump.serialNo.toString()) - aapsLogger.debug(LTag.PUMPCOMM, (if (newRecord) "**NEW** " else "") + "EVENT EXTENDEDSTOP (" + pumplogKind + ") " + dateUtil.dateAndTimeString(logDateTime) + " (" + logDateTime + ")" + " Delivered: " + logItem.injectSquareAmount / 100.0 + "U RealDuration: " + logItem.getInjectTime() + "min") - - diaconnG8HistoryRecord.code = RecordTypes.RECORD_TYPE_BOLUS - diaconnG8HistoryRecord.timestamp = logDateTime - diaconnG8HistoryRecord.value = logItem.injectNormAmount / 100.0 + logItem.injectSquareAmount / 100.0 - diaconnG8HistoryRecord.duration = logItem.getInjectTime() - diaconnG8HistoryRecord.bolusType = "D" - diaconnG8HistoryRecord.stringValue = getReasonName(pumplogKind, logItem.reason) - diaconnHistoryRecordDao.createOrUpdate(diaconnG8HistoryRecord) - status = "DUALBOLUS FAIL " + dateUtil.timeString(logDateTime) - } - - LOG_INJECTION_1HOUR_BASAL.LOG_KIND -> { - val logItem = LOG_INJECTION_1HOUR_BASAL.parse(logDataToHexString) - aapsLogger.debug(LTag.PUMPCOMM, "$logItem ") - val logStartDate = DateUtils.parseDate(logItem.dttm, "yyyy-MM-dd HH:mm:ss") - val logDateTime = logStartDate.time - diaconnG8HistoryRecord.code = RecordTypes.RECORD_TYPE_BASALHOUR - diaconnG8HistoryRecord.timestamp = logDateTime - diaconnG8HistoryRecord.value = logItem.beforeAmount / 100.0 - diaconnG8HistoryRecord.stringValue = "TB before: ${logItem.beforeAmount / 100.0} / TB after: ${logItem.afterAmount / 100.0}" - diaconnHistoryRecordDao.createOrUpdate(diaconnG8HistoryRecord) - status = "1HOUR BASAL " + dateUtil.dateAndTimeString(logDateTime) - } - - LOG_SUSPEND_V2.LOG_KIND -> { - val logItem = LOG_SUSPEND_V2.parse(logDataToHexString) - aapsLogger.debug(LTag.PUMPCOMM, "$logItem ") - val logStartDate = DateUtils.parseDate(logItem.dttm, "yyyy-MM-dd HH:mm:ss") - val logDateTime = logStartDate.time - diaconnG8HistoryRecord.code = RecordTypes.RECORD_TYPE_SUSPEND - diaconnG8HistoryRecord.timestamp = logDateTime - diaconnG8HistoryRecord.stringValue = rh.gs(R.string.diaconn_g8_lgosuspend, logItem.getBasalPattern()) - diaconnHistoryRecordDao.createOrUpdate(diaconnG8HistoryRecord) - status = "SUSPEND " + dateUtil.timeString(logDateTime) - } - - LOG_SUSPEND_RELEASE_V2.LOG_KIND -> { - val logItem = LOG_SUSPEND_RELEASE_V2.parse(logDataToHexString) - aapsLogger.debug(LTag.PUMPCOMM, "$logItem ") - val logStartDate = DateUtils.parseDate(logItem.dttm, "yyyy-MM-dd HH:mm:ss") - val logDateTime = logStartDate.time - diaconnG8HistoryRecord.code = RecordTypes.RECORD_TYPE_SUSPEND - diaconnG8HistoryRecord.timestamp = logDateTime - diaconnG8HistoryRecord.stringValue = rh.gs(R.string.diaconn_g8_lgorelease, logItem.getBasalPattern()) - diaconnHistoryRecordDao.createOrUpdate(diaconnG8HistoryRecord) - status = "SUSPENDRELEASE " + dateUtil.timeString(logDateTime) - } - - LOG_CHANGE_INJECTOR_SUCCESS.LOG_KIND -> { - val logItem = LOG_CHANGE_INJECTOR_SUCCESS.parse(logDataToHexString) - aapsLogger.debug(LTag.PUMPCOMM, "$logItem ") - val logStartDate = DateUtils.parseDate(logItem.dttm, "yyyy-MM-dd HH:mm:ss") - val logDateTime = logStartDate.time - if (sp.getBoolean(R.string.key_diaconn_g8_loginsulinchange, true)) { - val newRecord = pumpSync.insertTherapyEventIfNewWithTimestamp( + LOG_INJECT_MEAL_SUCCESS.LOG_KIND -> { + val logItem = LOG_INJECT_MEAL_SUCCESS.parse(logDataToHexString) + aapsLogger.debug(LTag.PUMPCOMM, "$logItem ") + val logStartDate = DateUtils.parseDate(logItem.dttm, "yyyy-MM-dd HH:mm:ss") + val logDateTime = logStartDate.time + val detailedBolusInfo = detailedBolusInfoStorage.findDetailedBolusInfo(logDateTime, logItem.injectAmount / 100.0) + val newRecord = pumpSync.syncBolusWithPumpId( timestamp = logDateTime, - type = DetailedBolusInfo.EventType.INSULIN_CHANGE, + amount = logItem.injectAmount / 100.0, + type = detailedBolusInfo?.bolusType, pumpId = logDateTime, pumpType = PumpType.DIACONN_G8, - pumpSerial = diaconnG8Pump.serialNo.toString() - ) - aapsLogger.debug(LTag.PUMPCOMM, (if (newRecord) "**NEW** " else "") + "EVENT INSULINCHANGE(" + pumplogKind + ") " + dateUtil.dateAndTimeString(logDateTime) + " (" + logDateTime + ")" + " Amount: " + logItem.remainAmount / 100.0 + "U") + pumpSerial = diaconnG8Pump.serialNo.toString()) + aapsLogger.debug(LTag.PUMPCOMM, (if (newRecord) "**NEW** " else "") + "EVENT MEALBOLUS (" + pumplogKind + ") " + dateUtil.dateAndTimeString(logDateTime) + " (" + logDateTime + ")" + " Bolus: " + logItem.injectAmount / 100.0 + "U ") + diaconnG8HistoryRecord.code = RecordTypes.RECORD_TYPE_BOLUS + diaconnG8HistoryRecord.timestamp = logDateTime + diaconnG8HistoryRecord.value = logItem.injectAmount / 100.0 + diaconnG8HistoryRecord.duration = logItem.getInjectTime() + diaconnG8HistoryRecord.bolusType = "M" // meal bolus + diaconnG8HistoryRecord.stringValue = rh.gs(R.string.diaconn_g8_logmealsuccess) + diaconnHistoryRecordDao.createOrUpdate(diaconnG8HistoryRecord) + if (!newRecord && detailedBolusInfo != null) { + // detailedInfo can be from another similar record. Reinsert + detailedBolusInfoStorage.add(detailedBolusInfo) + } + status = "MEALBOLUSSUCCESS" + dateUtil.timeString(logDateTime) } - diaconnG8HistoryRecord.code = RecordTypes.RECORD_TYPE_REFILL - diaconnG8HistoryRecord.timestamp = logDateTime - diaconnG8HistoryRecord.value = logItem.remainAmount / 100.0 - diaconnG8HistoryRecord.stringValue = rh.gs(R.string.diaconn_g8_loginjectorprime, logItem.primeAmount / 100.0) - diaconnHistoryRecordDao.createOrUpdate(diaconnG8HistoryRecord) - status = "INSULINCHANGE " + dateUtil.timeString(logDateTime) - } - LOG_CHANGE_TUBE_SUCCESS.LOG_KIND -> { - val logItem = LOG_CHANGE_TUBE_SUCCESS.parse(logDataToHexString) - aapsLogger.debug(LTag.PUMPCOMM, "$logItem ") - val logStartDate = DateUtils.parseDate(logItem.dttm, "yyyy-MM-dd HH:mm:ss") - val logDateTime = logStartDate.time - if (sp.getBoolean(R.string.key_diaconn_g8_logtubechange, true)) { - val newRecord = pumpSync.insertTherapyEventIfNewWithTimestamp( + LOG_INJECT_MEAL_FAIL.LOG_KIND -> { + val logItem = LOG_INJECT_MEAL_FAIL.parse(logDataToHexString) + aapsLogger.debug(LTag.PUMPCOMM, "$logItem ") + val logStartDate = DateUtils.parseDate(logItem.dttm, "yyyy-MM-dd HH:mm:ss") + val logDateTime = logStartDate.time + val detailedBolusInfo = detailedBolusInfoStorage.findDetailedBolusInfo(logDateTime, logItem.injectAmount / 100.0) + val newRecord = pumpSync.syncBolusWithPumpId( timestamp = logDateTime, - type = DetailedBolusInfo.EventType.NOTE, - note = rh.gs(R.string.diaconn_g8_logtubeprime, logItem.primeAmount / 100.0), + amount = logItem.injectAmount / 100.0, + type = detailedBolusInfo?.bolusType, pumpId = logDateTime, pumpType = PumpType.DIACONN_G8, - pumpSerial = diaconnG8Pump.serialNo.toString() - ) - aapsLogger.debug(LTag.PUMPCOMM, (if (newRecord) "**NEW** " else "") + "EVENT TUBECHANGE(" + pumplogKind + ") " + dateUtil.dateAndTimeString(logDateTime) + " (" + logDateTime + ")" + " Amount: " + logItem.primeAmount / 100.0 + "U") + pumpSerial = diaconnG8Pump.serialNo.toString()) + aapsLogger.debug(LTag.PUMPCOMM, (if (newRecord) "**NEW** " else "") + "EVENT MEALBOLUS (" + pumplogKind + ") " + dateUtil.dateAndTimeString(logDateTime) + " (" + logDateTime + ")" + " Bolus: " + logItem.injectAmount / 100.0 + "U ") + diaconnG8HistoryRecord.code = RecordTypes.RECORD_TYPE_BOLUS + diaconnG8HistoryRecord.timestamp = logDateTime + diaconnG8HistoryRecord.value = if ((logItem.injectAmount / 100.0) < 0) 0.0 else (logItem.injectAmount / 100.0) + diaconnG8HistoryRecord.duration = logItem.getInjectTime() + diaconnG8HistoryRecord.bolusType = "M" // Meal bolus + diaconnG8HistoryRecord.stringValue = rh.gs(R.string.diaconn_g8_logmealfail) + diaconnHistoryRecordDao.createOrUpdate(diaconnG8HistoryRecord) + if (!newRecord && detailedBolusInfo != null) { + // detailedInfo can be from another similar record. Reinsert + detailedBolusInfoStorage.add(detailedBolusInfo) + } + status = "MEALBOLUSFAIL " + dateUtil.timeString(logDateTime) } - diaconnG8HistoryRecord.code = RecordTypes.RECORD_TYPE_REFILL - diaconnG8HistoryRecord.timestamp = logDateTime - diaconnG8HistoryRecord.value = logItem.remainAmount / 100.0 - diaconnG8HistoryRecord.stringValue = rh.gs(R.string.diaconn_g8_logtubeprime, logItem.primeAmount / 100.0) - diaconnHistoryRecordDao.createOrUpdate(diaconnG8HistoryRecord) - status = "TUBECHANGE " + dateUtil.timeString(logDateTime) - } + LOG_INJECT_NORMAL_SUCCESS.LOG_KIND -> { + val logItem = LOG_INJECT_NORMAL_SUCCESS.parse(logDataToHexString) + aapsLogger.debug(LTag.PUMPCOMM, "$logItem ") - LOG_INJECTION_1DAY.LOG_KIND -> { // Daily Bolus Log - val logItem = LOG_INJECTION_1DAY.parse(logDataToHexString) - aapsLogger.debug(LTag.PUMPCOMM, "$logItem ") - val logStartDate = DateUtils.parseDate(logItem.dttm, "yyyy-MM-dd HH:mm:ss") - val logDateTime = logStartDate.time - - diaconnG8HistoryRecord.code = RecordTypes.RECORD_TYPE_DAILY - diaconnG8HistoryRecord.timestamp = DateTime(logDateTime).withTimeAtStartOfDay().millis - diaconnG8HistoryRecord.dailyBolus = logItem.extAmount / 100.0 + logItem.mealAmount / 100.0 - - val recordDateStr = "" + diaconnG8HistoryRecord.timestamp - var recordMap: MutableMap = mutableMapOf("dummy" to 0.0) - - if (dailyMaxvalInfo.containsKey(recordDateStr)) { - recordMap = dailyMaxvalInfo[recordDateStr]!! - } else { - recordMap["bolus"] = 0.0 - recordMap["basal"] = 0.0 - dailyMaxvalInfo[recordDateStr] = recordMap - } - - if (diaconnG8HistoryRecord.dailyBolus > recordMap["bolus"]!!) { - recordMap["bolus"] = diaconnG8HistoryRecord.dailyBolus - } else { - diaconnG8HistoryRecord.dailyBolus = recordMap["bolus"]!! - } - - if (recordMap["basal"]!! > 0.0) { - diaconnG8HistoryRecord.dailyBasal = recordMap["basal"]!! - } - - diaconnHistoryRecordDao.createOrUpdate(diaconnG8HistoryRecord) - - //If it is a TDD, store it for stats also. - pumpSync.createOrUpdateTotalDailyDose( - timestamp = diaconnG8HistoryRecord.timestamp, - bolusAmount = diaconnG8HistoryRecord.dailyBolus, - basalAmount = diaconnG8HistoryRecord.dailyBasal, - totalAmount = 0.0, - pumpId = null, - pumpType = PumpType.DIACONN_G8, - diaconnG8Pump.serialNo.toString() - ) - - status = "DAILYBOLUS " + dateUtil.timeString(logDateTime) - } - - LOG_INJECTION_1DAY_BASAL.LOG_KIND -> { // Daily Basal Log - val logItem = LOG_INJECTION_1DAY_BASAL.parse(logDataToHexString) - aapsLogger.debug(LTag.PUMPCOMM, "$logItem ") - val logStartDate = DateUtils.parseDate(logItem.dttm, "yyyy-MM-dd HH:mm:ss") - val logDateTime = logStartDate.time - - diaconnG8HistoryRecord.code = RecordTypes.RECORD_TYPE_DAILY - diaconnG8HistoryRecord.timestamp = DateTime(logDateTime).withTimeAtStartOfDay().millis - diaconnG8HistoryRecord.dailyBasal = logItem.amount / 100.0 - - val recordDateStr = "" + diaconnG8HistoryRecord.timestamp - var recordMap: MutableMap = mutableMapOf("dummy" to 0.0) - - if (dailyMaxvalInfo.containsKey(recordDateStr)) { - recordMap = dailyMaxvalInfo[recordDateStr]!! - } else { - recordMap["bolus"] = 0.0 - recordMap["basal"] = 0.0 - dailyMaxvalInfo[recordDateStr] = recordMap - } - - if (diaconnG8HistoryRecord.dailyBasal > recordMap["basal"]!!) { - recordMap["basal"] = diaconnG8HistoryRecord.dailyBasal - } else { - diaconnG8HistoryRecord.dailyBasal = recordMap["basal"]!! - } - - if (recordMap["bolus"]!! > 0.0) { - diaconnG8HistoryRecord.dailyBolus = recordMap["bolus"]!! - } - - diaconnHistoryRecordDao.createOrUpdate(diaconnG8HistoryRecord) - - //If it is a TDD, store it for stats also. - // pumpSync.createOrUpdateTotalDailyDose( - // timestamp = diaconnG8HistoryRecord.timestamp, - // bolusAmount = diaconnG8HistoryRecord.dailyBolus, - // basalAmount = diaconnG8HistoryRecord.dailyBasal, - // totalAmount = 0.0, - // pumpId = null, - // pumpType = PumpType.DIACONN_G8, - // diaconnG8Pump.serialNo.toString() - // ) - - status = "DAILYBASAL " + dateUtil.timeString(logDateTime) - } - - LOG_CHANGE_NEEDLE_SUCCESS.LOG_KIND -> { - val logItem = LOG_CHANGE_NEEDLE_SUCCESS.parse(logDataToHexString) - aapsLogger.debug(LTag.PUMPCOMM, "$logItem ") - val logStartDate = DateUtils.parseDate(logItem.dttm, "yyyy-MM-dd HH:mm:ss") - val logDateTime = logStartDate.time - if (sp.getBoolean(R.string.key_diaconn_g8_logneedlechange, true)) { - val newRecord = pumpSync.insertTherapyEventIfNewWithTimestamp( + val logStartDate = DateUtils.parseDate(logItem.dttm, "yyyy-MM-dd HH:mm:ss") + val logDateTime = logStartDate.time + val detailedBolusInfo = detailedBolusInfoStorage.findDetailedBolusInfo(logDateTime, logItem.injectAmount / 100.0) + val newRecord = pumpSync.syncBolusWithPumpId( timestamp = logDateTime, - type = DetailedBolusInfo.EventType.CANNULA_CHANGE, + amount = logItem.injectAmount / 100.0, + type = detailedBolusInfo?.bolusType, pumpId = logDateTime, pumpType = PumpType.DIACONN_G8, - pumpSerial = diaconnG8Pump.serialNo.toString() - ) - aapsLogger.debug(LTag.PUMPCOMM, (if (newRecord) "**NEW** " else "") + "EVENT NEEDLECHANGE(" + pumplogKind + ") " + dateUtil.dateAndTimeString(logDateTime) + " (" + logDateTime + ")" + " Amount: " + logItem.remainAmount / 100.0 + "U") + pumpSerial = diaconnG8Pump.serialNo.toString()) + aapsLogger.debug(LTag.PUMPCOMM, (if (newRecord) "**NEW** " else "") + "EVENT BOLUS (" + pumplogKind + ") " + dateUtil.dateAndTimeString(logDateTime) + " (" + logDateTime + ")" + " Bolus: " + logItem.injectAmount / 100.0 + "U ") + diaconnG8HistoryRecord.code = RecordTypes.RECORD_TYPE_BOLUS + diaconnG8HistoryRecord.timestamp = logDateTime + diaconnG8HistoryRecord.value = logItem.injectAmount / 100.0 + diaconnG8HistoryRecord.duration = logItem.getInjectTime() + diaconnG8HistoryRecord.bolusType = "B" // bolus + diaconnG8HistoryRecord.stringValue = rh.gs(R.string.diaconn_g8_logsuccess) + diaconnHistoryRecordDao.createOrUpdate(diaconnG8HistoryRecord) + if (!newRecord && detailedBolusInfo != null) { + // detailedInfo can be from another similar record. Reinsert + detailedBolusInfoStorage.add(detailedBolusInfo) + } + status = "BOLUSSUCCESS" + dateUtil.timeString(logDateTime) } - diaconnG8HistoryRecord.code = RecordTypes.RECORD_TYPE_REFILL - diaconnG8HistoryRecord.timestamp = logDateTime - diaconnG8HistoryRecord.value = logItem.remainAmount / 100.0 - diaconnG8HistoryRecord.stringValue = rh.gs(R.string.diaconn_g8_logneedleprime, logItem.primeAmount / 100.0) - diaconnHistoryRecordDao.createOrUpdate(diaconnG8HistoryRecord) - status = "NEEDLECHANGE " + dateUtil.timeString(logDateTime) - } + LOG_INJECT_NORMAL_FAIL.LOG_KIND -> { + val logItem = LOG_INJECT_NORMAL_FAIL.parse(logDataToHexString) + aapsLogger.debug(LTag.PUMPCOMM, "$logItem ") + val logStartDate = DateUtils.parseDate(logItem.dttm, "yyyy-MM-dd HH:mm:ss") + val logDateTime = logStartDate.time - LOG_TB_START_V3.LOG_KIND -> { - val logItem = LOG_TB_START_V3.parse(logDataToHexString) - aapsLogger.debug(LTag.PUMPCOMM, "$logItem ") - - val logStartDate = DateUtils.parseDate(logItem.dttm, "yyyy-MM-dd HH:mm:ss") - val logDateTime = logStartDate.time - var absoluteRate = 0.0 - if (logItem.getTbInjectRateRatio() >= 50000) { - val tempBasalPercent = logItem.getTbInjectRateRatio() - 50000 - absoluteRate = pumpDesc.pumpType.determineCorrectBasalSize(diaconnG8Pump.baseAmount * (tempBasalPercent / 100.0)) + // APS DB process + val detailedBolusInfo = detailedBolusInfoStorage.findDetailedBolusInfo(logDateTime, logItem.injectAmount / 100.0) + val newRecord = pumpSync.syncBolusWithPumpId( + timestamp = logDateTime, + amount = logItem.injectAmount / 100.0, + type = detailedBolusInfo?.bolusType, + pumpId = logDateTime, + pumpType = PumpType.DIACONN_G8, + pumpSerial = diaconnG8Pump.serialNo.toString()) + aapsLogger.debug(LTag.PUMPCOMM, (if (newRecord) "**NEW** " else "") + "EVENT BOLUS (" + pumplogKind + ") " + dateUtil.dateAndTimeString(logDateTime) + " (" + logDateTime + ")" + " Bolus: " + logItem.injectAmount / 100.0 + "U ") + // Diaconn History Process + diaconnG8HistoryRecord.code = RecordTypes.RECORD_TYPE_BOLUS + diaconnG8HistoryRecord.timestamp = logDateTime + diaconnG8HistoryRecord.value = if ((logItem.injectAmount / 100.0) < 0) 0.0 else (logItem.injectAmount / 100.0) + diaconnG8HistoryRecord.duration = logItem.getInjectTime() + diaconnG8HistoryRecord.bolusType = "B" // bolus + diaconnG8HistoryRecord.stringValue = getReasonName(pumplogKind, logItem.reason) + diaconnHistoryRecordDao.createOrUpdate(diaconnG8HistoryRecord) + if (!newRecord && detailedBolusInfo != null) { + // detailedInfo can be from another similar record. Reinsert + detailedBolusInfoStorage.add(detailedBolusInfo) + } + status = "BOLUSFAIL " + dateUtil.timeString(logDateTime) } - if (logItem.getTbInjectRateRatio() in 1000..2500) { - absoluteRate = (logItem.getTbInjectRateRatio() - 1000) / 100.0 + LOG_SET_SQUARE_INJECTION.LOG_KIND -> { + val logItem = LOG_SET_SQUARE_INJECTION.parse(logDataToHexString) + aapsLogger.debug(LTag.PUMPCOMM, "$logItem ") + val logStartDate = DateUtils.parseDate(logItem.dttm, "yyyy-MM-dd HH:mm:ss") + val logDateTime = logStartDate.time + val newRecord = pumpSync.syncExtendedBolusWithPumpId( + timestamp = logDateTime, + amount = logItem.setAmount / 100.0, + duration = T.mins((logItem.getInjectTime() * 10).toLong()).msecs(), + isEmulatingTB = false, + pumpId = logDateTime, + pumpType = PumpType.DIACONN_G8, + pumpSerial = diaconnG8Pump.serialNo.toString()) + aapsLogger.debug(LTag.PUMPCOMM, (if (newRecord) "**NEW** " else "") + "EVENT EXTENDEDSTART (" + pumplogKind + ") " + dateUtil.dateAndTimeString(logDateTime) + " (" + logDateTime + ")" + " Amount: " + logItem.setAmount / 100.0 + "U Duration: " + logItem.getInjectTime() * 10 + "min") + diaconnG8HistoryRecord.code = RecordTypes.RECORD_TYPE_BOLUS + diaconnG8HistoryRecord.timestamp = logDateTime + diaconnG8HistoryRecord.value = logItem.setAmount / 100.0 + diaconnG8HistoryRecord.duration = logItem.getInjectTime() + diaconnG8HistoryRecord.stringValue = rh.gs(R.string.diaconn_g8_logsquarestart) + diaconnG8HistoryRecord.bolusType = "E" // Extended + diaconnHistoryRecordDao.createOrUpdate(diaconnG8HistoryRecord) + status = "EXTENDEDBOLUSSTART " + dateUtil.timeString(logDateTime) } - val temporaryBasalInfo = temporaryBasalStorage.findTemporaryBasal(logDateTime, absoluteRate) - val newRecord = pumpSync.syncTemporaryBasalWithPumpId( - timestamp = logDateTime, - rate = absoluteRate, - duration = T.mins((logItem.tbTime * 15).toLong()).msecs(), - isAbsolute = true, - type = temporaryBasalInfo?.type, - pumpId = logDateTime, - pumpType = PumpType.DIACONN_G8, - pumpSerial = diaconnG8Pump.serialNo.toString()) - aapsLogger.debug(LTag.PUMPCOMM, (if (newRecord) "**NEW** " else "") + "EVENT TEMPSTART (" + pumplogKind + ") " + dateUtil.dateAndTimeString(logDateTime) + " (" + logDateTime + ")" + " Ratio: " + absoluteRate + "U Duration: " + logItem.tbTime * 15 + "min") - - diaconnG8HistoryRecord.code = RecordTypes.RECORD_TYPE_TB - diaconnG8HistoryRecord.timestamp = logDateTime - diaconnG8HistoryRecord.duration = logItem.tbTime * 15 - diaconnG8HistoryRecord.value = absoluteRate - diaconnG8HistoryRecord.stringValue = rh.gs(R.string.diaconn_g8_logtempstart) - diaconnHistoryRecordDao.createOrUpdate(diaconnG8HistoryRecord) - status = "TEMPSTART " + dateUtil.timeString(logDateTime) - } - - LOG_TB_STOP_V3.LOG_KIND -> { - val logItem = LOG_TB_STOP_V3.parse(logDataToHexString) - aapsLogger.debug(LTag.PUMPCOMM, "$logItem ") - val logStartDate = DateUtils.parseDate(logItem.dttm, "yyyy-MM-dd HH:mm:ss") - val logDateTime = logStartDate.time - var absoluteRate = 0.0 - if (logItem.getTbInjectRateRatio() >= 50000) { - val tempBasalPercent = logItem.getTbInjectRateRatio() - 50000 - absoluteRate = diaconnG8Pump.baseAmount * (tempBasalPercent / 100.0) - } - if (logItem.getTbInjectRateRatio() in 1000..2500) { - absoluteRate = (logItem.getTbInjectRateRatio() - 1000) / 100.0 + LOG_INJECT_SQUARE_SUCCESS.LOG_KIND -> { + val logItem = LOG_INJECT_SQUARE_SUCCESS.parse(logDataToHexString) + aapsLogger.debug(LTag.PUMPCOMM, "$logItem ") + val logStartDate = DateUtils.parseDate(logItem.dttm, "yyyy-MM-dd HH:mm:ss") + val logDateTime = logStartDate.time + diaconnG8HistoryRecord.code = RecordTypes.RECORD_TYPE_BOLUS + diaconnG8HistoryRecord.timestamp = logDateTime + diaconnG8HistoryRecord.duration = logItem.getInjectTime() + diaconnG8HistoryRecord.stringValue = rh.gs(R.string.diaconn_g8_logsquaresuccess) + diaconnG8HistoryRecord.bolusType = "E" // Extended + diaconnHistoryRecordDao.createOrUpdate(diaconnG8HistoryRecord) + status = "EXTENDEDBOLUSEND " + dateUtil.timeString(logDateTime) } - val newRecord = pumpSync.syncStopTemporaryBasalWithPumpId( - timestamp = logDateTime, - endPumpId = dateUtil.now(), - pumpType = PumpType.DIACONN_G8, - pumpSerial = diaconnG8Pump.serialNo.toString()) - aapsLogger.debug(LTag.PUMPCOMM, (if (newRecord) "**NEW** " else "") + "EVENT TEMPSTOP (" + pumplogKind + ") " + dateUtil.dateAndTimeString(logDateTime) + " (" + logDateTime + ")") + LOG_INJECT_SQUARE_FAIL.LOG_KIND -> { + val logItem = LOG_INJECT_SQUARE_FAIL.parse(logDataToHexString) + aapsLogger.debug(LTag.PUMPCOMM, "$logItem ") + val logStartDate = DateUtils.parseDate(logItem.dttm, "yyyy-MM-dd HH:mm:ss") + val logDateTime = logStartDate.time + val newRecord = pumpSync.syncStopExtendedBolusWithPumpId( + timestamp = logDateTime, + endPumpId = logDateTime, + pumpType = PumpType.DIACONN_G8, + pumpSerial = diaconnG8Pump.serialNo.toString()) + aapsLogger.debug(LTag.PUMPCOMM, (if (newRecord) "**NEW** " else "") + "EVENT EXTENDEDSTOP (" + pumplogKind + ") " + dateUtil.dateAndTimeString(logDateTime) + " (" + logDateTime + ")" + " Delivered: " + logItem.injectAmount / 100.0 + "U RealDuration: " + logItem.getInjectTime() + "min") + diaconnG8HistoryRecord.code = RecordTypes.RECORD_TYPE_BOLUS + diaconnG8HistoryRecord.timestamp = logDateTime + diaconnG8HistoryRecord.value = logItem.injectAmount / 100.0 + diaconnG8HistoryRecord.duration = logItem.getInjectTime() + diaconnG8HistoryRecord.stringValue = getReasonName(pumplogKind, logItem.reason) + diaconnG8HistoryRecord.bolusType = "E" + diaconnHistoryRecordDao.createOrUpdate(diaconnG8HistoryRecord) + status = "EXTENDEDBOLUSFAIL " + dateUtil.timeString(logDateTime) + } - diaconnG8HistoryRecord.code = RecordTypes.RECORD_TYPE_TB - diaconnG8HistoryRecord.timestamp = logDateTime - diaconnG8HistoryRecord.value = absoluteRate - diaconnG8HistoryRecord.stringValue = getReasonName(pumplogKind, logItem.reason) - diaconnHistoryRecordDao.createOrUpdate(diaconnG8HistoryRecord) - status = "TEMPSTOP " + dateUtil.timeString(logDateTime) - } + LOG_SET_DUAL_INJECTION.LOG_KIND -> { + val logItem = LOG_SET_DUAL_INJECTION.parse(logDataToHexString) + aapsLogger.debug(LTag.PUMPCOMM, "$logItem ") + val logStartDate = DateUtils.parseDate(logItem.dttm, "yyyy-MM-dd HH:mm:ss") + val logDateTime = logStartDate.time - LOG_ALARM_BATTERY.LOG_KIND -> { // BATTERY SHORTAGE ALARM - val logItem = LOG_ALARM_BATTERY.parse(logDataToHexString) - aapsLogger.debug(LTag.PUMPCOMM, "$logItem ") - val logStartDate = DateUtils.parseDate(logItem.dttm, "yyyy-MM-dd HH:mm:ss") - val logDateTime = logStartDate.time + // dual square 처리. + val newRecord = pumpSync.syncExtendedBolusWithPumpId( + timestamp = logDateTime, + amount = logItem.setSquareAmount / 100.0, + duration = T.mins((logItem.getInjectTime() * 10).toLong()).msecs(), + isEmulatingTB = false, + pumpId = logDateTime, + pumpType = PumpType.DIACONN_G8, + pumpSerial = diaconnG8Pump.serialNo.toString()) + aapsLogger.debug(LTag.PUMPCOMM, (if (newRecord) "**NEW** " else "") + "EVENT EXTENDEDSTART (" + pumplogKind + ") " + dateUtil.dateAndTimeString(logDateTime) + " (" + logDateTime + ")" + " Amount: " + logItem.setSquareAmount / 100.0 + "U Duration: " + logItem.getInjectTime() * 10 + "min") + diaconnG8HistoryRecord.code = RecordTypes.RECORD_TYPE_BOLUS + diaconnG8HistoryRecord.timestamp = logDateTime + diaconnG8HistoryRecord.value = logItem.setSquareAmount / 100.0 + diaconnG8HistoryRecord.duration = logItem.getInjectTime() * 10 // (1~30) 1:10min 30:300min + diaconnG8HistoryRecord.stringValue = rh.gs(R.string.diaconn_g8_logdualsquarestart) + diaconnG8HistoryRecord.bolusType = "D" // Extended + diaconnHistoryRecordDao.createOrUpdate(diaconnG8HistoryRecord) - diaconnG8HistoryRecord.code = RecordTypes.RECORD_TYPE_ALARM - diaconnG8HistoryRecord.timestamp = logDateTime - diaconnG8HistoryRecord.stringValue = rh.gs(R.string.diaconn_g8_logbatteryshorage) - diaconnHistoryRecordDao.createOrUpdate(diaconnG8HistoryRecord) - status = "BATTERYALARM " + dateUtil.timeString(logDateTime) - } + status = "DUALEXTENTEDSTART " + dateUtil.timeString(logDateTime) + } - LOG_ALARM_BLOCK.LOG_KIND -> { // INJECTION BLOCKED ALARM - val logItem = LOG_ALARM_BLOCK.parse(logDataToHexString) - aapsLogger.debug(LTag.PUMPCOMM, "$logItem ") + LOG_INJECTION_DUAL_NORMAL.LOG_KIND -> { + val logItem = LOG_INJECTION_DUAL_NORMAL.parse(logDataToHexString) + aapsLogger.debug(LTag.PUMPCOMM, "$logItem ") + val logStartDate = DateUtils.parseDate(logItem.dttm, "yyyy-MM-dd HH:mm:ss") + val logDateTime = logStartDate.time + val detailedBolusInfo = detailedBolusInfoStorage.findDetailedBolusInfo(logDateTime, logItem.injectAmount / 100.0) + val newRecord = pumpSync.syncBolusWithPumpId( + timestamp = logDateTime, + amount = logItem.injectAmount / 100.0, + type = detailedBolusInfo?.bolusType, + pumpId = logDateTime, + pumpType = PumpType.DIACONN_G8, + pumpSerial = diaconnG8Pump.serialNo.toString()) + aapsLogger.debug(LTag.PUMPCOMM, (if (newRecord) "**NEW** " else "") + "EVENT DUALBOLUS (" + pumplogKind + ") " + dateUtil.dateAndTimeString(logDateTime) + " (" + logDateTime + ")" + " Bolus: " + logItem.injectAmount / 100.0 + "U Duration: " + logItem.getInjectTime() + "min") - val logStartDate = DateUtils.parseDate(logItem.dttm, "yyyy-MM-dd HH:mm:ss") - val logDateTime = logStartDate.time + diaconnG8Pump.lastBolusAmount = logItem.injectAmount / 100.0 + diaconnG8Pump.lastBolusTime = logDateTime - diaconnG8HistoryRecord.code = RecordTypes.RECORD_TYPE_ALARM - diaconnG8HistoryRecord.timestamp = logDateTime - diaconnG8HistoryRecord.value = logItem.amount / 100.0 - diaconnG8HistoryRecord.stringValue = rh.gs(R.string.diaconn_g8_logalarmblock, getReasonName(pumplogKind, logItem.reason)) - diaconnHistoryRecordDao.createOrUpdate(diaconnG8HistoryRecord) - status = "BLOCKALARM " + dateUtil.timeString(logDateTime) - } + //Diaconn History + diaconnG8HistoryRecord.code = RecordTypes.RECORD_TYPE_BOLUS + diaconnG8HistoryRecord.timestamp = logDateTime + diaconnG8HistoryRecord.value = logItem.injectAmount / 100.0 + diaconnG8HistoryRecord.duration = logItem.getInjectTime() + diaconnG8HistoryRecord.bolusType = "D" // bolus + diaconnG8HistoryRecord.stringValue = rh.gs(R.string.diaconn_g8_logdualnormalsuccess) + diaconnHistoryRecordDao.createOrUpdate(diaconnG8HistoryRecord) + if (!newRecord && detailedBolusInfo != null) { + // detailedInfo can be from another similar record. Reinsert + detailedBolusInfoStorage.add(detailedBolusInfo) + } + status = "DUALBOLUS" + dateUtil.timeString(logDateTime) + } - LOG_ALARM_SHORTAGE.LOG_KIND -> { // INSULIN SHORTAGE ALARM - val logItem = LOG_ALARM_SHORTAGE.parse(logDataToHexString) - aapsLogger.debug(LTag.PUMPCOMM, "$logItem ") + LOG_INJECT_DUAL_SUCCESS.LOG_KIND -> { + val logItem = LOG_INJECT_DUAL_SUCCESS.parse(logDataToHexString) + aapsLogger.debug(LTag.PUMPCOMM, "$logItem ") + val logStartDate = DateUtils.parseDate(logItem.dttm, "yyyy-MM-dd HH:mm:ss") + val logDateTime = logStartDate.time - val logStartDate = DateUtils.parseDate(logItem.dttm, "yyyy-MM-dd HH:mm:ss") - val logDateTime = logStartDate.time + diaconnG8HistoryRecord.code = RecordTypes.RECORD_TYPE_BOLUS + diaconnG8HistoryRecord.timestamp = logDateTime + diaconnG8HistoryRecord.value = logItem.injectSquareAmount / 100.0 + diaconnG8HistoryRecord.duration = logItem.getInjectTime() + diaconnG8HistoryRecord.bolusType = "D" + diaconnG8HistoryRecord.stringValue = rh.gs(R.string.diaconn_g8_logdualsquaresuccess) + diaconnHistoryRecordDao.createOrUpdate(diaconnG8HistoryRecord) + status = "DUALBOLUS SQUARESUCCESS " + dateUtil.timeString(logDateTime) + } - diaconnG8HistoryRecord.code = RecordTypes.RECORD_TYPE_ALARM - diaconnG8HistoryRecord.timestamp = logDateTime - diaconnG8HistoryRecord.value = logItem.remain.toDouble() - diaconnG8HistoryRecord.stringValue = rh.gs(R.string.diaconn_g8_loginsulinshorage) - diaconnHistoryRecordDao.createOrUpdate(diaconnG8HistoryRecord) - status = "SHORTAGEALARM " + dateUtil.timeString(logDateTime) - } + LOG_INJECT_DUAL_FAIL.LOG_KIND -> { + val logItem = LOG_INJECT_DUAL_FAIL.parse(logDataToHexString) + aapsLogger.debug(LTag.PUMPCOMM, "$logItem ") + val logStartDate = DateUtils.parseDate(logItem.dttm, "yyyy-MM-dd HH:mm:ss") + val logDateTime = logStartDate.time + val newRecord = pumpSync.syncStopExtendedBolusWithPumpId( + timestamp = logDateTime, + endPumpId = logDateTime, + pumpType = PumpType.DIACONN_G8, + pumpSerial = diaconnG8Pump.serialNo.toString()) + aapsLogger.debug(LTag.PUMPCOMM, (if (newRecord) "**NEW** " else "") + "EVENT EXTENDEDSTOP (" + pumplogKind + ") " + dateUtil.dateAndTimeString(logDateTime) + " (" + logDateTime + ")" + " Delivered: " + logItem.injectSquareAmount / 100.0 + "U RealDuration: " + logItem.getInjectTime() + "min") - LOG_RESET_SYS_V3.LOG_KIND -> { - val logItem = LOG_RESET_SYS_V3.parse(logDataToHexString) - aapsLogger.debug(LTag.PUMPCOMM, "$logItem ") + diaconnG8HistoryRecord.code = RecordTypes.RECORD_TYPE_BOLUS + diaconnG8HistoryRecord.timestamp = logDateTime + diaconnG8HistoryRecord.value = logItem.injectNormAmount / 100.0 + logItem.injectSquareAmount / 100.0 + diaconnG8HistoryRecord.duration = logItem.getInjectTime() + diaconnG8HistoryRecord.bolusType = "D" + diaconnG8HistoryRecord.stringValue = getReasonName(pumplogKind, logItem.reason) + diaconnHistoryRecordDao.createOrUpdate(diaconnG8HistoryRecord) + status = "DUALBOLUS FAIL " + dateUtil.timeString(logDateTime) + } - val logStartDate = DateUtils.parseDate(logItem.dttm, "yyyy-MM-dd HH:mm:ss") - val logDateTime = logStartDate.time + LOG_INJECTION_1HOUR_BASAL.LOG_KIND -> { + val logItem = LOG_INJECTION_1HOUR_BASAL.parse(logDataToHexString) + aapsLogger.debug(LTag.PUMPCOMM, "$logItem ") + val logStartDate = DateUtils.parseDate(logItem.dttm, "yyyy-MM-dd HH:mm:ss") + val logDateTime = logStartDate.time + diaconnG8HistoryRecord.code = RecordTypes.RECORD_TYPE_BASALHOUR + diaconnG8HistoryRecord.timestamp = logDateTime + diaconnG8HistoryRecord.value = logItem.beforeAmount / 100.0 + diaconnG8HistoryRecord.stringValue = "TB before: ${logItem.beforeAmount / 100.0} / TB after: ${logItem.afterAmount / 100.0}" + diaconnHistoryRecordDao.createOrUpdate(diaconnG8HistoryRecord) + status = "1HOUR BASAL " + dateUtil.dateAndTimeString(logDateTime) + } - diaconnG8HistoryRecord.code = RecordTypes.RECORD_TYPE_ALARM - diaconnG8HistoryRecord.timestamp = logDateTime - diaconnG8HistoryRecord.stringValue = getReasonName(pumplogKind, logItem.reason) - diaconnHistoryRecordDao.createOrUpdate(diaconnG8HistoryRecord) - if (logItem.reason == 3.toByte()) { - if (sp.getBoolean(R.string.key_diaconn_g8_logbatterychange, true)) { + LOG_SUSPEND_V2.LOG_KIND -> { + val logItem = LOG_SUSPEND_V2.parse(logDataToHexString) + aapsLogger.debug(LTag.PUMPCOMM, "$logItem ") + val logStartDate = DateUtils.parseDate(logItem.dttm, "yyyy-MM-dd HH:mm:ss") + val logDateTime = logStartDate.time + diaconnG8HistoryRecord.code = RecordTypes.RECORD_TYPE_SUSPEND + diaconnG8HistoryRecord.timestamp = logDateTime + diaconnG8HistoryRecord.stringValue = rh.gs(R.string.diaconn_g8_lgosuspend, logItem.getBasalPattern()) + diaconnHistoryRecordDao.createOrUpdate(diaconnG8HistoryRecord) + status = "SUSPEND " + dateUtil.timeString(logDateTime) + } + + LOG_SUSPEND_RELEASE_V2.LOG_KIND -> { + val logItem = LOG_SUSPEND_RELEASE_V2.parse(logDataToHexString) + aapsLogger.debug(LTag.PUMPCOMM, "$logItem ") + val logStartDate = DateUtils.parseDate(logItem.dttm, "yyyy-MM-dd HH:mm:ss") + val logDateTime = logStartDate.time + diaconnG8HistoryRecord.code = RecordTypes.RECORD_TYPE_SUSPEND + diaconnG8HistoryRecord.timestamp = logDateTime + diaconnG8HistoryRecord.stringValue = rh.gs(R.string.diaconn_g8_lgorelease, logItem.getBasalPattern()) + diaconnHistoryRecordDao.createOrUpdate(diaconnG8HistoryRecord) + status = "SUSPENDRELEASE " + dateUtil.timeString(logDateTime) + } + + LOG_CHANGE_INJECTOR_SUCCESS.LOG_KIND -> { + val logItem = LOG_CHANGE_INJECTOR_SUCCESS.parse(logDataToHexString) + aapsLogger.debug(LTag.PUMPCOMM, "$logItem ") + val logStartDate = DateUtils.parseDate(logItem.dttm, "yyyy-MM-dd HH:mm:ss") + val logDateTime = logStartDate.time + if (sp.getBoolean(R.string.key_diaconn_g8_loginsulinchange, true)) { val newRecord = pumpSync.insertTherapyEventIfNewWithTimestamp( timestamp = logDateTime, - type = DetailedBolusInfo.EventType.PUMP_BATTERY_CHANGE, + type = DetailedBolusInfo.EventType.INSULIN_CHANGE, pumpId = logDateTime, pumpType = PumpType.DIACONN_G8, pumpSerial = diaconnG8Pump.serialNo.toString() ) - aapsLogger.debug(LTag.PUMPCOMM, (if (newRecord) "**NEW** " else "") + "EVENT BATTERYCHANGE(" + pumplogKind + ") " + dateUtil.dateAndTimeString(logDateTime) + " (" + logDateTime + ")" + " remainAmount: " + logItem.batteryRemain.toInt() + "%") + aapsLogger.debug(LTag.PUMPCOMM, (if (newRecord) "**NEW** " else "") + "EVENT INSULINCHANGE(" + pumplogKind + ") " + dateUtil.dateAndTimeString(logDateTime) + " (" + logDateTime + ")" + " Amount: " + logItem.remainAmount / 100.0 + "U") + } + diaconnG8HistoryRecord.code = RecordTypes.RECORD_TYPE_REFILL + diaconnG8HistoryRecord.timestamp = logDateTime + diaconnG8HistoryRecord.value = logItem.remainAmount / 100.0 + diaconnG8HistoryRecord.stringValue = rh.gs(R.string.diaconn_g8_loginjectorprime, logItem.primeAmount / 100.0) + diaconnHistoryRecordDao.createOrUpdate(diaconnG8HistoryRecord) + status = "INSULINCHANGE " + dateUtil.timeString(logDateTime) + } + + LOG_CHANGE_TUBE_SUCCESS.LOG_KIND -> { + val logItem = LOG_CHANGE_TUBE_SUCCESS.parse(logDataToHexString) + aapsLogger.debug(LTag.PUMPCOMM, "$logItem ") + val logStartDate = DateUtils.parseDate(logItem.dttm, "yyyy-MM-dd HH:mm:ss") + val logDateTime = logStartDate.time + if (sp.getBoolean(R.string.key_diaconn_g8_logtubechange, true)) { + val newRecord = pumpSync.insertTherapyEventIfNewWithTimestamp( + timestamp = logDateTime, + type = DetailedBolusInfo.EventType.NOTE, + note = rh.gs(R.string.diaconn_g8_logtubeprime, logItem.primeAmount / 100.0), + pumpId = logDateTime, + pumpType = PumpType.DIACONN_G8, + pumpSerial = diaconnG8Pump.serialNo.toString() + ) + aapsLogger.debug(LTag.PUMPCOMM, (if (newRecord) "**NEW** " else "") + "EVENT TUBECHANGE(" + pumplogKind + ") " + dateUtil.dateAndTimeString(logDateTime) + " (" + logDateTime + ")" + " Amount: " + logItem.primeAmount / 100.0 + "U") + } + + + diaconnG8HistoryRecord.code = RecordTypes.RECORD_TYPE_REFILL + diaconnG8HistoryRecord.timestamp = logDateTime + diaconnG8HistoryRecord.value = logItem.remainAmount / 100.0 + diaconnG8HistoryRecord.stringValue = rh.gs(R.string.diaconn_g8_logtubeprime, logItem.primeAmount / 100.0) + diaconnHistoryRecordDao.createOrUpdate(diaconnG8HistoryRecord) + status = "TUBECHANGE " + dateUtil.timeString(logDateTime) + } + + LOG_INJECTION_1DAY.LOG_KIND -> { // Daily Bolus Log + val logItem = LOG_INJECTION_1DAY.parse(logDataToHexString) + aapsLogger.debug(LTag.PUMPCOMM, "$logItem ") + val logStartDate = DateUtils.parseDate(logItem.dttm, "yyyy-MM-dd HH:mm:ss") + val logDateTime = logStartDate.time + + diaconnG8HistoryRecord.code = RecordTypes.RECORD_TYPE_DAILY + diaconnG8HistoryRecord.timestamp = DateTime(logDateTime).withTimeAtStartOfDay().millis + diaconnG8HistoryRecord.dailyBolus = logItem.extAmount / 100.0 + logItem.mealAmount / 100.0 + + val recordDateStr = "" + diaconnG8HistoryRecord.timestamp + var recordMap: MutableMap = mutableMapOf("dummy" to 0.0) + + if (dailyMaxvalInfo.containsKey(recordDateStr)) { + recordMap = dailyMaxvalInfo[recordDateStr]!! + } else { + recordMap["bolus"] = 0.0 + recordMap["basal"] = 0.0 + dailyMaxvalInfo[recordDateStr] = recordMap + } + + if (diaconnG8HistoryRecord.dailyBolus > recordMap["bolus"]!!) { + recordMap["bolus"] = diaconnG8HistoryRecord.dailyBolus + } else { + diaconnG8HistoryRecord.dailyBolus = recordMap["bolus"]!! + } + + if (recordMap["basal"]!! > 0.0) { + diaconnG8HistoryRecord.dailyBasal = recordMap["basal"]!! + } + + diaconnHistoryRecordDao.createOrUpdate(diaconnG8HistoryRecord) + + //If it is a TDD, store it for stats also. + pumpSync.createOrUpdateTotalDailyDose( + timestamp = diaconnG8HistoryRecord.timestamp, + bolusAmount = diaconnG8HistoryRecord.dailyBolus, + basalAmount = diaconnG8HistoryRecord.dailyBasal, + totalAmount = 0.0, + pumpId = null, + pumpType = PumpType.DIACONN_G8, + diaconnG8Pump.serialNo.toString() + ) + + status = "DAILYBOLUS " + dateUtil.timeString(logDateTime) + } + + LOG_INJECTION_1DAY_BASAL.LOG_KIND -> { // Daily Basal Log + val logItem = LOG_INJECTION_1DAY_BASAL.parse(logDataToHexString) + aapsLogger.debug(LTag.PUMPCOMM, "$logItem ") + val logStartDate = DateUtils.parseDate(logItem.dttm, "yyyy-MM-dd HH:mm:ss") + val logDateTime = logStartDate.time + + diaconnG8HistoryRecord.code = RecordTypes.RECORD_TYPE_DAILY + diaconnG8HistoryRecord.timestamp = DateTime(logDateTime).withTimeAtStartOfDay().millis + diaconnG8HistoryRecord.dailyBasal = logItem.amount / 100.0 + + val recordDateStr = "" + diaconnG8HistoryRecord.timestamp + var recordMap: MutableMap = mutableMapOf("dummy" to 0.0) + + if (dailyMaxvalInfo.containsKey(recordDateStr)) { + recordMap = dailyMaxvalInfo[recordDateStr]!! + } else { + recordMap["bolus"] = 0.0 + recordMap["basal"] = 0.0 + dailyMaxvalInfo[recordDateStr] = recordMap + } + + if (diaconnG8HistoryRecord.dailyBasal > recordMap["basal"]!!) { + recordMap["basal"] = diaconnG8HistoryRecord.dailyBasal + } else { + diaconnG8HistoryRecord.dailyBasal = recordMap["basal"]!! + } + + if (recordMap["bolus"]!! > 0.0) { + diaconnG8HistoryRecord.dailyBolus = recordMap["bolus"]!! + } + + diaconnHistoryRecordDao.createOrUpdate(diaconnG8HistoryRecord) + + //If it is a TDD, store it for stats also. + // pumpSync.createOrUpdateTotalDailyDose( + // timestamp = diaconnG8HistoryRecord.timestamp, + // bolusAmount = diaconnG8HistoryRecord.dailyBolus, + // basalAmount = diaconnG8HistoryRecord.dailyBasal, + // totalAmount = 0.0, + // pumpId = null, + // pumpType = PumpType.DIACONN_G8, + // diaconnG8Pump.serialNo.toString() + // ) + + + status = "DAILYBASAL " + dateUtil.timeString(logDateTime) + } + + LOG_CHANGE_NEEDLE_SUCCESS.LOG_KIND -> { + val logItem = LOG_CHANGE_NEEDLE_SUCCESS.parse(logDataToHexString) + aapsLogger.debug(LTag.PUMPCOMM, "$logItem ") + val logStartDate = DateUtils.parseDate(logItem.dttm, "yyyy-MM-dd HH:mm:ss") + val logDateTime = logStartDate.time + if (sp.getBoolean(R.string.key_diaconn_g8_logneedlechange, true)) { + val newRecord = pumpSync.insertTherapyEventIfNewWithTimestamp( + timestamp = logDateTime, + type = DetailedBolusInfo.EventType.CANNULA_CHANGE, + pumpId = logDateTime, + pumpType = PumpType.DIACONN_G8, + pumpSerial = diaconnG8Pump.serialNo.toString() + ) + aapsLogger.debug(LTag.PUMPCOMM, (if (newRecord) "**NEW** " else "") + "EVENT NEEDLECHANGE(" + pumplogKind + ") " + dateUtil.dateAndTimeString(logDateTime) + " (" + logDateTime + ")" + " Amount: " + logItem.remainAmount / 100.0 + "U") + } + + diaconnG8HistoryRecord.code = RecordTypes.RECORD_TYPE_REFILL + diaconnG8HistoryRecord.timestamp = logDateTime + diaconnG8HistoryRecord.value = logItem.remainAmount / 100.0 + diaconnG8HistoryRecord.stringValue = rh.gs(R.string.diaconn_g8_logneedleprime, logItem.primeAmount / 100.0) + diaconnHistoryRecordDao.createOrUpdate(diaconnG8HistoryRecord) + status = "NEEDLECHANGE " + dateUtil.timeString(logDateTime) + } + + LOG_TB_START_V3.LOG_KIND -> { + val logItem = LOG_TB_START_V3.parse(logDataToHexString) + aapsLogger.debug(LTag.PUMPCOMM, "$logItem ") + + val logStartDate = DateUtils.parseDate(logItem.dttm, "yyyy-MM-dd HH:mm:ss") + val logDateTime = logStartDate.time + var absoluteRate = 0.0 + if (logItem.getTbInjectRateRatio() >= 50000) { + val tempBasalPercent = logItem.getTbInjectRateRatio() - 50000 + absoluteRate = pumpDesc.pumpType.determineCorrectBasalSize(diaconnG8Pump.baseAmount * (tempBasalPercent / 100.0)) + } + + if (logItem.getTbInjectRateRatio() in 1000..2500) { + absoluteRate = (logItem.getTbInjectRateRatio() - 1000) / 100.0 + } + + val temporaryBasalInfo = temporaryBasalStorage.findTemporaryBasal(logDateTime, absoluteRate) + val newRecord = pumpSync.syncTemporaryBasalWithPumpId( + timestamp = logDateTime, + rate = absoluteRate, + duration = T.mins((logItem.tbTime * 15).toLong()).msecs(), + isAbsolute = true, + type = temporaryBasalInfo?.type, + pumpId = logDateTime, + pumpType = PumpType.DIACONN_G8, + pumpSerial = diaconnG8Pump.serialNo.toString()) + aapsLogger.debug(LTag.PUMPCOMM, (if (newRecord) "**NEW** " else "") + "EVENT TEMPSTART (" + pumplogKind + ") " + dateUtil.dateAndTimeString(logDateTime) + " (" + logDateTime + ")" + " Ratio: " + absoluteRate + "U Duration: " + logItem.tbTime * 15 + "min") + + diaconnG8HistoryRecord.code = RecordTypes.RECORD_TYPE_TB + diaconnG8HistoryRecord.timestamp = logDateTime + diaconnG8HistoryRecord.duration = logItem.tbTime * 15 + diaconnG8HistoryRecord.value = absoluteRate + diaconnG8HistoryRecord.stringValue = rh.gs(R.string.diaconn_g8_logtempstart) + diaconnHistoryRecordDao.createOrUpdate(diaconnG8HistoryRecord) + status = "TEMPSTART " + dateUtil.timeString(logDateTime) + } + + LOG_TB_STOP_V3.LOG_KIND -> { + val logItem = LOG_TB_STOP_V3.parse(logDataToHexString) + aapsLogger.debug(LTag.PUMPCOMM, "$logItem ") + val logStartDate = DateUtils.parseDate(logItem.dttm, "yyyy-MM-dd HH:mm:ss") + val logDateTime = logStartDate.time + var absoluteRate = 0.0 + if (logItem.getTbInjectRateRatio() >= 50000) { + val tempBasalPercent = logItem.getTbInjectRateRatio() - 50000 + absoluteRate = diaconnG8Pump.baseAmount * (tempBasalPercent / 100.0) + } + if (logItem.getTbInjectRateRatio() in 1000..2500) { + absoluteRate = (logItem.getTbInjectRateRatio() - 1000) / 100.0 + } + + val newRecord = pumpSync.syncStopTemporaryBasalWithPumpId( + timestamp = logDateTime, + endPumpId = dateUtil.now(), + pumpType = PumpType.DIACONN_G8, + pumpSerial = diaconnG8Pump.serialNo.toString()) + aapsLogger.debug(LTag.PUMPCOMM, (if (newRecord) "**NEW** " else "") + "EVENT TEMPSTOP (" + pumplogKind + ") " + dateUtil.dateAndTimeString(logDateTime) + " (" + logDateTime + ")") + + + diaconnG8HistoryRecord.code = RecordTypes.RECORD_TYPE_TB + diaconnG8HistoryRecord.timestamp = logDateTime + diaconnG8HistoryRecord.value = absoluteRate + diaconnG8HistoryRecord.stringValue = getReasonName(pumplogKind, logItem.reason) + diaconnHistoryRecordDao.createOrUpdate(diaconnG8HistoryRecord) + status = "TEMPSTOP " + dateUtil.timeString(logDateTime) + } + + LOG_ALARM_BATTERY.LOG_KIND -> { // BATTERY SHORTAGE ALARM + val logItem = LOG_ALARM_BATTERY.parse(logDataToHexString) + aapsLogger.debug(LTag.PUMPCOMM, "$logItem ") + val logStartDate = DateUtils.parseDate(logItem.dttm, "yyyy-MM-dd HH:mm:ss") + val logDateTime = logStartDate.time + + diaconnG8HistoryRecord.code = RecordTypes.RECORD_TYPE_ALARM + diaconnG8HistoryRecord.timestamp = logDateTime + diaconnG8HistoryRecord.stringValue = rh.gs(R.string.diaconn_g8_logbatteryshorage) + diaconnHistoryRecordDao.createOrUpdate(diaconnG8HistoryRecord) + status = "BATTERYALARM " + dateUtil.timeString(logDateTime) + } + + LOG_ALARM_BLOCK.LOG_KIND -> { // INJECTION BLOCKED ALARM + val logItem = LOG_ALARM_BLOCK.parse(logDataToHexString) + aapsLogger.debug(LTag.PUMPCOMM, "$logItem ") + + val logStartDate = DateUtils.parseDate(logItem.dttm, "yyyy-MM-dd HH:mm:ss") + val logDateTime = logStartDate.time + + diaconnG8HistoryRecord.code = RecordTypes.RECORD_TYPE_ALARM + diaconnG8HistoryRecord.timestamp = logDateTime + diaconnG8HistoryRecord.value = logItem.amount / 100.0 + diaconnG8HistoryRecord.stringValue = rh.gs(R.string.diaconn_g8_logalarmblock, getReasonName(pumplogKind, logItem.reason)) + diaconnHistoryRecordDao.createOrUpdate(diaconnG8HistoryRecord) + status = "BLOCKALARM " + dateUtil.timeString(logDateTime) + } + + LOG_ALARM_SHORTAGE.LOG_KIND -> { // INSULIN SHORTAGE ALARM + val logItem = LOG_ALARM_SHORTAGE.parse(logDataToHexString) + aapsLogger.debug(LTag.PUMPCOMM, "$logItem ") + + val logStartDate = DateUtils.parseDate(logItem.dttm, "yyyy-MM-dd HH:mm:ss") + val logDateTime = logStartDate.time + + diaconnG8HistoryRecord.code = RecordTypes.RECORD_TYPE_ALARM + diaconnG8HistoryRecord.timestamp = logDateTime + diaconnG8HistoryRecord.value = logItem.remain.toDouble() + diaconnG8HistoryRecord.stringValue = rh.gs(R.string.diaconn_g8_loginsulinshorage) + diaconnHistoryRecordDao.createOrUpdate(diaconnG8HistoryRecord) + status = "SHORTAGEALARM " + dateUtil.timeString(logDateTime) + } + + LOG_RESET_SYS_V3.LOG_KIND -> { + val logItem = LOG_RESET_SYS_V3.parse(logDataToHexString) + aapsLogger.debug(LTag.PUMPCOMM, "$logItem ") + + val logStartDate = DateUtils.parseDate(logItem.dttm, "yyyy-MM-dd HH:mm:ss") + val logDateTime = logStartDate.time + + diaconnG8HistoryRecord.code = RecordTypes.RECORD_TYPE_ALARM + diaconnG8HistoryRecord.timestamp = logDateTime + diaconnG8HistoryRecord.stringValue = getReasonName(pumplogKind, logItem.reason) + diaconnHistoryRecordDao.createOrUpdate(diaconnG8HistoryRecord) + if (logItem.reason == 3.toByte()) { + if (sp.getBoolean(R.string.key_diaconn_g8_logbatterychange, true)) { + val newRecord = pumpSync.insertTherapyEventIfNewWithTimestamp( + timestamp = logDateTime, + type = DetailedBolusInfo.EventType.PUMP_BATTERY_CHANGE, + pumpId = logDateTime, + pumpType = PumpType.DIACONN_G8, + pumpSerial = diaconnG8Pump.serialNo.toString() + ) + aapsLogger.debug(LTag.PUMPCOMM, (if (newRecord) "**NEW** " else "") + "EVENT BATTERYCHANGE(" + pumplogKind + ") " + dateUtil.dateAndTimeString(logDateTime) + " (" + logDateTime + ")" + " remainAmount: " + logItem.batteryRemain.toInt() + "%") + } + } + status = "RESET " + dateUtil.timeString(logDateTime) + } + + else -> { + status = rh.gs(R.string.diaconn_g8_logsyncinprogress) + rxBus.send(EventPumpStatusChanged(status)) + continue + } + } + rxBus.send(EventPumpStatusChanged(rh.gs(R.string.processinghistory) + ": " + status)) + } + + } + + // 플랫폼 동기화이면, + if(diaconnG8Pump.isPlatformUploadStarted){ + aapsLogger.debug(LTag.PUMPCOMM, "Diaconn api upload start!!") + var appUid:String = sp.getString(R.string.key_diaconn_g8_appuid, "") + if (appUid.isEmpty()) { + appUid = UUID.randomUUID().toString() + sp.putString(R.string.key_diaconn_g8_appuid, appUid) + } + //api send + val retrofit = diaconnLogUploader.getRetrofitInstance() + val api = retrofit?.create(DiaconnApiService::class.java) + val pumpLogDto = PumpLogDto( + app_uid = appUid, + app_version = context.packageManager.getPackageInfo(context.packageName, 0).versionName, + pump_uid = diaconnG8Pump.pumpUid, + pump_version = diaconnG8Pump.pumpVersion, + incarnation_num = diaconnG8Pump.pumpIncarnationNum, + pumplog_info = pumpLogs, + ) + try { + api?.uploadPumpLogs(pumpLogDto)?.enqueue( + object : retrofit2.Callback { + override fun onResponse(call: Call, response: Response) { + if(response.body()?.ok == true) { + aapsLogger.debug(LTag.PUMPCOMM, "logs upload Success") + } + } + override fun onFailure(call: Call, t: Throwable) { + t.printStackTrace() } } - status = "RESET " + dateUtil.timeString(logDateTime) - } - - else -> { - status = rh.gs(R.string.diaconn_g8_logsyncinprogress) - rxBus.send(EventPumpStatusChanged(status)) - continue - } + ) + } catch(e: Exception) { + aapsLogger.error("Unhandled exception", e) } - rxBus.send(EventPumpStatusChanged(rh.gs(R.string.processinghistory) + ": " + status)) } } @@ -780,5 +846,4 @@ class BigLogInquireResponsePacket( else -> "" } } - -} +} \ No newline at end of file diff --git a/diaconn/src/main/java/info/nightscout/androidaps/diaconn/service/DiaconnG8Service.kt b/diaconn/src/main/java/info/nightscout/androidaps/diaconn/service/DiaconnG8Service.kt index fbd6ab0dbf..b553e3a212 100644 --- a/diaconn/src/main/java/info/nightscout/androidaps/diaconn/service/DiaconnG8Service.kt +++ b/diaconn/src/main/java/info/nightscout/androidaps/diaconn/service/DiaconnG8Service.kt @@ -14,6 +14,8 @@ import info.nightscout.androidaps.data.PumpEnactResult import info.nightscout.androidaps.diaconn.DiaconnG8Plugin import info.nightscout.androidaps.diaconn.DiaconnG8Pump import info.nightscout.androidaps.diaconn.R +import info.nightscout.androidaps.diaconn.api.DiaconnApiService +import info.nightscout.androidaps.diaconn.api.DiaconnLogUploader import info.nightscout.androidaps.diaconn.events.EventDiaconnG8NewStatus import info.nightscout.androidaps.diaconn.events.EventDiaconnG8PumpLogReset import info.nightscout.androidaps.diaconn.packet.* @@ -51,6 +53,7 @@ import java.util.concurrent.TimeUnit import javax.inject.Inject import kotlin.math.abs import kotlin.math.ceil +import kotlin.math.floor import kotlin.math.min class DiaconnG8Service : DaggerService() { @@ -74,6 +77,7 @@ class DiaconnG8Service : DaggerService() { @Inject lateinit var pumpSync: PumpSync @Inject lateinit var dateUtil: DateUtil @Inject lateinit var aapsSchedulers: AapsSchedulers + @Inject lateinit var diaconnLogUploader: DiaconnLogUploader private val disposable = CompositeDisposable() private val mBinder: IBinder = LocalBinder() @@ -137,11 +141,14 @@ class DiaconnG8Service : DaggerService() { } private fun sendMessage(message: DiaconnG8Packet) { - bleCommonService.sendMessage(message, 500) + bleCommonService.sendMessage(message, 2000) } fun readPumpStatus() { try { + + aapsLogger.error(LTag.PUMPCOMM, "패키지 명!!! : "+context.packageManager.getPackageInfo(context.packageName, 0).versionName) + val pump = activePlugin.activePump rxBus.send(EventPumpStatusChanged(rh.gs(R.string.gettingpumpsettings))) @@ -283,51 +290,89 @@ class DiaconnG8Service : DaggerService() { apsWrappingCount = sp.getInt(rh.gs(R.string.apsWrappingCount), 0) apsLastLogNum = sp.getInt(rh.gs(R.string.apslastLogNum), 0) - val apsLastNum = apsWrappingCount * 10000 + apsLastLogNum + aapsLogger.debug(LTag.PUMPCOMM, "apsWrappingCount : $apsWrappingCount, apsLastLogNum : $apsLastLogNum") + if ((pumpWrappingCount * 10000 + pumpLastNum) < apsLastLogNum) { pumpLogDefaultSetting() } - - val start: Int? // log sync startNo - val end: Int? // log sync endNo - if (((pumpWrappingCount * 10000 + pumpLastNum) - apsLastNum) > 10000) { - start = pumpLastNum - end = 10000 - } else if (pumpWrappingCount > apsWrappingCount && apsLastLogNum < 9999) { - start = apsLastLogNum + 1 - end = 10000 - } else if (pumpWrappingCount > apsWrappingCount && apsLastLogNum >= 9999) { - start = 0 - end = pumpLastNum - } else { - start = apsLastLogNum + 1 - end = pumpLastNum - } - // pump log loop size val pumpLogPageSize = 11 - val loopCount: Int = ceil(((end - start) / 11.0)).toInt() - + val (start, end, loopSize) = getLogLoopCount(apsLastLogNum, apsWrappingCount, pumpLastNum, pumpWrappingCount) // log sync start! - if (loopCount > 0) { - diaconnG8Pump.isProgressPumpLogSync = true - - for (i in 0 until loopCount) { + if (loopSize > 0) { + for (i in 0 until loopSize) { val startLogNo: Int = start + i * pumpLogPageSize val endLogNo: Int = startLogNo + min(end - startLogNo, pumpLogPageSize) val msg = BigLogInquirePacket(injector, startLogNo, endLogNo, 100) - sendMessage(msg) - } - diaconnG8Pump.historyDoneReceived = true - while (!diaconnG8Pump.historyDoneReceived && bleCommonService.isConnected) { - SystemClock.sleep(100) + sendMessage(msg, 500) } result.success(true) diaconnG8Pump.lastConnection = System.currentTimeMillis() } + + // upload pump log to Diaconn Cloud + if (sp.getBoolean(R.string.key_diaconn_g8_cloudsend, true)) { + SystemClock.sleep(1000) + try { + // getting last uploaded log number + val retrofit = diaconnLogUploader.getRetrofitInstance() + val api = retrofit?.create(DiaconnApiService::class.java) + val response = api?.getPumpLastNo(diaconnG8Pump.pumpUid, diaconnG8Pump.pumpVersion, diaconnG8Pump.pumpIncarnationNum)?.execute() + if(response?.body()?.ok == true) { + aapsLogger.debug(LTag.PUMPCOMM, "pumplog_no = ${response.body()?.info?.pumplog_no}") + val platformLastNo = response.body()?.info?.pumplog_no!! + val platformWrappingCount: Int = floor(platformLastNo / 10000.0).toInt() + val platformLogNo: Int = if (platformLastNo.toInt() == -1) { + 9999 + } else { + (platformLastNo % 10000).toInt() + } + aapsLogger.debug(LTag.PUMPCOMM, "platformLogNo: $platformLogNo, platformWrappingCount: $platformWrappingCount") + + // 페이지 사이즈로 처리할 때 루핑 횟수 계산 + val (platformStart, platformEnd, platformLoopSize) = getLogLoopCount(platformLogNo, platformWrappingCount, pumpLastNum, pumpWrappingCount) + if(platformLoopSize > 0) { + diaconnG8Pump.isPlatformUploadStarted = true + for (i in 0 until platformLoopSize) { + rxBus.send(EventPumpStatusChanged("클라우드동기화 진행 중 : $i / $platformLoopSize")) + val startLogNo: Int = platformStart + i * pumpLogPageSize + val endLogNo: Int = startLogNo + min(platformEnd - startLogNo, pumpLogPageSize) + val msg = BigLogInquirePacket(injector, startLogNo, endLogNo, 100) + sendMessage(msg, 500) + } + SystemClock.sleep(1000) + diaconnG8Pump.isPlatformUploadStarted = false + } + } + } catch (e:Exception) { + aapsLogger.error("Unhandled exception", e) + } + } return result } + private fun getLogLoopCount(lastLogNum: Int, wrappingCount: Int, pumpLastNum: Int, pumpWrappingCount: Int): Triple { + val start: Int// log sync start number + val end: Int // log sync end number1311 + + if (pumpWrappingCount * 10000 + pumpLastNum - lastLogNum > 10000) { + start = pumpLastNum + end = 10000 + } else if (pumpWrappingCount > wrappingCount && lastLogNum < 9999) { + start = (lastLogNum + 1) + end = 10000 + } else if (pumpWrappingCount > wrappingCount && lastLogNum >= 9999) { + start = 0 // 처음부터 시작 + end = pumpLastNum + } else { + start = (lastLogNum + 1) + end = pumpLastNum + } + val size = ceil((end - start) / 11.0).toInt() + // + return Triple(start, end, size) + } + fun setUserSettings(): PumpEnactResult { val result = PumpEnactResult(injector) @@ -344,7 +389,7 @@ class DiaconnG8Service : DaggerService() { if (diaconnG8Pump.otpNumber == 0) { aapsLogger.error(LTag.PUMPCOMM, "otp is not received yet") result.success(false) - result.comment("펌프와 연결 상태를 확인해주세요.") + result.comment("") return result } sendMessage(AppConfirmSettingPacket(injector, msg.msgType, diaconnG8Pump.otpNumber)) @@ -356,6 +401,13 @@ class DiaconnG8Service : DaggerService() { fun bolus(insulin: Double, carbs: Int, carbTime: Long, t: EventOverviewBolusProgress.Treatment): Boolean { if (!isConnected) return false if (BolusProgressDialog.stopPressed) return false + + // Only Carbs + if (carbs > 0 && insulin == 0.0) { + pumpSync.syncCarbsWithTimestamp(carbTime, carbs.toDouble(), null, PumpType.DIACONN_G8, diaconnG8Pump.serialNo.toString()) + return true + } + rxBus.send(EventPumpStatusChanged(rh.gs(R.string.startingbolus))) // bolus speed setting @@ -622,9 +674,9 @@ class DiaconnG8Service : DaggerService() { (basalList[23] * 100).toInt() ) // setting basal pattern 1,2,3,4 - sendMessage(requestReqPacket1) - sendMessage(requestReqPacket2) - sendMessage(requestReqPacket3) + sendMessage(requestReqPacket1, 500) + sendMessage(requestReqPacket2, 500) + sendMessage(requestReqPacket3, 500) sendMessage(requestReqPacket4) // otp process @@ -671,7 +723,7 @@ class DiaconnG8Service : DaggerService() { return false } - sendMessage(AppConfirmSettingPacket(injector, msgType, diaconnG8Pump.otpNumber), 2000) + sendMessage(AppConfirmSettingPacket(injector, msgType, diaconnG8Pump.otpNumber)) diaconnG8Pump.otpNumber = 0 return true } diff --git a/diaconn/src/main/res/values-ko-rKR/strings.xml b/diaconn/src/main/res/values-ko-rKR/strings.xml index 29cb87c694..f17e4a3c77 100644 --- a/diaconn/src/main/res/values-ko-rKR/strings.xml +++ b/diaconn/src/main/res/values-ko-rKR/strings.xml @@ -89,6 +89,8 @@ 바늘 교체 로그 동기화 시 케어포털 \"위치 교체\" 정보 자동 업로드 로그 동기화 시 케어포털 \"베터리 교체\" 정보 자동 업로드 + 펌프 로그정보를 \"디아콘 클라우드\"로 자동 업로드 + 디아콘 클라우드 전송 베터리 교체 로그 동기화 진행 중 인슐린 부족 경고 diff --git a/diaconn/src/main/res/values/strings.xml b/diaconn/src/main/res/values/strings.xml index ac1ad0868e..ea014d7ef3 100644 --- a/diaconn/src/main/res/values/strings.xml +++ b/diaconn/src/main/res/values/strings.xml @@ -10,6 +10,7 @@ diaconn_g8_logbatterychanges diaconn_g8_loginsulinchange diaconn_g8_logneedlechange + diaconn_g8_cloudsend Reset Pairing No Device available @@ -166,5 +167,7 @@ LGS status is OFF, OFF Command is declined. Tempbasal start is rejected when tempbasal is running Tempbasal stop is rejected when tempbasal is not running - + Send pump logs to the Diaconn Cloud. + Diaconn Cloud Sync + diaconn_g8_appuid diff --git a/diaconn/src/main/res/xml/pref_diaconn.xml b/diaconn/src/main/res/xml/pref_diaconn.xml index f81699e887..ec409e5636 100644 --- a/diaconn/src/main/res/xml/pref_diaconn.xml +++ b/diaconn/src/main/res/xml/pref_diaconn.xml @@ -45,6 +45,12 @@ android:summary="@string/diaconn_g8_logbatterychange_summary" android:title="@string/diaconn_g8_logbatterychange_title" /> + + \ No newline at end of file