Merge pull request #745 from TebbeUbben/dev

Update to SightRemote 1.4.0
This commit is contained in:
Milos Kozak 2018-03-10 23:39:52 +01:00 committed by GitHub
commit 6117ac97cf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 213 additions and 135 deletions

Binary file not shown.

View file

@ -27,13 +27,15 @@ import info.nightscout.androidaps.interfaces.ConstraintsInterface;
import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.androidaps.interfaces.PumpDescription;
import info.nightscout.androidaps.interfaces.PumpInterface;
import info.nightscout.androidaps.plugins.Overview.events.EventDismissNotification;
import info.nightscout.androidaps.plugins.Overview.events.EventNewNotification;
import info.nightscout.androidaps.plugins.Overview.events.EventOverviewBolusProgress;
import info.nightscout.androidaps.plugins.Overview.notifications.Notification;
import info.nightscout.androidaps.plugins.PumpInsight.connector.AbsoluteTBRTaskRunner;
import info.nightscout.androidaps.plugins.PumpInsight.connector.CancelBolusTaskRunner;
import info.nightscout.androidaps.plugins.PumpInsight.connector.Connector;
import info.nightscout.androidaps.plugins.PumpInsight.connector.ReadBasalProfileTaskRunner;
import info.nightscout.androidaps.plugins.PumpInsight.connector.SetTBRTaskRunner;
import info.nightscout.androidaps.plugins.PumpInsight.connector.WriteBasalProfileTaskRunner;
import info.nightscout.androidaps.plugins.PumpInsight.events.EventInsightPumpCallback;
import info.nightscout.androidaps.plugins.PumpInsight.events.EventInsightPumpUpdateGui;
import info.nightscout.androidaps.plugins.PumpInsight.history.HistoryReceiver;
@ -46,6 +48,7 @@ import info.nightscout.utils.SP;
import sugar.free.sightparser.applayer.descriptors.ActiveBolus;
import sugar.free.sightparser.applayer.descriptors.ActiveBolusType;
import sugar.free.sightparser.applayer.descriptors.PumpStatus;
import sugar.free.sightparser.applayer.descriptors.configuration_blocks.BRProfileBlock;
import sugar.free.sightparser.applayer.messages.AppLayerMessage;
import sugar.free.sightparser.applayer.messages.remote_control.BolusMessage;
import sugar.free.sightparser.applayer.messages.remote_control.CancelTBRMessage;
@ -56,7 +59,6 @@ import sugar.free.sightparser.handling.TaskRunner;
import sugar.free.sightparser.handling.taskrunners.StatusTaskRunner;
import static info.nightscout.androidaps.plugins.PumpInsight.history.PumpIdCache.getRecordUniqueID;
import static info.nightscout.androidaps.plugins.PumpInsight.utils.Helpers.roundDouble;
/**
@ -93,6 +95,7 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface, Constraints
private double basalRate = 0;
private Connector connector;
private volatile boolean connector_enabled = false;
private List<BRProfileBlock.ProfileBlock> profileBlocks;
private final TaskRunner.ResultCallback statusResultHandler = new TaskRunner.ResultCallback() {
@Override
@ -110,13 +113,17 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface, Constraints
@Override
public synchronized void onResult(Object result) {
log("GOT STATUS RESULT!!!");
statusResult = (StatusTaskRunner.StatusResult) result;
statusResultTime = Helpers.tsl();
processStatusResult();
updateGui();
connector.requestHistoryReSync();
connector.requestHistorySync();
if (result instanceof StatusTaskRunner.StatusResult) {
log("GOT STATUS RESULT!!!");
statusResult = (StatusTaskRunner.StatusResult) result;
statusResultTime = Helpers.tsl();
processStatusResult();
updateGui();
connector.requestHistoryReSync();
connector.requestHistorySync();
} else if (result instanceof List) {
profileBlocks = (List<BRProfileBlock.ProfileBlock>) result;
}
}
};
@ -142,7 +149,7 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface, Constraints
pumpDescription.tempDurationStep30mAllowed = true;
pumpDescription.tempMaxDuration = 24 * 60;
pumpDescription.isSetBasalProfileCapable = false; // leave this for now
pumpDescription.isSetBasalProfileCapable = true; // leave this for now
pumpDescription.basalStep = 0.01d;
pumpDescription.basalMinimumRate = 0.02d;
@ -378,6 +385,7 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface, Constraints
public void run() {
taskRunner = new StatusTaskRunner(connector.getServiceConnector());
taskRunner.fetch(statusResultHandler);
new ReadBasalProfileTaskRunner(connector.getServiceConnector()).fetch(statusResultHandler);
}
}
, 1000);
@ -386,21 +394,59 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface, Constraints
}
}
// TODO implement
@Override
public PumpEnactResult setNewBasalProfile(Profile profile) {
lastDataTime = new Date();
// Do nothing here. we are using MainApp.getConfigBuilder().getActiveProfile().getProfile();
PumpEnactResult result = new PumpEnactResult();
result.enacted = false;
result.success = false;
Notification notification = new Notification(Notification.PROFILE_SET_OK, MainApp.sResources.getString(R.string.profile_set_ok), Notification.INFO, 60);
MainApp.bus().post(new EventNewNotification(notification));
if (!isInitialized()) {
log.error("setNewBasalProfile not initialized");
Notification notification = new Notification(Notification.PROFILE_NOT_SET_NOT_INITIALIZED, MainApp.sResources.getString(R.string.pumpNotInitializedProfileNotSet), Notification.URGENT);
MainApp.bus().post(new EventNewNotification(notification));
result.comment = MainApp.sResources.getString(R.string.pumpNotInitializedProfileNotSet);
return result;
} else {
MainApp.bus().post(new EventDismissNotification(Notification.PROFILE_NOT_SET_NOT_INITIALIZED));
}
List<BRProfileBlock.ProfileBlock> profileBlocks = new ArrayList<>();
for (int i = 0; i < profile.getBasalValues().length; i++) {
Profile.BasalValue basalValue = profile.getBasalValues()[i];
Profile.BasalValue nextValue = null;
if (profile.getBasalValues().length > i + 1) nextValue = profile.getBasalValues()[i + 1];
profileBlocks.add(new BRProfileBlock.ProfileBlock((((nextValue != null ? nextValue.timeAsSeconds : 24 * 60 * 60) - basalValue.timeAsSeconds) / 60), basalValue.value));
}
final UUID uuid = aSyncTaskRunner(new WriteBasalProfileTaskRunner(connector.getServiceConnector(), profileBlocks), "Write basal profile");
final Mstatus ms = async.busyWaitForCommandResult(uuid, BUSY_WAIT_TIME);
if (ms.success()) {
MainApp.bus().post(new EventDismissNotification(Notification.PROFILE_NOT_SET_NOT_INITIALIZED));
MainApp.bus().post(new EventDismissNotification(Notification.FAILED_UDPATE_PROFILE));
Notification notification = new Notification(Notification.PROFILE_SET_OK, MainApp.sResources.getString(R.string.profile_set_ok), Notification.INFO, 60);
MainApp.bus().post(new EventNewNotification(notification));
result.success = true;
result.enacted = true;
result.comment = "OK";
} else {
Notification notification = new Notification(Notification.FAILED_UDPATE_PROFILE, MainApp.sResources.getString(R.string.failedupdatebasalprofile), Notification.URGENT);
MainApp.bus().post(new EventNewNotification(notification));
result.comment = MainApp.sResources.getString(R.string.failedupdatebasalprofile);
}
return result;
}
@Override
public boolean isThisProfileSet(Profile profile) {
if (!isInitialized() || profileBlocks == null) return true;
if (profile.getBasalValues().length != profileBlocks.size()) return false;
for (int i = 0; i < profileBlocks.size(); i++) {
BRProfileBlock.ProfileBlock profileBlock = profileBlocks.get(i);
Profile.BasalValue basalValue = profile.getBasalValues()[i];
Profile.BasalValue nextValue = null;
if (profile.getBasalValues().length > i + 1) nextValue = profile.getBasalValues()[i + 1];
if (profileBlock == null || basalValue == null) return false;
if (profileBlock.getDuration() * 60 != (nextValue != null ? nextValue.timeAsSeconds : 24 * 60 * 60) - basalValue.timeAsSeconds) return false;
//Allow a little imprecision due to rounding errors
if (Math.abs(profileBlock.getAmount() - basalValue.value) > 0.2D) return false;
}
return true;
}
@ -431,7 +477,7 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface, Constraints
// is there an insulin component to the treatment?
if (detailedBolusInfo.insulin > 0) {
final UUID cmd = deliverBolus((float) detailedBolusInfo.insulin); // actually request delivery
final UUID cmd = deliverBolus(detailedBolusInfo.insulin); // actually request delivery
if (cmd == null) {
return pumpEnactFailure();
}
@ -545,51 +591,6 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface, Constraints
return pumpEnactResult;
}
//@Override
public PumpEnactResult setTempBasalAbsoluteOld(Double absoluteRate, Integer durationInMinutes, boolean enforceNew) {
absoluteRate = Helpers.roundDouble(absoluteRate, 3);
log("Set TBR absolute: " + absoluteRate);
final AbsoluteTBRTaskRunner task = new AbsoluteTBRTaskRunner(connector.getServiceConnector(), absoluteRate, durationInMinutes);
final UUID cmd = aSyncTaskRunner(task, "Set TBR abs: " + absoluteRate + " " + durationInMinutes + "m");
if (cmd == null) {
return pumpEnactFailure();
}
Mstatus ms = async.busyWaitForCommandResult(cmd, BUSY_WAIT_TIME);
log("Got command status: " + ms);
PumpEnactResult pumpEnactResult = new PumpEnactResult().enacted(true).isPercent(false).duration(durationInMinutes);
pumpEnactResult.absolute = absoluteRate; // TODO get converted value?
pumpEnactResult.success = ms.success();
pumpEnactResult.isTempCancel = false; // do we test this here?
pumpEnactResult.comment = ms.getCommandComment();
if (pumpEnactResult.success) {
// create log entry
final TemporaryBasal tempBasal = new TemporaryBasal();
tempBasal.date = System.currentTimeMillis();
tempBasal.isAbsolute = true;
tempBasal.absoluteRate = task.getCalculatedAbsolute(); // is this the correct figure to use?
tempBasal.durationInMinutes = durationInMinutes;
tempBasal.source = Source.USER;
MainApp.getConfigBuilder().addToHistoryTempBasal(tempBasal);
}
if (Config.logPumpComm)
log.debug("Setting temp basal absolute: " + pumpEnactResult.success);
lastDataTime = new Date();
updateGui();
connector.requestHistorySync(5000);
connector.tryToGetPumpStatusAgain();
return pumpEnactResult;
}
@Override
public PumpEnactResult setTempBasalPercent(Integer percent, Integer durationInMinutes, boolean enforceNew) {
@ -686,8 +687,8 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface, Constraints
public PumpEnactResult setExtendedBolus(Double insulin, Integer durationInMinutes) {
log("Set Extended bolus " + insulin + " " + durationInMinutes);
ExtendedBolusMessage extendedBolusMessage = new ExtendedBolusMessage();
extendedBolusMessage.setAmount((float) ((double) insulin));
extendedBolusMessage.setDuration((short) ((int) durationInMinutes));
extendedBolusMessage.setAmount(insulin);
extendedBolusMessage.setDuration(durationInMinutes);
final UUID cmd = aSyncSingleCommand(extendedBolusMessage, "Extended bolus U" + insulin + " mins:" + durationInMinutes);
if (cmd == null) {
return pumpEnactFailure();
@ -755,7 +756,7 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface, Constraints
}
private synchronized UUID deliverBolus(float bolusValue) {
private synchronized UUID deliverBolus(double bolusValue) {
log("DeliverBolus: " + bolusValue);
if (bolusValue == 0) return null;
@ -838,7 +839,7 @@ public class InsightPumpPlugin implements PluginBase, PumpInterface, Constraints
if (statusResult != null) {
batteryPercent = statusResult.getBatteryAmountMessage().getBatteryAmount();
reservoirInUnits = (int) statusResult.getCartridgeAmountMessage().getCartridgeAmount();
basalRate = roundDouble(statusResult.getCurrentBasalMessage().getCurrentBasalAmount(), 2);
basalRate = statusResult.getCurrentBasalMessage().getCurrentBasalAmount();
initialized = true; // basic communication test
}
}

View file

@ -1,60 +0,0 @@
package info.nightscout.androidaps.plugins.PumpInsight.connector;
import info.nightscout.androidaps.plugins.PumpInsight.utils.Helpers;
import sugar.free.sightparser.applayer.messages.AppLayerMessage;
import sugar.free.sightparser.applayer.messages.remote_control.ChangeTBRMessage;
import sugar.free.sightparser.applayer.messages.remote_control.SetTBRMessage;
import sugar.free.sightparser.applayer.messages.status.CurrentBasalMessage;
import sugar.free.sightparser.applayer.messages.status.CurrentTBRMessage;
import sugar.free.sightparser.handling.SightServiceConnector;
import sugar.free.sightparser.handling.TaskRunner;
// by Tebbe Ubben
public class AbsoluteTBRTaskRunner extends TaskRunner {
private double absolute;
private int amount;
private int duration;
private int calculated_percentage;
private double calculated_absolute;
public AbsoluteTBRTaskRunner(SightServiceConnector serviceConnector, double absolute, int duration) {
super(serviceConnector);
if (absolute < 0) absolute = 0;
this.absolute = absolute;
this.duration = duration;
}
public int getCalculatedPercentage() {
return calculated_percentage;
}
public double getCalculatedAbsolute() {
return calculated_absolute;
}
@Override
protected AppLayerMessage run(AppLayerMessage message) throws Exception {
if (message == null) return new CurrentBasalMessage();
else if (message instanceof CurrentBasalMessage) {
float currentBasal = ((CurrentBasalMessage) message).getCurrentBasalAmount();
amount = (int) (100d / currentBasal * absolute);
amount = ((int) amount / 10) * 10;
if (amount > 250) amount = 250;
calculated_percentage = amount;
calculated_absolute = Helpers.roundDouble(calculated_percentage * (double) currentBasal / 100d, 3);
Connector.log("Asked: " + absolute + " current: " + currentBasal + " calculated as: " + amount + "%" + " = " + calculated_absolute);
return new CurrentTBRMessage();
} else if (message instanceof CurrentTBRMessage) {
SetTBRMessage setTBRMessage;
if (((CurrentTBRMessage) message).getPercentage() == 100)
setTBRMessage = new SetTBRMessage();
else setTBRMessage = new ChangeTBRMessage();
setTBRMessage.setAmount((short) amount);
setTBRMessage.setDuration((short) duration);
return setTBRMessage;
} else if (message instanceof SetTBRMessage) finish(amount);
return null;
}
}

View file

@ -0,0 +1,65 @@
package info.nightscout.androidaps.plugins.PumpInsight.connector;
import android.util.Log;
import sugar.free.sightparser.applayer.descriptors.configuration_blocks.ActiveProfileBlock;
import sugar.free.sightparser.applayer.descriptors.configuration_blocks.BRProfile1Block;
import sugar.free.sightparser.applayer.descriptors.configuration_blocks.BRProfile2Block;
import sugar.free.sightparser.applayer.descriptors.configuration_blocks.BRProfile3Block;
import sugar.free.sightparser.applayer.descriptors.configuration_blocks.BRProfile4Block;
import sugar.free.sightparser.applayer.descriptors.configuration_blocks.BRProfile5Block;
import sugar.free.sightparser.applayer.descriptors.configuration_blocks.BRProfileBlock;
import sugar.free.sightparser.applayer.descriptors.configuration_blocks.ConfigurationBlock;
import sugar.free.sightparser.applayer.messages.AppLayerMessage;
import sugar.free.sightparser.applayer.messages.configuration.ReadConfigurationBlockMessage;
import sugar.free.sightparser.handling.SightServiceConnector;
import sugar.free.sightparser.handling.TaskRunner;
/**
* Created by Tebbe Ubben on 10.03.2018.
*/
public class ReadBasalProfileTaskRunner extends TaskRunner {
public ReadBasalProfileTaskRunner(SightServiceConnector serviceConnector) {
super(serviceConnector);
}
@Override
protected AppLayerMessage run(AppLayerMessage message) throws Exception {
if (message == null) {
ReadConfigurationBlockMessage readMessage = new ReadConfigurationBlockMessage();
readMessage.setConfigurationBlockID(ActiveProfileBlock.ID);
return readMessage;
} else if (message instanceof ReadConfigurationBlockMessage) {
ConfigurationBlock configurationBlock = ((ReadConfigurationBlockMessage) message).getConfigurationBlock();
if (configurationBlock instanceof ActiveProfileBlock) {
ActiveProfileBlock activeProfileBlock = (ActiveProfileBlock) configurationBlock;
ReadConfigurationBlockMessage readMessage = new ReadConfigurationBlockMessage();
switch (activeProfileBlock.getActiveProfile()) {
case BR_PROFILE_1:
readMessage.setConfigurationBlockID(BRProfile1Block.ID);
break;
case BR_PROFILE_2:
readMessage.setConfigurationBlockID(BRProfile2Block.ID);
break;
case BR_PROFILE_3:
readMessage.setConfigurationBlockID(BRProfile3Block.ID);
break;
case BR_PROFILE_4:
readMessage.setConfigurationBlockID(BRProfile4Block.ID);
break;
case BR_PROFILE_5:
readMessage.setConfigurationBlockID(BRProfile5Block.ID);
break;
}
ReadConfigurationBlockMessage test = new ReadConfigurationBlockMessage();
test.setConfigurationBlockID(BRProfile1Block.ID);
return test;
} else if (configurationBlock instanceof BRProfileBlock) {
finish(((BRProfileBlock) configurationBlock).getProfileBlocks());
}
}
return null;
}
}

