BatteryLevel -> ChargingStateReceiver

This commit is contained in:
Milos Kozak 2020-04-25 22:59:22 +02:00
parent 403c609319
commit d86c5f1dd2
9 changed files with 52 additions and 71 deletions

View file

@ -1,3 +1,3 @@
package info.nightscout.androidaps.events
class EventChargingState(val isCharging: Boolean) : Event()
class EventChargingState(val isCharging: Boolean, val batterLevel: Int) : Event()

View file

@ -17,8 +17,6 @@ import org.jetbrains.annotations.Nullable;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.Date;
import javax.inject.Inject;
import javax.inject.Singleton;
@ -49,7 +47,6 @@ import info.nightscout.androidaps.interfaces.PluginType;
import info.nightscout.androidaps.interfaces.PumpDescription;
import info.nightscout.androidaps.interfaces.PumpInterface;
import info.nightscout.androidaps.logging.AAPSLogger;
import info.nightscout.androidaps.logging.L;
import info.nightscout.androidaps.logging.LTag;
import info.nightscout.androidaps.plugins.aps.loop.events.EventLoopSetLastRunGui;
import info.nightscout.androidaps.plugins.aps.loop.events.EventLoopUpdateGui;
@ -65,6 +62,7 @@ import info.nightscout.androidaps.plugins.pump.virtual.VirtualPumpPlugin;
import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin;
import info.nightscout.androidaps.queue.Callback;
import info.nightscout.androidaps.queue.commands.Command;
import info.nightscout.androidaps.receivers.ReceiverStatusStore;
import info.nightscout.androidaps.utils.DateUtil;
import info.nightscout.androidaps.utils.FabricPrivacy;
import info.nightscout.androidaps.utils.T;
@ -88,6 +86,8 @@ public class LoopPlugin extends PluginBase {
private final VirtualPumpPlugin virtualPumpPlugin;
private final Lazy<ActionStringHandler> actionStringHandler;
private final IobCobCalculatorPlugin iobCobCalculatorPlugin;
private final ReceiverStatusStore receiverStatusStore;
private final FabricPrivacy fabricPrivacy;
private CompositeDisposable disposable = new CompositeDisposable();
@ -130,7 +130,9 @@ public class LoopPlugin extends PluginBase {
TreatmentsPlugin treatmentsPlugin,
VirtualPumpPlugin virtualPumpPlugin,
Lazy<ActionStringHandler> actionStringHandler, // TODO Adrian use RxBus instead of Lazy
IobCobCalculatorPlugin iobCobCalculatorPlugin
IobCobCalculatorPlugin iobCobCalculatorPlugin,
ReceiverStatusStore receiverStatusStore,
FabricPrivacy fabricPrivacy
) {
super(new PluginDescription()
.mainType(PluginType.LOOP)
@ -154,6 +156,8 @@ public class LoopPlugin extends PluginBase {
this.virtualPumpPlugin = virtualPumpPlugin;
this.actionStringHandler = actionStringHandler;
this.iobCobCalculatorPlugin = iobCobCalculatorPlugin;
this.receiverStatusStore = receiverStatusStore;
this.fabricPrivacy = fabricPrivacy;
loopSuspendedTill = sp.getLong("loopSuspendedTill", 0L);
isSuperBolus = sp.getBoolean("isSuperBolus", false);
@ -167,7 +171,7 @@ public class LoopPlugin extends PluginBase {
disposable.add(rxBus
.toObservable(EventTempTargetChange.class)
.observeOn(Schedulers.io())
.subscribe(event -> invoke("EventTempTargetChange", true), exception -> FabricPrivacy.getInstance().logException(exception))
.subscribe(event -> invoke("EventTempTargetChange", true), fabricPrivacy::logException)
);
/**
* This method is triggered once autosens calculation has completed, so the LoopPlugin
@ -191,7 +195,7 @@ public class LoopPlugin extends PluginBase {
lastBgTriggeredRun = bgReading.date;
invoke("AutosenseCalculation for " + bgReading, true);
}, exception -> FabricPrivacy.getInstance().logException(exception))
}, fabricPrivacy::logException)
);
}
@ -391,7 +395,7 @@ public class LoopPlugin extends PluginBase {
lastRun.lastSMBEnact = 0;
lastRun.lastSMBRequest = 0;
NSUpload.uploadDeviceStatus(this, iobCobCalculatorPlugin, profileFunction, activePlugin.getActivePump());
NSUpload.uploadDeviceStatus(this, iobCobCalculatorPlugin, profileFunction, activePlugin.getActivePump(), receiverStatusStore);
if (isSuspended()) {
getAapsLogger().debug(LTag.APS, resourceHelper.gs(R.string.loopsuspended));
@ -418,7 +422,7 @@ public class LoopPlugin extends PluginBase {
if (resultAfterConstraints.bolusRequested)
lastRun.smbSetByPump = waiting;
rxBus.send(new EventLoopUpdateGui());
FabricPrivacy.getInstance().logCustom("APSRequest");
fabricPrivacy.logCustom("APSRequest");
applyTBRRequest(resultAfterConstraints, profile, new Callback() {
@Override
public void run() {
@ -516,13 +520,13 @@ public class LoopPlugin extends PluginBase {
lastRun.lastTBRRequest = lastRun.lastAPSRun;
lastRun.lastTBREnact = DateUtil.now();
lastRun.lastOpenModeAccept = DateUtil.now();
NSUpload.uploadDeviceStatus(lp, iobCobCalculatorPlugin, profileFunction, activePlugin.getActivePump());
NSUpload.uploadDeviceStatus(lp, iobCobCalculatorPlugin, profileFunction, activePlugin.getActivePump(), receiverStatusStore);
sp.incInt(R.string.key_ObjectivesmanualEnacts);
}
rxBus.send(new EventAcceptOpenLoopChange());
}
});
FabricPrivacy.getInstance().logCustom("AcceptTemp");
fabricPrivacy.logCustom("AcceptTemp");
}
/**

View file

@ -4,7 +4,6 @@ import android.content.Context
import android.content.Intent
import android.content.pm.ResolveInfo
import android.os.Bundle
import dagger.Lazy
import dagger.android.HasAndroidInjector
import info.nightscout.androidaps.Config
import info.nightscout.androidaps.R
@ -30,8 +29,8 @@ import info.nightscout.androidaps.plugins.general.overview.events.EventOverviewB
import info.nightscout.androidaps.plugins.iob.iobCobCalculator.GlucoseStatus
import info.nightscout.androidaps.plugins.iob.iobCobCalculator.IobCobCalculatorPlugin
import info.nightscout.androidaps.plugins.iob.iobCobCalculator.events.EventAutosensCalculationFinished
import info.nightscout.androidaps.receivers.ReceiverStatusStore
import info.nightscout.androidaps.services.Intents
import info.nightscout.androidaps.utils.BatteryLevel
import info.nightscout.androidaps.utils.DefaultValueHelper
import info.nightscout.androidaps.utils.FabricPrivacy
import info.nightscout.androidaps.utils.resources.ResourceHelper
@ -52,8 +51,10 @@ class DataBroadcastPlugin @Inject constructor(
private val profileFunction: ProfileFunction,
private val defaultValueHelper: DefaultValueHelper,
private val nsDeviceStatus: NSDeviceStatus,
private val lazyLoopPlugin: Lazy<LoopPlugin>,
private val activePlugin: ActivePluginProvider
private val loopPlugin: LoopPlugin,
private val activePlugin: ActivePluginProvider,
private var receiverStatusStore: ReceiverStatusStore
) : PluginBase(PluginDescription()
.mainType(PluginType.GENERAL)
@ -154,16 +155,16 @@ class DataBroadcastPlugin @Inject constructor(
private fun loopStatus(bundle: Bundle) {
//batteries
bundle.putInt("phoneBattery", BatteryLevel.getBatteryLevel())
bundle.putInt("phoneBattery", receiverStatusStore.batteryLevel)
bundle.putInt("rigBattery", nsDeviceStatus.uploaderStatus.replace("%", "").trim { it <= ' ' }.toInt())
if (Config.APS && lazyLoopPlugin.get().lastRun?.lastTBREnact != 0L) { //we are AndroidAPS
bundle.putLong("suggestedTimeStamp", lazyLoopPlugin.get().lastRun?.lastAPSRun ?: -1L)
bundle.putString("suggested", lazyLoopPlugin.get().lastRun?.request?.json().toString())
if (lazyLoopPlugin.get().lastRun?.tbrSetByPump != null && lazyLoopPlugin.get().lastRun?.tbrSetByPump?.enacted == true) {
bundle.putLong("enactedTimeStamp", lazyLoopPlugin.get().lastRun?.lastTBREnact
if (Config.APS && loopPlugin.lastRun?.lastTBREnact != 0L) { //we are AndroidAPS
bundle.putLong("suggestedTimeStamp", loopPlugin.lastRun?.lastAPSRun ?: -1L)
bundle.putString("suggested", loopPlugin.lastRun?.request?.json().toString())
if (loopPlugin.lastRun?.tbrSetByPump != null && loopPlugin.lastRun?.tbrSetByPump?.enacted == true) {
bundle.putLong("enactedTimeStamp", loopPlugin.lastRun?.lastTBREnact
?: -1L)
bundle.putString("enacted", lazyLoopPlugin.get().lastRun?.request?.json().toString())
bundle.putString("enacted", loopPlugin.lastRun?.request?.json().toString())
}
} else { //NSClient or remote
val data = NSDeviceStatus.deviceStatusOpenAPSData

View file

@ -39,11 +39,9 @@ import info.nightscout.androidaps.logging.StacktraceLoggerWrapper;
import info.nightscout.androidaps.plugins.aps.loop.APSResult;
import info.nightscout.androidaps.plugins.aps.loop.DeviceStatus;
import info.nightscout.androidaps.plugins.aps.loop.LoopPlugin;
import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin;
import info.nightscout.androidaps.plugins.configBuilder.PluginStore;
import info.nightscout.androidaps.plugins.configBuilder.ProfileFunction;
import info.nightscout.androidaps.plugins.iob.iobCobCalculator.IobCobCalculatorPlugin;
import info.nightscout.androidaps.utils.BatteryLevel;
import info.nightscout.androidaps.receivers.ReceiverStatusStore;
import info.nightscout.androidaps.utils.DateUtil;
import info.nightscout.androidaps.utils.JsonHelper;
import info.nightscout.androidaps.utils.SP;
@ -160,7 +158,7 @@ public class NSUpload {
}
}
public static void uploadDeviceStatus(LoopPlugin loopPlugin, IobCobCalculatorPlugin iobCobCalculatorPlugin, ProfileFunction profileFunction, PumpInterface pumpInterface) {
public static void uploadDeviceStatus(LoopPlugin loopPlugin, IobCobCalculatorPlugin iobCobCalculatorPlugin, ProfileFunction profileFunction, PumpInterface pumpInterface, ReceiverStatusStore receiverStatusStore) {
Profile profile = profileFunction.getProfile();
String profileName = profileFunction.getProfileName();
@ -216,7 +214,7 @@ public class NSUpload {
deviceStatus.pump = pumpstatus;
}
int batteryLevel = BatteryLevel.getBatteryLevel();
int batteryLevel = receiverStatusStore.getBatteryLevel();
deviceStatus.uploaderBattery = batteryLevel;
deviceStatus.created_at = DateUtil.toISOString(new Date());

View file

@ -50,7 +50,7 @@ import info.nightscout.androidaps.plugins.iob.iobCobCalculator.GlucoseStatus;
import info.nightscout.androidaps.plugins.iob.iobCobCalculator.IobCobCalculatorPlugin;
import info.nightscout.androidaps.plugins.treatments.Treatment;
import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin;
import info.nightscout.androidaps.utils.BatteryLevel;
import info.nightscout.androidaps.receivers.ReceiverStatusStore;
import info.nightscout.androidaps.utils.DecimalFormatter;
import info.nightscout.androidaps.utils.DefaultValueHelper;
import info.nightscout.androidaps.utils.ToastUtils;
@ -71,6 +71,7 @@ public class WatchUpdaterService extends WearableListenerService implements Goog
@Inject public IobCobCalculatorPlugin iobCobCalculatorPlugin;
@Inject public TreatmentsPlugin treatmentsPlugin;
@Inject public ActionStringHandler actionStringHandler;
@Inject ReceiverStatusStore receiverStatusStore;
public static final String ACTION_RESEND = WatchUpdaterService.class.getName().concat(".Resend");
public static final String ACTION_OPEN_SETTINGS = WatchUpdaterService.class.getName().concat(".OpenSettings");
@ -705,7 +706,7 @@ public class WatchUpdaterService extends WearableListenerService implements Goog
//batteries
int phoneBattery = BatteryLevel.getBatteryLevel();
int phoneBattery = receiverStatusStore.getBatteryLevel();
String rigBattery = nsDeviceStatus.getUploaderStatus().trim();

View file

@ -19,16 +19,24 @@ class ChargingStateReceiver : DaggerBroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
super.onReceive(context, intent)
rxBus.send(grabChargingState(context))
aapsLogger.debug(LTag.CORE, receiverStatusStore.lastChargingEvent!!.toString())
aapsLogger.debug(LTag.CORE, receiverStatusStore.lastChargingEvent?.toString()
?: "Unknown charging state")
}
private fun grabChargingState(context: Context): EventChargingState {
val batteryStatus: Intent? = IntentFilter(Intent.ACTION_BATTERY_CHANGED).let { iFilter ->
context.registerReceiver(null, iFilter)
}
val batteryStatus = context.registerReceiver(null, IntentFilter(Intent.ACTION_BATTERY_CHANGED))
// Level
var batteryLevel = 0
val level = batteryStatus?.getIntExtra(BatteryManager.EXTRA_LEVEL, -1) ?: -1
val scale = batteryStatus?.getIntExtra(BatteryManager.EXTRA_SCALE, -1) ?: -1
if (level != -1 && scale != -1)
batteryLevel = (level.toFloat() / scale.toFloat() * 100.0f).toInt()
// Status
val status: Int = batteryStatus?.getIntExtra(BatteryManager.EXTRA_STATUS, -1) ?: -1
val isCharging: Boolean = status == BatteryManager.BATTERY_STATUS_CHARGING
|| status == BatteryManager.BATTERY_STATUS_FULL
return EventChargingState(isCharging).also { receiverStatusStore.lastChargingEvent = it }
return EventChargingState(isCharging, batteryLevel).also { receiverStatusStore.lastChargingEvent = it }
}
}

View file

@ -37,6 +37,7 @@ class KeepAliveReceiver : DaggerBroadcastReceiver() {
@Inject lateinit var iobCobCalculatorPlugin: IobCobCalculatorPlugin
@Inject lateinit var localAlertUtils: LocalAlertUtils
@Inject lateinit var fabricPrivacy: FabricPrivacy
@Inject lateinit var receiverStatusStore: ReceiverStatusStore
companion object {
@ -106,7 +107,7 @@ class KeepAliveReceiver : DaggerBroadcastReceiver() {
else if (DateUtil.isOlderThan(activePlugin.activeAPS.lastAPSRun, 5)) shouldUploadStatus = true
if (DateUtil.isOlderThan(lastIobUpload, IOB_UPDATE_FREQUENCY) && shouldUploadStatus) {
lastIobUpload = DateUtil.now()
NSUpload.uploadDeviceStatus(loopPlugin, iobCobCalculatorPlugin, profileFunction, activePlugin.activePump)
NSUpload.uploadDeviceStatus(loopPlugin, iobCobCalculatorPlugin, profileFunction, activePlugin.activePump, receiverStatusStore)
}
}

View file

@ -28,6 +28,9 @@ class ReceiverStatusStore @Inject constructor(val context: Context, val rxBus: R
val isCharging: Boolean
get() = lastChargingEvent?.isCharging ?: false
val batteryLevel: Int
get() = lastChargingEvent?.batterLevel ?: 0
fun broadcastChargingState() {
lastChargingEvent?.let { rxBus.send(it) }
}

View file

@ -1,35 +0,0 @@
package info.nightscout.androidaps.utils;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.logging.StacktraceLoggerWrapper;
/**
* Created by mike on 20.02.2017.
*/
public class BatteryLevel {
private static Logger log = StacktraceLoggerWrapper.getLogger(BatteryLevel.class);
static public int lastUploadedLevel = 0;
static public int getBatteryLevel() {
int batteryLevel = 0;
Intent batteryIntent = MainApp.instance().registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
if (batteryIntent != null) {
int level = batteryIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = batteryIntent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
if (level != -1 && scale != -1) {
batteryLevel = (int) (((float) level / (float) scale) * 100.0f);
}
}
log.debug("Battery level: " + batteryLevel);
return batteryLevel;
}
}