Merge pull request #2092 from PoweRGbg/triggerPumpDisconnected

TriggerPumpDisconnected: Initial work
This commit is contained in:
Milos Kozak 2019-10-08 18:19:43 +02:00 committed by GitHub
commit 32865f6d34
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 264 additions and 1 deletions

View file

@ -221,7 +221,8 @@ object AutomationPlugin : PluginBase(PluginDescription()
TriggerWifiSsid(),
TriggerLocation(),
TriggerAutosensValue(),
TriggerBolusAgo()
TriggerBolusAgo(),
TriggerPumpLastConnection()
)
}

View file

@ -0,0 +1,146 @@
package info.nightscout.androidaps.plugins.general.automation.triggers;
import android.widget.LinearLayout;
import androidx.fragment.app.FragmentManager;
import com.google.common.base.Optional;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.logging.L;
import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin;
import info.nightscout.androidaps.plugins.general.automation.elements.Comparator;
import info.nightscout.androidaps.plugins.general.automation.elements.InputDuration;
import info.nightscout.androidaps.plugins.general.automation.elements.LabelWithElement;
import info.nightscout.androidaps.plugins.general.automation.elements.LayoutBuilder;
import info.nightscout.androidaps.plugins.general.automation.elements.StaticLabel;
import info.nightscout.androidaps.utils.DateUtil;
import info.nightscout.androidaps.utils.JsonHelper;
import info.nightscout.androidaps.utils.T;
public class TriggerPumpLastConnection extends Trigger {
private static Logger log = LoggerFactory.getLogger(L.AUTOMATION);
private InputDuration minutesAgo = new InputDuration(0, InputDuration.TimeUnit.MINUTES);
private Comparator comparator = new Comparator();
public TriggerPumpLastConnection() {
super();
}
private TriggerPumpLastConnection(TriggerPumpLastConnection triggerPumpLastConnection) {
super();
minutesAgo = new InputDuration(triggerPumpLastConnection.minutesAgo);
lastRun = triggerPumpLastConnection.lastRun;
comparator = new Comparator(triggerPumpLastConnection.comparator);
}
public double getValue() {
return minutesAgo.getValue();
}
public Comparator getComparator() {
return comparator;
}
@Override
public synchronized boolean shouldRun() {
if (lastRun > DateUtil.now() - T.mins(5).msecs())
return false;
long lastConnection = ConfigBuilderPlugin.getPlugin().getActivePump().lastDataTime();
if (lastConnection == 0 && comparator.getValue() == Comparator.Compare.IS_NOT_AVAILABLE)
return true;
double minutesAgo = (double) (DateUtil.now() - lastConnection) / (60 * 1000);
if (L.isEnabled(L.AUTOMATION))
log.debug("Last connection min ago: " + minutesAgo);
boolean doRun = comparator.getValue().check((minutesAgo), getValue());
if (doRun) {
if (L.isEnabled(L.AUTOMATION))
log.debug("Ready for execution: " + friendlyDescription());
return true;
}
return false;
}
@Override
public synchronized String toJSON() {
JSONObject o = new JSONObject();
try {
o.put("type", TriggerPumpLastConnection.class.getName());
JSONObject data = new JSONObject();
data.put("minutesAgo", getValue());
data.put("lastRun", lastRun);
data.put("comparator", comparator.getValue().toString());
o.put("data", data);
} catch (JSONException e) {
e.printStackTrace();
}
return o.toString();
}
@Override
Trigger fromJSON(String data) {
try {
JSONObject d = new JSONObject(data);
minutesAgo.setMinutes(JsonHelper.safeGetInt(d, "minutesAgo"));
lastRun = JsonHelper.safeGetLong(d, "lastRun");
comparator.setValue(Comparator.Compare.valueOf(JsonHelper.safeGetString(d, "comparator")));
} catch (Exception e) {
e.printStackTrace();
}
return this;
}
@Override
public int friendlyName() {
return R.string.automation_trigger_pump_last_connection_label;
}
@Override
public String friendlyDescription() {
return MainApp.gs(R.string.automation_trigger_pump_last_connection_compared, MainApp.gs(comparator.getValue().getStringRes()), (int) getValue());
}
@Override
public Optional<Integer> icon() {
return Optional.of(R.drawable.remove);
}
@Override
public Trigger duplicate() {
return new TriggerPumpLastConnection(this);
}
TriggerPumpLastConnection setValue(int requestedValue) {
this.minutesAgo.setMinutes(requestedValue);
return this;
}
TriggerPumpLastConnection lastRun(long lastRun) {
this.lastRun = lastRun;
return this;
}
TriggerPumpLastConnection comparator(Comparator.Compare compare) {
this.comparator = new Comparator().setValue(compare);
return this;
}
@Override
public void generateDialog(LinearLayout root, FragmentManager fragmentManager) {
new LayoutBuilder()
.add(new StaticLabel(R.string.automation_trigger_pump_last_connection_label))
.add(comparator)
.add(new LabelWithElement(MainApp.gs(R.string.automation_trigger_pump_last_connection_description) + ": ", "", minutesAgo))
.build(root);
}
}

View file

@ -1404,6 +1404,7 @@
<string name="remove_label">REMOVE</string>
<string name="preconditions">Preconditions:</string>
<!-- Pump Abstract -->
<string name="pump_operation_not_supported_by_pump_driver">Operation not supported by pump and/or driver.</string>
<string name="pump_operation_not_yet_supported_by_pump">Operation not YET supported by pump.</string>
@ -1574,6 +1575,9 @@
<string name="medtronic_cmd_desc_set_bolus">Set Bolus</string>
<string name="profilename">Change profile to</string>
<string name="changengetoprofilename">Change profile to %1$s</string>
<string name="automation_trigger_pump_last_connection_label">Last connection to pump</string>
<string name="automation_trigger_pump_last_connection_description">Last connection to pump [minutes ago]</string>
<string name="automation_trigger_pump_last_connection_compared">Last connection to pump %1$s %2$s min ago</string>
<string name="sendsmsactionlabel">Send SMS: %1$s</string>
<string name="sendsmsactiondescription">Send SMS to all numbers in preferences</string>
<string name="sendsmsactiontext">Send SMS with text</string>

