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 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.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import java.util.Date;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Singleton; 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.PumpDescription;
import info.nightscout.androidaps.interfaces.PumpInterface; import info.nightscout.androidaps.interfaces.PumpInterface;
import info.nightscout.androidaps.logging.AAPSLogger; import info.nightscout.androidaps.logging.AAPSLogger;
import info.nightscout.androidaps.logging.L;
import info.nightscout.androidaps.logging.LTag; import info.nightscout.androidaps.logging.LTag;
import info.nightscout.androidaps.plugins.aps.loop.events.EventLoopSetLastRunGui; import info.nightscout.androidaps.plugins.aps.loop.events.EventLoopSetLastRunGui;
import info.nightscout.androidaps.plugins.aps.loop.events.EventLoopUpdateGui; 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.plugins.treatments.TreatmentsPlugin;
import info.nightscout.androidaps.queue.Callback; import info.nightscout.androidaps.queue.Callback;
import info.nightscout.androidaps.queue.commands.Command; import info.nightscout.androidaps.queue.commands.Command;
import info.nightscout.androidaps.receivers.ReceiverStatusStore;
import info.nightscout.androidaps.utils.DateUtil; import info.nightscout.androidaps.utils.DateUtil;
import info.nightscout.androidaps.utils.FabricPrivacy; import info.nightscout.androidaps.utils.FabricPrivacy;
import info.nightscout.androidaps.utils.T; import info.nightscout.androidaps.utils.T;
@ -88,6 +86,8 @@ public class LoopPlugin extends PluginBase {
private final VirtualPumpPlugin virtualPumpPlugin; private final VirtualPumpPlugin virtualPumpPlugin;
private final Lazy<ActionStringHandler> actionStringHandler; private final Lazy<ActionStringHandler> actionStringHandler;
private final IobCobCalculatorPlugin iobCobCalculatorPlugin; private final IobCobCalculatorPlugin iobCobCalculatorPlugin;
private final ReceiverStatusStore receiverStatusStore;
private final FabricPrivacy fabricPrivacy;
private CompositeDisposable disposable = new CompositeDisposable(); private CompositeDisposable disposable = new CompositeDisposable();
@ -130,7 +130,9 @@ public class LoopPlugin extends PluginBase {
TreatmentsPlugin treatmentsPlugin, TreatmentsPlugin treatmentsPlugin,
VirtualPumpPlugin virtualPumpPlugin, VirtualPumpPlugin virtualPumpPlugin,
Lazy<ActionStringHandler> actionStringHandler, // TODO Adrian use RxBus instead of Lazy Lazy<ActionStringHandler> actionStringHandler, // TODO Adrian use RxBus instead of Lazy
IobCobCalculatorPlugin iobCobCalculatorPlugin IobCobCalculatorPlugin iobCobCalculatorPlugin,
ReceiverStatusStore receiverStatusStore,
FabricPrivacy fabricPrivacy
) { ) {
super(new PluginDescription() super(new PluginDescription()
.mainType(PluginType.LOOP) .mainType(PluginType.LOOP)
@ -154,6 +156,8 @@ public class LoopPlugin extends PluginBase {
this.virtualPumpPlugin = virtualPumpPlugin; this.virtualPumpPlugin = virtualPumpPlugin;
this.actionStringHandler = actionStringHandler; this.actionStringHandler = actionStringHandler;
this.iobCobCalculatorPlugin = iobCobCalculatorPlugin; this.iobCobCalculatorPlugin = iobCobCalculatorPlugin;
this.receiverStatusStore = receiverStatusStore;
this.fabricPrivacy = fabricPrivacy;
loopSuspendedTill = sp.getLong("loopSuspendedTill", 0L); loopSuspendedTill = sp.getLong("loopSuspendedTill", 0L);
isSuperBolus = sp.getBoolean("isSuperBolus", false); isSuperBolus = sp.getBoolean("isSuperBolus", false);
@ -167,7 +171,7 @@ public class LoopPlugin extends PluginBase {
disposable.add(rxBus disposable.add(rxBus
.toObservable(EventTempTargetChange.class) .toObservable(EventTempTargetChange.class)
.observeOn(Schedulers.io()) .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 * This method is triggered once autosens calculation has completed, so the LoopPlugin
@ -191,7 +195,7 @@ public class LoopPlugin extends PluginBase {
lastBgTriggeredRun = bgReading.date; lastBgTriggeredRun = bgReading.date;
invoke("AutosenseCalculation for " + bgReading, true); 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.lastSMBEnact = 0;
lastRun.lastSMBRequest = 0; lastRun.lastSMBRequest = 0;
NSUpload.uploadDeviceStatus(this, iobCobCalculatorPlugin, profileFunction, activePlugin.getActivePump()); NSUpload.uploadDeviceStatus(this, iobCobCalculatorPlugin, profileFunction, activePlugin.getActivePump(), receiverStatusStore);
if (isSuspended()) { if (isSuspended()) {
getAapsLogger().debug(LTag.APS, resourceHelper.gs(R.string.loopsuspended)); getAapsLogger().debug(LTag.APS, resourceHelper.gs(R.string.loopsuspended));
@ -418,7 +422,7 @@ public class LoopPlugin extends PluginBase {
if (resultAfterConstraints.bolusRequested) if (resultAfterConstraints.bolusRequested)
lastRun.smbSetByPump = waiting; lastRun.smbSetByPump = waiting;
rxBus.send(new EventLoopUpdateGui()); rxBus.send(new EventLoopUpdateGui());
FabricPrivacy.getInstance().logCustom("APSRequest"); fabricPrivacy.logCustom("APSRequest");
applyTBRRequest(resultAfterConstraints, profile, new Callback() { applyTBRRequest(resultAfterConstraints, profile, new Callback() {
@Override @Override
public void run() { public void run() {
@ -516,13 +520,13 @@ public class LoopPlugin extends PluginBase {
lastRun.lastTBRRequest = lastRun.lastAPSRun; lastRun.lastTBRRequest = lastRun.lastAPSRun;
lastRun.lastTBREnact = DateUtil.now(); lastRun.lastTBREnact = DateUtil.now();
lastRun.lastOpenModeAccept = 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); sp.incInt(R.string.key_ObjectivesmanualEnacts);
} }
rxBus.send(new EventAcceptOpenLoopChange()); 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.Intent
import android.content.pm.ResolveInfo import android.content.pm.ResolveInfo
import android.os.Bundle import android.os.Bundle
import dagger.Lazy
import dagger.android.HasAndroidInjector import dagger.android.HasAndroidInjector
import info.nightscout.androidaps.Config import info.nightscout.androidaps.Config
import info.nightscout.androidaps.R 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.GlucoseStatus
import info.nightscout.androidaps.plugins.iob.iobCobCalculator.IobCobCalculatorPlugin import info.nightscout.androidaps.plugins.iob.iobCobCalculator.IobCobCalculatorPlugin
import info.nightscout.androidaps.plugins.iob.iobCobCalculator.events.EventAutosensCalculationFinished 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.services.Intents
import info.nightscout.androidaps.utils.BatteryLevel
import info.nightscout.androidaps.utils.DefaultValueHelper import info.nightscout.androidaps.utils.DefaultValueHelper
import info.nightscout.androidaps.utils.FabricPrivacy import info.nightscout.androidaps.utils.FabricPrivacy
import info.nightscout.androidaps.utils.resources.ResourceHelper import info.nightscout.androidaps.utils.resources.ResourceHelper
@ -52,8 +51,10 @@ class DataBroadcastPlugin @Inject constructor(
private val profileFunction: ProfileFunction, private val profileFunction: ProfileFunction,
private val defaultValueHelper: DefaultValueHelper, private val defaultValueHelper: DefaultValueHelper,
private val nsDeviceStatus: NSDeviceStatus, private val nsDeviceStatus: NSDeviceStatus,
private val lazyLoopPlugin: Lazy<LoopPlugin>, private val loopPlugin: LoopPlugin,
private val activePlugin: ActivePluginProvider private val activePlugin: ActivePluginProvider,
private var receiverStatusStore: ReceiverStatusStore
) : PluginBase(PluginDescription() ) : PluginBase(PluginDescription()
.mainType(PluginType.GENERAL) .mainType(PluginType.GENERAL)
@ -154,16 +155,16 @@ class DataBroadcastPlugin @Inject constructor(
private fun loopStatus(bundle: Bundle) { private fun loopStatus(bundle: Bundle) {
//batteries //batteries
bundle.putInt("phoneBattery", BatteryLevel.getBatteryLevel()) bundle.putInt("phoneBattery", receiverStatusStore.batteryLevel)
bundle.putInt("rigBattery", nsDeviceStatus.uploaderStatus.replace("%", "").trim { it <= ' ' }.toInt()) bundle.putInt("rigBattery", nsDeviceStatus.uploaderStatus.replace("%", "").trim { it <= ' ' }.toInt())
if (Config.APS && lazyLoopPlugin.get().lastRun?.lastTBREnact != 0L) { //we are AndroidAPS if (Config.APS && loopPlugin.lastRun?.lastTBREnact != 0L) { //we are AndroidAPS
bundle.putLong("suggestedTimeStamp", lazyLoopPlugin.get().lastRun?.lastAPSRun ?: -1L) bundle.putLong("suggestedTimeStamp", loopPlugin.lastRun?.lastAPSRun ?: -1L)
bundle.putString("suggested", lazyLoopPlugin.get().lastRun?.request?.json().toString()) bundle.putString("suggested", loopPlugin.lastRun?.request?.json().toString())
if (lazyLoopPlugin.get().lastRun?.tbrSetByPump != null && lazyLoopPlugin.get().lastRun?.tbrSetByPump?.enacted == true) { if (loopPlugin.lastRun?.tbrSetByPump != null && loopPlugin.lastRun?.tbrSetByPump?.enacted == true) {
bundle.putLong("enactedTimeStamp", lazyLoopPlugin.get().lastRun?.lastTBREnact bundle.putLong("enactedTimeStamp", loopPlugin.lastRun?.lastTBREnact
?: -1L) ?: -1L)
bundle.putString("enacted", lazyLoopPlugin.get().lastRun?.request?.json().toString()) bundle.putString("enacted", loopPlugin.lastRun?.request?.json().toString())
} }
} else { //NSClient or remote } else { //NSClient or remote
val data = NSDeviceStatus.deviceStatusOpenAPSData 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.APSResult;
import info.nightscout.androidaps.plugins.aps.loop.DeviceStatus; import info.nightscout.androidaps.plugins.aps.loop.DeviceStatus;
import info.nightscout.androidaps.plugins.aps.loop.LoopPlugin; 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.configBuilder.ProfileFunction;
import info.nightscout.androidaps.plugins.iob.iobCobCalculator.IobCobCalculatorPlugin; 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.DateUtil;
import info.nightscout.androidaps.utils.JsonHelper; import info.nightscout.androidaps.utils.JsonHelper;
import info.nightscout.androidaps.utils.SP; 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(); Profile profile = profileFunction.getProfile();
String profileName = profileFunction.getProfileName(); String profileName = profileFunction.getProfileName();
@ -216,7 +214,7 @@ public class NSUpload {
deviceStatus.pump = pumpstatus; deviceStatus.pump = pumpstatus;
} }
int batteryLevel = BatteryLevel.getBatteryLevel(); int batteryLevel = receiverStatusStore.getBatteryLevel();
deviceStatus.uploaderBattery = batteryLevel; deviceStatus.uploaderBattery = batteryLevel;
deviceStatus.created_at = DateUtil.toISOString(new Date()); 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.iob.iobCobCalculator.IobCobCalculatorPlugin;
import info.nightscout.androidaps.plugins.treatments.Treatment; import info.nightscout.androidaps.plugins.treatments.Treatment;
import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin; 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.DecimalFormatter;
import info.nightscout.androidaps.utils.DefaultValueHelper; import info.nightscout.androidaps.utils.DefaultValueHelper;
import info.nightscout.androidaps.utils.ToastUtils; import info.nightscout.androidaps.utils.ToastUtils;
@ -71,6 +71,7 @@ public class WatchUpdaterService extends WearableListenerService implements Goog
@Inject public IobCobCalculatorPlugin iobCobCalculatorPlugin; @Inject public IobCobCalculatorPlugin iobCobCalculatorPlugin;
@Inject public TreatmentsPlugin treatmentsPlugin; @Inject public TreatmentsPlugin treatmentsPlugin;
@Inject public ActionStringHandler actionStringHandler; @Inject public ActionStringHandler actionStringHandler;
@Inject ReceiverStatusStore receiverStatusStore;
public static final String ACTION_RESEND = WatchUpdaterService.class.getName().concat(".Resend"); public static final String ACTION_RESEND = WatchUpdaterService.class.getName().concat(".Resend");
public static final String ACTION_OPEN_SETTINGS = WatchUpdaterService.class.getName().concat(".OpenSettings"); public static final String ACTION_OPEN_SETTINGS = WatchUpdaterService.class.getName().concat(".OpenSettings");
@ -705,7 +706,7 @@ public class WatchUpdaterService extends WearableListenerService implements Goog
//batteries //batteries
int phoneBattery = BatteryLevel.getBatteryLevel(); int phoneBattery = receiverStatusStore.getBatteryLevel();
String rigBattery = nsDeviceStatus.getUploaderStatus().trim(); String rigBattery = nsDeviceStatus.getUploaderStatus().trim();

View file

@ -19,16 +19,24 @@ class ChargingStateReceiver : DaggerBroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) { override fun onReceive(context: Context, intent: Intent) {
super.onReceive(context, intent) super.onReceive(context, intent)
rxBus.send(grabChargingState(context)) 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 { private fun grabChargingState(context: Context): EventChargingState {
val batteryStatus: Intent? = IntentFilter(Intent.ACTION_BATTERY_CHANGED).let { iFilter -> val batteryStatus = context.registerReceiver(null, IntentFilter(Intent.ACTION_BATTERY_CHANGED))
context.registerReceiver(null, iFilter)
} // 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 status: Int = batteryStatus?.getIntExtra(BatteryManager.EXTRA_STATUS, -1) ?: -1
val isCharging: Boolean = status == BatteryManager.BATTERY_STATUS_CHARGING val isCharging: Boolean = status == BatteryManager.BATTERY_STATUS_CHARGING
|| status == BatteryManager.BATTERY_STATUS_FULL || 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 iobCobCalculatorPlugin: IobCobCalculatorPlugin
@Inject lateinit var localAlertUtils: LocalAlertUtils @Inject lateinit var localAlertUtils: LocalAlertUtils
@Inject lateinit var fabricPrivacy: FabricPrivacy @Inject lateinit var fabricPrivacy: FabricPrivacy
@Inject lateinit var receiverStatusStore: ReceiverStatusStore
companion object { companion object {
@ -106,7 +107,7 @@ class KeepAliveReceiver : DaggerBroadcastReceiver() {
else if (DateUtil.isOlderThan(activePlugin.activeAPS.lastAPSRun, 5)) shouldUploadStatus = true else if (DateUtil.isOlderThan(activePlugin.activeAPS.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(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 val isCharging: Boolean
get() = lastChargingEvent?.isCharging ?: false get() = lastChargingEvent?.isCharging ?: false
val batteryLevel: Int
get() = lastChargingEvent?.batterLevel ?: 0
fun broadcastChargingState() { fun broadcastChargingState() {
lastChargingEvent?.let { rxBus.send(it) } 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;
}
}