2023-04-21 00:43:40 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
$sensor_data = explode("\n", shell_exec('sensors'));
|
|
|
|
|
|
|
|
function get_lines($data) {
|
|
|
|
$lines = array();
|
|
|
|
|
|
|
|
$new_line = '';
|
|
|
|
$first = TRUE;
|
|
|
|
foreach ($data as $line) {
|
|
|
|
if($line != '' && $line[0] == " ")
|
|
|
|
$new_line .= $line;
|
|
|
|
else {
|
|
|
|
if(!$first) {
|
|
|
|
$lines[] = explode('(', str_replace(' ', '', $new_line))[0];
|
|
|
|
} else {
|
|
|
|
$first = FALSE;
|
|
|
|
}
|
|
|
|
$new_line = $line;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $lines;
|
|
|
|
}
|
|
|
|
|
|
|
|
function get_blocks($lines) {
|
|
|
|
$blocks = array();
|
|
|
|
|
|
|
|
$new_block = array();
|
|
|
|
foreach ($lines as $line) {
|
2023-04-22 15:19:44 +00:00
|
|
|
if($line == '') {
|
2023-04-21 00:43:40 +00:00
|
|
|
$blocks[] = $new_block;
|
2023-04-22 15:19:44 +00:00
|
|
|
$new_block = array();
|
|
|
|
}
|
2023-04-21 00:43:40 +00:00
|
|
|
else {
|
|
|
|
if(str_contains($line, ':'))
|
|
|
|
$new_block[] = explode(':', $line);
|
|
|
|
else
|
|
|
|
$new_block[] = $line;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return $blocks;
|
|
|
|
}
|
|
|
|
|
|
|
|
function generate_json($blocks) {
|
|
|
|
$json_array = array();
|
|
|
|
foreach ($blocks as $block) {
|
|
|
|
$block_json_array = array();
|
|
|
|
$block_title = '';
|
|
|
|
foreach($block as $item) {
|
|
|
|
if(gettype($item) == 'array') {
|
2023-04-22 15:19:44 +00:00
|
|
|
$block_json_array[$item[0]] = $item[1];
|
2023-04-21 00:43:40 +00:00
|
|
|
}
|
2023-04-22 15:19:44 +00:00
|
|
|
else {
|
2023-04-21 00:43:40 +00:00
|
|
|
$block_title = $item;
|
2023-04-22 15:19:44 +00:00
|
|
|
}
|
2023-04-21 00:43:40 +00:00
|
|
|
}
|
2023-04-22 15:19:44 +00:00
|
|
|
$json_array[$block_title] = $block_json_array;
|
|
|
|
reset($block_json_array);
|
2023-04-21 00:43:40 +00:00
|
|
|
}
|
|
|
|
return json_encode($json_array);
|
|
|
|
}
|
|
|
|
|
|
|
|
header('Content-Type: application/json');
|
|
|
|
|
|
|
|
echo generate_json(get_blocks(get_lines($sensor_data)));
|