use of de.avanux.smartapplianceenabler.schedule.Schedule in project SmartApplianceEnabler by camueller.
the class RunningTimeMonitorTest method getRemainingMinRunningTimeOfCurrentTimeFrame_2TimeFrames.
@Test
public void getRemainingMinRunningTimeOfCurrentTimeFrame_2TimeFrames() {
List<Schedule> schedules = new ArrayList<Schedule>();
schedules.add(new Schedule(600, null, new TimeOfDay(11, 0, 0), new TimeOfDay(12, 0, 0)));
schedules.add(new Schedule(1200, null, new TimeOfDay(14, 0, 0), new TimeOfDay(15, 0, 0)));
runningTimeMonitor.setSchedules(schedules);
//
// 1. timeframe
//
runningTimeMonitor.updateActiveTimeframeInterval(toToday(10, 00, 0));
Assert.assertNull("Timeframe not yet started should return NULL", runningTimeMonitor.getRemainingMinRunningTimeOfCurrentTimeFrame(toToday(10, 0, 0)));
runningTimeMonitor.updateActiveTimeframeInterval(toToday(11, 00, 0));
Assert.assertEquals("With timeframe started but device switched off max running time should be returned", 600, runningTimeMonitor.getRemainingMinRunningTimeOfCurrentTimeFrame(toToday(11, 20, 0)).intValue());
runningTimeMonitor.setRunning(true, toToday(11, 30, 0));
Assert.assertEquals("With timeframe started and device switched on max running time should be reduced by 5 minutes", 300, runningTimeMonitor.getRemainingMinRunningTimeOfCurrentTimeFrame(toToday(11, 35, 0)).intValue());
Assert.assertEquals("Running time has to be 0 at the end of the timeframe", 0, runningTimeMonitor.getRemainingMinRunningTimeOfCurrentTimeFrame(toToday(11, 40, 0)).intValue());
runningTimeMonitor.setRunning(false, toToday(11, 40, 0));
Assert.assertEquals("Running time has to be 0 at the end of running time", 0, runningTimeMonitor.getRemainingMinRunningTimeOfCurrentTimeFrame(toToday(11, 45, 0)).intValue());
runningTimeMonitor.updateActiveTimeframeInterval(toToday(12, 00, 0));
//
// 2. timeframe
//
runningTimeMonitor.updateActiveTimeframeInterval(toToday(13, 00, 0));
Assert.assertNull("Timeframe not yet started should return NULL", runningTimeMonitor.getRemainingMinRunningTimeOfCurrentTimeFrame(toToday(13, 45, 0)));
runningTimeMonitor.updateActiveTimeframeInterval(toToday(14, 00, 0));
Assert.assertEquals("With timeframe started but device switched off max running time should be returned", 1200, runningTimeMonitor.getRemainingMinRunningTimeOfCurrentTimeFrame(toToday(14, 10, 0)).intValue());
runningTimeMonitor.setRunning(true, toToday(14, 15, 0));
Assert.assertEquals("With timeframe started and device switched on max running time should be reduced by 5 minutes", 900, runningTimeMonitor.getRemainingMinRunningTimeOfCurrentTimeFrame(toToday(14, 20, 0)).intValue());
Assert.assertEquals("Min running time has been exceeded by 10 minutes", -600, runningTimeMonitor.getRemainingMinRunningTimeOfCurrentTimeFrame(toToday(14, 45, 0)).intValue());
runningTimeMonitor.setRunning(false, toToday(14, 45, 0));
Assert.assertEquals("Running time after power off is 0", 0, runningTimeMonitor.getRemainingMinRunningTimeOfCurrentTimeFrame(toToday(14, 45, 0)).intValue());
runningTimeMonitor.updateActiveTimeframeInterval(toToday(15, 00, 0));
}
use of de.avanux.smartapplianceenabler.schedule.Schedule in project SmartApplianceEnabler by camueller.
the class RunningTimeMonitor method activateTimeframeInterval.
/**
* Activate the given timeframe interval.
* @param now
* @param timeframeIntervalToBeActivated the timeframe interval or null
*/
public void activateTimeframeInterval(LocalDateTime now, TimeframeInterval timeframeIntervalToBeActivated) {
boolean intervalChanged = false;
if (timeframeIntervalToBeActivated != null && activeTimeframeInterval == null) {
Schedule schedule = timeframeIntervalToBeActivated.getTimeframe().getSchedule();
runningTime = 0;
remainingMinRunningTimeWhileNotRunning = schedule.getMinRunningTime();
remainingMaxRunningTimeWhileNotRunning = schedule.getMaxRunningTime();
intervalChanged = true;
logger.debug("{}: Interval activated: ", applianceId, timeframeIntervalToBeActivated);
} else if (timeframeIntervalToBeActivated == null && activeTimeframeInterval != null) {
logger.debug("{}: Interval expired: {}", applianceId, activeTimeframeInterval);
running = false;
runningTime = null;
interrupted = false;
statusChangedAt = null;
remainingMinRunningTimeWhileNotRunning = null;
remainingMaxRunningTimeWhileNotRunning = null;
intervalChanged = true;
}
TimeframeInterval deactivatedTimeframeInterval = activeTimeframeInterval;
activeTimeframeInterval = timeframeIntervalToBeActivated;
if (intervalChanged) {
logger.debug("{}: Active interval changed. deactivatedTimeframeInterval={} activeTimeframeInterval={}", applianceId, deactivatedTimeframeInterval, activeTimeframeInterval);
for (ActiveIntervalChangedListener listener : scheduleChangedListeners) {
logger.debug("{}: Notifying {} {}", applianceId, ActiveIntervalChangedListener.class.getSimpleName(), listener.getClass().getSimpleName());
listener.activeIntervalChanged(now, applianceId, deactivatedTimeframeInterval, activeTimeframeInterval);
}
}
}
use of de.avanux.smartapplianceenabler.schedule.Schedule in project SmartApplianceEnabler by camueller.
the class SempControllerTest method getPlanningRequest_intervalAlreadyActive.
@Test
public void getPlanningRequest_intervalAlreadyActive() {
Identification identification = new Identification();
identification.setDeviceId(DEVICE_ID);
DeviceInfo deviceInfo = new DeviceInfo();
deviceInfo.setIdentification(identification);
setDeviceInfo(deviceInfo);
Appliance appliance = new Appliance();
appliance.setId(DEVICE_ID);
LocalDateTime now = toToday(9, 30, 0);
int remainingMaxRunningTime = 1800;
Schedule schedule = new Schedule(600, null, new TimeOfDay(now.minusSeconds(1)), new TimeOfDay(now.plusSeconds(remainingMaxRunningTime)));
de.avanux.smartapplianceenabler.schedule.Timeframe timeframe = schedule.getTimeframe();
timeframe.setSchedule(schedule);
RunningTimeMonitor runningTimeMonitor = mock(RunningTimeMonitor.class);
when(runningTimeMonitor.getSchedules()).thenReturn(Collections.singletonList(schedule));
when(runningTimeMonitor.getActiveTimeframeInterval()).thenReturn(timeframe.getIntervals(now).get(0));
when(runningTimeMonitor.getRemainingMinRunningTimeOfCurrentTimeFrame(now)).thenReturn(schedule.getMinRunningTime());
when(runningTimeMonitor.getRemainingMaxRunningTimeOfCurrentTimeFrame(now)).thenReturn(schedule.getMaxRunningTime());
appliance.setRunningTimeMonitor(runningTimeMonitor);
Appliances appliances = new Appliances();
appliances.setAppliances(Collections.singletonList(appliance));
ApplianceManager.getInstanceWithoutTimer().setAppliances(appliances);
Device2EM device2EM = sempController.createDevice2EM(now);
List<PlanningRequest> planningRequests = device2EM.getPlanningRequest();
Assert.assertEquals(1, planningRequests.size());
List<Timeframe> timeframes = planningRequests.get(0).getTimeframes();
Assert.assertEquals(3, timeframes.size());
assertTimeframe(timeframes.get(0), 0, 1800, 599, 600);
assertTimeframe(timeframes.get(1), 86399, 88200, 599, 600);
assertTimeframe(timeframes.get(2), 172799, 174600, 599, 600);
}
use of de.avanux.smartapplianceenabler.schedule.Schedule in project SmartApplianceEnabler by camueller.
the class RunningTimeMonitor method setSchedules.
public void setSchedules(List<Schedule> schedules) {
List<Schedule> enabledSchedules = new ArrayList<>();
for (Schedule schedule : schedules) {
if (schedule.isEnabled()) {
logger.debug("{}: Using enabled time frame {}", applianceId, schedule.toString());
enabledSchedules.add(schedule);
} else {
logger.debug("{}: Ignoring disabled time frame {}", applianceId, schedule.toString());
}
}
this.schedules = enabledSchedules;
this.activeTimeframeInterval = null;
}
use of de.avanux.smartapplianceenabler.schedule.Schedule in project SmartApplianceEnabler by camueller.
the class ScheduleTest method getCurrentOrNextTimeframeInterval_alreadyStarted_remainingRunningTimeInsufficient_secondTimeFrameOfDay.
@Test
public void getCurrentOrNextTimeframeInterval_alreadyStarted_remainingRunningTimeInsufficient_secondTimeFrameOfDay() {
List<Schedule> schedules = new ArrayList<Schedule>();
addSchedule(schedules, 7200, 15, 0, 18, 0);
addSchedule(schedules, 7200, 10, 0, 14, 0);
Interval expectedInterval = new Interval(toDay(1, 10, 0, 0).toDateTime(), toDay(1, 14, 0, 0).toDateTime());
Assert.assertEquals(expectedInterval, Schedule.getCurrentOrNextTimeframeInterval(toToday(16, 1), schedules, false, true).getInterval());
}
Aggregations