init commit
This commit is contained in:
165
both.php
Normal file
165
both.php
Normal file
@@ -0,0 +1,165 @@
|
||||
<?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' => 'model',
|
||||
);
|
||||
//POST (ecowitt):
|
||||
// [PASSKEY] => 123*****************************
|
||||
// [stationtype] => GW2000A_V3.2.4
|
||||
// [runtime] => 296
|
||||
// [heap] => 110140
|
||||
// [dateutc] => 2025-05-08 16:00:56
|
||||
// [tempinf] => 69.26
|
||||
// [humidityin] => 61
|
||||
// [baromrelin] => 28.804
|
||||
// [baromabsin] => 28.804
|
||||
// [tempf] => 69.44
|
||||
// [humidity] => 60
|
||||
// [vpd] => 0.290
|
||||
// [winddir] => 69
|
||||
// [windspeedmph] => 0.00
|
||||
// [windgustmph] => 3.36
|
||||
// [maxdailygust] => 11.41
|
||||
// [solarradiation] => 0.15
|
||||
// [uv] => 0
|
||||
// [rainratein] => 0.000
|
||||
// [eventrainin] => 0.000
|
||||
// [hourlyrainin] => 0.000
|
||||
// [dailyrainin] => 0.000
|
||||
// [weeklyrainin] => 0.000
|
||||
// [monthlyrainin] => 0.000
|
||||
// [yearlyrainin] => 0.000
|
||||
// [totalrainin] => 0.000
|
||||
// [wh65batt] => 0
|
||||
// [freq] => 868M
|
||||
// [model] => GW2000A
|
||||
// [interval] => 8
|
||||
|
||||
//GET (wunderground):
|
||||
// [ID] => 1
|
||||
// [PASSWORD] => 1
|
||||
// [tempf] => 69.44
|
||||
// [humidity] => 60
|
||||
// [dewptf] => 55.04
|
||||
// [windchillf] => 69.44
|
||||
// [winddir] => 69
|
||||
// [windspeedmph] => 0.00
|
||||
// [windgustmph] => 0.00
|
||||
// [rainin] => 0.000
|
||||
// [dailyrainin] => 0.000
|
||||
// [weeklyrainin] => 0.000
|
||||
// [monthlyrainin] => 0.000
|
||||
// [yearlyrainin] => 0.000
|
||||
// [solarradiation] => 0.15
|
||||
// [UV] => 0
|
||||
// [indoortempf] => 69.26
|
||||
// [indoorhumidity] => 61
|
||||
// [baromin] => 28.804
|
||||
// [lowbatt] => 0
|
||||
// [dateutc] => now
|
||||
// [softwaretype] => GW2000A_V3.2.4
|
||||
// [action] => updateraw
|
||||
// [realtime] => 1
|
||||
// [rtfreq] => 5
|
||||
|
||||
$NORMALIZATION = array(
|
||||
'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),
|
||||
);
|
||||
|
||||
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);
|
Reference in New Issue
Block a user