-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdaily.php
More file actions
108 lines (96 loc) · 4.03 KB
/
daily.php
File metadata and controls
108 lines (96 loc) · 4.03 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
<?php
include 'config.php';
include 'functions.php';
include 'header.php';
require_once 'auth.php';
/** @var array $config */
$moduleId = $_GET['udid'] ?? '';
$userId = $_GET['user_id'] ?? '';
$zoneLabel = $_GET['zone_label'] ?? '';
if (!$moduleId || !$userId) {
http_response_code(400);
echo '<div class="container mt-5"><div class="alert alert-danger">Brak wymaganych parametrów.</div></div>';
include 'footer.php';
exit;
}
$auth = login($config['username'], $config['password']);
$dataByZone = [];
if (!empty($auth['token']) && $moduleId) {
$url = "https://emodul.eu/api/v1/modules/{$moduleId}/statistics/linear/range/day";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, ["Authorization: Bearer {$auth['token']}"]);
$response = curl_exec($ch);
curl_close($ch);
$responseData = json_decode($response, true);
if (
is_array($responseData)
&& isset($responseData['status'], $responseData['data']['history'])
&& $responseData['status'] === 'success'
) {
foreach ($responseData['data']['history'] as $zoneRaw => $points) {
if (!empty($zoneLabel) && stripos($zoneRaw, $zoneLabel) === false) continue;
$zoneName = preg_replace('/^\|.*?\|/', '', $zoneRaw);
foreach ($points as $p) {
if (!isset($p['x'], $p['y'])) continue;
$date = DateTime::createFromFormat('YmdHi', $p['x']);
$label = $date ? $date->format('H:i') : $p['x'];
$dataByZone[$zoneName]['labels'][] = $label;
$dataByZone[$zoneName]['values'][] = $p['y'];
}
}
}
}
?>
<div class="container mt-5">
<h1 class="mb-4"><i class="fas fa-chart-line"></i> Statystyki dzienne (<?= date('Y-m-d') ?>)</h1>
<?php if (!empty($zoneLabel)): ?>
<div class="alert alert-info">
Pokazano tylko strefę zawierającą: <strong><?= htmlspecialchars($zoneLabel) ?></strong>
</div>
<?php endif; ?>
<?php if (empty($dataByZone)): ?>
<div class="alert alert-warning">Brak danych do wyświetlenia.</div>
<?php else: ?>
<?php foreach ($dataByZone as $zone => $dataset): ?>
<h3 class="mt-5"><?= htmlspecialchars($zone) ?></h3>
<canvas id="chart_<?= md5($zone) ?>" height="100"></canvas>
<script>
new Chart(document.getElementById("chart_<?= md5($zone) ?>").getContext('2d'), {
type: 'line',
data: {
labels: <?= json_encode($dataset['labels']) ?>,
datasets: [{
label: 'Temperatura (°C)',
data: <?= json_encode($dataset['values']) ?>,
borderColor: 'rgb(75, 192, 192)',
backgroundColor: 'rgba(75, 192, 192, 0.2)',
fill: true,
tension: 0.3
}]
},
options: {
responsive: true,
plugins: {
title: {
display: true,
text: 'Strefa: <?= addslashes($zone) ?>'
}
},
scales: {
x: {
ticks: {
maxRotation: 45,
autoSkip: true,
maxTicksLimit: 20
}
}
}
}
});
</script>
<?php endforeach; ?>
<?php endif; ?>
<a href="zones.php?user_id=<?= urlencode($userId) ?>&udid=<?= urlencode($moduleId) ?>" class="btn btn-secondary mt-4">« Powrót</a>
</div>
<?php include 'footer.php'; ?>