NSCv3: upload EffectiveProfileSwitch

This commit is contained in:
Milos Kozak 2022-12-14 22:26:40 +01:00
parent 09e61772ab
commit e28271fac6
4 changed files with 70 additions and 11 deletions

View file

@ -5,14 +5,14 @@ import org.json.JSONObject
data class NSEffectiveProfileSwitch( data class NSEffectiveProfileSwitch(
override val date: Long, override val date: Long,
override val device: String?, override val device: String? = null,
override val identifier: String?, override val identifier: String? = null,
override val units: NsUnits?, override val units: NsUnits? = null,
override val srvModified: Long?, override val srvModified: Long? = null,
override val srvCreated: Long?, override val srvCreated: Long? = null,
override val utcOffset: Long, override val utcOffset: Long,
override val subject: String?, override val subject: String? = null ,
override var isReadOnly: Boolean, override var isReadOnly: Boolean = false,
override val isValid: Boolean, override val isValid: Boolean,
override val eventType: EventType, override val eventType: EventType,
override val notes: String?, override val notes: String?,

View file

@ -72,7 +72,7 @@ class RunningConfigurationImpl @Inject constructor(
assert(config.NSCLIENT) assert(config.NSCLIENT)
configuration.version?.let { configuration.version?.let {
rxBus.send(EventNSClientNewLog("VERSION", "Received AndroidAPS version $it")) rxBus.send(EventNSClientNewLog("VERSION", "Received AAPS version $it"))
if (config.VERSION_NAME.startsWith(it).not()) if (config.VERSION_NAME.startsWith(it).not())
uiInteraction.addNotification(Notification.NSCLIENT_VERSION_DOES_NOT_MATCH, rh.gs(R.string.nsclient_version_does_not_match), Notification.NORMAL) uiInteraction.addNotification(Notification.NSCLIENT_VERSION_DOES_NOT_MATCH, rh.gs(R.string.nsclient_version_does_not_match), Notification.NORMAL)
} }

View file

@ -34,6 +34,7 @@ import info.nightscout.plugins.sync.nsShared.events.EventNSClientResend
import info.nightscout.plugins.sync.nsShared.events.EventNSClientUpdateGUI import info.nightscout.plugins.sync.nsShared.events.EventNSClientUpdateGUI
import info.nightscout.plugins.sync.nsclient.NsClientReceiverDelegate import info.nightscout.plugins.sync.nsclient.NsClientReceiverDelegate
import info.nightscout.plugins.sync.nsclientV3.extensions.toNSBolus import info.nightscout.plugins.sync.nsclientV3.extensions.toNSBolus
import info.nightscout.plugins.sync.nsclientV3.extensions.toNSEffectiveProfileSwitch
import info.nightscout.plugins.sync.nsclientV3.workers.LoadBgWorker import info.nightscout.plugins.sync.nsclientV3.workers.LoadBgWorker
import info.nightscout.plugins.sync.nsclientV3.workers.LoadLastModificationWorker import info.nightscout.plugins.sync.nsclientV3.workers.LoadLastModificationWorker
import info.nightscout.plugins.sync.nsclientV3.workers.LoadStatusWorker import info.nightscout.plugins.sync.nsclientV3.workers.LoadStatusWorker
@ -43,6 +44,8 @@ import info.nightscout.rx.events.EventAppExit
import info.nightscout.rx.events.EventChargingState import info.nightscout.rx.events.EventChargingState
import info.nightscout.rx.events.EventNSClientNewLog import info.nightscout.rx.events.EventNSClientNewLog
import info.nightscout.rx.events.EventNetworkChange import info.nightscout.rx.events.EventNetworkChange
import info.nightscout.rx.events.EventNewBG
import info.nightscout.rx.events.EventNewHistoryData
import info.nightscout.rx.events.EventPreferenceChange import info.nightscout.rx.events.EventPreferenceChange
import info.nightscout.rx.events.EventSWSyncStatus import info.nightscout.rx.events.EventSWSyncStatus
import info.nightscout.rx.logging.AAPSLogger import info.nightscout.rx.logging.AAPSLogger
@ -59,6 +62,9 @@ import io.reactivex.rxjava3.kotlin.plusAssign
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import kotlinx.serialization.decodeFromString import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json import kotlinx.serialization.json.Json
import java.util.concurrent.Executors
import java.util.concurrent.ScheduledFuture
import java.util.concurrent.TimeUnit
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
@ -170,6 +176,14 @@ class NSClientV3Plugin @Inject constructor(
.toObservable(EventNSClientResend::class.java) .toObservable(EventNSClientResend::class.java)
.observeOn(aapsSchedulers.io) .observeOn(aapsSchedulers.io)
.subscribe({ event -> resend(event.reason) }, fabricPrivacy::logException) .subscribe({ event -> resend(event.reason) }, fabricPrivacy::logException)
disposable += rxBus
.toObservable(EventNewBG::class.java)
.observeOn(aapsSchedulers.io)
.subscribe({ scheduleExecution() }, fabricPrivacy::logException)
disposable += rxBus
.toObservable(EventNewHistoryData::class.java)
.observeOn(aapsSchedulers.io)
.subscribe({ scheduleExecution() }, fabricPrivacy::logException)
runLoop = Runnable { runLoop = Runnable {
handler.postDelayed(runLoop, REFRESH_INTERVAL) handler.postDelayed(runLoop, REFRESH_INTERVAL)
@ -314,7 +328,7 @@ class NSClientV3Plugin @Inject constructor(
// is DataSyncSelector.PairTemporaryBasal -> dataPair.value.toJson(false, profileFunction.getProfile(dataPair.value.timestamp), dateUtil) // is DataSyncSelector.PairTemporaryBasal -> dataPair.value.toJson(false, profileFunction.getProfile(dataPair.value.timestamp), dateUtil)
// is DataSyncSelector.PairExtendedBolus -> dataPair.value.toJson(false, profileFunction.getProfile(dataPair.value.timestamp), dateUtil) // is DataSyncSelector.PairExtendedBolus -> dataPair.value.toJson(false, profileFunction.getProfile(dataPair.value.timestamp), dateUtil)
// is DataSyncSelector.PairProfileSwitch -> dataPair.value.toJson(false, dateUtil) // is DataSyncSelector.PairProfileSwitch -> dataPair.value.toJson(false, dateUtil)
// is DataSyncSelector.PairEffectiveProfileSwitch -> dataPair.value.toJson(false, dateUtil) is DataSyncSelector.PairEffectiveProfileSwitch -> dataPair.value.toNSEffectiveProfileSwitch(dateUtil)
// is DataSyncSelector.PairOfflineEvent -> dataPair.value.toJson(false, dateUtil) // is DataSyncSelector.PairOfflineEvent -> dataPair.value.toJson(false, dateUtil)
else -> null else -> null
}?.let { data -> }?.let { data ->
@ -353,7 +367,14 @@ class NSClientV3Plugin @Inject constructor(
// is DataSyncSelector.PairTemporaryBasal -> dataPair.value.toJson(false, profileFunction.getProfile(dataPair.value.timestamp), dateUtil) // is DataSyncSelector.PairTemporaryBasal -> dataPair.value.toJson(false, profileFunction.getProfile(dataPair.value.timestamp), dateUtil)
// is DataSyncSelector.PairExtendedBolus -> dataPair.value.toJson(false, profileFunction.getProfile(dataPair.value.timestamp), dateUtil) // is DataSyncSelector.PairExtendedBolus -> dataPair.value.toJson(false, profileFunction.getProfile(dataPair.value.timestamp), dateUtil)
// is DataSyncSelector.PairProfileSwitch -> dataPair.value.toJson(false, dateUtil) // is DataSyncSelector.PairProfileSwitch -> dataPair.value.toJson(false, dateUtil)
// is DataSyncSelector.PairEffectiveProfileSwitch -> dataPair.value.toJson(false, dateUtil) is DataSyncSelector.PairEffectiveProfileSwitch -> {
if (result.response == 201) { // created
dataPair.value.interfaceIDs.nightscoutId = result.identifier
storeDataForDb.nsIdEffectiveProfileSwitches.add(dataPair.value)
storeDataForDb.scheduleNsIdUpdate()
}
dataSyncSelector.confirmLastEffectiveProfileSwitchIdIfGreater(dataPair.id)
}
// is DataSyncSelector.PairOfflineEvent -> dataPair.value.toJson(false, dateUtil) // is DataSyncSelector.PairOfflineEvent -> dataPair.value.toJson(false, dateUtil)
} }
} catch (e: Exception) { } catch (e: Exception) {
@ -415,4 +436,20 @@ class NSClientV3Plugin @Inject constructor(
return true return true
return false return false
} }
private val eventWorker = Executors.newSingleThreadScheduledExecutor()
private var scheduledEventPost: ScheduledFuture<*>? = null
fun scheduleExecution() {
class PostRunnable : Runnable {
override fun run() {
scheduledEventPost = null
executeLoop()
}
}
// cancel waiting task to prevent sending multiple posts
scheduledEventPost?.cancel(false)
val task: Runnable = PostRunnable()
scheduledEventPost = eventWorker.schedule(task, 10, TimeUnit.SECONDS)
}
} }

View file

@ -5,6 +5,7 @@ import info.nightscout.core.profile.ProfileSealed
import info.nightscout.database.entities.EffectiveProfileSwitch import info.nightscout.database.entities.EffectiveProfileSwitch
import info.nightscout.database.entities.embedments.InterfaceIDs import info.nightscout.database.entities.embedments.InterfaceIDs
import info.nightscout.plugins.sync.nsclient.extensions.fromConstant import info.nightscout.plugins.sync.nsclient.extensions.fromConstant
import info.nightscout.sdk.localmodel.treatment.EventType
import info.nightscout.sdk.localmodel.treatment.NSEffectiveProfileSwitch import info.nightscout.sdk.localmodel.treatment.NSEffectiveProfileSwitch
import info.nightscout.shared.utils.DateUtil import info.nightscout.shared.utils.DateUtil
@ -30,4 +31,25 @@ fun NSEffectiveProfileSwitch.toEffectiveProfileSwitch(dateUtil: DateUtil): Effec
insulinConfiguration = profileSealed.insulinConfiguration, insulinConfiguration = profileSealed.insulinConfiguration,
interfaceIDs_backing = InterfaceIDs(nightscoutId = identifier, pumpId = pumpId, pumpType = InterfaceIDs.PumpType.fromString(pumpType), pumpSerial = pumpSerial, endId = endId) interfaceIDs_backing = InterfaceIDs(nightscoutId = identifier, pumpId = pumpId, pumpType = InterfaceIDs.PumpType.fromString(pumpType), pumpSerial = pumpSerial, endId = endId)
) )
} }
fun EffectiveProfileSwitch.toNSEffectiveProfileSwitch(dateUtil: DateUtil) : NSEffectiveProfileSwitch =
NSEffectiveProfileSwitch(
eventType = EventType.NOTE,
isValid = isValid,
date = timestamp,
utcOffset = utcOffset,
profileJson = ProfileSealed.EPS(this).toPureNsJson(dateUtil),
originalProfileName = originalProfileName,
originalCustomizedName = originalCustomizedName,
originalTimeshift = originalTimeshift,
originalPercentage = originalPercentage,
originalDuration = originalDuration,
originalEnd = originalEnd,
notes = originalCustomizedName,
identifier = interfaceIDs.nightscoutId,
pumpId = interfaceIDs.pumpId,
pumpType = interfaceIDs.pumpType?.name,
pumpSerial = interfaceIDs.pumpSerial,
endId = interfaceIDs.endId
)