diff --git a/plugins/sync/src/main/java/info/nightscout/plugins/sync/nsclientV3/NSClientV3Plugin.kt b/plugins/sync/src/main/java/info/nightscout/plugins/sync/nsclientV3/NSClientV3Plugin.kt index d21159e7c1..58dbd232f0 100644 --- a/plugins/sync/src/main/java/info/nightscout/plugins/sync/nsclientV3/NSClientV3Plugin.kt +++ b/plugins/sync/src/main/java/info/nightscout/plugins/sync/nsclientV3/NSClientV3Plugin.kt @@ -111,14 +111,14 @@ class NSClientV3Plugin @Inject constructor( when { sp.getBoolean(R.string.key_ns_client_paused, false) -> rh.gs(info.nightscout.core.ui.R.string.paused) isAllowed.not() -> blockingReason - nsAndroidClient.lastStatus == null -> rh.gs(R.string.not_connected) + nsAndroidClient?.lastStatus == null -> rh.gs(R.string.not_connected) workIsRunning(arrayOf(JOB_NAME)) -> rh.gs(R.string.working) - nsAndroidClient.lastStatus?.apiPermissions?.isFull() == true -> rh.gs(info.nightscout.shared.R.string.connected) - nsAndroidClient.lastStatus?.apiPermissions?.isRead() == true -> rh.gs(R.string.read_only) + nsAndroidClient?.lastStatus?.apiPermissions?.isFull() == true -> rh.gs(info.nightscout.shared.R.string.connected) + nsAndroidClient?.lastStatus?.apiPermissions?.isRead() == true -> rh.gs(R.string.read_only) else -> rh.gs(info.nightscout.core.ui.R.string.unknown) } - internal lateinit var nsAndroidClient: NSAndroidClient + internal var nsAndroidClient: NSAndroidClient? = null private val isAllowed get() = nsClientReceiverDelegate.allowed private val blockingReason get() = nsClientReceiverDelegate.blockingReason @@ -211,8 +211,8 @@ class NSClientV3Plugin @Inject constructor( preferenceFragment.findPreference(rh.gs(R.string.key_ns_receive_tbr_eb))?.isVisible = config.isEngineeringMode() } - override val hasWritePermission: Boolean get() = nsAndroidClient.lastStatus?.apiPermissions?.isFull() ?: false - override val connected: Boolean get() = nsAndroidClient.lastStatus != null + override val hasWritePermission: Boolean get() = nsAndroidClient?.lastStatus?.apiPermissions?.isFull() ?: false + override val connected: Boolean get() = nsAndroidClient?.lastStatus != null override fun clearLog() { handler.post { synchronized(listLog) { listLog.clear() } @@ -314,8 +314,8 @@ class NSClientV3Plugin @Inject constructor( private val gson: Gson = GsonBuilder().create() private fun dbOperation(collection: String, dataPair: DataSyncSelector.DataPair, progress: String, operation: Operation) { val call = when (operation) { - Operation.CREATE -> nsAndroidClient::createTreatment - Operation.UPDATE -> nsAndroidClient::updateTreatment + Operation.CREATE -> nsAndroidClient?.let { return@let it::createTreatment } + Operation.UPDATE -> nsAndroidClient?.let { return@let it::updateTreatment } } when (dataPair) { is DataSyncSelector.PairBolus -> dataPair.value.toNSBolus() @@ -348,34 +348,35 @@ class NSClientV3Plugin @Inject constructor( } ) ) - val result = call(data) - when (dataPair) { - is DataSyncSelector.PairBolus -> { - if (result.response == 201) { // created - dataPair.value.interfaceIDs.nightscoutId = result.identifier - storeDataForDb.nsIdBoluses.add(dataPair.value) - storeDataForDb.scheduleNsIdUpdate() + call?.let { it(data) }?.let { result -> + when (dataPair) { + is DataSyncSelector.PairBolus -> { + if (result.response == 201) { // created + dataPair.value.interfaceIDs.nightscoutId = result.identifier + storeDataForDb.nsIdBoluses.add(dataPair.value) + storeDataForDb.scheduleNsIdUpdate() + } + dataSyncSelector.confirmLastBolusIdIfGreater(dataPair.id) } - dataSyncSelector.confirmLastBolusIdIfGreater(dataPair.id) - } - // is DataSyncSelector.PairCarbs -> dataPair.value.toJson(false, dateUtil) - // is DataSyncSelector.PairBolusCalculatorResult -> dataPair.value.toJson(false, dateUtil, profileFunction) - // is DataSyncSelector.PairTemporaryTarget -> dataPair.value.toJson(false, profileFunction.getUnits(), dateUtil) - // is DataSyncSelector.PairFood -> dataPair.value.toJson(false) - // is DataSyncSelector.PairGlucoseValue -> dataPair.value.toJson(false, dateUtil) - // is DataSyncSelector.PairTherapyEvent -> dataPair.value.toJson(false, 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.PairProfileSwitch -> 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() + // is DataSyncSelector.PairCarbs -> dataPair.value.toJson(false, dateUtil) + // is DataSyncSelector.PairBolusCalculatorResult -> dataPair.value.toJson(false, dateUtil, profileFunction) + // is DataSyncSelector.PairTemporaryTarget -> dataPair.value.toJson(false, profileFunction.getUnits(), dateUtil) + // is DataSyncSelector.PairFood -> dataPair.value.toJson(false) + // is DataSyncSelector.PairGlucoseValue -> dataPair.value.toJson(false, dateUtil) + // is DataSyncSelector.PairTherapyEvent -> dataPair.value.toJson(false, 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.PairProfileSwitch -> 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) } - dataSyncSelector.confirmLastEffectiveProfileSwitchIdIfGreater(dataPair.id) + // is DataSyncSelector.PairOfflineEvent -> dataPair.value.toJson(false, dateUtil) } - // is DataSyncSelector.PairOfflineEvent -> dataPair.value.toJson(false, dateUtil) } } catch (e: Exception) { aapsLogger.error(LTag.NSCLIENT, "Upload exception", e) @@ -439,7 +440,7 @@ class NSClientV3Plugin @Inject constructor( private val eventWorker = Executors.newSingleThreadScheduledExecutor() private var scheduledEventPost: ScheduledFuture<*>? = null - fun scheduleExecution() { + private fun scheduleExecution() { class PostRunnable : Runnable { override fun run() { diff --git a/plugins/sync/src/main/java/info/nightscout/plugins/sync/nsclientV3/workers/LoadBgWorker.kt b/plugins/sync/src/main/java/info/nightscout/plugins/sync/nsclientV3/workers/LoadBgWorker.kt index 47dacb6e81..c95a2b69a2 100644 --- a/plugins/sync/src/main/java/info/nightscout/plugins/sync/nsclientV3/workers/LoadBgWorker.kt +++ b/plugins/sync/src/main/java/info/nightscout/plugins/sync/nsclientV3/workers/LoadBgWorker.kt @@ -40,6 +40,7 @@ class LoadBgWorker( } override fun doWorkAndLog(): Result { + val nsAndroidClient = nsClientV3Plugin.nsAndroidClient ?: return Result.failure(workDataOf("Error" to "AndroidClient is null")) var ret = Result.success() val isFirstLoad = nsClientV3Plugin.isFirstLoad(NsClient.Collection.ENTRIES) val lastLoaded = @@ -50,9 +51,9 @@ class LoadBgWorker( try { val sgvs: List val response: NSAndroidClient.ReadResponse>? - if (isFirstLoad) sgvs = nsClientV3Plugin.nsAndroidClient.getSgvsNewerThan(lastLoaded, 500) + if (isFirstLoad) sgvs = nsAndroidClient.getSgvsNewerThan(lastLoaded, 500) else { - response = nsClientV3Plugin.nsAndroidClient.getSgvsModifiedSince(lastLoaded, 500) + response = nsAndroidClient.getSgvsModifiedSince(lastLoaded, 500) sgvs = response.values nsClientV3Plugin.lastLoadedSrvModified.collections.entries = response.lastServerModified nsClientV3Plugin.storeLastFetched() diff --git a/plugins/sync/src/main/java/info/nightscout/plugins/sync/nsclientV3/workers/LoadDeviceStatusWorker.kt b/plugins/sync/src/main/java/info/nightscout/plugins/sync/nsclientV3/workers/LoadDeviceStatusWorker.kt index ca910880b1..13db32d9ed 100644 --- a/plugins/sync/src/main/java/info/nightscout/plugins/sync/nsclientV3/workers/LoadDeviceStatusWorker.kt +++ b/plugins/sync/src/main/java/info/nightscout/plugins/sync/nsclientV3/workers/LoadDeviceStatusWorker.kt @@ -30,12 +30,13 @@ class LoadDeviceStatusWorker( @Inject lateinit var nsDeviceStatusHandler: NSDeviceStatusHandler override fun doWorkAndLog(): Result { + val nsAndroidClient = nsClientV3Plugin.nsAndroidClient ?: return Result.failure(workDataOf("Error" to "AndroidClient is null")) var ret = Result.success() runBlocking { try { val from = dateUtil.now() - T.mins(7).msecs() - val deviceStatuses = nsClientV3Plugin.nsAndroidClient.getDeviceStatusModifiedSince(from) + val deviceStatuses = nsAndroidClient.getDeviceStatusModifiedSince(from) aapsLogger.debug("DEVICESTATUSES: $deviceStatuses") if (deviceStatuses.isNotEmpty()) { rxBus.send(EventNSClientNewLog("RCV", "${deviceStatuses.size} DSs from ${dateUtil.dateAndTimeAndSecondsString(from)}")) diff --git a/plugins/sync/src/main/java/info/nightscout/plugins/sync/nsclientV3/workers/LoadLastModificationWorker.kt b/plugins/sync/src/main/java/info/nightscout/plugins/sync/nsclientV3/workers/LoadLastModificationWorker.kt index 8c897985e0..493a373835 100644 --- a/plugins/sync/src/main/java/info/nightscout/plugins/sync/nsclientV3/workers/LoadLastModificationWorker.kt +++ b/plugins/sync/src/main/java/info/nightscout/plugins/sync/nsclientV3/workers/LoadLastModificationWorker.kt @@ -15,11 +15,12 @@ class LoadLastModificationWorker( @Inject lateinit var nsClientV3Plugin: NSClientV3Plugin override fun doWorkAndLog(): Result { + val nsAndroidClient = nsClientV3Plugin.nsAndroidClient ?: return Result.failure(workDataOf("Error" to "AndroidClient is null")) var ret = Result.success() runBlocking { try { - val lm = nsClientV3Plugin.nsAndroidClient.getLastModified() + val lm = nsAndroidClient.getLastModified() nsClientV3Plugin.newestDataOnServer = lm aapsLogger.debug("LAST MODIFIED: ${nsClientV3Plugin.newestDataOnServer}") } catch (error: Exception) { diff --git a/plugins/sync/src/main/java/info/nightscout/plugins/sync/nsclientV3/workers/LoadStatusWorker.kt b/plugins/sync/src/main/java/info/nightscout/plugins/sync/nsclientV3/workers/LoadStatusWorker.kt index b58bdcf552..cbf6183cb9 100644 --- a/plugins/sync/src/main/java/info/nightscout/plugins/sync/nsclientV3/workers/LoadStatusWorker.kt +++ b/plugins/sync/src/main/java/info/nightscout/plugins/sync/nsclientV3/workers/LoadStatusWorker.kt @@ -15,11 +15,12 @@ class LoadStatusWorker( @Inject lateinit var nsClientV3Plugin: NSClientV3Plugin override fun doWorkAndLog(): Result { + val nsAndroidClient = nsClientV3Plugin.nsAndroidClient ?: return Result.failure(workDataOf("Error" to "AndroidClient is null")) var ret = Result.success() runBlocking { try { - val status = nsClientV3Plugin.nsAndroidClient.getStatus() + val status = nsAndroidClient.getStatus() aapsLogger.debug("STATUS: $status") } catch (error: Exception) { aapsLogger.error("Error: ", error) diff --git a/plugins/sync/src/main/java/info/nightscout/plugins/sync/nsclientV3/workers/LoadTreatmentsWorker.kt b/plugins/sync/src/main/java/info/nightscout/plugins/sync/nsclientV3/workers/LoadTreatmentsWorker.kt index c1d7e557c4..670f1e5e29 100644 --- a/plugins/sync/src/main/java/info/nightscout/plugins/sync/nsclientV3/workers/LoadTreatmentsWorker.kt +++ b/plugins/sync/src/main/java/info/nightscout/plugins/sync/nsclientV3/workers/LoadTreatmentsWorker.kt @@ -33,6 +33,7 @@ class LoadTreatmentsWorker( @Inject lateinit var storeDataForDb: StoreDataForDb override fun doWorkAndLog(): Result { + val nsAndroidClient = nsClientV3Plugin.nsAndroidClient ?: return Result.failure(workDataOf("Error" to "AndroidClient is null")) var ret = Result.success() val isFirstLoad = nsClientV3Plugin.isFirstLoad(NsClient.Collection.TREATMENTS) @@ -45,11 +46,11 @@ class LoadTreatmentsWorker( val treatments: List val response: NSAndroidClient.ReadResponse>? if (isFirstLoad) { - treatments = nsClientV3Plugin.nsAndroidClient.getTreatmentsNewerThan(lastLoaded, 500) + treatments = nsAndroidClient.getTreatmentsNewerThan(lastLoaded, 500) response = NSAndroidClient.ReadResponse(0, treatments) } else { - response = nsClientV3Plugin.nsAndroidClient.getTreatmentsModifiedSince(lastLoaded, 500) + response = nsAndroidClient.getTreatmentsModifiedSince(lastLoaded, 500) treatments = response.values nsClientV3Plugin.lastLoadedSrvModified.collections.treatments = response.lastServerModified nsClientV3Plugin.storeLastFetched()