Ajax Refactor/Oprimalization #2

This commit is contained in:
JonatanRek 2019-10-09 19:26:29 +02:00
parent 4c69cf2f02
commit b420ded943
3 changed files with 42 additions and 4 deletions

View File

@ -6,8 +6,7 @@ class ChartScale{
const YEAR = 'error'; const YEAR = 'error';
} }
class ChartManager{ class ChartManager{
function generateChart($data, $min = 0, $max = 100) function generateChart($data, $min = 0, $max = 100) {
{
echo '<br>Aktuální Hodnota: '.$data[0]['value']; echo '<br>Aktuální Hodnota: '.$data[0]['value'];
echo "<style> echo "<style>
@ -88,5 +87,43 @@ class ChartManager{
} }
</script>'; </script>';
} }
function generateChartData(int $subDeviceId, string $periode, string $groupBy) {
$chartData = [];
$subDevice = SubDeviceManager::getSubDevice($subDeviceId);
$records = RecordManager::getAllRecordForGraph($subDeviceId, $periode, $groupBy);
$array = array_column($records, 'value');
$arrayTime = array_column($records, 'time');
$output = [];
foreach ($array as $key => $value) {
$output[$key]['y'] = $value;
if ($subDevice['type'] == 'light'){
if ($value > 810){
$output[$key]['y'] = 1;
} else {
$output[$key]['y'] = 0;
}
}
$timeStamp = new DateTime($arrayTime[$key]);
$output[$key]['t'] = $timeStamp->format("Y-m-d") . 'T' . $timeStamp->format("H:i:s") . 'Z';
}
$data = json_encode($output);
$data = $output;
$arrayTimeStamps = array_column($records, 'time');
foreach ($arrayTimeStamps as $key => $value) {
$arrayTimeStamps[$key] = (new DateTime($value))->format(TIMEFORMAT);
}
$chartData['graphRange'] = RANGES[$subDevice['type']];
$chartData['graphType'] = RANGES[$subDevice['type']]['graph'];
$chartData['graphData'] = $data;
return $chartData;
}
} }
?> ?>

View File

@ -51,7 +51,7 @@ function waitForExecution(params, elements, msg_state){
url: 'ajax', url: 'ajax',
type: 'POST', type: 'POST',
data: { data: {
lastRecord:'', action:'executed',
subDevice_id : params['subDevice_id'] subDevice_id : params['subDevice_id']
}, },
success: function(msg){ success: function(msg){

View File

@ -29,6 +29,7 @@ if ('serviceWorker' in navigator) {
url: 'ajax', url: 'ajax',
type: 'POST', type: 'POST',
data: { data: {
"notification" : '',
"token": token "token": token
}, },
success: function(data){ success: function(data){
@ -179,7 +180,7 @@ function ajaxChart(id, period = 'day', group = 'hour'){
type: 'POST', type: 'POST',
dataType: 'json', dataType: 'json',
data: { data: {
"subDevice": id, "subDevice_id": id,
"action": 'chart', "action": 'chart',
"period": period, "period": period,
"group": group "group": group