summaryrefslogblamecommitdiffstats
path: root/modules-available/rebootcontrol/inc/scheduler.inc.php
blob: 27a22646c5d033b3d595f53d4d886899bb9343a9 (plain) (tree)
1
2
3
4
5
6
7
8
9
10









                                                                                              
                                                                                 
                                                      
                                                                             



                                                                     
                                                               





                                                                          



























                                                                                                                                                                     

         

 
                                                                                   
                                                                            
                                               




                                                                          
                                                                                                                    


                                                                         
                                                            


                                                         

                                                                         
                                                                                  
                                                            








                                                             
<?php

class Scheduler
{

	public static function updateSchedule($locationid, $action, $options, $openingTimes) {
		if ($openingTimes == '') {
			self::deleteSchedule($locationid, $action);
			return false;
		}
		$nextexec = self::calcNextexec($action, $options, $openingTimes);
		$json_options = json_encode($options);
		self::upsert($locationid, $action, $nextexec, $json_options);
		return true;
	}

	public static function deleteSchedule($locationid, $action) {
		Database::exec("DELETE FROM `reboot_scheduler` 
			WHERE locationid = :lid AND action = :act", array(
				'lid' => $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;
	}

}