-
Notifications
You must be signed in to change notification settings - Fork 3
/
update_egg.php
71 lines (61 loc) · 3.09 KB
/
update_egg.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
61
62
63
64
65
66
67
68
69
70
71
<?php
// edit the $password variable to change the password
$password = 'faf01b58a34e26f5ed05a4edc5e5c3ee';
if ( $_GET['pass'] == $password ) {
include('class/simplexmlextended.class.php');
include('class/cosmapi.class.php');
include('class/mysqlconnection.class.php');
class UpdateEgg {
private $feedId;
private $limit = 50;
private $metadata = array('title', 'description', 'locationName', 'lat', 'lon', 'ele', 'status', 'exposure');
private $sensors = array('co', 'no2', 'humidity', 'temperature');
public function __construct() {
print $this->updateEgg();
}
private function updateEgg() {
if ( isset($_GET['fid']) && is_numeric($_GET['fid']) ) {
$this->feedId = $_GET['fid'];
$cosmAPI = new CosmAPI();
$mySqlConnection = new MySqlConnection();
$egg = mysql_fetch_object(mysql_query('SELECT `lastupdated` FROM `egg` WHERE `feed_id` = '.$this->feedId));
if ( $egg->lastupdated > 0 ) {
$start = $egg->lastupdated;
}
else {
$start = time() - 82800;
}
date_default_timezone_set('UTC');
$end = time();
if ( ! $dataArray = $cosmAPI->parseFeed($this->feedId, $start, $end, $this->limit, 60) ) {
return 'cosm_error';
}
else if ( ! is_array($dataArray) ) {
return $dataArray;
}
else {
mysql_query('UPDATE `egg` SET `title` = \''.$dataArray['title'].'\', `description` = \''.$dataArray['description'].'\', `location_name` = \''.$dataArray['locationName'].'\', `ele` = \''.$dataArray['ele'].'\', `status` = \''.$dataArray['status'].'\', `exposure` = \''.$dataArray['exposure'].'\', `lastupdated` = \''.time().'\' WHERE `feed_id` = '.$this->feedId);
foreach ( $this->metadata as $mdata ) {
if ( isset($dataArray[$mdata]) ) {
unset($dataArray[$mdata]);
}
}
foreach ( $dataArray as $time => $val ) {
$nullsensors = 0;
foreach ( $this->sensors as $sensor ) {
if ( ! isset($val[$sensor]) ) {
$val[$sensor] = 0;
$nullsensors++;
}
}
if ( $nullsensors < 4 && floatval($time) != 0.0 ) {
mysql_query('INSERT INTO `eggdata_'.$this->feedId.'` ( `timestamp`, `co`, `no2`, `temperature`, `humidity` ) VALUES (\''.date('Y-m-d H:i:s', $time).'\', \''.$val['co'].'\', \''.$val['no2'].'\', \''.$val['temperature'].'\', \''.$val['humidity'].'\')');
}
}
}
}
}
}
new UpdateEgg();
}
?>