[CODE] comments, add public variable for day/night status

This commit is contained in:
Jannik Beyerstedt 2017-08-23 14:02:31 +02:00
parent 596b256e64
commit d29232cb3c

View file

@ -8,26 +8,29 @@
$config_filename = 'config_times.json'; $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]))/'; $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 = 96; /* Civilian Twilight */
//$sun_zenith = 90+50/60; /* sunrise/ sunset */ //$sun_zenith = 90+50/60; /* "true" sunrise/ sunset */
$timezone = "Europe/Berlin"; $timezone = "Europe/Berlin";
class LightStatus { class LightStatus {
public $state = true; public $state = true; /* current state: lights on/off */
public $changeTime = 1; public $changeTime = 1; /* minutes until next state change */
public $waitTime = 0; 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() { function __construct() {
return $this->readTimes(); return $this->readTimes();
} }
/* return string for the service API */
function __toString() { function __toString() {
return 's='.(int)$this->state.' t='.$this->waitTime; return 's='.(int)$this->state.' t='.$this->waitTime;
} }
/* re-calculate values */
public function update() { public function update() {
global $sun_zenith; global $sun_zenith;
global $timezone; global $timezone;
@ -63,6 +66,8 @@ class LightStatus {
// first check, if it's at daylight // first check, if it's at daylight
$currentTime = time(); $currentTime = time();
if ($currentTime < $sunrise || $currentTime > $sunset) { if ($currentTime < $sunrise || $currentTime > $sunset) {
$this->isNight = true;
// it's night: check, if we are in one of the time intervals // it's night: check, if we are in one of the time intervals
for ($i = 1; $i <= 3; $i++) { for ($i = 1; $i <= 3; $i++) {
$timeKey = 'time'.$i; $timeKey = 'time'.$i;
@ -101,6 +106,8 @@ class LightStatus {
return true; return true;
} else if ($currentTime > $sunrise && $currentTime < $sunset) { } else if ($currentTime > $sunrise && $currentTime < $sunset) {
$this->isNight = false;
// it's day: nothing else check // it's day: nothing else check
$this->state = false; $this->state = false;
$this->changeTime = (int)(($sunset - $currentTime) / 60); $this->changeTime = (int)(($sunset - $currentTime) / 60);
@ -113,6 +120,7 @@ class LightStatus {
} }
} }
/* save modified $times array to config file */
public function saveTimes() { public function saveTimes() {
global $config_filename; global $config_filename;