-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
141 lines (120 loc) · 4.85 KB
/
Copy pathindex.php
File metadata and controls
141 lines (120 loc) · 4.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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
/**
* disc-cloud-tracker — telemetry ingest endpoint
*
* Accepts a POST request with a JSON body sent by a Teltonika RUT9xx router
* (via its "Data to Server" feature), performs reverse geocoding via
* OpenStreetMap Nominatim, and stores the enriched row into MySQL.
*/
date_default_timezone_set('Europe/Paris'); // Adjust to your local timezone
// ---------------------------------------------------------------------------
// Fail fast on missing critical environment variables (no placeholder fallback)
// ---------------------------------------------------------------------------
function require_env(string $name): string {
$v = getenv($name);
if ($v === false || $v === '' || $v === 'changeme') {
http_response_code(500);
error_log("FATAL: environment variable $name is not set");
die("Configuration error");
}
return $v;
}
// ---------------------------------------------------------------------------
// Database configuration (from environment variables; DB_PASSWORD is required)
// ---------------------------------------------------------------------------
$DB_HOST = getenv('DB_HOST') ?: 'localhost';
$DB_PORT = (int) (getenv('DB_PORT') ?: 3306);
$DB_NAME = getenv('DB_NAME') ?: 'disc';
$DB_USER = getenv('DB_USER') ?: 'disc';
$DB_PASSWORD = require_env('DB_PASSWORD');
// ---------------------------------------------------------------------------
// Reverse geocoding helper (OpenStreetMap Nominatim)
// Please respect https://operations.osmfoundation.org/policies/nominatim/
// ---------------------------------------------------------------------------
function getAddress($RG_Lat, $RG_Lon)
{
$url = "https://nominatim.openstreetmap.org/reverse?format=json"
. "&lat=" . urlencode($RG_Lat)
. "&lon=" . urlencode($RG_Lon)
. "&zoom=27&addressdetails=1";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, "disc-cloud-tracker/1.0 (+https://github.com/replicatorbe/disc-cloud-tracker)");
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$jsonfile = curl_exec($ch);
curl_close($ch);
if ($jsonfile === false) {
return null;
}
$RG_array = json_decode($jsonfile, true);
return isset($RG_array['display_name']) ? $RG_array['display_name'] : null;
}
// ---------------------------------------------------------------------------
// Only POST is accepted
// ---------------------------------------------------------------------------
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
http_response_code(405);
header('Allow: POST');
echo "Error: this endpoint only accepts POST requests.";
exit;
}
// Read raw request body
$json_data = file_get_contents('php://input');
if ($json_data === false || $json_data === '') {
http_response_code(400);
echo "Error: no JSON payload received.";
exit;
}
$data = json_decode($json_data);
if ($data === null) {
http_response_code(400);
echo "Error decoding JSON: " . json_last_error_msg();
exit;
}
// Extract telemetry fields (all optional — router templates vary)
$V1 = $data->V1 ?? null;
$V2 = $data->V2 ?? null;
$POW = $data->POW ?? null;
$TEMP = $data->TEMP ?? null;
$R1 = $data->R1 ?? null;
$GSM_DATA = $data->GSM_DATA ?? null;
$SERIAL_DATA_LATITUDE = $data->SERIAL_DATA->LATITUDE ?? null;
$LATITUDE = $data->LATITUDE ?? null;
$LONGITUDE = $data->LONGITUDE ?? null;
$ACPLUG = $data->ACPLUG ?? null;
$DISC = $data->disc ?? null;
// Enrich with reverse-geocoded address when coordinates are present
$ADRESSE = null;
if ($LATITUDE !== null && $LONGITUDE !== null) {
$ADRESSE = getAddress($LATITUDE, $LONGITUDE);
}
// Connect to MySQL
$mysqli = new mysqli($DB_HOST, $DB_USER, $DB_PASSWORD, $DB_NAME, $DB_PORT);
if ($mysqli->connect_error) {
http_response_code(500);
die("Database connection error: " . $mysqli->connect_error);
}
// Prepared insert (protects against SQL injection)
$query = "INSERT INTO mesures
(V1, V2, POW, TEMP, R1, GSM_DATA, SERIAL_DATA_LATITUDE, LATITUDE, LONGITUDE, ACPLUG, DISC, ADRESSE)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
$stmt = $mysqli->prepare($query);
if ($stmt === false) {
http_response_code(500);
die("Error preparing SQL statement: " . $mysqli->error);
}
$stmt->bind_param(
"dddddsddddss",
$V1, $V2, $POW, $TEMP, $R1,
$GSM_DATA,
$SERIAL_DATA_LATITUDE, $LATITUDE, $LONGITUDE,
$ACPLUG, $DISC, $ADRESSE
);
if ($stmt->execute()) {
echo "OK";
} else {
http_response_code(500);
echo "Error inserting data: " . $stmt->error;
}
$stmt->close();
$mysqli->close();