add debug output and change data matching

This commit is contained in:
Jeremias Wolff
2021-04-05 17:50:38 +02:00
parent 1729be563c
commit 33de69c737

View File

@@ -22,14 +22,16 @@ class EcowittExportCommand extends Command
* *
* @var string * @var string
*/ */
protected $signature = 'export {--user=} {--pass=} {startDate} {endDate}'; protected $signature = 'export {--debug} {--user=} {--pass=} {startDate} {endDate}';
/** /**
* The console command description. * The console command description.
* *
* @var string * @var string
*/ */
protected $description = 'Command description'; protected $description = 'fetch all data from ecowitt';
protected $times = [];
/** /**
* Create a new command instance. * Create a new command instance.
@@ -59,106 +61,125 @@ class EcowittExportCommand extends Command
$session_id = $this->getSessionId(); $session_id = $this->getSessionId();
$device_ids = $this->getDeviceIds($session_id); $device_ids = $this->getDeviceIds($session_id);
$this->startDate = Carbon::parse($this->argument('startDate'))->startOfDay() $this->startDate = Carbon::parse($this->argument('startDate'))->startOfDay();
->format('Y-m-d H:i');
$this->endDate = Carbon::parse($this->argument('endDate'))->endOfDay() $this->endDate = Carbon::parse($this->argument('endDate'))->endOfDay();
->format('Y-m-d H:i');
$device_ids->each(function ($deviceId) use ($session_id) { $device_ids->each(function ($deviceId) use ($session_id) {
$startDate = $this->startDate; $startDate = $this->startDate->clone();
$endDate = $this->endDate; $endDate = $this->endDate->clone();
$response = Http::withCookies(
[
'ousaite_session' => $session_id,
], 'www.ecowitt.net')
->asForm()
->post('https://webapi.www.ecowitt.net/index/get_data', [
'device_id' => $deviceId,
'is_list' => 0,
'mode' => 0,
'sdate' => $startDate,
'edate' => $endDate,
'page' => 1,
]);
$ecowitt = $response->json();
// declare output variable // declare output variable
$outputData = []; $outputData = [];
// Temperature in C do {
$outdoorTemp = $this->getData($ecowitt, 'list.tempf.list.tempf'); $response = Http::withCookies(
[
'ousaite_session' => $session_id,
], 'www.ecowitt.net')
->asForm()
->post('https://webapi.www.ecowitt.net/index/get_data', [
'device_id' => $deviceId,
'is_list' => 0,
'mode' => 0,
'sdate' => $startDate->clone()->startOfDay()->format('Y-m-d H:i'),
'edate' => $startDate->clone()->endOfDay()->format('Y-m-d H:i'),
'page' => 1,
]);
// Feels Like in C $this->comment("fetching range: {$startDate->clone()->startOfDay()} - {$startDate->clone()->endOfDay()}");
$outdoorTempGust = $this->getData($ecowitt, 'list.tempf.list.sendible_temp');
// Dew Point in C $ecowitt = $response->json();
$outdoorDewTemp = $this->getData($ecowitt, 'list.tempf.list.drew_temp');
// humidity in % $this->times = data_get($ecowitt,'times',[]);
$outdoorHumidity = $this->getData($ecowitt, 'list.humidity.list.humidity');
// temp indoor in C // Temperature in C
$indoorTemp = $this->getData($ecowitt, 'list.tempinf.list.tempinf'); $this->debug('fetch outdoor temp');
$outdoorTemp = $this->getData($ecowitt, 'list.tempf.list.tempf');
// humidityin in % // Feels Like in C
$indoorHumidity = $this->getData($ecowitt, 'list.humidity.list.humidity'); $this->debug('fetch outdoor temp gust');
$outdoorTempGust = $this->getData($ecowitt, 'list.tempf.list.sendible_temp');
// solar in lx -- Solar and UVI // Dew Point in C
$solarradiation = $this->getData($ecowitt, 'list.solarradiation.list.solarradiation'); $this->debug('collecting: Dew Point in C');
$outdoorDewTemp = $this->getData($ecowitt, 'list.tempf.list.drew_temp');
// uv // humidity in %
$uvi = $this->getData($ecowitt, 'list.uv.list.uv'); $this->debug('collecting: humidity in %');
$outdoorHumidity = $this->getData($ecowitt, 'list.humidity.list.humidity');
// rainrate in mm/hr b // temp indoor in C
$rainRateH = $this->getData($ecowitt, 'list.rain.list.rainratein'); $this->debug('collecting: temp indoor in C');
$indoorTemp = $this->getData($ecowitt, 'list.tempinf.list.tempinf');
// daily rainrate total mm/hr // humidityin in %
$rainRateDaily = $this->getData($ecowitt, 'list.rain.list.dailyrainin'); $this->debug('collecting: humidityin in %');
$indoorHumidity = $this->getData($ecowitt, 'list.humidity.list.humidity');
// wind_speed in m/s // solar in lx -- Solar and UVI
$windspeed = $this->getData($ecowitt, 'list.wind_speed.list.windspeedmph'); $this->debug('collecting: solar in lx -- Solar and UVI');
$solarradiation = $this->getData($ecowitt, 'list.solarradiation.list.solarradiation');
// windGust // uv
$windGust = $this->getData($ecowitt, 'list.wind_speed.list.windgustmph'); $this->debug('collecting: uv');
$uvi = $this->getData($ecowitt, 'list.uv.list.uv');
// winddir in degree // rainrate in mm/hr b
$windir = $this->getData($ecowitt, 'list.winddir.list.winddir'); $this->debug('collecting: rainrate in mm/hr b');
$rainRateH = $this->getData($ecowitt, 'list.rain.list.rainratein');
// pressure relative in hPa // daily rainrate total mm/hr
$pressureRel = $this->getData($ecowitt, 'list.pressure.list.baromrelin'); $this->debug('collecting: daily rainrate total mm/hr');
$rainRateDaily = $this->getData($ecowitt, 'list.rain.list.dailyrainin');
// pressure absolute in hPa // wind_speed in m/s
$pressureAbs = $this->getData($ecowitt, 'list.pressure.list.baromabsin'); $this->debug('collecting: wind_speed in m/s');
$windspeed = $this->getData($ecowitt, 'list.wind_speed.list.windspeedmph');
foreach ($outdoorTemp as $date => $temp) { // windGust
$tmp = [ $this->debug('collecting: windGust');
'date_and_time' => $date, // %Y-%m-%d %H:%M:%S $windGust = $this->getData($ecowitt, 'list.wind_speed.list.windgustmph');
'temp_out' => $temp, // degree
'temp_out_gust' => data_get($outdoorTempGust, $date), // degree // winddir in degree
'temp_out_dew' => data_get($outdoorDewTemp, $date), // degree $this->debug('collecting: winddir in degree');
'humid_out' => data_get($outdoorHumidity, $date), // percent $windir = $this->getData($ecowitt, 'list.winddir.list.winddir');
'temp_in' => data_get($indoorTemp, $date), // degree
'humid_in' => data_get($indoorHumidity, $date), // percent // pressure relative in hPa
'rad' => data_get($solarradiation, $date), // lx $this->debug('collecting: pressure relative in hPa');
'uv' => data_get($uvi, $date), $pressureRel = $this->getData($ecowitt, 'list.pressure.list.baromrelin');
'rain' => data_get($rainRateH, // mm
$date // pressure absolute in hPa
), $this->debug('collecting: pressure absolute in hPa');
'rain_daily' => data_get($rainRateDaily, $date), // mm $pressureAbs = $this->getData($ecowitt, 'list.pressure.list.baromabsin');
'wind' => data_get($windspeed, // m_per_second
$date foreach ($outdoorTemp as $date => $temp) {
), $tmp = [
'wind_gust' => data_get($windGust, $date), // m_per_second 'date_and_time' => $date, // %Y-%m-%d %H:%M:%S
'wind_dir' => data_get($windir, $date), // degree_compass 'temp_out' => $temp, // degree
'pressure_rel' => data_get($pressureRel, $date), // hPa 'temp_out_gust' => data_get($outdoorTempGust, $date), // degree
'pressure_abs' => data_get($pressureAbs, $date), // hPa 'temp_out_dew' => data_get($outdoorDewTemp, $date), // degree
]; 'humid_out' => data_get($outdoorHumidity, $date), // percent
$outputData[] = $tmp; 'temp_in' => data_get($indoorTemp, $date), // degree
} 'humid_in' => data_get($indoorHumidity, $date), // percent
'rad' => data_get($solarradiation, $date), // lx
'uv' => data_get($uvi, $date),
'rain' => data_get($rainRateH, // mm
$date
),
'rain_daily' => data_get($rainRateDaily, $date), // mm
'wind' => data_get($windspeed, // m_per_second
$date
),
'wind_gust' => data_get($windGust, $date), // m_per_second
'wind_dir' => data_get($windir, $date), // degree_compass
'pressure_rel' => data_get($pressureRel, $date), // hPa
'pressure_abs' => data_get($pressureAbs, $date), // hPa
];
$outputData[] = $tmp;
}
$startDate = $startDate->addDay()->startOfDay();
} while ( $startDate->lte($endDate) );
$this->export(getcwd() . "/ecowitt_{$deviceId}.csv", $outputData); $this->export(getcwd() . "/ecowitt_{$deviceId}.csv", $outputData);
@@ -168,8 +189,9 @@ class EcowittExportCommand extends Command
protected function getData($stack, $key) protected function getData($stack, $key)
{ {
return collect(data_get($stack, $key)) return collect(data_get($stack, $key))
->mapWithKeys(function ($value) { ->mapWithKeys(function ($value, $idx) {
return [$value[0] => $value[1] ?: null]; $dateTime = data_get($this->times, $idx);
return [$dateTime => $value ?: null];
}); });
} }
@@ -232,4 +254,21 @@ class EcowittExportCommand extends Command
fclose($fp); fclose($fp);
} }
} }
/**
* @param string $msg
* @param mixed ...$args
*/
protected function debug(string $msg, ...$args)
{
if ($this->option('debug')) {
$this->info($msg);
if (!empty($args)) {
dump($args);
}
}
}
} }