From d29232cb3cd0814e5427bbf8471fbd48b3b69b14 Mon Sep 17 00:00:00 2001 From: Jannik Beyerstedt Date: Wed, 23 Aug 2017 14:02:31 +0200 Subject: [PATCH] [CODE] comments, add public variable for day/night status --- functions.php | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/functions.php b/functions.php index e562f4c..6d5da75 100644 --- a/functions.php +++ b/functions.php @@ -8,26 +8,29 @@ $config_filename = 'config_times.json'; $timeRegex = '/^((2[0-3]|1[0-9]|0[0-9]|[^0-9][0-9]):([0-5][0-9]|[0-9]))/'; $sun_zenith = 96; /* Civilian Twilight */ -//$sun_zenith = 90+50/60; /* sunrise/ sunset */ +//$sun_zenith = 90+50/60; /* "true" sunrise/ sunset */ $timezone = "Europe/Berlin"; class LightStatus { - public $state = true; - public $changeTime = 1; - public $waitTime = 0; + public $state = true; /* current state: lights on/off */ + public $changeTime = 1; /* minutes until next state change */ + public $waitTime = 0; /* minutes the client should wait until next request */ - public $times = null; + public $times = null; /* config array */ - public $sunrise, $sunset; + public $sunrise, $sunset; /* time string hh:mm */ + public $isNight; /* bool, true if it's at night */ function __construct() { return $this->readTimes(); } + /* return string for the service API */ function __toString() { return 's='.(int)$this->state.' t='.$this->waitTime; } + /* re-calculate values */ public function update() { global $sun_zenith; global $timezone; @@ -63,6 +66,8 @@ class LightStatus { // first check, if it's at daylight $currentTime = time(); if ($currentTime < $sunrise || $currentTime > $sunset) { + $this->isNight = true; + // it's night: check, if we are in one of the time intervals for ($i = 1; $i <= 3; $i++) { $timeKey = 'time'.$i; @@ -101,6 +106,8 @@ class LightStatus { return true; } else if ($currentTime > $sunrise && $currentTime < $sunset) { + $this->isNight = false; + // it's day: nothing else check $this->state = false; $this->changeTime = (int)(($sunset - $currentTime) / 60); @@ -113,6 +120,7 @@ class LightStatus { } } + /* save modified $times array to config file */ public function saveTimes() { global $config_filename;