forked from timleland/rfoutlet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.php
More file actions
130 lines (118 loc) · 3.73 KB
/
Copy pathsetup.php
File metadata and controls
130 lines (118 loc) · 3.73 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
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
120
121
122
123
124
125
126
127
128
129
130
<?php
// Check and Set Setup Done flag
if (isset($setupPhpDone)) {
// do nothing
} else {
$setupPhpDone = true;
// common functions
function errorExit($str)
{
error_log($str, 0);
print "<pre>";
print date("Y M j \(D\) G:i:s T\n");
print "$str\n";
print "</pre>";
die(json_encode(array('success' => false)));
}
// changing file in $modeDir to record current mode
function changeMode($modeDir, $newMode, $allModes, $matchMessage)
{
if (!file_exists($modeDir . $newMode)) {
// Changing mode in $modeDir
foreach($allModes as $mode => $value) {
$fname = $modeDir . $mode;
if ($mode <> $newMode) {
if (file_exists($fname)) {
// deleting unmaching mode file
unlink($fname); // delete file
}
} else {
// creating new modefile
$cmd = "echo > " . $fname;
exec($cmd);
error_log($matchMessage . ": exec($cmd);");
}
}
}
}
// Set Timezone
date_default_timezone_set('America/Los_Angeles');
// Read JSON database for outlet coding
$outletCodeFile='./outletCodes.json';
if (!file_exists($outletCodeFile)) {
errorExit("$outletCodeFile is missing, please set it up.");
} else {
$rules = file_get_contents($outletCodeFile);
// To prevent error
$rules = str_replace('"', '"', $rules);
$codes = json_decode($rules, true);
// $codes = json_decode($rules);
if (!$codes) {
errorExit("Error in $outletCodeFile");
}
}
// Read Timersetup database
$timerSetupFile='./timerSetup.json';
if (!file_exists($timerSetupFile)) {
errorExit("$timerSetupFile is missing, please set it up.");
die(json_encode(array('success' => false)));
} else {
$timers = file_get_contents($timerSetupFile);
// To prevent error
$timers = str_replace('"', '"', $timers);
$timerSetup = json_decode($timers, true);
// $codes = json_decode($timers);
if (!$timerSetup) {
errorExit("Error in $timerSetupFile");
}
}
// Path to the codesend binary (current directory is the default)
$codeSendPath = './codesend';
if (!file_exists($codeSendPath)) {
errorExit("$codeSendPath is missing, please edit the script");
}
// Path to onState file location
$onStateDir = './OnState/';
$shuffleModeDir = './OnState/ShuffleMode/';
// print "<pre>";
// print_r($codes);
// error_log($outletCodeFile);
// var_dump($codes);
// error_log(ob_get_clean()); // get vardump string
// error_log(print_r($codes));
// print "</pre>";
// This PIN is not the first PIN on the Raspberry Pi GPIO header!
// Consult https://projects.drogon.net/raspberry-pi/wiringpi/pins/
// for more information.
$codeSendPIN = (string) $codes['Global']['codeSendPin'];
// Pulse length depends on the RF outlets you are using. Use RFSniffer to see what pulse length your device uses.
$codeSendPulseLength = (string) $codes['Global']['pulseLen'];
// Setting Subarray
// codesExist for the receiver (Outlet) exist. Removed 'NA'.
// codesUsed for the outlet is used. Removed 'NA' and 'Unused'.
$outletsExists = array();
$outletsUsed = array();
foreach ($codes['Outlets'] as $light => $value) {
if ($value['loc'] <> 'NA') {
$outletsExists = $outletsExists + array($light => $value);
if ($value['loc'] <> 'Unused') {
$outletsUsed = $outletsUsed + array($light => $value);
}
}
}
// debug code
if (0) {
print "<pre>";
print '---- $codes["Outlets"] ----' . "\n";
var_dump($codes['Outlets']);
print '---- $outletsExists ----' . "\n";
var_dump($outletsExists);
print '---- $outletsUsed ----' . "\n";
var_dump($outletsUsed);
print "</pre>";
}
// Specify code book to use.
// $codeBook = $codes['Outlets'];
$codeBook = $outletsExists; // now works.
}
?>