Move uploading Care Portal events from some UI class to AapsOmnipodManager

This commit is contained in:
Bart Sopers 2020-09-24 23:23:08 +02:00
parent 50c3ec702c
commit fc90d3eb43
4 changed files with 43 additions and 108 deletions

View file

@ -11,7 +11,6 @@ import info.nightscout.androidaps.plugins.pump.omnipod.ui.PodHistoryActivity
import info.nightscout.androidaps.plugins.pump.omnipod.ui.PodManagementActivity
import info.nightscout.androidaps.plugins.pump.omnipod.ui.wizard.initpod.InitActionFragment
import info.nightscout.androidaps.plugins.pump.omnipod.ui.wizard.initpod.InitPodTask
import info.nightscout.androidaps.plugins.pump.omnipod.ui.wizard.pages.InitPodRefreshAction
import info.nightscout.androidaps.plugins.pump.omnipod.ui.wizard.pages.PodInfoFragment
import info.nightscout.androidaps.plugins.pump.omnipod.ui.wizard.removepod.RemoveActionFragment
@ -34,7 +33,6 @@ abstract class OmnipodModule {
abstract fun omnipodCommunicationManagerProvider(): OmnipodRileyLinkCommunicationManager
// Data
@ContributesAndroidInjector abstract fun initPodRefreshAction(): InitPodRefreshAction
@ContributesAndroidInjector abstract fun initPodTask(): InitPodTask
@ContributesAndroidInjector abstract fun rlHistoryItemOmnipod(): RLHistoryItemOmnipod

View file

@ -2,6 +2,8 @@ package info.nightscout.androidaps.plugins.pump.omnipod.manager;
import org.joda.time.DateTime;
import org.joda.time.Duration;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.Date;
@ -15,15 +17,18 @@ import dagger.android.HasAndroidInjector;
import info.nightscout.androidaps.data.DetailedBolusInfo;
import info.nightscout.androidaps.data.Profile;
import info.nightscout.androidaps.data.PumpEnactResult;
import info.nightscout.androidaps.db.CareportalEvent;
import info.nightscout.androidaps.db.OmnipodHistoryRecord;
import info.nightscout.androidaps.db.Source;
import info.nightscout.androidaps.db.TemporaryBasal;
import info.nightscout.androidaps.events.Event;
import info.nightscout.androidaps.interfaces.ActivePluginProvider;
import info.nightscout.androidaps.interfaces.DatabaseHelperInterface;
import info.nightscout.androidaps.interfaces.ProfileFunction;
import info.nightscout.androidaps.logging.AAPSLogger;
import info.nightscout.androidaps.logging.LTag;
import info.nightscout.androidaps.plugins.bus.RxBusWrapper;
import info.nightscout.androidaps.plugins.general.nsclient.NSUpload;
import info.nightscout.androidaps.plugins.general.overview.events.EventDismissNotification;
import info.nightscout.androidaps.plugins.general.overview.events.EventNewNotification;
import info.nightscout.androidaps.plugins.general.overview.events.EventOverviewBolusProgress;
@ -75,6 +80,7 @@ import info.nightscout.androidaps.plugins.pump.omnipod.event.EventOmnipodPumpVal
import info.nightscout.androidaps.plugins.pump.omnipod.rileylink.manager.OmnipodRileyLinkCommunicationManager;
import info.nightscout.androidaps.plugins.pump.omnipod.util.AapsOmnipodUtil;
import info.nightscout.androidaps.plugins.pump.omnipod.util.OmnipodAlertUtil;
import info.nightscout.androidaps.utils.DateUtil;
import info.nightscout.androidaps.utils.resources.ResourceHelper;
import info.nightscout.androidaps.utils.sharedPreferences.SP;
import io.reactivex.subjects.SingleSubject;
@ -93,6 +99,8 @@ public class AapsOmnipodManager {
private final OmnipodManager delegate;
private final DatabaseHelperInterface databaseHelper;
private final OmnipodAlertUtil omnipodAlertUtil;
private final NSUpload nsUpload;
private final ProfileFunction profileFunction;
private boolean basalBeepsEnabled;
private boolean bolusBeepsEnabled;
@ -113,10 +121,10 @@ public class AapsOmnipodManager {
HasAndroidInjector injector,
ActivePluginProvider activePlugin,
DatabaseHelperInterface databaseHelper,
OmnipodAlertUtil omnipodAlertUtil) {
if (podStateManager == null) {
throw new IllegalArgumentException("Pod state manager can not be null");
}
OmnipodAlertUtil omnipodAlertUtil,
NSUpload nsUpload,
ProfileFunction profileFunction
) {
this.podStateManager = podStateManager;
this.aapsOmnipodUtil = aapsOmnipodUtil;
this.aapsLogger = aapsLogger;
@ -127,6 +135,8 @@ public class AapsOmnipodManager {
this.activePlugin = activePlugin;
this.databaseHelper = databaseHelper;
this.omnipodAlertUtil = omnipodAlertUtil;
this.nsUpload = nsUpload;
this.profileFunction = profileFunction;
delegate = new OmnipodManager(aapsLogger, sp, communicationService, podStateManager);
@ -185,6 +195,10 @@ public class AapsOmnipodManager {
return new PumpEnactResult(injector).success(false).enacted(false).comment(errorMessage);
}
uploadCareportalEvent(System.currentTimeMillis() - 2000, CareportalEvent.PUMPBATTERYCHANGE);
uploadCareportalEvent(System.currentTimeMillis() - 1000, CareportalEvent.INSULINCHANGE);
uploadCareportalEvent(System.currentTimeMillis(), CareportalEvent.SITECHANGE);
rxBus.send(new EventDismissNotification(Notification.OMNIPOD_POD_NOT_ATTACHED));
cancelSuspendedFakeTbrIfExists();
@ -866,4 +880,29 @@ public class AapsOmnipodManager {
return new BasalSchedule(entries);
}
private void uploadCareportalEvent(long date, String event) {
if (databaseHelper.getCareportalEventFromTimestamp(date) != null)
return;
try {
JSONObject data = new JSONObject();
String enteredBy = sp.getString("careportal_enteredby", "");
if (enteredBy.isEmpty()) {
data.put("enteredBy", enteredBy);
}
data.put("created_at", DateUtil.toISOString(date));
data.put("mills", date);
data.put("eventType", event);
data.put("units", profileFunction.getUnits());
CareportalEvent careportalEvent = new CareportalEvent(injector);
careportalEvent.date = date;
careportalEvent.source = Source.USER;
careportalEvent.eventType = event;
careportalEvent.json = data.toString();
databaseHelper.createOrUpdate(careportalEvent);
nsUpload.uploadCareportalEntryToNS(data);
} catch (JSONException e) {
aapsLogger.error(LTag.PUMPCOMM, "Unhandled exception when uploading SiteChange event.", e);
}
}
}

View file

@ -20,11 +20,9 @@ import info.nightscout.androidaps.plugins.pump.omnipod.driver.definition.PodProg
import info.nightscout.androidaps.plugins.pump.omnipod.driver.manager.PodStateManager
import info.nightscout.androidaps.plugins.pump.omnipod.event.EventOmnipodPumpValuesChanged
import info.nightscout.androidaps.plugins.pump.omnipod.manager.AapsOmnipodManager
import info.nightscout.androidaps.plugins.pump.omnipod.ui.wizard.defs.PodActionType
import info.nightscout.androidaps.plugins.pump.omnipod.ui.wizard.model.FullInitPodWizardModel
import info.nightscout.androidaps.plugins.pump.omnipod.ui.wizard.model.RemovePodWizardModel
import info.nightscout.androidaps.plugins.pump.omnipod.ui.wizard.model.ShortInitPodWizardModel
import info.nightscout.androidaps.plugins.pump.omnipod.ui.wizard.pages.InitPodRefreshAction
import info.nightscout.androidaps.utils.FabricPrivacy
import info.nightscout.androidaps.utils.alertDialogs.OKDialog
import info.nightscout.androidaps.utils.extensions.plusAssign
@ -93,15 +91,11 @@ class PodManagementActivity : NoSplashAppCompatActivity() {
private fun initPodAction() {
val pagerSettings = WizardPagerSettings()
var refreshAction = InitPodRefreshAction(injector, PodActionType.INIT_POD)
pagerSettings.setWizardStepsWayType(WizardStepsWayType.CancelNext)
pagerSettings.setFinishStringResourceId(R.string.close)
pagerSettings.setFinishButtonBackground(R.drawable.finish_background)
pagerSettings.setNextButtonBackground(R.drawable.selectable_item_background)
pagerSettings.setBackStringResourceId(R.string.cancel)
pagerSettings.cancelAction = refreshAction
pagerSettings.finishAction = refreshAction
pagerSettings.pagerAdapterBehavior = FragmentStatePagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT
val wizardPagerContext = WizardPagerContext.getInstance()
@ -121,15 +115,12 @@ class PodManagementActivity : NoSplashAppCompatActivity() {
private fun deactivatePodAction() {
val pagerSettings = WizardPagerSettings()
var refreshAction = InitPodRefreshAction(injector, PodActionType.DEACTIVATE_POD)
pagerSettings.setWizardStepsWayType(WizardStepsWayType.CancelNext)
pagerSettings.setFinishStringResourceId(R.string.close)
pagerSettings.setFinishButtonBackground(R.drawable.finish_background)
pagerSettings.setNextButtonBackground(R.drawable.selectable_item_background)
pagerSettings.setBackStringResourceId(R.string.cancel)
pagerSettings.cancelAction = refreshAction
pagerSettings.finishAction = refreshAction
pagerSettings.pagerAdapterBehavior = FragmentStatePagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT
val wizardPagerContext = WizardPagerContext.getInstance();

View file

@ -1,93 +0,0 @@
package info.nightscout.androidaps.plugins.pump.omnipod.ui.wizard.pages;
import com.atech.android.library.wizardpager.defs.action.AbstractCancelAction;
import com.atech.android.library.wizardpager.defs.action.FinishActionInterface;
import org.json.JSONException;
import org.json.JSONObject;
import javax.inject.Inject;
import dagger.android.HasAndroidInjector;
import info.nightscout.androidaps.db.CareportalEvent;
import info.nightscout.androidaps.db.Source;
import info.nightscout.androidaps.interfaces.DatabaseHelperInterface;
import info.nightscout.androidaps.interfaces.ProfileFunction;
import info.nightscout.androidaps.logging.AAPSLogger;
import info.nightscout.androidaps.logging.LTag;
import info.nightscout.androidaps.plugins.general.nsclient.NSUpload;
import info.nightscout.androidaps.plugins.pump.omnipod.driver.manager.PodStateManager;
import info.nightscout.androidaps.plugins.pump.omnipod.ui.wizard.defs.PodActionType;
import info.nightscout.androidaps.utils.DateUtil;
import info.nightscout.androidaps.utils.sharedPreferences.SP;
/**
* Created by andy on 12/11/2019
*/
public class InitPodRefreshAction extends AbstractCancelAction implements FinishActionInterface {
private final PodActionType actionType;
private final HasAndroidInjector injector;
@Inject PodStateManager podStateManager;
@Inject AAPSLogger aapsLogger;
@Inject SP sp;
@Inject NSUpload nsUpload;
@Inject DatabaseHelperInterface databaseHelper;
@Inject ProfileFunction profileFunction;
public InitPodRefreshAction(HasAndroidInjector injector, PodActionType actionType) {
this.injector = injector;
injector.androidInjector().inject(this);
this.actionType = actionType;
}
@Override
public void execute(String cancelReason) {
if (cancelReason != null && cancelReason.trim().length() > 0) {
this.cancelActionText = cancelReason;
}
}
@Override
public void execute() {
if (actionType == PodActionType.INIT_POD) {
if (podStateManager.isPodRunning()) {
uploadCareportalEvent(System.currentTimeMillis() - 2000, CareportalEvent.PUMPBATTERYCHANGE);
uploadCareportalEvent(System.currentTimeMillis() - 1000, CareportalEvent.INSULINCHANGE);
uploadCareportalEvent(System.currentTimeMillis(), CareportalEvent.SITECHANGE);
}
}
}
private void uploadCareportalEvent(long date, String event) {
if (databaseHelper.getCareportalEventFromTimestamp(date) != null)
return;
try {
JSONObject data = new JSONObject();
String enteredBy = sp.getString("careportal_enteredby", "");
if (enteredBy.isEmpty()) {
data.put("enteredBy", enteredBy);
}
data.put("created_at", DateUtil.toISOString(date));
data.put("mills", date);
data.put("eventType", event);
data.put("units", profileFunction.getUnits());
CareportalEvent careportalEvent = new CareportalEvent(injector);
careportalEvent.date = date;
careportalEvent.source = Source.USER;
careportalEvent.eventType = event;
careportalEvent.json = data.toString();
databaseHelper.createOrUpdate(careportalEvent);
nsUpload.uploadCareportalEntryToNS(data);
} catch (JSONException e) {
aapsLogger.error(LTag.PUMPCOMM, "Unhandled exception when uploading SiteChange event.", e);
}
}
@Override
public String getFinishActionText() {
return "Finish_OK";
}
}