summaryrefslogtreecommitdiffstats
path: root/modules-available/rebootcontrol/inc/scheduler.inc.php
blob: 613fbbeec3434fc46525423d2cab634917dd5db1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php

class Scheduler
{

	public static function updateSchedule($locationid, $options, $openingTimes)
	{
		if (empty($openingTimes)) {
			self::deleteSchedule($locationid);
			return false;
		}
		$nextexec = self::calculateNext($options, $openingTimes);
		if ($nextexec !== false) {
			$json_options = json_encode($options);
			Database::exec("INSERT INTO `reboot_scheduler` (locationid, action, nextexecution, options) 
				VALUES (:lid, :act, :next, :opt)
				ON DUPLICATE KEY UPDATE
					action = VALUES(action), nextexecution = VALUES(nextexecution), options = VALUES(options)", [
				'lid' => $locationid,
				'act' => $nextexec['action'],
				'next' => $nextexec['time'],
				'opt' => $json_options,
			]);
		} else {
			// All times are getting ignored because they are within 5 minutes of each other, delete possible db entries.
			self::deleteSchedule($locationid);
		}
		return true;
	}

	public static function deleteSchedule($locationid)
	{
		Database::exec("DELETE FROM `reboot_scheduler` 
			WHERE locationid = :lid", ['lid' => $locationid]);
	}

	private static function calculateTimestamp($now, $day, $time)
	{
		$ts = strtotime("$day $time");
		if ($ts < $now) {
			$ts = strtotime("next $day $time");
		}
		if ($ts < $now) {
			EventLog::warning("Invalid params to calculateTimestamp(): 'next $day $time'");
			$ts = $now + 864000;
		}
		return $ts;
	}

	private static function calculateNext($options, $openingTimes)
	{
		if (!$options['wol'] && !$options['sd'])
			return false;
		$openingTimes = json_decode($openingTimes, true);
		$now = time();
		$events = [];
		foreach ($openingTimes as $row) {
			foreach ($row['days'] as $day) {
				if ($options['wol']) {
					$events[] = ['action' => 'wol',
						'time' => self::calculateTimestamp($now, $day, $row['openingtime'])];
				}
				if ($options['sd']) {
					$events[] = ['action' => 'sd',
						'time' => self::calculateTimestamp($now, $day, $row['closingtime'])];
				}
			}
		}
		$tmp = ArrayUtil::flattenByKey($events, 'time');
		array_multisort($tmp, SORT_NUMERIC | SORT_ASC, $events);

		// Only apply offsets now, so we can detect nonsensical overlap
		$wolOffset = $options['wol-offset'] * 60;
		$sdOffset = $options['sd-offset'] * 60;
		$prev = PHP_INT_MAX;
		for ($i = count($events) - 1; $i >= 0; --$i) {
			$event =& $events[$i];
			if ($event['action'] === 'wol') {
				$event['time'] -= $wolOffset;
			} elseif ($event['action'] === 'sd') {
				$event['time'] += $sdOffset;
			} else {
				error_log('BUG Unhandled event type ' . $event['action']);
			}
			if ($event['time'] >= $prev || $event['time'] < $now) {
				// Overlap, delete this event
				unset($events[$i]);
			} else {
				$prev = $event['time'];
			}
		}
		unset($event);
		// Reset array keys
		$events = array_values($events);

		// See which is the next suitable event to act upon
		$lastEvent = count($events) - 1;
		for ($i = 0; $i <= $lastEvent; $i++) {
			$event =& $events[$i];
			$diff = ($i === $lastEvent ? PHP_INT_MAX : $events[$i + 1]['time'] - $event['time']);
			if ($diff < 300 && $event['action'] !== $events[$i + 1]['action']) {
				// If difference to next event is < 5 min, ignore.
				continue;
			}
			if ($diff < 900 && $event['action'] === 'sd' && $events[$i + 1]['action'] === 'wol') {
				// If difference to next WOL is < 15 min and this is a shutdown, reboot instead.
				$res['action'] = 'rb';
				$res['time'] = $event['time'];
			} else {
				// Use first event.
				$res = $event;
			}
			return $res;
		}
		unset($event);
		return false;
	}

}