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.R;
import info.nightscout.androidaps.logging.L; import info.nightscout.androidaps.logging.L;
import info.nightscout.androidaps.plugins.general.automation.elements.Comparator; 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.DateUtil;
import info.nightscout.androidaps.utils.JsonHelper; import info.nightscout.androidaps.utils.JsonHelper;
import info.nightscout.androidaps.utils.T; import info.nightscout.androidaps.utils.T;
@ -36,18 +35,23 @@ import info.nightscout.androidaps.utils.T;
public class TriggerTimeOfDay extends Trigger { public class TriggerTimeOfDay extends Trigger {
private static Logger log = LoggerFactory.getLogger(L.AUTOMATION); 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(); private Comparator comparator = new Comparator();
public TriggerTimeOfDay() { public TriggerTimeOfDay() {
minSinceMidnight = getMinSinceMidnight(DateUtil.now());
start = getMinSinceMidnight(DateUtil.now());
end = getMinSinceMidnight(DateUtil.now());
} }
private TriggerTimeOfDay(TriggerTimeOfDay triggerTimeOfDay) { private TriggerTimeOfDay(TriggerTimeOfDay triggerTimeOfDay) {
super(); super();
lastRun = triggerTimeOfDay.lastRun; lastRun = triggerTimeOfDay.lastRun;
minSinceMidnight = triggerTimeOfDay.minSinceMidnight; start = triggerTimeOfDay.start;
end = triggerTimeOfDay.end;
} }
@Override @Override
@ -57,7 +61,14 @@ public class TriggerTimeOfDay extends Trigger {
if (lastRun > DateUtil.now() - T.mins(5).msecs()) if (lastRun > DateUtil.now() - T.mins(5).msecs())
return false; 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 (doRun) {
if (L.isEnabled(L.AUTOMATION)) if (L.isEnabled(L.AUTOMATION))
log.debug("Ready for execution: " + friendlyDescription()); log.debug("Ready for execution: " + friendlyDescription());
@ -72,14 +83,16 @@ public class TriggerTimeOfDay extends Trigger {
JSONObject data = new JSONObject(); JSONObject data = new JSONObject();
// check for too big values // check for too big values
if (minSinceMidnight > 1440) if (start > 1440)
minSinceMidnight = getMinSinceMidnight(minSinceMidnight); start = getMinSinceMidnight(start);
if (end > 1440)
end = getMinSinceMidnight(end);
try { try {
data.put("minSinceMidnight", getMinSinceMidnight(minSinceMidnight)); data.put("start", getMinSinceMidnight(start));
data.put("end", getMinSinceMidnight(end));
data.put("lastRun", lastRun); data.put("lastRun", lastRun);
object.put("type", TriggerTimeOfDay.class.getName()); object.put("type", TriggerTimeOfDay.class.getName());
data.put("comparator", comparator.getValue().toString());
object.put("data", data); object.put("data", data);
} catch (JSONException e) { } catch (JSONException e) {
e.printStackTrace(); e.printStackTrace();
@ -94,8 +107,8 @@ public class TriggerTimeOfDay extends Trigger {
try { try {
o = new JSONObject(data); o = new JSONObject(data);
lastRun = JsonHelper.safeGetLong(o, "lastRun"); lastRun = JsonHelper.safeGetLong(o, "lastRun");
minSinceMidnight = JsonHelper.safeGetInt(o, "minSinceMidnight"); start = JsonHelper.safeGetInt(o, "start");
comparator.setValue(Comparator.Compare.valueOf(JsonHelper.safeGetString(o, "comparator"))); end = JsonHelper.safeGetInt(o, "end");
} catch (JSONException e) { } catch (JSONException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -109,7 +122,7 @@ public class TriggerTimeOfDay extends Trigger {
@Override @Override
public String friendlyDescription() { 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 @Override
@ -117,8 +130,8 @@ public class TriggerTimeOfDay extends Trigger {
return Optional.of(R.drawable.ic_access_alarm_24dp); return Optional.of(R.drawable.ic_access_alarm_24dp);
} }
TriggerTimeOfDay minSinceMidnight(int minSinceMidnight) { TriggerTimeOfDay start(int start) {
this.minSinceMidnight = minSinceMidnight; this.start = start;
return this; return this;
} }
@ -133,8 +146,8 @@ public class TriggerTimeOfDay extends Trigger {
} }
long toMilis(long minutesSinceMidnight) { long toMilis(long minutesSinceMidnight) {
long hours = minSinceMidnight / 60;//hours long hours = minutesSinceMidnight / 60;//hours
long minutes = minSinceMidnight % 60;//hours long minutes = minutesSinceMidnight % 60;//hours
return (hours*60*60*1000)+(minutes*60*1000); 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); return (calendar.get(Calendar.HOUR_OF_DAY) * 60) + calendar.get(Calendar.MINUTE);
} }
int getMinSinceMidnight(){ int getStart(){
return minSinceMidnight; return start;
}
int getEnd(){
return end;
} }
@Override @Override
public void generateDialog(LinearLayout root, FragmentManager fragmentManager) { public void generateDialog(LinearLayout root, FragmentManager fragmentManager) {
TextView label = new TextView(root.getContext()); 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(); GregorianCalendar calendar = new GregorianCalendar();
calendar.setTimeInMillis(toMilis(minSinceMidnight)); calendar.setTimeInMillis(toMilis(start));
TimePickerDialog tpd = TimePickerDialog.newInstance( TimePickerDialog tpd = TimePickerDialog.newInstance(
(view12, hourOfDay, minute, second) -> { (view12, hourOfDay, minute, second) -> {
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay); calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
calendar.set(Calendar.MINUTE, minute); calendar.set(Calendar.MINUTE, minute);
minSinceMidnight = getMinSinceMidnight(calendar.getTimeInMillis()); start = getMinSinceMidnight(calendar.getTimeInMillis());
timeButton.setText(DateUtil.timeString(minSinceMidnight)); 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.HOUR_OF_DAY),
calendar.get(Calendar.MINUTE), calendar.get(Calendar.MINUTE),
@ -181,19 +220,18 @@ public class TriggerTimeOfDay extends Trigger {
}); });
int px = MainApp.dpToPx(10); 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.setTypeface(label.getTypeface(), Typeface.BOLD);
label.setPadding(px, px, px, px); startButton.setPadding(px, px, px, px);
timeButton.setPadding(px, px, px, px); endButton.setPadding(px, px, px, px);
new LayoutBuilder()
.add(comparator)
.build(root);
LinearLayout l = new LinearLayout(root.getContext()); LinearLayout l = new LinearLayout(root.getContext());
l.setOrientation(LinearLayout.HORIZONTAL); l.setOrientation(LinearLayout.HORIZONTAL);
l.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)); l.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
l.addView(label); l.addView(label);
l.addView(timeButton); l.addView(startButton);
l.addView(endButton);
root.addView(l); 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="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="loading">Loading ...</string>
<string name="snooze">Snooze</string> <string name="snooze">Snooze</string>
<string name="time_of_day">Time of day</string> <string name="time_of_day">Time range</string>
<string name="tod_value">Time of day %1$s %2$s</string> <string name="tod_value">Time is between %1$s and %2$s</string>
<string name="thanspecifiedtime">%1$s</string> <string name="thanspecifiedtime">Between </string>
<plurals name="objective_days"> <plurals name="objective_days">
<item quantity="one">%1$d day</item> <item quantity="one">%1$d day</item>