-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetchWeatherData.php
More file actions
194 lines (173 loc) · 5.67 KB
/
fetchWeatherData.php
File metadata and controls
194 lines (173 loc) · 5.67 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php
require('sanitize.inc.php');
include_once 'weatherInterface.php';
define('APPLICATION_ENV', 'production');
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
* Description of fetchWeatherData
*
* @author john
*/
class WeatherData implements weatherInterface {
public $allowedFields = array(
'Id' => 'Id',
'Date' => 'dateutc',
'Temp' => 'temp',
'WindSpeed' => 'windspeedmph',
'Pressure' => 'barom',
'Rain' => 'rain',
'Humidity' => 'humidity',
'WindDirection' => 'winddir',
'WindGust' => 'windgustmph',
'FeelsLike' => 'dewpt',
'DailyRain' => 'dailyrain');
public $validCommands;
public $defaultSort;
public $defaultDirection;
public $defaultLimit;
private $dbh;
private $config;
private $fieldList;
private $direction;
private $interval;
private $limit = 1000;
private $tableData;
/**
*
* @param type $vc
* @param type $ds
* @param type $dd
* @param type $dl
*/
public function __construct($vc = array("Fields", "Sort", "Interval", "Limit"), $ds = "Id", $dd = "DESC", $dl = 10) {
$this->validCommands = $vc;
$this->defaultSort = $ds;
$this->defaultDirection = $dd;
$this->defaultLimit = $dl;
$this->getConfig();
$this->getConnection();
}
/**
*
* @param type $string
* @return type
*/
function isJson($string) {
json_decode($string);
return (json_last_error() == JSON_ERROR_NONE);
}
/**
*
* @param String $data
* @param type $returnType
*/
public function fetchData($data, $returnType = "json") {
if ($this->isJson($data)) {
$dataArray = json_decode($data, true);
$this->fieldList = explode(",", $dataArray['Fields']);
$this->direction = $dataArray['Sort'];
$this->interval = $dataArray['Interval'];
$this->limit = $dataArray['Limit'];
} else {
// assume that the format is the name value pairs
$decodedStr = urldecode($data);
$cleanData = sanitize($decodedStr, HTML + SQL);
$commands = explode('|', $cleanData);
// Create and clean the requested fields
$fields = explode(':', $commands[0]);
$this->fieldList = explode(',', $fields[1]);
// Create and Clean the interval
list($junk, $interval) = explode(':', $commands[2]);
// Make Sure interval is an integer
$this->interval = sanitize($interval, INT);
// Create and Clean the Limit
if (isset($commands[3])) {
list($limitCommand, $limit) = explode(':', $commands[3]);
// Make Sure limit is an integer
$this->limit = sanitize($limit, INT);
} else {
$this->limit = $this->defaultLimit;
}
}
switch ($returnType) {
case "json" : $this->fetchDataAsJson();
break;
}
}
/**
*
* @param type $fieldList
* @param type $direction
* @param type $interval
* @param type $limit
*/
private function fetchDataAsCSV($fieldList, $direction = "DESC", $interval = 1, $limit) {
$this->fieldList = $fieldList;
$this->direction = $direction;
$this->interval = $interval;
$this->limit = $limit;
$this->prepareSQL();
// TODO : implement to csv
}
private function fetchDataAsJson() {
$this->prepareSQL();
$jsonData = json_encode($this->tableData, JSON_NUMERIC_CHECK);
print $jsonData;
}
private function prepareSQL() {
foreach ($this->fieldList as $requestedField) {
$requestedField = trim($requestedField);
if (array_key_exists($requestedField, $this->allowedFields)) {
$sqlFields[] = "{$this->allowedFields[$requestedField]} AS $requestedField";
//print $requestedField;
}
}
$fieldSQL = implode(",", $sqlFields);
if ($this->direction == "Ascending") {
$this->direction = "ASC";
} else {
$this->direction = "DESC";
}
$limitSQL = " Limit $this->limit";
$sql = "SELECT $fieldSQL
FROM raw
WHERE dateutc >= DATE_ADD(CURDATE(), INTERVAL -:interval DAY)
order by dateutc DESC $limitSQL";
try {
$sth = $this->dbh->prepare($sql);
} catch (PDOException $e) {
echo 'Prepare failed: ' . $e->getMessage();
die("Died in Prepare");
}
try {
$sth->execute(array(
":interval" => $this->interval,
//":sortOn" => $sortOn,
//":direction" => $direction
));
} catch (PDOException $e) {
echo 'Execution failed: ' . $e->getMessage();
die("Died in execute");
}
$this->tableData = $sth->fetchAll(PDO::FETCH_ASSOC);
}
private function getConfig() {
$this->config = parse_ini_file("configuration.php", true);
// print_r($this->config);
}
private function getConnection() {
$dsn = $this->config[APPLICATION_ENV]['dsn'];
$user = $this->config[APPLICATION_ENV]['user'];
$password = $this->config[APPLICATION_ENV]['password'];
try {
$this->dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
die("Died in Connection");
}
}
}