summaryrefslogtreecommitdiffstats
path: root/modules-available/rebootcontrol/inc/scheduler.inc.php
diff options
context:
space:
mode:
Diffstat (limited to 'modules-available/rebootcontrol/inc/scheduler.inc.php')
-rw-r--r--modules-available/rebootcontrol/inc/scheduler.inc.php58
1 files changed, 58 insertions, 0 deletions
diff --git a/modules-available/rebootcontrol/inc/scheduler.inc.php b/modules-available/rebootcontrol/inc/scheduler.inc.php
new file mode 100644
index 00000000..839d7d81
--- /dev/null
+++ b/modules-available/rebootcontrol/inc/scheduler.inc.php
@@ -0,0 +1,58 @@
+<?php
+
+class Scheduler
+{
+
+ public static function updateSchedule($locationid, $action, $options, $openingTimes) {
+ if ($openingTimes == '') {
+ self::deleteSchedule($locationid, $action);
+ return false;
+ }
+ $nextwake = self::calcNextwake($action, $openingTimes);
+ $json_options = json_encode($options);
+ self::upsert($locationid, $action, $nextwake, $json_options);
+ return true;
+ }
+
+ public static function deleteSchedule($locationid, $action) {
+ Database::exec("DELETE FROM `scheduler`
+ WHERE locationid = :lid AND action = :act", array(
+ 'lid' => $locationid,
+ 'act' => $action
+ ));
+ }
+
+ private static function calcNextwake($action, $openingTimes) {
+ //TODO: Calculate nextWake based on openingTimes. Needs action to know if openTimes or closingTimes are used.
+ return 0;
+ }
+
+ private static function upsert($locationid, $action, $nextwake, $options) {
+ $schedule = Database::queryFirst("SELECT locationid, action
+ FROM `scheduler`
+ WHERE locationid = :lid AND action = :act", array(
+ 'lid' => $locationid,
+ 'act' => $action
+ ));
+ if ($schedule === false) {
+ Database::exec("INSERT INTO `scheduler` (locationid, action, nextwake, options)
+ VALUES (:lid, :act, :next, :opt)", array(
+ 'lid' => $locationid,
+ 'act' => $action,
+ 'next' => $nextwake,
+ 'opt' => $options
+ ));
+ } else {
+ Database::exec("UPDATE `scheduler`
+ SET nextwake = :next, options = :opt
+ WHERE locationid = :lid AND action = :act", array(
+ 'next' => $nextwake,
+ 'opt' => $options,
+ 'lid' => $locationid,
+ 'act' => $action
+ ));
+ }
+ return true;
+ }
+
+} \ No newline at end of file