NSCv3: resolve nullability

This commit is contained in:
Milos Kozak 2022-12-15 09:37:15 +01:00
parent fee7059559
commit 4a1fc0f45c
6 changed files with 47 additions and 41 deletions

View file

@ -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<SwitchPreference>(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,7 +348,7 @@ class NSClientV3Plugin @Inject constructor(
}
)
)
val result = call(data)
call?.let { it(data) }?.let { result ->
when (dataPair) {
is DataSyncSelector.PairBolus -> {
if (result.response == 201) { // created
@ -377,6 +377,7 @@ class NSClientV3Plugin @Inject constructor(
}
// 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() {

View file

@ -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<NSSgvV3>
val response: NSAndroidClient.ReadResponse<List<NSSgvV3>>?
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()

View file

@ -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)}"))

View file

@ -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) {

View file

@ -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)

View file

@ -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<NSTreatment>
val response: NSAndroidClient.ReadResponse<List<NSTreatment>>?
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()