-
Notifications
You must be signed in to change notification settings - Fork 3
/
create_egg.php
76 lines (65 loc) · 3.93 KB
/
create_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
72
73
74
75
76
<?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 CreateEgg {
private $feedId;
private $limit = 1000;
private $metadata = array('title', 'description', 'locationName', 'lat', 'lon', 'ele', 'status', 'exposure');
private $sensors = array('co', 'no2', 'humidity', 'temperature');
public function __construct() {
print $this->createEgg();
}
private function createEgg() {
if ( isset($_GET['fid']) && is_numeric($_GET['fid']) ) {
$this->feedId = $_GET['fid'];
$cosmAPI = new CosmAPI();
$mySqlConnection = new MySqlConnection();
date_default_timezone_set('UTC');
mysql_query('CREATE TABLE IF NOT EXISTS `eggdata_'.$this->feedId.'` ( `timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, `co` double(9,2) NOT NULL, `no2` double(9,2) NOT NULL, `temperature` double(6,2) NOT NULL, `humidity` double(6,2) NOT NULL, PRIMARY KEY (`timestamp`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1');
$num_rows = mysql_num_rows(mysql_query('SELECT `timestamp` FROM `eggdata_'.$this->feedId.'`'));
if ( $num_rows < 1 ) {
$metadata = false;
for ( $seconds = 0; $seconds <= 3; $seconds++ ) {
$start = time() - $seconds * 60000 - 60000;
$end = time() - $seconds * 60000;
if ( ! $dataArray = $cosmAPI->parseFeed($this->feedId, $start, $end, $this->limit, 60) ) {
return 'cosm_error';
}
else if ( ! is_array($dataArray) ) {
return $dataArray;
}
else {
if ( ! $metadata ) {
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);
$metadata = true;
}
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 CreateEgg();
}
?>