139 lines
3.1 KiB
PHP
139 lines
3.1 KiB
PHP
<?php
|
|
|
|
$dbPath = 'sqlite:./weatherdata.sqlite3';
|
|
|
|
$WUNDERGROUND_TO_ECOWITT = array(
|
|
'tempf' => 'tempf',
|
|
'humidity' => 'humidity',
|
|
'indoortempf' => 'tempinf',
|
|
'indoorhumidity' => 'humidityin',
|
|
'UV' => 'uv',
|
|
'winddir' => 'winddir',
|
|
'windspeedmph' => 'windspeedmph',
|
|
'windgustmph' => 'windgustmph',
|
|
'dailyrainin' => 'dailyrainin',
|
|
'weeklyrainin' => 'weeklyrainin',
|
|
'monthlyrainin' => 'monthlyrainin',
|
|
'yearlyrainin' => 'yearlyrainin',
|
|
'dateutc' => 'dateutc',
|
|
'solarradiation' => 'solarradiation',
|
|
'softwaretype' => 'stationtype',
|
|
// not sure about the following keys
|
|
'lowbatt'=> 'wh65batt',
|
|
'rainin' => 'rainratein',
|
|
);
|
|
|
|
// TDOD: make mapping for these keys
|
|
//POST (ecowitt):
|
|
// [PASSKEY] => 123*****************************
|
|
// [runtime] => 296
|
|
// [heap] => 110140
|
|
// [baromrelin] => 28.804
|
|
// [baromabsin] => 28.804
|
|
// [vpd] => 0.290
|
|
// [maxdailygust] => 11.41
|
|
// [eventrainin] => 0.000
|
|
// [hourlyrainin] => 0.000
|
|
// [totalrainin] => 0.000
|
|
// [freq] => 868M
|
|
// [model] => GW2000A
|
|
// [interval] => 8
|
|
|
|
//GET (wunderground):
|
|
// [ID] => 1
|
|
// [PASSWORD] => 1
|
|
// [dewptf] => 55.04
|
|
// [windchillf] => 69.44
|
|
// [baromin] => 28.804
|
|
// [action] => updateraw
|
|
// [realtime] => 1
|
|
// [rtfreq] => 5
|
|
|
|
$NORMALIZATION = array(
|
|
// TODO: round float to only two decimal places
|
|
'dateutc' => fn ($dt) => new DateTimeImmutable($dt),
|
|
'tempf' => fn ($temp) => normalizeTemperature($temp),
|
|
'indoortempf' => fn ($temp) => normalizeTemperature($temp),
|
|
'windspeedmph' => fn ($mph) => normalizeSpeed($mph),
|
|
'windgustmph' => fn ($mph) => normalizeSpeed($mph),
|
|
// TODO: add missing keys from mapping
|
|
);
|
|
|
|
function ecowittKey($key)
|
|
{
|
|
global $WUNDERGROUND_TO_ECOWITT;
|
|
|
|
return empty($WUNDERGROUND_TO_ECOWITT[$key]) ? $key : $WUNDERGROUND_TO_ECOWITT[$key];
|
|
}
|
|
|
|
function normalizeSpeed($mph)
|
|
{
|
|
// string to float
|
|
$mph = (float) $mph;
|
|
return mphToKmh($mph);
|
|
}
|
|
|
|
function normalizeTemperature($fahrenheit)
|
|
{
|
|
// string to float
|
|
$fahrenheit = (float) $fahrenheit;
|
|
// missing readings are represented as -9999
|
|
return $fahrenheit == -9999
|
|
? null
|
|
: fToC($fahrenheit);
|
|
}
|
|
|
|
function fToC($fahrenheit)
|
|
{
|
|
return ($fahrenheit - 32) * 5 / 9;
|
|
}
|
|
|
|
function mphToKmh($mph)
|
|
{
|
|
// TODO: check
|
|
return $mph * 1.609344;
|
|
}
|
|
|
|
function extractParameter($key)
|
|
{
|
|
global $NORMALIZATION;
|
|
|
|
$value = empty($_GET[$key]) ? $_POST[ecowittKey($key)] : $_GET[$key];
|
|
|
|
return isset($NORMALIZATION[$key])
|
|
? ($NORMALIZATION[$key])($value)
|
|
: $value;
|
|
}
|
|
|
|
function dbConn($path)
|
|
{
|
|
|
|
try {
|
|
return new \PDO($path);
|
|
} catch (\PDOException $e) {
|
|
die($e->getMessage());
|
|
}
|
|
}
|
|
|
|
function createTable($conn)
|
|
{
|
|
// TODO:
|
|
$conn->exec("CREATE TABLE IF NOT EXISTS weatherdata (
|
|
id INTEGER PRIMARY KEY,
|
|
reading_date TIMESTAMP,
|
|
temperature INTEGER,
|
|
humidity INTEGER,
|
|
barometer_rel INTEGER,
|
|
barometer_abs INTEGER
|
|
)");
|
|
}
|
|
|
|
$conn = dbConn($dbPath);
|
|
createTable($conn);
|
|
|
|
$res = extractParameter('dateutc');
|
|
var_dump($res);
|
|
|
|
$res = extractParameter('tempf');
|
|
var_dump($res);
|