Limit the sunrise time to a minimum
authorArjen Baart <arjen@andromeda.nl>
Tue, 30 Mar 2021 15:51:24 +0000 (17:51 +0200)
committerArjen Baart <arjen@andromeda.nl>
Tue, 30 Mar 2021 15:51:24 +0000 (17:51 +0200)
src/event.cpp
src/event.h
test/Makefile.am
test/sunset_summer [new file with mode: 0755]
test/sunset_summer.log.expect [new file with mode: 0644]
test/sunset_winter.log.expect
test/wakeup_alarms.xml
test/wakeup_spring.log.expect
test/wakeup_summer
test/wakeup_summer.log.expect

index 1c81ec4..01ef249 100644 (file)
@@ -13,7 +13,8 @@ void Event::FromXML(xml_element x)
    std::vector<xml_element> elems;
    std::vector<xml_element> recur;
    std::map<String, String> attr;
-
+   std::map<String, String> 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 += "    <action>";
-      xml_text += (*act)->command_line();
-      xml_text += "</action>\n";
-   }
-*/
    xml_text += "  </event>\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;
       }
    }
 
index 37b8617..384a7f4 100644 (file)
@@ -40,6 +40,7 @@ class Event
 {
    String              label;
    String              start_time;
+   String              minimum_time;
 
    String              pattern;    // for recurrance
    UTC                 recurrance_interval;
index d99a522..8ebcaf2 100644 (file)
@@ -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 (executable)
index 0000000..3c29f71
--- /dev/null
@@ -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 <wakeup.run
+Time=`date --date '2020-06-18 15:00' +%s`
+tachyon -t $Time $TACHYON_NAME
+tachyon -a 1000   $TACHYON_NAME
+
+ln -f -s sunset_alarms.xml wakeup_link.xml
+
+kill -HUP `cat wakeup.pid`
+
+fg %1
+#kill  `cat wakeup.pid`
+
+# Remove timestamps from the log.
+cut -c 22- wakeup.log >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 (file)
index 0000000..481e3cf
--- /dev/null
@@ -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
index 1d3b946..6ce8390 100644 (file)
@@ -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
  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
index 87b2f77..a6cf38c 100644 (file)
@@ -11,7 +11,7 @@
     <action>lightcontrol -w 10 -f 300 -a 1000</action>
   </event>
   <event id='sunrise'>
-    <start>sunrise</start>
+    <start minimum_time="06:00:05">sunrise</start>
     <action>cancel lightson_workdays</action>
     <action>curtain -o 2 -t</action>
     <action>lightcontrol -r 0 -g 0 -w 0 -f 100 -a 1000</action>
index e7157fa..c834ac1 100644 (file)
@@ -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
index 442d775..3145e8d 100755 (executable)
@@ -18,9 +18,9 @@ wakeup &
 sleep 1
 
 read TACHYON_NAME <wakeup.run
-Time=`date --date '2020-06-17 05:00' +%s`
+Time=`date --date '2020-06-18 04:00' +%s`
 tachyon -t $Time $TACHYON_NAME
-tachyon -a 100   $TACHYON_NAME
+tachyon -a 1000   $TACHYON_NAME
 
 ln -f -s wakeup_alarms.xml wakeup_link.xml
 
index ff80e7f..b8be187 100644 (file)
@@ -1,11 +1,67 @@
-wakeup started
-Reading alarms from wakeup_link.xml
-SIGHUP received.
-Reading alarms from wakeup_link.xml
-Alarm sunrise triggered at 2020-06-17 05:27:06
-Executing curtain -o 2 -t
-Executing lightcontrol -r 0 -g 0 -w 0 -f 2
-Alarm open_curtains triggered at 2020-06-17 09:00:00
-Executing curtain -o 1 -t
-Alarm end_test triggered at 2020-06-17 11:00:00
-Executing exit
+ wakeup started
+ Reading alarms from wakeup_link.xml
+ SIGHUP received.
+ Reading alarms from wakeup_link.xml
+ Alarm lightson_workdays triggered at 2020-06-18 06:00:00
+ [lightson_workdays] Executing lightcontrol -r 10 -f 300 -a 1000
+ Alarm sunrise triggered at 2020-06-18 06:00:05
+ [lightson_workdays] Stopping sequence 
+ [sunrise] Executing cancel lightson_workdays
+ [sunrise] Executing curtain -o 2 -t
+ [sunrise] Executing lightcontrol -r 0 -g 0 -w 0 -f 100 -a 1000
+ Alarm open_curtains triggered at 2020-06-18 09:00:00
+ [sunrise] Stopping sequence 
+ [open_curtains] Executing curtain -o 1 -t
+ Alarm lightson_workdays triggered at 2020-06-19 06:00:00
+ [open_curtains] Stopping sequence 
+ [lightson_workdays] Executing lightcontrol -r 10 -f 300 -a 1000
+ Alarm sunrise triggered at 2020-06-19 06:00:05
+ [lightson_workdays] Stopping sequence 
+ [sunrise] Executing cancel lightson_workdays
+ [sunrise] Executing curtain -o 2 -t
+ [sunrise] Executing lightcontrol -r 0 -g 0 -w 0 -f 100 -a 1000
+ Alarm open_curtains triggered at 2020-06-19 09:00:00
+ [sunrise] Stopping sequence 
+ [open_curtains] Executing curtain -o 1 -t
+ Alarm open_curtains triggered at 2020-06-20 09:00:00
+ [open_curtains] Stopping sequence 
+ [open_curtains] Executing curtain -o 1 -t
+ Alarm open_curtains triggered at 2020-06-21 09:00:00
+ [open_curtains] Stopping sequence 
+ [open_curtains] Executing curtain -o 1 -t
+ Alarm lightson_workdays triggered at 2020-06-22 06:00:00
+ [open_curtains] Stopping sequence 
+ [lightson_workdays] Executing lightcontrol -r 10 -f 300 -a 1000
+ Alarm sunrise triggered at 2020-06-22 06:00:05
+ [lightson_workdays] Stopping sequence 
+ [sunrise] Executing cancel lightson_workdays
+ [sunrise] Executing curtain -o 2 -t
+ [sunrise] Executing lightcontrol -r 0 -g 0 -w 0 -f 100 -a 1000
+ Alarm open_curtains triggered at 2020-06-22 09:00:00
+ [sunrise] Stopping sequence 
+ [open_curtains] Executing curtain -o 1 -t
+ Alarm lightson_workdays triggered at 2020-06-23 06:00:00
+ [open_curtains] Stopping sequence 
+ [lightson_workdays] Executing lightcontrol -r 10 -f 300 -a 1000
+ Alarm sunrise triggered at 2020-06-23 06:00:05
+ [lightson_workdays] Stopping sequence 
+ [sunrise] Executing cancel lightson_workdays
+ [sunrise] Executing curtain -o 2 -t
+ [sunrise] Executing lightcontrol -r 0 -g 0 -w 0 -f 100 -a 1000
+ Alarm open_curtains triggered at 2020-06-23 09:00:00
+ [sunrise] Stopping sequence 
+ [open_curtains] Executing curtain -o 1 -t
+ Alarm lightson_workdays triggered at 2020-06-24 06:00:00
+ [open_curtains] Stopping sequence 
+ [lightson_workdays] Executing lightcontrol -r 10 -f 300 -a 1000
+ Alarm sunrise triggered at 2020-06-24 06:00:05
+ [lightson_workdays] Stopping sequence 
+ [sunrise] Executing cancel lightson_workdays
+ [sunrise] Executing curtain -o 2 -t
+ [sunrise] Executing lightcontrol -r 0 -g 0 -w 0 -f 100 -a 1000
+ Alarm open_curtains triggered at 2020-06-24 09:00:00
+ [sunrise] Stopping sequence 
+ [open_curtains] Executing curtain -o 1 -t
+ Alarm end_test triggered at 2020-06-24 11:00:00
+ [open_curtains] Stopping sequence 
+ [end_test] Executing exit