-
Notifications
You must be signed in to change notification settings - Fork 2
/
rc.local.php
52 lines (38 loc) · 1.33 KB
/
rc.local.php
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
<?php
if (php_sapi_name() !== 'cli') {
die('CLI only');
}
$config = json_decode(file_get_contents('/opt/allsky.py/camera/common/config.py.json'), true);
$dbh = new PDO(
'mysql:dbname='. $config['db']['database'] .';host='. $config['db']['host'],
$config['db']['user'],
$config['db']['passwd']);
$sth = $dbh->prepare('select * from config');
$sth->execute();
while ($row = $sth->fetch(PDO::FETCH_ASSOC)) {
$config[ $row['id'] ] = json_decode($row['val'], true);
}
echo "Opening relays GPIO ports for output\n";
foreach ($config['relays'] as $relay) {
if (!file_exists('/sys/class/gpio/gpio'. intval($relay['gpio']))) {
file_put_contents('/sys/class/gpio/export', (int) $relay['gpio']);
}
}
sleep(1);
foreach ($config['relays'] as $relay) {
file_put_contents('/sys/class/gpio/gpio'. intval($relay['gpio']) .'/direction', 'out');
}
sleep(1);
$sth = $dbh->prepare('select id, state from relay');
$sth->execute();
$states = [];
while ($row = $sth->fetch(PDO::FETCH_ASSOC)) {
$states[ $row['id'] ] = $row['state'];
}
echo "Setting initial relays GPIO state\n";
foreach ($config['relays'] as $relay) {
echo '+ set relay '. $relay['gpio'] .' to '. ($states[ (int) $relay['gpio'] ] ?? 0) ."\n";
$file = '/sys/class/gpio/gpio'. intval($relay['gpio']) .'/value';
file_put_contents($file, $states[ (int) $relay['gpio'] ] ?? 0);
chown($file, 82);
}