forked from geerlingguy/airgradient-prometheus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
49 lines (41 loc) · 1.01 KB
/
index.php
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
<?php
/**
* Handle POST requests from AirGradient sensors.
*/
// Defaults so script writes a file if parts fail.
$sensorid = 'abc';
$json_output = '';
// Handle the URL and get args.
if (!isset($_GET['path'])) {
http_response_code(501);
return;
}
$path = explode('/', $_GET['path']);
// Handle AirGradient requests.
if ($path[0] == 'sensors') {
$path_part = explode(':', $path[1]);
$instance = $path_part[1];
}
// Get the data from the POST request.
if ($data = json_decode(file_get_contents('php://input'), true)) {
// Add instance to the array.
$data['instance'] = $instance;
$json_output = json_encode($data);
}
else {
print('No data in POST request.');
http_response_code(501);
return;
}
// Prepare output directory.
$sensors_dir = '/sensors';
if (!is_dir($sensors_dir)) {
mkdir($sensors_dir);
}
// Write data to file.
$output_file = $sensors_dir . '/' . $instance . '.json';
if (!file_put_contents($output_file, $json_output)) {
print('Could not write file.');
http_response_code(500);
return;
}