View file

@ -0,0 +1,112 @@
package info.nightscout.androidaps.plugins.general.automation.triggers;
import com.google.common.base.Optional;
import com.squareup.otto.Bus;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import info.AAPSMocker;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.plugins.configBuilder.ConfigBuilderPlugin;
import info.nightscout.androidaps.plugins.configBuilder.ProfileFunctions;
import info.nightscout.androidaps.plugins.general.automation.elements.Comparator;
import info.nightscout.androidaps.plugins.pump.virtual.VirtualPumpPlugin;
import info.nightscout.androidaps.plugins.treatments.TreatmentsPlugin;
import info.nightscout.androidaps.utils.DateUtil;
import static org.powermock.api.mockito.PowerMockito.when;
@RunWith(PowerMockRunner.class)
@PrepareForTest({MainApp.class, Bus.class, ProfileFunctions.class, DateUtil.class, TreatmentsPlugin.class, ConfigBuilderPlugin.class, System.class})
public class TriggerPumpLastConnectionTest {
long now = 1514766900000L;
@Test
public void shouldRunTest() {
// System.currentTimeMillis() is always 0
// and so is every last connection time
VirtualPumpPlugin virtualPumpPlugin = VirtualPumpPlugin.getPlugin();
when(ConfigBuilderPlugin.getPlugin().getActivePump()).thenReturn(virtualPumpPlugin);
Assert.assertEquals(0L, virtualPumpPlugin.lastDataTime());
TriggerPumpLastConnection t = new TriggerPumpLastConnection().setValue(110).comparator(Comparator.Compare.IS_EQUAL).lastRun(now - 1);
Assert.assertFalse(t.shouldRun());
when(DateUtil.now()).thenReturn(now + (10*60*1000)); // set current time to now + 10 min
t = new TriggerPumpLastConnection().setValue(110).comparator(Comparator.Compare.IS_EQUAL);
Assert.assertEquals(110, t.getValue(), 0.01d);
Assert.assertEquals(Comparator.Compare.IS_EQUAL, t.getComparator().getValue());
Assert.assertFalse(t.shouldRun());
t = new TriggerPumpLastConnection().setValue(10).comparator(Comparator.Compare.IS_EQUAL);
Assert.assertEquals(10, t.getValue(), 0.01d);
Assert.assertFalse(t.shouldRun()); // 0 == 10 -> FALSE
t = new TriggerPumpLastConnection().setValue(5).comparator(Comparator.Compare.IS_EQUAL_OR_GREATER);
Assert.assertTrue(t.shouldRun()); // 5 => 0 -> TRUE
t = new TriggerPumpLastConnection().setValue(310).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER);
Assert.assertFalse(t.shouldRun()); // 310 <= 0 -> FALSE
t = new TriggerPumpLastConnection().setValue(420).comparator(Comparator.Compare.IS_EQUAL);
Assert.assertFalse(t.shouldRun()); // 420 == 0 -> FALSE
}
@Test
public void copyConstructorTest() {
TriggerPumpLastConnection t = new TriggerPumpLastConnection().setValue(213).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER);
TriggerPumpLastConnection t1 = (TriggerPumpLastConnection) t.duplicate();
Assert.assertEquals(213, t1.getValue(), 0.01d);
Assert.assertEquals(Comparator.Compare.IS_EQUAL_OR_LESSER, t.getComparator().getValue());
}
@Test
public void executeTest() {
TriggerPumpLastConnection t = new TriggerPumpLastConnection().setValue(213).comparator(Comparator.Compare.IS_EQUAL_OR_LESSER);
t.executed(1);
Assert.assertEquals(1l, t.getLastRun());
}
String LBJson = "{\"data\":{\"comparator\":\"IS_EQUAL\",\"lastRun\":0,\"minutesAgo\":410},\"type\":\"info.nightscout.androidaps.plugins.general.automation.triggers.TriggerPumpLastConnection\"}";
@Test
public void toJSONTest() {
TriggerPumpLastConnection t = new TriggerPumpLastConnection().setValue(410).comparator(Comparator.Compare.IS_EQUAL);
Assert.assertEquals(LBJson, t.toJSON());
}
@Test
public void fromJSONTest() throws JSONException {
TriggerPumpLastConnection t = new TriggerPumpLastConnection().setValue(410).comparator(Comparator.Compare.IS_EQUAL);
TriggerPumpLastConnection t2 = (TriggerPumpLastConnection) Trigger.instantiate(new JSONObject(t.toJSON()));
Assert.assertEquals(Comparator.Compare.IS_EQUAL, t2.getComparator().getValue());
Assert.assertEquals(410, t2.getValue(), 0.01d);
}
@Test
public void iconTest() {
Assert.assertEquals(Optional.of(R.drawable.remove), new TriggerPumpLastConnection().icon());
}
@Test
public void friendlyNameTest() {
Assert.assertEquals(R.string.automation_trigger_pump_last_connection_label, new TriggerPumpLastConnection().friendlyName());
}
@Before
public void mock() {
AAPSMocker.mockMainApp();
AAPSMocker.mockConfigBuilder();
AAPSMocker.mockBus();
PowerMockito.mockStatic(DateUtil.class);
when(DateUtil.now()).thenReturn(now);
}
}