$locationid, 'act' => $action )); } private static function calcNextexec($action, $options, $openingTimes) { $openingTimes = json_decode($openingTimes, true); $now = time(); $times = []; foreach ($openingTimes as $row) { // Fetch hour and minutes of opening / closing time. $hourmin = explode(':', ($action == 'wol' ? $row['openingtime'] : $row['closingtime'])); // Calculate time based on offset. $min = ($action == 'wol' ? $hourmin[0] * 60 + $hourmin[1] - $options['wol-offset'] : $hourmin[0] * 60 + $hourmin[1] + $options['sd-offset']); // Calculate opening / closing time of each day. foreach ($row['days'] as $day) { $next = strtotime(date('Y-m-d H:i', strtotime($day . ' ' . $min . ' minutes'))); if ($next < $now) { $times[] = strtotime(date('Y-m-d H:i', strtotime('next '.$day . ' ' . $min . ' minutes'))); } else { $times[] = $next; } } } // Iterate over days, use timestamp with smallest difference to now. $res = 0; $smallestDiff = 0; foreach ($times as $time) { $diff = $time - $now; if ($res == 0 || $diff < $smallestDiff) { $smallestDiff = $diff; $res = $time; } } return $res; } private static function upsert($locationid, $action, $nextexec, $options) { $schedule = Database::queryFirst("SELECT locationid, action FROM `reboot_scheduler` WHERE locationid = :lid AND action = :act", array( 'lid' => $locationid, 'act' => $action )); if ($schedule === false) { Database::exec("INSERT INTO `reboot_scheduler` (locationid, action, nextexecution, options) VALUES (:lid, :act, :next, :opt)", array( 'lid' => $locationid, 'act' => $action, 'next' => $nextexec, 'opt' => $options )); } else { Database::exec("UPDATE `reboot_scheduler` SET nextexecution = :next, options = :opt WHERE locationid = :lid AND action = :act", array( 'next' => $nextexec, 'opt' => $options, 'lid' => $locationid, 'act' => $action )); } return true; } }