From 8dd5261b00a06b19f8f1a313de4786920160d759 Mon Sep 17 00:00:00 2001 From: Arjen Baart Date: Tue, 30 Mar 2021 17:51:24 +0200 Subject: [PATCH] Limit the sunrise time to a minimum --- src/event.cpp | 129 +++++++++++++++++------------------------- src/event.h | 1 + test/Makefile.am | 6 +- test/sunset_summer | 37 ++++++++++++ test/sunset_summer.log.expect | 66 +++++++++++++++++++++ test/sunset_winter.log.expect | 15 ++--- test/wakeup_alarms.xml | 2 +- test/wakeup_spring.log.expect | 2 +- test/wakeup_summer | 4 +- test/wakeup_summer.log.expect | 78 +++++++++++++++++++++---- 10 files changed, 237 insertions(+), 103 deletions(-) create mode 100755 test/sunset_summer create mode 100644 test/sunset_summer.log.expect diff --git a/src/event.cpp b/src/event.cpp index 1c81ec4..01ef249 100644 --- a/src/event.cpp +++ b/src/event.cpp @@ -13,7 +13,8 @@ void Event::FromXML(xml_element x) std::vector elems; std::vector recur; std::map attr; - + std::map start_attr; + attr = x.attributes(); @@ -22,6 +23,9 @@ void Event::FromXML(xml_element x) elems = x["start"]; start_time = elems[0].content(); + start_attr = elems[0].attributes(); + //minimum_time = start_attr[String("minimum_time")]; + minimum_time = elems[0].attribute("minimum_time"); elems = x["action"]; for (unsigned int i = 0; i < elems.size(); i++) @@ -132,14 +136,6 @@ String Event::ToXML(void) xml_text += seq->ToXML(); -/* - for (act = sequence.begin(); act != sequence.end(); act++) - { - xml_text += " "; - xml_text += (*act)->command_line(); - xml_text += "\n"; - } -*/ xml_text += " \n"; return xml_text; @@ -208,64 +204,9 @@ void Sequence::add_action(String command) void Event::add_action(String command) { - String to_execute, parameters; - int separation; seq->add_action(command); - separation = command.index(' '); - if (separation > 0) - { - to_execute = command(0, separation); - parameters = command << separation + 1; - } - else - { - to_execute = command; - } - - if (to_execute == "lightcontrol") - { - Lightstep *act; - act = new Lightstep; - - act->set_parameters(parameters); - sequence.push_back(act); - } - else if (to_execute == "curtain") - { - Curtainstep *act; - act = new Curtainstep; - - act->set_parameters(parameters); - sequence.push_back(act); - } - else if (to_execute == "sleep") - { - Sleepstep *act; - act = new Sleepstep; - - act->set_parameters(parameters); - sequence.push_back(act); - } - else if (to_execute == "cancel") - { - Cancelstep *act; - act = new Cancelstep; - - act->set_parameters(parameters); - sequence.push_back(act); - } - else if (to_execute == "exit") - { - Exitstep *act; - act = new Exitstep; - sequence.push_back(act); - } - else - { - Log << "Action " + to_execute + " not recognized"; - } } void _run_(Sequence * sequence) @@ -305,6 +246,23 @@ Sequence *Event::run_sequence() return seq; } +UTC clip_time(UTC t, String minimum) +{ + //std::cout << "Minimum_time = \"" << minimum << "\"\n"; // DEBUG + + if (minimum != "") + { + hour t_min(minimum); + + if (hour(t) < t_min) + { + t = UTC(date(t), t_min); + } + } + + return t; +} + UTC Event::next_occurance(UTC after, Location loc) { UTC next; @@ -314,32 +272,51 @@ UTC Event::next_occurance(UTC after, Location loc) { hour sunrise_at = loc.calculate_sunrise(date(after)); next = loc.utc_to_localtime(date(after), sunrise_at); + next = clip_time(next, minimum_time); + if (next <= after) + { + // Today's sunrise already passed. Calculate for tomorrow + + sunrise_at = loc.calculate_sunrise(date(after) + 1); + next = loc.utc_to_localtime(date(after) + 1, sunrise_at); + next = clip_time(next, minimum_time); + } + //Log << " sunrise time = " + sunrise_at.format(); + //Log << " next sunrise after " + after.format() + " is " + next.format(); } else if (start_time == "sunset") { hour sunset_at = loc.calculate_sunset(date(after)); next = loc.utc_to_localtime(date(after), sunset_at); + next = clip_time(next, minimum_time); + if (next <= after) + { + // Today's sunset already passed. Calculate for tomorrow + + sunset_at = loc.calculate_sunset(date(after) + 1); + next = loc.utc_to_localtime(date(after) + 1, sunset_at); + next = clip_time(next, minimum_time); + } + //Log << " sunset time = " + sunset_at.format(); + //Log << " next sunset after " + after.format() + " is " + next.format(); } else { next = start_time; } - if (pattern != "none") + if (pattern == "weekdays") + { + while (next <= after || weekdays.find(date(next).WeekDay()) == weekdays.end()) + { + next += UTC(date(1, 0, 0), hour(0)); + } + } + else if (pattern != "none") { while (next <= after) { - if (pattern == "weekdays") - { - while (next <= after || weekdays.find(date(next).WeekDay()) == weekdays.end()) - { - next += UTC(date(1, 0, 0), hour(0)); - } - } - else - { - next += recurrance_interval; - } + next += recurrance_interval; } } diff --git a/src/event.h b/src/event.h index 37b8617..384a7f4 100644 --- a/src/event.h +++ b/src/event.h @@ -40,6 +40,7 @@ class Event { String label; String start_time; + String minimum_time; String pattern; // for recurrance UTC recurrance_interval; diff --git a/test/Makefile.am b/test/Makefile.am index d99a522..8ebcaf2 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -1,8 +1,7 @@ TESTS = lightctrl lightctrl-oor lightctrl-fade \ wakeup_load_events sunrise_test event_recurrance event_run_sequence \ - wakeup_winter sunset_winter wakeup_spring \ + wakeup_winter sunset_winter wakeup_spring wakeup_summer sunset_summer \ check_output -# wakeup_summer AM_CPPFLAGS = -I../src LDADD = -lTachyon -lACL @@ -15,3 +14,6 @@ event_run_sequence_SOURCES = event_run_sequence.cpp ../src/event.cpp ../src/logg sunrise_test_SOURCES = sunrise_test.cpp ../src/sunrise.cpp log_retime_SOURCES = log_retime.cpp + +clean-local: + rm -f *.pid *.run *.log *.log2 *.log.test diff --git a/test/sunset_summer b/test/sunset_summer new file mode 100755 index 0000000..3c29f71 --- /dev/null +++ b/test/sunset_summer @@ -0,0 +1,37 @@ +#!/bin/bash +# +# Test the wakeup sequence in winter. +# +set -m +PATH=../src:$PATH + +# Reset the lights and clear the log files + +lightcontrol -r 0 -g 0 -b 0 -w 0 +> lightcontrol.log +> wakeup.log + +ln -f -s wakeup_empty.xml wakeup_link.xml + +wakeup & +sleep 1 + +read TACHYON_NAME sunset_summer.log.test + +diff sunset_summer.log.test sunset_summer.log.expect + + +exit $? diff --git a/test/sunset_summer.log.expect b/test/sunset_summer.log.expect new file mode 100644 index 0000000..481e3cf --- /dev/null +++ b/test/sunset_summer.log.expect @@ -0,0 +1,66 @@ + wakeup started + Reading alarms from wakeup_link.xml + SIGHUP received. + Reading alarms from wakeup_link.xml + Alarm sunset triggered at 2020-06-18 21:54:19 + [sunset] Executing curtain -c 1 -t + [sunset] Executing lightcontrol -b 99 -w 50 + Alarm evening_dim triggered at 2020-06-18 22:00:00 + [sunset] Stopping sequence + [evening_dim] Executing lightcontrol -b 50 -w 0 -f 900 -a 1000 + Alarm lights_off triggered at 2020-06-18 22:30:00 + [evening_dim] Stopping sequence + [lights_off] Executing lightcontrol -b 0 -f 1800 -a 1000 + Alarm sunset triggered at 2020-06-19 21:54:25 + [lights_off] Stopping sequence + [sunset] Executing curtain -c 1 -t + [sunset] Executing lightcontrol -b 99 -w 50 + Alarm evening_dim triggered at 2020-06-19 22:00:00 + [sunset] Stopping sequence + [evening_dim] Executing lightcontrol -b 50 -w 0 -f 900 -a 1000 + Alarm lights_off triggered at 2020-06-19 22:30:00 + [evening_dim] Stopping sequence + [lights_off] Executing lightcontrol -b 0 -f 1800 -a 1000 + Alarm sunset triggered at 2020-06-20 21:54:28 + [lights_off] Stopping sequence + [sunset] Executing curtain -c 1 -t + [sunset] Executing lightcontrol -b 99 -w 50 + Alarm evening_dim triggered at 2020-06-20 22:00:00 + [sunset] Stopping sequence + [evening_dim] Executing lightcontrol -b 50 -w 0 -f 900 -a 1000 + Alarm lights_off triggered at 2020-06-20 22:30:00 + [evening_dim] Stopping sequence + [lights_off] Executing lightcontrol -b 0 -f 1800 -a 1000 + Alarm sunset triggered at 2020-06-21 21:54:28 + [lights_off] Stopping sequence + [sunset] Executing curtain -c 1 -t + [sunset] Executing lightcontrol -b 99 -w 50 + Alarm evening_dim triggered at 2020-06-21 22:00:00 + [sunset] Stopping sequence + [evening_dim] Executing lightcontrol -b 50 -w 0 -f 900 -a 1000 + Alarm lights_off triggered at 2020-06-21 22:30:00 + [evening_dim] Stopping sequence + [lights_off] Executing lightcontrol -b 0 -f 1800 -a 1000 + Alarm sunset triggered at 2020-06-22 21:54:25 + [lights_off] Stopping sequence + [sunset] Executing curtain -c 1 -t + [sunset] Executing lightcontrol -b 99 -w 50 + Alarm evening_dim triggered at 2020-06-22 22:00:00 + [sunset] Stopping sequence + [evening_dim] Executing lightcontrol -b 50 -w 0 -f 900 -a 1000 + Alarm lights_off triggered at 2020-06-22 22:30:00 + [evening_dim] Stopping sequence + [lights_off] Executing lightcontrol -b 0 -f 1800 -a 1000 + Alarm sunset triggered at 2020-06-23 21:54:19 + [lights_off] Stopping sequence + [sunset] Executing curtain -c 1 -t + [sunset] Executing lightcontrol -b 99 -w 50 + Alarm evening_dim triggered at 2020-06-23 22:00:00 + [sunset] Stopping sequence + [evening_dim] Executing lightcontrol -b 50 -w 0 -f 900 -a 1000 + Alarm lights_off triggered at 2020-06-23 22:30:00 + [evening_dim] Stopping sequence + [lights_off] Executing lightcontrol -b 0 -f 1800 -a 1000 + Alarm end_test triggered at 2020-06-24 11:00:00 + [lights_off] Stopping sequence + [end_test] Executing exit diff --git a/test/sunset_winter.log.expect b/test/sunset_winter.log.expect index 1d3b946..6ce8390 100644 --- a/test/sunset_winter.log.expect +++ b/test/sunset_winter.log.expect @@ -11,7 +11,7 @@ Alarm lights_off triggered at 2020-12-17 22:30:00 [evening_dim] Stopping sequence [lights_off] Executing lightcontrol -b 0 -f 1800 -a 1000 - Alarm sunset triggered at 2020-12-18 16:27:00 + Alarm sunset triggered at 2020-12-18 16:26:53 [lights_off] Stopping sequence [sunset] Executing curtain -c 1 -t [sunset] Executing lightcontrol -b 99 -w 50 @@ -21,7 +21,7 @@ Alarm lights_off triggered at 2020-12-18 22:30:00 [evening_dim] Stopping sequence [lights_off] Executing lightcontrol -b 0 -f 1800 -a 1000 - Alarm sunset triggered at 2020-12-19 16:26:53 + Alarm sunset triggered at 2020-12-19 16:26:48 [lights_off] Stopping sequence [sunset] Executing curtain -c 1 -t [sunset] Executing lightcontrol -b 99 -w 50 @@ -31,7 +31,7 @@ Alarm lights_off triggered at 2020-12-19 22:30:00 [evening_dim] Stopping sequence [lights_off] Executing lightcontrol -b 0 -f 1800 -a 1000 - Alarm sunset triggered at 2020-12-20 16:26:48 + Alarm sunset triggered at 2020-12-20 16:26:47 [lights_off] Stopping sequence [sunset] Executing curtain -c 1 -t [sunset] Executing lightcontrol -b 99 -w 50 @@ -41,7 +41,7 @@ Alarm lights_off triggered at 2020-12-20 22:30:00 [evening_dim] Stopping sequence [lights_off] Executing lightcontrol -b 0 -f 1800 -a 1000 - Alarm sunset triggered at 2020-12-21 16:26:47 + Alarm sunset triggered at 2020-12-21 16:26:48 [lights_off] Stopping sequence [sunset] Executing curtain -c 1 -t [sunset] Executing lightcontrol -b 99 -w 50 @@ -51,15 +51,10 @@ Alarm lights_off triggered at 2020-12-21 22:30:00 [evening_dim] Stopping sequence [lights_off] Executing lightcontrol -b 0 -f 1800 -a 1000 - Alarm sunset triggered at 2020-12-22 16:26:48 - [lights_off] Stopping sequence - [sunset] Executing curtain -c 1 -t Alarm sunset triggered at 2020-12-22 16:26:53 - [sunset] Stopping sequence + [lights_off] Stopping sequence [sunset] Executing curtain -c 1 -t [sunset] Executing lightcontrol -b 99 -w 50 - [sunset] Executing lightcontrol -b 99 -w 50 - [sunset] Executing lightcontrol -b 99 -w 50 Alarm evening_dim triggered at 2020-12-22 22:00:00 [sunset] Stopping sequence [evening_dim] Executing lightcontrol -b 50 -w 0 -f 900 -a 1000 diff --git a/test/wakeup_alarms.xml b/test/wakeup_alarms.xml index 87b2f77..a6cf38c 100644 --- a/test/wakeup_alarms.xml +++ b/test/wakeup_alarms.xml @@ -11,7 +11,7 @@ lightcontrol -w 10 -f 300 -a 1000 - sunrise + sunrise cancel lightson_workdays curtain -o 2 -t lightcontrol -r 0 -g 0 -w 0 -f 100 -a 1000 diff --git a/test/wakeup_spring.log.expect b/test/wakeup_spring.log.expect index e7157fa..c834ac1 100644 --- a/test/wakeup_spring.log.expect +++ b/test/wakeup_spring.log.expect @@ -56,7 +56,7 @@ Alarm lightson_workdays triggered at 2020-05-13 06:00:00 [open_curtains] Stopping sequence [lightson_workdays] Executing lightcontrol -r 10 -f 300 -a 1000 - Alarm sunrise triggered at 2020-05-13 06:00:03 + Alarm sunrise triggered at 2020-05-13 06:00:05 [lightson_workdays] Stopping sequence [sunrise] Executing cancel lightson_workdays [sunrise] Executing curtain -o 2 -t diff --git a/test/wakeup_summer b/test/wakeup_summer index 442d775..3145e8d 100755 --- a/test/wakeup_summer +++ b/test/wakeup_summer @@ -18,9 +18,9 @@ wakeup & sleep 1 read TACHYON_NAME