Fix DeviceStatus upload

This commit is contained in:
Milos Kozak 2020-01-03 21:02:36 +01:00
parent 2470cc7b17
commit 5602819321
9 changed files with 21 additions and 19 deletions

View file

@ -98,6 +98,8 @@ abstract class PluginBase(pluginDesc: PluginDescription) {
open val preferencesId: Int open val preferencesId: Int
get() = pluginDescription.preferencesId get() = pluginDescription.preferencesId
fun isEnabled() = isEnabled(pluginDescription.mainType)
fun isEnabled(type: PluginType): Boolean { fun isEnabled(type: PluginType): Boolean {
if (pluginDescription.alwaysEnabled && type == pluginDescription.mainType) return true if (pluginDescription.alwaysEnabled && type == pluginDescription.mainType) return true
if (pluginDescription.mainType == PluginType.CONSTRAINTS && type == PluginType.CONSTRAINTS) return true if (pluginDescription.mainType == PluginType.CONSTRAINTS && type == PluginType.CONSTRAINTS) return true

View file

@ -367,7 +367,7 @@ public class LoopPlugin extends PluginBase {
lastRun.tbrSetByPump = null; lastRun.tbrSetByPump = null;
lastRun.smbSetByPump = null; lastRun.smbSetByPump = null;
NSUpload.uploadDeviceStatus(); NSUpload.uploadDeviceStatus(this);
if (isSuspended()) { if (isSuspended()) {
aapsLogger.debug(LTag.APS, resourceHelper.gs(R.string.loopsuspended)); aapsLogger.debug(LTag.APS, resourceHelper.gs(R.string.loopsuspended));
@ -480,7 +480,7 @@ public class LoopPlugin extends PluginBase {
public void acceptChangeRequest() { public void acceptChangeRequest() {
Profile profile = profileFunction.getProfile(); Profile profile = profileFunction.getProfile();
final LoopPlugin lp = this;
applyTBRRequest(lastRun.constraintsProcessed, profile, new Callback() { applyTBRRequest(lastRun.constraintsProcessed, profile, new Callback() {
@Override @Override
public void run() { public void run() {
@ -488,7 +488,7 @@ public class LoopPlugin extends PluginBase {
lastRun.tbrSetByPump = result; lastRun.tbrSetByPump = result;
lastRun.lastEnact = new Date(); lastRun.lastEnact = new Date();
lastRun.lastOpenModeAccept = new Date(); lastRun.lastOpenModeAccept = new Date();
NSUpload.uploadDeviceStatus(); NSUpload.uploadDeviceStatus(lp);
sp.incInt(R.string.key_ObjectivesmanualEnacts); sp.incInt(R.string.key_ObjectivesmanualEnacts);
} }
rxBus.send(new EventAcceptOpenLoopChange()); rxBus.send(new EventAcceptOpenLoopChange());

View file

@ -50,16 +50,9 @@ import info.nightscout.androidaps.utils.SP;
/** /**
* Created by mike on 26.05.2017. * Created by mike on 26.05.2017.
*/ */
@Singleton
public class NSUpload { public class NSUpload {
private static Logger log = LoggerFactory.getLogger(L.NSCLIENT); private static Logger log = LoggerFactory.getLogger(L.NSCLIENT);
private static LoopPlugin loopPlugin; // Ugly but temporary
@Inject
public NSUpload(LoopPlugin loopPlugin) {
this.loopPlugin = loopPlugin;
}
public static void uploadTempBasalStartAbsolute(TemporaryBasal temporaryBasal, Double originalExtendedAmount) { public static void uploadTempBasalStartAbsolute(TemporaryBasal temporaryBasal, Double originalExtendedAmount) {
try { try {
JSONObject data = new JSONObject(); JSONObject data = new JSONObject();
@ -167,7 +160,7 @@ public class NSUpload {
} }
} }
public static void uploadDeviceStatus() { public static void uploadDeviceStatus(LoopPlugin loopPlugin) {
Profile profile = ProfileFunctions.getInstance().getProfile(); Profile profile = ProfileFunctions.getInstance().getProfile();
String profileName = ProfileFunctions.getInstance().getProfileName(); String profileName = ProfileFunctions.getInstance().getProfileName();
@ -178,7 +171,6 @@ public class NSUpload {
DeviceStatus deviceStatus = new DeviceStatus(); DeviceStatus deviceStatus = new DeviceStatus();
try { try {
if (loopPlugin == null) return; // TODO ugly - not initialized yet
LoopPlugin.LastRun lastRun = loopPlugin.lastRun; LoopPlugin.LastRun lastRun = loopPlugin.lastRun;
if (lastRun != null && lastRun.lastAPSRun.getTime() > System.currentTimeMillis() - 300 * 1000L) { if (lastRun != null && lastRun.lastAPSRun.getTime() > System.currentTimeMillis() - 300 * 1000L) {
// do not send if result is older than 1 min // do not send if result is older than 1 min

View file

@ -229,7 +229,7 @@ public class DanaRExecutionService extends AbstractDanaRExecutionService {
RxBus.Companion.getINSTANCE().send(new EventDanaRNewStatus()); RxBus.Companion.getINSTANCE().send(new EventDanaRNewStatus());
RxBus.Companion.getINSTANCE().send(new EventInitializationChanged()); RxBus.Companion.getINSTANCE().send(new EventInitializationChanged());
NSUpload.uploadDeviceStatus(); //NSUpload.uploadDeviceStatus();
if (danaRPump.dailyTotalUnits > danaRPump.maxDailyTotalUnits * Constants.dailyLimitWarning) { if (danaRPump.dailyTotalUnits > danaRPump.maxDailyTotalUnits * Constants.dailyLimitWarning) {
if (L.isEnabled(L.PUMP)) if (L.isEnabled(L.PUMP))
log.debug("Approaching daily limit: " + danaRPump.dailyTotalUnits + "/" + danaRPump.maxDailyTotalUnits); log.debug("Approaching daily limit: " + danaRPump.dailyTotalUnits + "/" + danaRPump.maxDailyTotalUnits);

View file

@ -219,7 +219,7 @@ public class DanaRKoreanExecutionService extends AbstractDanaRExecutionService {
RxBus.Companion.getINSTANCE().send(new EventDanaRNewStatus()); RxBus.Companion.getINSTANCE().send(new EventDanaRNewStatus());
RxBus.Companion.getINSTANCE().send(new EventInitializationChanged()); RxBus.Companion.getINSTANCE().send(new EventInitializationChanged());
NSUpload.uploadDeviceStatus(); //NSUpload.uploadDeviceStatus();
if (danaRPump.dailyTotalUnits > danaRPump.maxDailyTotalUnits * Constants.dailyLimitWarning) { if (danaRPump.dailyTotalUnits > danaRPump.maxDailyTotalUnits * Constants.dailyLimitWarning) {
if (L.isEnabled(L.PUMP)) if (L.isEnabled(L.PUMP))
log.debug("Approaching daily limit: " + danaRPump.dailyTotalUnits + "/" + danaRPump.maxDailyTotalUnits); log.debug("Approaching daily limit: " + danaRPump.dailyTotalUnits + "/" + danaRPump.maxDailyTotalUnits);

View file

@ -233,7 +233,7 @@ public class DanaRSService extends Service {
RxBus.Companion.getINSTANCE().send(new EventDanaRNewStatus()); RxBus.Companion.getINSTANCE().send(new EventDanaRNewStatus());
RxBus.Companion.getINSTANCE().send(new EventInitializationChanged()); RxBus.Companion.getINSTANCE().send(new EventInitializationChanged());
NSUpload.uploadDeviceStatus(); //NSUpload.uploadDeviceStatus();
if (danaRPump.dailyTotalUnits > danaRPump.maxDailyTotalUnits * Constants.dailyLimitWarning) { if (danaRPump.dailyTotalUnits > danaRPump.maxDailyTotalUnits * Constants.dailyLimitWarning) {
if (L.isEnabled(L.PUMPCOMM)) if (L.isEnabled(L.PUMPCOMM))
log.debug("Approaching daily limit: " + danaRPump.dailyTotalUnits + "/" + danaRPump.maxDailyTotalUnits); log.debug("Approaching daily limit: " + danaRPump.dailyTotalUnits + "/" + danaRPump.maxDailyTotalUnits);

View file

@ -262,7 +262,7 @@ public class DanaRv2ExecutionService extends AbstractDanaRExecutionService {
RxBus.Companion.getINSTANCE().send(new EventDanaRNewStatus()); RxBus.Companion.getINSTANCE().send(new EventDanaRNewStatus());
RxBus.Companion.getINSTANCE().send(new EventInitializationChanged()); RxBus.Companion.getINSTANCE().send(new EventInitializationChanged());
NSUpload.uploadDeviceStatus(); //NSUpload.uploadDeviceStatus();
if (danaRPump.dailyTotalUnits > danaRPump.maxDailyTotalUnits * Constants.dailyLimitWarning) { if (danaRPump.dailyTotalUnits > danaRPump.maxDailyTotalUnits * Constants.dailyLimitWarning) {
if (L.isEnabled(L.PUMP)) if (L.isEnabled(L.PUMP))
log.debug("Approaching daily limit: " + danaRPump.dailyTotalUnits + "/" + danaRPump.maxDailyTotalUnits); log.debug("Approaching daily limit: " + danaRPump.dailyTotalUnits + "/" + danaRPump.maxDailyTotalUnits);

View file

@ -166,7 +166,7 @@ class VirtualPumpPlugin @Inject constructor(
override fun finishHandshaking() {} override fun finishHandshaking() {}
override fun connect(reason: String) { override fun connect(reason: String) {
if (!Config.NSCLIENT) NSUpload.uploadDeviceStatus() //if (!Config.NSCLIENT) NSUpload.uploadDeviceStatus()
lastDataTime = System.currentTimeMillis() lastDataTime = System.currentTimeMillis()
} }

View file

@ -8,6 +8,8 @@ import android.content.Intent
import android.os.PowerManager import android.os.PowerManager
import android.os.SystemClock import android.os.SystemClock
import dagger.android.DaggerBroadcastReceiver import dagger.android.DaggerBroadcastReceiver
import info.nightscout.androidaps.Config
import info.nightscout.androidaps.db.DatabaseHelper
import info.nightscout.androidaps.events.EventProfileNeedsUpdate import info.nightscout.androidaps.events.EventProfileNeedsUpdate
import info.nightscout.androidaps.logging.AAPSLogger import info.nightscout.androidaps.logging.AAPSLogger
import info.nightscout.androidaps.logging.L import info.nightscout.androidaps.logging.L
@ -87,14 +89,20 @@ class KeepAliveReceiver : DaggerBroadcastReceiver() {
} }
} }
// Usually devicestatus is uploaded through LoopPlugin after every loop cycle.
// if there is no BG available, we have to upload anyway to have correct
// IOB displayed in NS
private fun checkAPS() { private fun checkAPS() {
val usedAPS = configBuilderPlugin.activeAPS val usedAPS = configBuilderPlugin.activeAPS
var shouldUploadStatus = false var shouldUploadStatus = false
if (usedAPS == null) shouldUploadStatus = true if (Config.NSCLIENT) return
if (Config.PUMPCONTROL) shouldUploadStatus = true
if (usedAPS == null || !loopPlugin.isEnabled() || DatabaseHelper.actualBg() == null)
shouldUploadStatus = true
else if (DateUtil.isOlderThan(usedAPS.lastAPSRun, 5)) shouldUploadStatus = true else if (DateUtil.isOlderThan(usedAPS.lastAPSRun, 5)) shouldUploadStatus = true
if (DateUtil.isOlderThan(lastIobUpload, IOB_UPDATE_FREQUENCY) && shouldUploadStatus) { if (DateUtil.isOlderThan(lastIobUpload, IOB_UPDATE_FREQUENCY) && shouldUploadStatus) {
lastIobUpload = DateUtil.now() lastIobUpload = DateUtil.now()
NSUpload.uploadDeviceStatus() NSUpload.uploadDeviceStatus(loopPlugin)
} }
} }