View file

@ -28,20 +28,20 @@ public class SetTBRTaskRunner extends TaskRunner {
if (amount == 100) finish(amount);
else {
SetTBRMessage setTBRMessage = new SetTBRMessage();
setTBRMessage.setDuration((short) duration);
setTBRMessage.setAmount((short) amount);
setTBRMessage.setDuration(duration);
setTBRMessage.setAmount(amount);
return setTBRMessage;
}
} else {
if (amount == 100) {
ChangeTBRMessage changeTBRMessage = new ChangeTBRMessage();
changeTBRMessage.setDuration((short) 1);
changeTBRMessage.setAmount((short) 90);
changeTBRMessage.setDuration(1);
changeTBRMessage.setAmount(90);
return changeTBRMessage;
} else {
ChangeTBRMessage changeTBRMessage = new ChangeTBRMessage();
changeTBRMessage.setDuration((short) duration);
changeTBRMessage.setAmount((short) amount);
changeTBRMessage.setDuration(duration);
changeTBRMessage.setAmount(amount);
return changeTBRMessage;
}
}

View file

@ -0,0 +1,72 @@
package info.nightscout.androidaps.plugins.PumpInsight.connector;
import java.util.List;
import sugar.free.sightparser.applayer.descriptors.configuration_blocks.ActiveProfileBlock;
import sugar.free.sightparser.applayer.descriptors.configuration_blocks.BRProfile1Block;
import sugar.free.sightparser.applayer.descriptors.configuration_blocks.BRProfile2Block;
import sugar.free.sightparser.applayer.descriptors.configuration_blocks.BRProfile3Block;
import sugar.free.sightparser.applayer.descriptors.configuration_blocks.BRProfile4Block;
import sugar.free.sightparser.applayer.descriptors.configuration_blocks.BRProfile5Block;
import sugar.free.sightparser.applayer.descriptors.configuration_blocks.BRProfileBlock;
import sugar.free.sightparser.applayer.descriptors.configuration_blocks.ConfigurationBlock;
import sugar.free.sightparser.applayer.messages.AppLayerMessage;
import sugar.free.sightparser.applayer.messages.configuration.CloseWriteSessionMessage;
import sugar.free.sightparser.applayer.messages.configuration.OpenWriteSessionMessage;
import sugar.free.sightparser.applayer.messages.configuration.ReadConfigurationBlockMessage;
import sugar.free.sightparser.applayer.messages.configuration.WriteConfigurationBlockMessage;
import sugar.free.sightparser.handling.SightServiceConnector;
import sugar.free.sightparser.handling.TaskRunner;
/**
* Created by Tebbe Ubben on 10.03.2018.
*/
public class WriteBasalProfileTaskRunner extends TaskRunner {
private List<BRProfileBlock.ProfileBlock> profileBlocks;
private BRProfileBlock profileBlock;
public WriteBasalProfileTaskRunner(SightServiceConnector serviceConnector, List<BRProfileBlock.ProfileBlock> profileBlocks) {
super(serviceConnector);
this.profileBlocks = profileBlocks;
}
@Override
protected AppLayerMessage run(AppLayerMessage message) throws Exception {
if (message == null) {
ReadConfigurationBlockMessage readMessage = new ReadConfigurationBlockMessage();
readMessage.setConfigurationBlockID(ActiveProfileBlock.ID);
return readMessage;
} else if (message instanceof ReadConfigurationBlockMessage) {
ConfigurationBlock configurationBlock = ((ReadConfigurationBlockMessage) message).getConfigurationBlock();
ActiveProfileBlock activeProfileBlock = (ActiveProfileBlock) configurationBlock;
switch (activeProfileBlock.getActiveProfile()) {
case BR_PROFILE_1:
profileBlock = new BRProfile1Block();
break;
case BR_PROFILE_2:
profileBlock = new BRProfile2Block();
break;
case BR_PROFILE_3:
profileBlock = new BRProfile3Block();
break;
case BR_PROFILE_4:
profileBlock = new BRProfile4Block();
break;
case BR_PROFILE_5:
profileBlock = new BRProfile5Block();
break;
}
profileBlock.setProfileBlocks(profileBlocks);
return new OpenWriteSessionMessage();
} else if (message instanceof OpenWriteSessionMessage) {
WriteConfigurationBlockMessage writeMessage = new WriteConfigurationBlockMessage();
writeMessage.setConfigurationBlock(profileBlock);
return writeMessage;
} else if (message instanceof WriteConfigurationBlockMessage) {
return new CloseWriteSessionMessage();
} else if (message instanceof CloseWriteSessionMessage) finish(null);
return null;
}
}

View file

@ -71,8 +71,8 @@ class HistoryIntentAdapter {
final long pump_serial_number = Long.parseLong(intent.getStringExtra(HistoryBroadcast.EXTRA_PUMP_SERIAL_NUMBER));
final Date event_time = getDateExtra(intent, HistoryBroadcast.EXTRA_EVENT_TIME);
final Date start_time = getDateExtra(intent, HistoryBroadcast.EXTRA_START_TIME);
final float immediate_amount = intent.getFloatExtra(HistoryBroadcast.EXTRA_IMMEDIATE_AMOUNT, -1);
final float extended_insulin = intent.getFloatExtra(HistoryBroadcast.EXTRA_EXTENDED_AMOUNT, -1);
final double immediate_amount = intent.getDoubleExtra(HistoryBroadcast.EXTRA_IMMEDIATE_AMOUNT, -1);
final double extended_insulin = intent.getDoubleExtra(HistoryBroadcast.EXTRA_EXTENDED_AMOUNT, -1);
final int extended_minutes = intent.getIntExtra(HistoryBroadcast.EXTRA_DURATION, -1);
final long record_unique_id = getRecordUniqueID(pump_serial_number, bolus_id > -1 ? bolus_id : pump_record_id);