2017-07-24 01:34:53 +02:00
< ? php
// -------------------------------------------
// outdoor lights control -- server
// copyright: Jannik Beyerstedt | https://jannikbeyerstedt.de
// license: http://www.gnu.org/licenses/gpl-3.0.txt GPLv3 License
// -------------------------------------------
$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 */
$timezone = " Europe/Berlin " ;
class LightStatus {
public $state = true ;
public $changeTime = 1 ;
2017-07-24 12:24:19 +02:00
public $waitTime = 0 ;
2017-07-24 01:34:53 +02:00
public $times = null ;
public $sunrise , $sunset ;
function __construct () {
return $this -> readTimes ();
}
function __toString () {
2017-07-24 12:24:19 +02:00
return 's=' . ( int ) $this -> state . ' t=' . $this -> waitTime ;
2017-07-24 01:34:53 +02:00
}
public function update () {
global $sun_zenith ;
global $timezone ;
// calculate sunset and sunrise times
date_default_timezone_set ( $timezone );
$gmtOffset = date ( " Z " ) / ( 60 * 60 );
$this -> sunrise = date_sunrise ( time (), SUNFUNCS_RET_STRING , $this -> times [ 'position' ][ 'lat' ], $this -> times [ 'position' ][ 'long' ], $sun_zenith , $gmtOffset );
$this -> sunset = date_sunset ( time (), SUNFUNCS_RET_STRING , $this -> times [ 'position' ][ 'lat' ], $this -> times [ 'position' ][ 'long' ], $sun_zenith , $gmtOffset );
$sunrise = date_sunrise ( time (), SUNFUNCS_RET_TIMESTAMP , $this -> times [ 'position' ][ 'lat' ], $this -> times [ 'position' ][ 'long' ], $sun_zenith , $gmtOffset );
$sunset = date_sunset ( time (), SUNFUNCS_RET_TIMESTAMP , $this -> times [ 'position' ][ 'lat' ], $this -> times [ 'position' ][ 'long' ], $sun_zenith , $gmtOffset );
// -- first check, if an instant-on time is set --
if ( $this -> times [ 'instant-on' ] != null ) {
if ( $this -> times [ 'instant-on' ] > time ()) {
$this -> state = true ;
$this -> changeTime = ( int )(( $this -> times [ 'instant-on' ] - time ()) / 60 );
2017-07-24 12:24:19 +02:00
$this -> waitTime = $this -> changeTime ;
2017-07-24 01:34:53 +02:00
return true ;
} else {
// reset instant-on
$this -> state = false ;
$this -> times [ 'instant-on' ] = null ;
$this -> saveTimes ();
}
}
// -- otherwise check, if the ruleset requires the lights on --
// first check, if it's at daylight
$currentTime = time ();
if ( $currentTime < $sunrise || $currentTime > $sunset ) {
// it's night: check, if we are in one of the time intervals
for ( $i = 1 ; $i <= 3 ; $i ++ ) {
$timeKey = 'time' . $i ;
$startTime = strtotime ( $this -> times [ $timeKey ][ 'on' ]);
$stopTime = strtotime ( $this -> times [ $timeKey ][ 'off' ]);
if ( $stopTime < $startTime ) {
2017-08-02 22:58:44 +02:00
$stopTime += ( 24 * 60 * 60 );
if (( $stopTime - $currentTime ) > ( 24 * 60 * 60 )) {
$startTime -= ( 24 * 60 * 60 );
$stopTime -= ( 24 * 60 * 60 );
2017-07-24 01:34:53 +02:00
}
}
if ( $currentTime > $startTime && $currentTime < $stopTime ) {
$this -> state = true ;
$this -> changeTime = ( int )(( $stopTime - $currentTime ) / 60 );
2017-07-24 12:24:19 +02:00
$this -> waitTime = 1 ;
2017-07-24 01:34:53 +02:00
return true ;
2017-08-02 22:58:44 +02:00
} elseif ( $currentTime < $startTime || ( $startTime + ( 24 * 60 * 60 ) - $currentTime ) < ( 12 * 60 * 60 ) ) {
// right before the next time interval
$this -> state = false ;
if ( $currentTime < $startTime ) {
$this -> changeTime = ( int )( ( $startTime - $currentTime ) / 60 );
} else {
$this -> changeTime = ( int )( (( $startTime + ( 24 * 60 * 60 )) - $currentTime ) / 60 );
}
$this -> waitTime = 1 ;
return true ;
2017-07-24 01:34:53 +02:00
}
}
$this -> state = false ;
$this -> changeTime = ( int )(( $stopTime - $currentTime ) / 60 );
2017-07-24 12:24:19 +02:00
$this -> waitTime = 1 ;
2017-07-24 01:34:53 +02:00
return true ;
} else if ( $currentTime > $sunrise && $currentTime < $sunset ) {
// it's day: nothing else check
$this -> state = false ;
$this -> changeTime = ( int )(( $sunset - $currentTime ) / 60 );
2017-07-24 12:24:19 +02:00
$this -> waitTime = $this -> changeTime ;
2017-07-24 01:34:53 +02:00
return true ;
} else {
echo " ERROR: invalid time of day. " ;
return 'ERROR' ;
}
}
public function saveTimes () {
global $config_filename ;
if ( empty ( $this -> times )) {
return 'ERROR' ;
} else {
$json_string = json_encode ( $this -> times , JSON_PRETTY_PRINT );
file_put_contents ( $config_filename , $json_string );
return true ;
}
}
private function readTimes () {
global $config_filename ;
$file_content = file_get_contents ( $config_filename );
if ( $file_content == FALSE ) {
return 'ERROR' ;
} else {
$this -> times = json_decode ( $file_content , true );
return true ;
}
}
}
function checkTimesInput ( $times ) {
global $timeRegex ;
if ( ! isset ( $_POST [ 'time1-start' ]) || ! preg_match ( $timeRegex , $_POST [ 'time1-start' ])) {
return " time1-start not valid " ;
}
if ( ! isset ( $_POST [ 'time1-stop' ]) || ! preg_match ( $timeRegex , $_POST [ 'time1-stop' ])) {
return " time1-stop not valid " ;
}
if ( ! isset ( $_POST [ 'time2-start' ]) || ! preg_match ( $timeRegex , $_POST [ 'time2-start' ])) {
return " time2-start not valid " ;
}
if ( ! isset ( $_POST [ 'time2-stop' ]) || ! preg_match ( $timeRegex , $_POST [ 'time2-stop' ])) {
return " time2-stop not valid " ;
}
if ( ! isset ( $_POST [ 'time3-start' ]) || ! preg_match ( $timeRegex , $_POST [ 'time3-start' ])) {
return " time3-start not valid " ;
}
if ( ! isset ( $_POST [ 'time3-stop' ]) || ! preg_match ( $timeRegex , $_POST [ 'time3-stop' ])) {
return " time3-stop not valid " ;
}
return true ;
}
?>