From: Arjen Baart Date: Sun, 2 Aug 2020 07:08:01 +0000 (+0200) Subject: Started on execution of actions X-Git-Url: http://www.andromeda.nl/gitweb/?a=commitdiff_plain;h=153d84115f60cb7f89ba9642b58e7c9d7c142b7e;p=wakeup.git Started on execution of actions --- diff --git a/doc/wakeup-classes.svg b/doc/wakeup-classes.svg index 379d44f..d75d0c9 100644 --- a/doc/wakeup-classes.svg +++ b/doc/wakeup-classes.svg @@ -26,7 +26,7 @@ inkscape:pageopacity="0.0" inkscape:pageshadow="2" inkscape:zoom="1.2271424" - inkscape:cx="233.1688" + inkscape:cx="66.928924" inkscape:cy="743.6865" inkscape:document-units="mm" inkscape:current-layer="layer1" @@ -338,5 +338,16 @@ x="46.571613" y="108.98867" style="stroke-width:0.26458332px">parameters + run_sequence() diff --git a/src/Makefile.am b/src/Makefile.am index 268461c..337d117 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,9 +1,10 @@ -bin_PROGRAMS = wakeup pwm curtain lightcontrol read_serial +bin_PROGRAMS = wakeup pwm curtain lightswitch lightcontrol read_serial sbin_SCRIPTS = startpwm -wakeup_SOURCES = wakeup.cpp event.cpp +wakeup_SOURCES = wakeup.cpp event.cpp logging.cpp pwm_SOURCES = pwm.c curtain_SOURCES = curtain.c +lightswitch_SOURCES = lightswitch.c lightcontrol_SOURCES = lightcontrol.cpp logging.cpp sunrise_SOURCES = sunrise.cpp diff --git a/src/action.h b/src/action.h index 797ab80..d05c423 100644 --- a/src/action.h +++ b/src/action.h @@ -18,7 +18,8 @@ public: { return String("no command"); } -//virtual void execute() = 0; + + virtual int execute() = 0; }; @@ -30,6 +31,13 @@ class Lightstep : public Action cmd += parameters; return cmd; } + + virtual int execute() + { + String cmd("lightcontrol "); + cmd += parameters; + return system(cmd); + } }; class Sleepstep : public Action @@ -40,5 +48,12 @@ class Sleepstep : public Action cmd += parameters; return cmd; } + + virtual int execute() + { + String cmd("sleep "); + cmd += parameters; + return system(cmd); + } }; diff --git a/src/event.cpp b/src/event.cpp index dde8372..1009070 100644 --- a/src/event.cpp +++ b/src/event.cpp @@ -1,4 +1,7 @@ #include "event.h" +#include "logging.h" + +extern logstream Log; void Event::FromXML(xml_element x) { @@ -138,6 +141,17 @@ void Event::add_action(String command) } } +void Event::run_sequence() +{ + std::list::iterator cmd; + + for (cmd = sequence.begin(); cmd != sequence.end(); cmd++) + { + Log << "Executing " + (*cmd)->command_line(); + (*cmd)->execute(); + } +} + UTC Event::next_occurance(UTC after) { UTC next; diff --git a/src/event.h b/src/event.h index 72f4f3a..2351aa5 100644 --- a/src/event.h +++ b/src/event.h @@ -35,6 +35,7 @@ public: String ToXML(void); void add_action(String command); + void run_sequence(); UTC next_occurance(UTC after); }; diff --git a/src/lightswitch.c b/src/lightswitch.c new file mode 100644 index 0000000..7b81626 --- /dev/null +++ b/src/lightswitch.c @@ -0,0 +1,113 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define PAGE_SIZE (4*1024) +#define BLOCK_SIZE (4*1024) + +/* Direct access to GPIO hardware */ + +// Access from ARM Running Linux +// For Raspberry Pi 2 and Pi 3, change BCM2708_PERI_BASE to 0x3F000000 for the code to work. + +//#define BCM2708_PERI_BASE 0x20000000 +#define BCM2708_PERI_BASE 0x3F000000 +#define GPIO_BASE (BCM2708_PERI_BASE + 0x200000) /* GPIO controller */ + + +// I/O access +volatile unsigned *gpio; + + +// GPIO setup macros. Always use INP_GPIO(x) before using OUT_GPIO(x) or SET_GPIO_ALT(x,y) +#define INP_GPIO(g) *(gpio+((g)/10)) &= ~(7<<(((g)%10)*3)) +#define OUT_GPIO(g) *(gpio+((g)/10)) |= (1<<(((g)%10)*3)) +#define SET_GPIO_ALT(g,a) *(gpio+(((g)/10))) |= (((a)<=3?(a)+4:(a)==4?3:2)<<(((g)%10)*3)) + +#define GPIO_SET *(gpio+7) // sets bits which are 1 ignores bits which are 0 +#define GPIO_CLR *(gpio+10) // clears bits which are 1 ignores bits which are 0 + +#define GET_GPIO(g) (*(gpio+13)&(1< #include -class logstream : public std::ofstream +class logstream { std::ofstream destination; public: + logstream() + { + } + logstream(const char * filename) { destination.open(filename, std::ios_base::app); } + void open(const char * filename) + { + destination.open(filename, std::ios_base::app); + } + logstream & operator << (const char *msg); logstream & operator << (const String &msg); }; diff --git a/src/wakeup.conf b/src/wakeup.conf new file mode 100644 index 0000000..fe64aa2 --- /dev/null +++ b/src/wakeup.conf @@ -0,0 +1,12 @@ + + + + file + . + 0 + + + wakeup_alarms.xml + + + diff --git a/src/wakeup.cpp b/src/wakeup.cpp index 7dd048f..5ccbe74 100644 --- a/src/wakeup.cpp +++ b/src/wakeup.cpp @@ -1,10 +1,103 @@ +#include + +#include +#include + +#include "logging.h" #include "event.h" +// Global objects + +configuration Config; +logstream Log; +Tachyon Timebase; + +std::list calculate_occurances(std::list alarms) +{ + std::list future_alarms; + std::list::iterator alarm; + + for (alarm = alarms.begin(); alarm != alarms.end(); alarm++) + { + UTC next_time; + + next_time = alarm->next_occurance(Now()); + //DEBUG + std::cout << "Next alarm after will be " << next_time << ".\n"; + if ( next_time != UTC(date(0,0,0), hour(0))) + { + future_alarms.push_back(*alarm); + } + } + + return future_alarms; +} + +// Reload the configuration and list of events. + +void reload() +{ + Config.read("wakeup"); +} + +// Signal handler for SIGHUP: reload the configuration, +// event list and recalculate the next event. + +void sighup_handler(int sig) +{ + std::cout << "Caught signal " << sig << "\n"; +} + int main() { + String logdir; + String alarms_file; + + reload(); + + signal(SIGHUP, sighup_handler); + + logdir = Config.find_parameter("logging", "destination"); + if (logdir == "") + { + logdir = "."; // default to current dir + } + Log.open(logdir + "/wakeup.log"); + + std::ofstream runfile("wakeup.run"); + runfile << Timebase.name() << "\n"; + runfile.close(); + + Log << "wakeup started"; + std::list set_alarms; + std::list pending_alarms; + + alarms_file = Config.find_parameter("alarms", "filename"); + set_alarms = read_alarms(alarms_file); + pending_alarms = calculate_occurances(set_alarms); + // DEBUG + std::cout << alarms_to_XML(pending_alarms); + + // Find the next alarm + + Event next_alarm; + std::list::iterator alarm; - set_alarms = read_alarms("wakeup_alarms.xml"); + alarm = pending_alarms.begin(); + next_alarm = *alarm; + while (alarm != pending_alarms.end()) + { + if (alarm->next_occurance(Now()) < next_alarm.next_occurance(Now())) + { + next_alarm = *alarm; + } + alarm++; + } + // DEBUG + std::cout << "Sleep until " << next_alarm.next_occurance(Now()) << "\n"; + Timebase.nanosleep(3000); - std::cout << alarms_to_XML(set_alarms); + std::cout << "wakeup exitting.\n"; + remove("wakeup.run"); } diff --git a/src/wakeup_alarms.xml b/src/wakeup_alarms.xml new file mode 100644 index 0000000..6038304 --- /dev/null +++ b/src/wakeup_alarms.xml @@ -0,0 +1,13 @@ + + + + 2020-04-02 06:00 + + weekdays + 1,2,3,4,5 + + lightcontrol -r 100 -f 200 + lightcontrol -g 100 -f 200 + lightcontrol -w 100 -f 200 + + diff --git a/test/Makefile.am b/test/Makefile.am index 6e8b0fe..30b6217 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -2,8 +2,9 @@ TESTS = lightctrl lightctrl-oor lightctrl-fade $(check_PROGRAMS) check_output AM_CPPFLAGS = -I../src LDADD = -lTachyon -lACL -check_PROGRAMS = wakeup_load_events sunrise_test event_recurrance +check_PROGRAMS = wakeup_load_events sunrise_test event_recurrance event_run_sequence -wakeup_load_events_SOURCES = wakeup_load_events.cpp ../src/event.cpp -event_recurrance_SOURCES = event_recurrance.cpp ../src/event.cpp +wakeup_load_events_SOURCES = wakeup_load_events.cpp ../src/event.cpp ../src/logging.cpp +event_recurrance_SOURCES = event_recurrance.cpp ../src/event.cpp ../src/logging.cpp +event_run_sequence_SOURCES = event_run_sequence.cpp ../src/event.cpp ../src/logging.cpp sunrise_test_SOURCES = sunrise_test.cpp ../src/sunrise.cpp diff --git a/test/event_recurrance.cpp b/test/event_recurrance.cpp index 3f12c0e..9598113 100644 --- a/test/event_recurrance.cpp +++ b/test/event_recurrance.cpp @@ -1,5 +1,8 @@ #include "event.h" #include "assert.h" +#include "logging.h" + +logstream Log; int main() { @@ -9,6 +12,8 @@ int main() UTC time_expected; UTC next_time; + Log.open("event_recurrance.log2"); + set_alarms = read_alarms("wakeup-02.xml"); // The first alarm at 2020-03-22 06:00 does not recurr diff --git a/test/event_run_sequence.cpp b/test/event_run_sequence.cpp new file mode 100644 index 0000000..ec52214 --- /dev/null +++ b/test/event_run_sequence.cpp @@ -0,0 +1,34 @@ +#include "event.h" +#include "assert.h" +#include "logging.h" + +logstream Log; + +int main() +{ + String PATH; + + std::list set_alarms; + std::list::iterator alarm; + UTC time_to_check; + UTC time_expected; + UTC next_time; + + Log.open("event_run_sequence.log2"); + + // Make sure we execute in our own directory + + PATH = getenv("PATH"); + PATH = "PATH=../src:" + PATH; + putenv(PATH); + + set_alarms = read_alarms("wakeup-03.xml"); + + // The first alarm at 2020-03-22 06:00 has a sequence of 3 actions + + alarm = set_alarms.begin(); + + // Time of the alarm is irrelevant, just run the sequence + alarm->run_sequence(); + +} diff --git a/test/lightctrl-fade b/test/lightctrl-fade index 58a4f29..e423a69 100755 --- a/test/lightctrl-fade +++ b/test/lightctrl-fade @@ -10,7 +10,7 @@ lightcontrol -r 30 -g 40 -b 20 -w 40 -f 120 & sleep 1 read TACHYON_NAME + + + 2020-03-22 06:00 + lightcontrol -r 100 -f 20 + sleep 30 + lightcontrol -r 0 + + + diff --git a/test/wakeup.conf b/test/wakeup.conf new file mode 100644 index 0000000..fe64aa2 --- /dev/null +++ b/test/wakeup.conf @@ -0,0 +1,12 @@ + + + + file + . + 0 + + + wakeup_alarms.xml + + + diff --git a/test/wakeup_alarms.xml b/test/wakeup_alarms.xml new file mode 100644 index 0000000..6038304 --- /dev/null +++ b/test/wakeup_alarms.xml @@ -0,0 +1,13 @@ + + + + 2020-04-02 06:00 + + weekdays + 1,2,3,4,5 + + lightcontrol -r 100 -f 200 + lightcontrol -g 100 -f 200 + lightcontrol -w 100 -f 200 + + diff --git a/test/wakeup_load_events.cpp b/test/wakeup_load_events.cpp index 6e7a7d0..ed8e2ff 100644 --- a/test/wakeup_load_events.cpp +++ b/test/wakeup_load_events.cpp @@ -1,7 +1,12 @@ #include "event.h" +#include "logging.h" + +logstream Log; int main() { + Log.open("wakeup_load_events.log2"); + std::list set_alarms; set_alarms = read_alarms("wakeup-01.xml");