From b137342f6a7ab02700b02e0275bb6eac1ccf17c7 Mon Sep 17 00:00:00 2001 From: KiboOst Date: Wed, 11 Apr 2018 12:20:42 +0200 Subject: [PATCH] Cron task script --- linkyCron.php | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 linkyCron.php diff --git a/linkyCron.php b/linkyCron.php new file mode 100644 index 0000000..7aa3aef --- /dev/null +++ b/linkyCron.php @@ -0,0 +1,59 @@ +error)) echo '__ERROR__: ', $_Linky->error, "
"; + +$newJson = $_Linky->_data; + +if (@file_exists($filePath)) +{ + $prevDatas = json_decode(file_get_contents($filePath), true); +} +else +{ + $prevDatas = $newJson; +} + +//Update current month: +$timezone = 'Europe/Paris'; +$today = new DateTime('NOW', new DateTimeZone($timezone)); +$thisMonth = $today->format('M Y'); + +$updateTime = $today->format('d/m/Y H:i:s'); +$prevDatas['Update'] = $updateTime; + +if ($newJson['months'][$thisMonth] != null) $prevDatas['months'][$thisMonth] = $newJson['months'][$thisMonth]; + +//Update current year: +$thisYear = new DateTime(); +$thisYear = $thisYear->format('Y'); + +if ($newJson['years'][$thisYear] != null) $prevDatas['years'][$thisYear] = $newJson['years'][$thisYear]; + +//Does yesterday hours exists ? +$yesterday = clone $today; +$yesterday->sub(new DateInterval('P1D')); +$yesterday = $yesterday->format('d/m/Y'); +if (!isset($prevDatas['hours'][$yesterday])) +{ + //avoid empty data: + $h = $newJson['hours'][$yesterday]["00:00"]; + if ($h != 'kW' and $h != '-2kW') $prevDatas['hours'][$yesterday] = $newJson['hours'][$yesterday]; +} + +//Add yesterday day: +if (!isset($prevDatas['days'][$yesterday])) +{ + $prevDatas['days'][$yesterday] = $newJson['days'][$yesterday]; +} + + +file_put_contents($filePath, json_encode($prevDatas, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT)); + +?>