reworked to have start and end of time range

This commit is contained in:
Roumen Georgiev 2019-09-09 16:50:48 +03:00
parent 2a80477bb5
commit c88841b361
2 changed files with 73 additions and 35 deletions

View file

@ -25,7 +25,6 @@ import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.logging.L;
import info.nightscout.androidaps.plugins.general.automation.elements.Comparator;
import info.nightscout.androidaps.plugins.general.automation.elements.LayoutBuilder;
import info.nightscout.androidaps.utils.DateUtil;
import info.nightscout.androidaps.utils.JsonHelper;
import info.nightscout.androidaps.utils.T;
@ -35,19 +34,24 @@ import info.nightscout.androidaps.utils.T;
public class TriggerTimeOfDay extends Trigger {
private static Logger log = LoggerFactory.getLogger(L.AUTOMATION);
private int minSinceMidnight;
// in minutes since midnight 6- means 1AM
private int start;
private int end;
private Comparator comparator = new Comparator();
public TriggerTimeOfDay() {
minSinceMidnight = getMinSinceMidnight(DateUtil.now());
start = getMinSinceMidnight(DateUtil.now());
end = getMinSinceMidnight(DateUtil.now());
}
private TriggerTimeOfDay(TriggerTimeOfDay triggerTimeOfDay) {
super();
lastRun = triggerTimeOfDay.lastRun;
minSinceMidnight = triggerTimeOfDay.minSinceMidnight;
start = triggerTimeOfDay.start;
end = triggerTimeOfDay.end;
}
@Override
@ -57,7 +61,14 @@ public class TriggerTimeOfDay extends Trigger {
if (lastRun > DateUtil.now() - T.mins(5).msecs())
return false;
boolean doRun = comparator.getValue().check(currentMinSinceMidnight, minSinceMidnight);
boolean doRun = false;
if ( start < end && start < currentMinSinceMidnight && currentMinSinceMidnight < end)
doRun = true;
// handle cases like 10PM to 6AM
else if ( start > end && (start < currentMinSinceMidnight || currentMinSinceMidnight < end))
doRun = true;
if (doRun) {
if (L.isEnabled(L.AUTOMATION))
log.debug("Ready for execution: " + friendlyDescription());
@ -72,14 +83,16 @@ public class TriggerTimeOfDay extends Trigger {
JSONObject data = new JSONObject();
// check for too big values
if (minSinceMidnight > 1440)
minSinceMidnight = getMinSinceMidnight(minSinceMidnight);
if (start > 1440)
start = getMinSinceMidnight(start);
if (end > 1440)
end = getMinSinceMidnight(end);
try {
data.put("minSinceMidnight", getMinSinceMidnight(minSinceMidnight));
data.put("start", getMinSinceMidnight(start));
data.put("end", getMinSinceMidnight(end));
data.put("lastRun", lastRun);
object.put("type", TriggerTimeOfDay.class.getName());
data.put("comparator", comparator.getValue().toString());
object.put("data", data);
} catch (JSONException e) {
e.printStackTrace();
@ -94,8 +107,8 @@ public class TriggerTimeOfDay extends Trigger {
try {
o = new JSONObject(data);
lastRun = JsonHelper.safeGetLong(o, "lastRun");
minSinceMidnight = JsonHelper.safeGetInt(o, "minSinceMidnight");
comparator.setValue(Comparator.Compare.valueOf(JsonHelper.safeGetString(o, "comparator")));
start = JsonHelper.safeGetInt(o, "start");
end = JsonHelper.safeGetInt(o, "end");
} catch (JSONException e) {
e.printStackTrace();
}
@ -109,7 +122,7 @@ public class TriggerTimeOfDay extends Trigger {
@Override
public String friendlyDescription() {
return MainApp.gs(R.string.tod_value, MainApp.gs(comparator.getValue().getStringRes()), DateUtil.timeString(toMilis(minSinceMidnight)));
return MainApp.gs(R.string.tod_value, DateUtil.timeString(toMilis(start)), R.string.and, " ", DateUtil.timeString(toMilis(end)));
}
@Override
@ -117,8 +130,8 @@ public class TriggerTimeOfDay extends Trigger {
return Optional.of(R.drawable.ic_access_alarm_24dp);
}
TriggerTimeOfDay minSinceMidnight(int minSinceMidnight) {
this.minSinceMidnight = minSinceMidnight;
TriggerTimeOfDay start(int start) {
this.start = start;
return this;
}
@ -133,8 +146,8 @@ public class TriggerTimeOfDay extends Trigger {
}
long toMilis(long minutesSinceMidnight) {
long hours = minSinceMidnight / 60;//hours
long minutes = minSinceMidnight % 60;//hours
long hours = minutesSinceMidnight / 60;//hours
long minutes = minutesSinceMidnight % 60;//hours
return (hours*60*60*1000)+(minutes*60*1000);
}
@ -148,26 +161,52 @@ public class TriggerTimeOfDay extends Trigger {
return (calendar.get(Calendar.HOUR_OF_DAY) * 60) + calendar.get(Calendar.MINUTE);
}
int getMinSinceMidnight(){
return minSinceMidnight;
int getStart(){
return start;
}
int getEnd(){
return end;
}
@Override
public void generateDialog(LinearLayout root, FragmentManager fragmentManager) {
TextView label = new TextView(root.getContext());
TextView timeButton = new TextView(root.getContext());
TextView startButton = new TextView(root.getContext());
TextView endButton = new TextView(root.getContext());
timeButton.setText(DateUtil.timeString(toMilis(minSinceMidnight)));
startButton.setText(DateUtil.timeString(toMilis(start)));
endButton.setText(MainApp.gs(R.string.and) + " " + DateUtil.timeString(toMilis(end)));
timeButton.setOnClickListener(view -> {
startButton.setOnClickListener(view -> {
GregorianCalendar calendar = new GregorianCalendar();
calendar.setTimeInMillis(toMilis(minSinceMidnight));
calendar.setTimeInMillis(toMilis(start));
TimePickerDialog tpd = TimePickerDialog.newInstance(
(view12, hourOfDay, minute, second) -> {
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
calendar.set(Calendar.MINUTE, minute);
minSinceMidnight = getMinSinceMidnight(calendar.getTimeInMillis());
timeButton.setText(DateUtil.timeString(minSinceMidnight));
start = getMinSinceMidnight(calendar.getTimeInMillis());
startButton.setText(DateUtil.timeString(start));
},
calendar.get(Calendar.HOUR_OF_DAY),
calendar.get(Calendar.MINUTE),
DateFormat.is24HourFormat(root.getContext())
);
tpd.setThemeDark(true);
tpd.dismissOnPause(true);
Activity a = scanForActivity(root.getContext());
if (a != null)
tpd.show(a.getFragmentManager(), "TimePickerDialog");
});
endButton.setOnClickListener(view -> {
GregorianCalendar calendar = new GregorianCalendar();
calendar.setTimeInMillis(toMilis(end));
TimePickerDialog tpd = TimePickerDialog.newInstance(
(view12, hourOfDay, minute, second) -> {
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
calendar.set(Calendar.MINUTE, minute);
end = getMinSinceMidnight(calendar.getTimeInMillis());
endButton.setText(MainApp.gs(R.string.and) + " " + DateUtil.timeString(end));
},
calendar.get(Calendar.HOUR_OF_DAY),
calendar.get(Calendar.MINUTE),
@ -181,19 +220,18 @@ public class TriggerTimeOfDay extends Trigger {
});
int px = MainApp.dpToPx(10);
label.setText(MainApp.gs(R.string.thanspecifiedtime, ""));
label.setText(MainApp.gs(R.string.thanspecifiedtime));
label.setTypeface(label.getTypeface(), Typeface.BOLD);
label.setPadding(px, px, px, px);
timeButton.setPadding(px, px, px, px);
new LayoutBuilder()
.add(comparator)
.build(root);
startButton.setPadding(px, px, px, px);
endButton.setPadding(px, px, px, px);
LinearLayout l = new LinearLayout(root.getContext());
l.setOrientation(LinearLayout.HORIZONTAL);
l.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
l.addView(label);
l.addView(timeButton);
l.addView(startButton);
l.addView(endButton);
root.addView(l);
}
}

View file

@ -1605,9 +1605,9 @@
<string name="deliverpartofboluswizard">Bolus wizard performs calculation but only this part of calculated insulin is delivered. Useful with SMB algorithm.</string>
<string name="loading">Loading ...</string>
<string name="snooze">Snooze</string>
<string name="time_of_day">Time of day</string>
<string name="tod_value">Time of day %1$s %2$s</string>
<string name="thanspecifiedtime">%1$s</string>
<string name="time_of_day">Time range</string>
<string name="tod_value">Time is between %1$s and %2$s</string>
<string name="thanspecifiedtime">Between </string>
<plurals name="objective_days">
<item quantity="one">%1$d day</item>