-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapi.php
More file actions
113 lines (96 loc) · 4.49 KB
/
Copy pathapi.php
File metadata and controls
113 lines (96 loc) · 4.49 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
<?
function getEndpointsDynamic_RDS() {
$endpoints = array(
array('method' => 'GET', 'endpoint' => 'FastUpdate', 'callback' => 'DynRDSFastUpdate'),
array('method' => 'GET', 'endpoint' => 'Status', 'callback' => 'DynRDSStatus'),
array('method' => 'POST', 'endpoint' => 'PiBootChange/:SettingName', 'callback' => 'DynRDSPiBootChange'),
array('method' => 'POST', 'endpoint' => 'ScriptStream', 'callback' => 'DynRDSScriptStream')
);
return $endpoints;
}
function DynRDSFastUpdate() {
shell_exec("sudo /home/fpp/media/plugins/Dynamic_RDS/callbacks.py --update");
}
function DynRDSStatus() {
$statusFile = __DIR__ . '/Dynamic_RDS_Status.json';
if (!is_file($statusFile)) {
return json_encode(['error' => 'No status file - Dynamic RDS Engine may not have started']);
}
$status = json_decode(file_get_contents($statusFile), true);
if (!is_array($status)) {
return json_encode(['error' => 'Unable to parse status file']);
}
// Writes only happen when the broadcast content changes, so file age is not
// a heartbeat - minutes of silence during a long track is normal. Liveness
// comes from checking the recorded pid is still a running Engine.
$status['age'] = time() - filemtime($statusFile);
$status['running'] = DynRDSEngineAlive($status['pid'] ?? 0);
return json_encode($status);
}
function DynRDSEngineAlive($pid) {
$pid = (int)$pid;
if ($pid <= 0) {
return false;
}
$cmdline = @file_get_contents("/proc/{$pid}/cmdline");
if ($cmdline === false) {
return false;
}
// Guard against a recycled pid now belonging to an unrelated process
return str_contains($cmdline, 'Dynamic_RDS_Engine.py');
}
function DynRDSPiBootChange() {
$settingName = params('SettingName');
$myPluginSettings = json_decode(file_get_contents('php://input'), true);
switch ($settingName) {
case 'DynRDSAdvPISoftwareI2C':
if (strcmp($myPluginSettings[$settingName],'1') == 0) {
exec("sudo sed -i -e 's/^dtparam=i2c_arm=on/#dtparam=i2c_arm=on/' /boot/firmware/config.txt");
exec("sudo sed -i -e '/^#dtparam=i2c_arm=on/a dtoverlay=i2c-gpio,i2c_gpio_sda=2,i2c_gpio_scl=3,i2c_gpio_delay_us=4,bus=1' /boot/firmware/config.txt");
} else {
exec("sudo sed -i -e '/^dtoverlay=i2c-gpio,i2c_gpio_sda=2,i2c_gpio_scl=3,i2c_gpio_delay_us=4,bus=1/d' /boot/firmware/config.txt");
exec("sudo sed -i -e 's/^#dtparam=i2c_arm=on/dtparam=i2c_arm=on/' /boot/firmware/config.txt");
}
break;
case 'DynRDSQN8066PIPWM':
if (strcmp($myPluginSettings[$settingName],'1') == 0) {
exec("sudo sed -i -e 's/^dtparam=audio=on/#dtparam=audio=on/' /boot/firmware/config.txt");
if (is_numeric(strpos($myPluginSettings['DynRDSAdvPIPWMPin'], ','))) {
exec("sudo sed -i -e '/^#dtparam=audio=on/a dtoverlay=pwm,pin=" . escapeshellarg(str_replace(",", ",func=", $myPluginSettings['DynRDSAdvPIPWMPin'])) . "' /boot/firmware/config.txt");
}
} else {
exec("sudo sed -i -e '/^dtoverlay=pwm/d' /boot/firmware/config.txt");
exec("sudo sed -i -e 's/^#dtparam=audio=on/dtparam=audio=on/' /boot/firmware/config.txt");
}
break;
case 'DynRDSAdvPIPWMPin':
if (is_numeric(strpos($myPluginSettings['DynRDSAdvPIPWMPin'], ','))) {
exec("sudo sed -i -e 's/^#dtoverlay=pwm/dtoverlay=pwm/' /boot/firmware/config.txt");
exec("sudo sed -i -e '/^dtoverlay=pwm/c dtoverlay=pwm,pin=" . escapeshellarg(str_replace(",", ",func=", $myPluginSettings['DynRDSAdvPIPWMPin'])) . "' /boot/firmware/config.txt");
} else {
exec("sudo sed -i -e 's/^dtoverlay=pwm/#dtoverlay=pwm/' /boot/firmware/config.txt");
}
break;
case 'DynRDSQN8066AmpPower':
DynRDSFastUpdate();
break;
default:
DynRDSFastUpdate();
}
}
function DynRDSScriptStream() {
$postData = json_decode(file_get_contents('php://input'), true);
DisableOutputBuffering();
switch ($postData['script']) {
case 'dependencies':
system('~/media/plugins/Dynamic_RDS/scripts/fpp_install.sh', $return_val);
break;
case 'python3-paho-mqtt':
system('~/media/plugins/Dynamic_RDS/scripts/paho_install.sh', $return_val);
break;
default:
return "\nUnknown script\n";
}
return "\nDone\n";
}
?>