-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.php
More file actions
68 lines (56 loc) · 2.85 KB
/
Copy pathServer.php
File metadata and controls
68 lines (56 loc) · 2.85 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
<?php
// Устанавливаем заголовки ответа
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: POST");
// Проверяем, что метод запроса именно POST
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
http_response_code(405); // Method Not Allowed
echo json_encode(["status" => "error", "message" => "Only POST requests are allowed"]);
exit;
}
// Читаем сырые данные JSON из тела запроса
$rawInput = file_get_contents("php://input");
// Если тело запроса пустое
if (empty($rawInput)) {
http_response_code(400); // Bad Request
echo json_encode(["status" => "error", "message" => "Empty request body"]);
exit;
}
// Декодируем JSON в ассоциативный массив
$data = json_decode($rawInput, true);
// Проверяем на ошибки парсинга JSON
if (json_last_error() !== JSON_ERROR_NONE) {
http_response_code(400); // Bad Request
echo json_encode([
"status" => "error",
"message" => "Invalid JSON: " . json_last_error_msg()
]);
exit;
}
// --- 1. ЛОГИРОВАНИЕ ДАННЫХ В ФАЙЛ (Для быстрой проверки работы) ---
$logFile = 'esp32_reports.log';
$timestamp = date('Y-m-d H:i:s');
$logEntry = "[$timestamp] IP: " . $_SERVER['REMOTE_ADDR'] . "\n" . $rawInput . "\n\n";
file_put_contents($logFile, $logEntry, FILE_APPEND);
// --- 2. РАЗБОР ДАННЫХ В ПЕРЕМЕННЫЕ ---
// Извлекаем системные данные (если они есть)
$system = isset($data['system']) ? $data['system'] : [];
$chipModel = isset($system['chip_model']) ? $system['chip_model'] : 'Unknown';
$chipCores = isset($system['chip_cores']) ? (int)$system['chip_cores'] : 0;
$chipId = isset($system['chip_id']) ? $system['chip_id'] : 'Unknown';
$freeHeap = isset($system['free_heap']) ? (int)$system['free_heap'] : 0;
$uptimeMs = isset($system['uptime_ms']) ? (int)$system['uptime_ms'] : 0;
// Извлекаем пользовательские данные (если они есть)
$custom = isset($data['custom']) ? $data['custom'] : [];
$temperature = isset($custom['temperature']) ? (float)$custom['temperature'] : null;
$humidity = isset($custom['humidity']) ? (float)$custom['humidity'] : null;
$deviceStatus= isset($custom['device_status']) ? $custom['device_status'] : 'Unknown';
// --- 3. ОТПРАВКА УСПЕШНОГО ОТВЕТА НА ESP32 ---
http_response_code(200); // OK
echo json_encode([
"status" => "success",
"message" => "Report received and processed",
"chip_id" => $chipId,
"received_at" => $timestamp
]);