[CODE] first hack

This commit is contained in:
Jannik Beyerstedt 2017-07-24 01:34:53 +02:00
parent 81bb48099c
commit 0e18048954
5 changed files with 308 additions and 1 deletions

View File

@ -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
}

152
functions.php Normal file
View File

@ -0,0 +1,152 @@
<?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;
public $times = null;
public $sunrise, $sunset;
function __construct() {
return $this->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;
}
?>

127
index.php Normal file
View File

@ -0,0 +1,127 @@
<?php
// -------------------------------------------
// outdoor lights control -- server
// copyright: Jannik Beyerstedt | https://jannikbeyerstedt.de
// license: http://www.gnu.org/licenses/gpl-3.0.txt GPLv3 License
// -------------------------------------------
include "functions.php";
$light = new LightStatus();
$light->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";
}
}
?>
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<meta name="format-detection" content="telephone=no">
<title>Lightscontrol Control Interface</title>
<meta name="robots" content="noindex, nofollow">
<link rel="stylesheet" href="bootstrap/css/bootstrap.min.css">
<link rel="stylesheet" href="style.css">
</head>
<body class="container-fluid">
<h1>Außenbeleuchtung</h1>
<?php if ($error_string != '') : ?>
<div class="alert alert-danger" role="alert"><?php echo $error_string ?></div>
<?php endif; ?>
<h2>Aktueller Status</h2>
<p>
<?php if ($light->state) : ?>
Licht: <span class="label label-success">AN</span><br>
<?php else : ?>
Licht: <span class="label label-default">AUS</span><br>
<?php endif; ?>
Zeit bis nächste Änderung: <span class="label label-default"><?php echo ($light->changeTime === 1) ? $light->changeTime." Minute" : $light->changeTime." Minuten" ?></span>
</p>
<a class="btn btn-success" href="index.php?action=enlighten">Licht temporär an (15 Min)</a>
<h5>weitere Informationen:</h5>
<p>
Sonnenaufgang: <?php echo $light->sunrise ?><br>
Sonnenuntergang: <?php echo $light->sunset ?><br>
Aktuelle Zeit: <?php echo date("Y-m-d H:i",time()) ?><br>
</p>
<h2>Einstellungen</h2>
<form class="form-horizontal" action="index.php?action=update" method="post">
<div class="form-group">
<label for="time1-start" class="col-xs-3 col-sm-2 control-label">Zeit 1</label>
<div class="col-xs-9 col-sm-10">
<input type="time" class="form-control" name="time1-start" value="<?php echo $light->times['time1']['on'] ?>">
<input type="time" class="form-control" name="time1-stop" value="<?php echo $light->times['time1']['off'] ?>">
</div>
</div>
<div class="form-group">
<label for="time2-start" class="col-xs-3 col-sm-2 control-label">Zeit 2</label>
<div class="col-xs-9 col-sm-10">
<input type="time" class="form-control" name="time2-start" value="<?php echo $light->times['time2']['on'] ?>">
<input type="time" class="form-control" name="time2-stop" value="<?php echo $light->times['time2']['off'] ?>">
</div>
</div>
<div class="form-group">
<label for="time3-start" class="col-xs-3 col-sm-2 control-label">Zeit 3</label>
<div class="col-xs-9 col-sm-10">
<input type="time" class="form-control" name="time3-start" value="<?php echo $light->times['time3']['on'] ?>">
<input type="time" class="form-control" name="time3-stop" value="<?php echo $light->times['time3']['off'] ?>">
</div>
</div>
<button type="submit" class="btn btn-warning">Zeiten ändern</button>
</form>
</body>
</html>
<script src="bootstrap/jquery-2.2.4.min.js"></script>
<script src="bootstrap/js/bootstrap.min.js"></script>

14
service.php Normal file
View File

@ -0,0 +1,14 @@
<?php
// -------------------------------------------
// outdoor lights control -- server
// copyright: Jannik Beyerstedt | https://jannikbeyerstedt.de
// license: http://www.gnu.org/licenses/gpl-3.0.txt GPLv3 License
// -------------------------------------------
include "functions.php";
$light = new LightStatus();
$light->update();
echo $light;
?>

9
style.css Normal file
View File

@ -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%;
}