forked from timleland/rfoutlet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimerDaemon.php
More file actions
executable file
·93 lines (90 loc) · 2.63 KB
/
Copy pathtimerDaemon.php
File metadata and controls
executable file
·93 lines (90 loc) · 2.63 KB
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
<?php
include 'setup.php'; // setup variables reading ./outletCodes.json
$timerDaemon=true;
if ($argv[1]) {
print '$argv: ';
var_dump($argv);
$now = $argv[1];
print '$now: ';
var_dump($now);
print '$timerSetup: ';
print ' Num of Outer Entries=';
print count($timerSetup);
var_dump($timerSetup);
} else {
$now = date("G:i");
}
$utsNow = strtotime($now); // now in Unix Time Stamp, offset in seconds from 1970/1/1 00:00:00
$matchRange = 300; // matchs +- this range in second
$matchRange = $matchRange * rand(50, 100) / 100; // randomize to 40% to 100%
if ($argv[1]) {
print '$now=' . $now;
print '$utsNow:';
var_dump($utsNow);
print '$matchRange:';
var_dump($matchRange);
}
// shuffle operation
if (file_exists($onStateDir . 'Shuffle')) {
foreach ($timerSetup['Shuffle'] as $time => $rule) {
$utsTime = strtotime($time);
if ( (($utsNow - $matchRange) <= $utsTime) &&
($utsTime <= ($utsNow + $matchRange)) ) {
print "Changing timerMode for Shuffle\n";
do {
$newMode = array_rand($timerSetup, 1); // get one random key
if ($argv[1]) {
printf("\$newMode=%d \$shuffleModeDir=%s\n",
$newMode, $shuffleModeDir);
}
} while ($newMode == 'TimerOff' || $newMode == 'Shuffle'); // avoiding psedo mode.
changeMode($shuffleModeDir, $newMode, $timerSetup, "timerDaemon.php:Shuffling :");
}
}
}
// controling swtich
foreach ($timerSetup as $mode => $values) {
if (file_exists($onStateDir . 'Shuffle')) {
if(!file_exists($shuffleModeDir . $mode)) {
// specially treated for shuffle mode
continue;
}
} elseif (!file_exists($onStateDir . $mode)) {
// not this timer mode
continue;
}
print $now . ' Timer: ' . $mode . "\n"; // thron to dev null in cron
if ($mode == 'TimerOff') {
break;
}
foreach ($values as $time => $rules) { // now rules is a list of rule
$utsTime = strtotime($time);
if ($argv[1]) {
print '$utsTime:';
var_dump($utsTime);
print " \$rulse: ";
var_dump($rules);
}
if ( (($utsNow - $matchRange) <= $utsTime) &&
($utsTime <= ($utsNow + $matchRange)) ) {
// in the range, now trigger the event
if ($argv[1]) {
print " --- > Matched\n";
}
foreach ($rules as $light => $switch) {
print "Triggering : " . $light . ': ' . $switch . ', ';
$_POST['outletId'] = $light;
$_POST['outletStatus'] = $switch;
include 'toggle.php';
}
if ($argv[1]) {
print " --- Trigger Done ---\n";
}
} else {
if ($argv[1]) {
print " --- > UnMatched\n";
}
}
}
}
?>