From 0e18048954bdd21ea0005b87c36bcbb0bab4e137 Mon Sep 17 00:00:00 2001 From: Jannik Beyerstedt Date: Mon, 24 Jul 2017 01:34:53 +0200 Subject: [PATCH] [CODE] first hack --- config_times.json | 7 ++- functions.php | 152 ++++++++++++++++++++++++++++++++++++++++++++++ index.php | 127 ++++++++++++++++++++++++++++++++++++++ service.php | 14 +++++ style.css | 9 +++ 5 files changed, 308 insertions(+), 1 deletion(-) create mode 100644 functions.php create mode 100644 index.php create mode 100644 service.php create mode 100644 style.css diff --git a/config_times.json b/config_times.json index 994439e..6e6aec1 100644 --- a/config_times.json +++ b/config_times.json @@ -1,4 +1,8 @@ { + "position": { + "lat": "53.48", + "long": "9.86" + }, "time1": { "on": "16:00", "off": "23:00" @@ -10,5 +14,6 @@ "time3": { "on": "06:45", "off": "08:00" - } + }, + "instant-on": null } \ No newline at end of file diff --git a/functions.php b/functions.php new file mode 100644 index 0000000..071b9a1 --- /dev/null +++ b/functions.php @@ -0,0 +1,152 @@ +readTimes(); + } + + function __toString() { + return 's='.(int)$this->state.' t='.$this->changeTime; + } + + 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); + + 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) { + $stopTime += (24 * 60 * 60); + if (($stopTime - $currentTime) > (24 * 60 * 60)) { + $startTime -= (24 * 60 * 60); + $stopTime -= (24 * 60 * 60); + } + } + + if ($currentTime > $startTime && $currentTime < $stopTime) { + $this->state = true; + $this->changeTime = (int)(($stopTime - $currentTime) / 60); + return true; + } + } + + $this->state = false; + $this->changeTime = (int)(($stopTime - $currentTime) / 60); + return true; + + } else if ($currentTime > $sunrise && $currentTime < $sunset) { + // it's day: nothing else check + $this->state = false; + $this->changeTime = (int)(($sunset - $currentTime) / 60); + 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; +} +?> diff --git a/index.php b/index.php new file mode 100644 index 0000000..84bc50b --- /dev/null +++ b/index.php @@ -0,0 +1,127 @@ +update(); + +$error_string = ""; + +// route actions +if (isset($_GET['action'])) { + if ($_GET['action'] == 'enlighten') { + // set off time to now + 15 minutes + $light->times['instant-on'] = time() + (15*60); + if ($light->saveTimes() === true) { + header('Location: index.php?success=true'); + } else { + $error_string = "Error saving config file"; + } + + } else if ($_GET['action'] == 'update') { + if (isset($_POST) && !empty($_POST)) { + // parse form data + $parse_state = checkTimesInput($_POST); + if ($parse_state === true) { + // change values of $times arrays + $light->times['time1']['on'] = $_POST['time1-start']; + $light->times['time1']['off'] = $_POST['time1-stop']; + $light->times['time2']['on'] = $_POST['time2-start']; + $light->times['time2']['off'] = $_POST['time2-stop']; + $light->times['time3']['on'] = $_POST['time3-start']; + $light->times['time3']['off'] = $_POST['time3-stop']; + + // save new $times array + $light->saveTimes(); + + header('Location: index.php?success=true'); + + } else { + $error_string = $parse_state; + } + + } else { + $error_string = "ERROR: no form data received"; + } + + } else { + echo "invalid action\n"; + } +} + +?> + + + + + + + + Lightscontrol Control Interface + + + + + + + +

Außenbeleuchtung

+ + + + + +

Aktueller Status

+

+state) : ?> + Licht: AN
+ + Licht: AUS
+ + Zeit bis nächste Änderung: changeTime === 1) ? $light->changeTime." Minute" : $light->changeTime." Minuten" ?> +

+ Licht temporär an (15 Min) + +
weitere Informationen:
+

+ Sonnenaufgang: sunrise ?>
+ Sonnenuntergang: sunset ?>
+ Aktuelle Zeit:
+

+ +

Einstellungen

+
+
+ +
+ + +
+
+
+ +
+ + +
+
+
+ +
+ + +
+
+ +
+ + + + + + diff --git a/service.php b/service.php new file mode 100644 index 0000000..03a9f0d --- /dev/null +++ b/service.php @@ -0,0 +1,14 @@ +update(); + +echo $light; +?> diff --git a/style.css b/style.css new file mode 100644 index 0000000..d9bd003 --- /dev/null +++ b/style.css @@ -0,0 +1,9 @@ +/* STYLE for outdoor lights control -- server + * copyright: Jannik Beyerstedt | https://jannikbeyerstedt.de + * license: http://www.gnu.org/licenses/gpl-3.0.txt GPLv3 License + */ + +form div.form-group > div > input.form-control { + display: inline-block; + width: 49%; +}