-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker.php
60 lines (47 loc) · 1.69 KB
/
worker.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
50
51
52
53
54
55
56
57
58
59
60
<?php
include 'sql.php';
include 'curl.php';
// report errors
error_reporting(E_ALL);
ini_set('display_errors', 1);
//login data to database
$servername = "localhost";
$username = "";
$password = "";
$dbname = "";
//create connection
$conn = connectToDatabase($servername, $username, $password, $dbname);
//read and load the JSON file
/*
$json = loadJSON('https://api.opensensemap.org/boxes/5a699afe411a790019628c13/sensors');
$time = $json['sensors'][0]['lastMeasurement']['createdAt'];
$time = new \DateTime($time);
$timestamp = $time->getTimestamp();
$pm10 = $json['sensors'][0]['lastMeasurement']['value'];
$pm2p5 = $json['sensors'][1]['lastMeasurement']['value'];
$temperature = $json['sensors'][2]['lastMeasurement']['value'];
$humidity = $json['sensors'][3]['lastMeasurement']['value'];
insertDataSet($conn, $timestamp, $temperature, $humidity, $pm10, $pm2p5);
printCurrentData($timestamp, $temperature, $humidity, $pm10, $pm2p5);
*/
while(TRUE) {
$json = loadJSON('https://api.opensensemap.org/boxes/5a699afe411a790019628c13/sensors');
$pm10 = $json['sensors'][0]['lastMeasurement']['value'];
$pm2p5 = $json['sensors'][1]['lastMeasurement']['value'];
$temperature = $json['sensors'][2]['lastMeasurement']['value'];
$humidity = $json['sensors'][3]['lastMeasurement']['value'];
$oldTimestamp = $timestamp;
$time = $json['sensors'][0]['lastMeasurement']['createdAt'];
$time = new \DateTime($time);
$timestamp = $time->getTimestamp();
if($timestamp != $oldTimestamp){
echo "updated!\n";
printCurrentData($timestamp, $temperature, $humidity, $pm10, $pm2p5, "\n");
insertDataSet($conn, $timestamp, $temperature, $humidity, $pm10, $pm2p5);
}else{
echo "wait\n";
sleep(15);
}
}
$conn->close();
?>