Initial commit

This commit is contained in:
Thomas Klaehn 2021-04-27 12:26:38 +02:00 committed by Thomas Klaehn
commit abdb3fe00c
11 changed files with 585 additions and 0 deletions

4
.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
dist/
build/
__pycache__/
*.egg-info/

27
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,27 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: Flask",
"type": "python",
"request": "launch",
"module": "flask",
"env": {
"FLASK_APP": "hochbeet/app.py",
"FLASK_ENV": "development",
"FLASK_DEBUG": "0"
},
"args": [
"run",
"--no-debugger",
"--no-reload",
"--host=0.0.0.0",
"--port=8000"
],
"jinja": true
}
]
}

36
README.md Normal file
View File

@ -0,0 +1,36 @@
# Greenhouse
Greenhous control
## Installation
```shell
python setup.py install
```
## Usage
### Manual
Create wrapper script (e.g. wrapper.py):
```python
from greenhous import app
if __name__ == "__main__":
app.run()
```
Execute the wrapper script:
```shell
python3 wrapper.py
```
### gunicorn
```shell
gunicorn --bind 0.0.0.0:80 greenhouse:app
```

11
hochbeet.service Normal file
View File

@ -0,0 +1,11 @@
[Unit]
Description=Hochbeet service
After=multi-user.target
[Service]
Type=idle
ExecStart=gunicorn --bind 0.0.0.0:80 hochbeet:app
[Install]
WantedBy=multi-user.target

1
hochbeet/__init__.py Normal file
View File

@ -0,0 +1 @@
from .app import app

176
hochbeet/app.py Normal file
View File

@ -0,0 +1,176 @@
import os
import json
import datetime
import logging
import shutil
from threading import Thread
from time import sleep
from flask import Flask
from flask import render_template
from flask import make_response
from flask import request
from flask import jsonify
import RPi.GPIO as GPIO
log_level = logging.INFO
LOG_FILE = "/var/log/hochbeet.log"
LOG_FORMAT = "%(asctime)s %(levelname)s %(message)s"
# logging.basicConfig(format=LOG_FORMAT, level=log_level, filename=LOG_FILE)
logging.basicConfig(format=LOG_FORMAT, level=log_level)
log = logging.getLogger('hochbeet')
class GreenControl(Thread):
def __init__(self):
super(GreenControl, self).__init__()
self.__config_file = os.path.join(os.path.expanduser('~'), ".config/hochbeet/config.json")
self.__config = None
self.__pin = None
self.__run_condition = True
self.__water_state = False
self.__trigger_read_config = True
try:
with open(self.__config_file, "r") as handle:
self.__config = json.load(handle)
except FileNotFoundError:
# create default config
os.makedirs(os.path.dirname(self.__config_file), exist_ok=True)
shutil.copyfile("hochbeet/config/config.json", self.__config_file)
with open(self.__config_file, "r") as handle:
self.__config = json.load(handle)
def reload_config(self):
self.__trigger_read_config = True
def run(self):
while self.__run_condition:
if self.__trigger_read_config:
self.__trigger_read_config = False
with open(self.__config_file, "r") as f:
self.__config = json.load(f)
self.__pin = int(self.__config['water'][0]['pin'])
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(self.__pin, GPIO.OUT)
now = datetime.datetime.now()
# Check if auto-water is on
if self.__config['water'][0]['autostate']:
index = 0
if int(now.hour) >= 12:
index = 1
on_time_pattern = self.__config['water'][0]['times'][index]['on_time']
on_time_pattern = on_time_pattern.split(':')
on_time = now.replace(hour=int(on_time_pattern[0]), minute=int(on_time_pattern[1]), second=0, microsecond=0)
off_time_pattern = self.__config['water'][0]['times'][index]['off_time']
off_time_pattern = off_time_pattern.split(':')
off_time = now.replace(hour=int(off_time_pattern[0]), minute=int(off_time_pattern[1]), second=0, microsecond=0)
if now > on_time and now <= off_time and not self.__water_state:
GPIO.output(self.__pin, 1)
self.__water_state = True
log.info("Switch water on by time")
elif now > off_time and self.__water_state:
GPIO.output(self.__pin, 0)
self.__water_state = False
log.info("Switch water off by time")
sleep(1)
def state(self):
return self.__water_state
def get_auto_state(self):
state = False
if self.__config['water'][0]['autostate']:
state = self.__config['water'][0]['autostate']
return state
def set_auto_state(self, state):
self.__config['water'][0]['autostate'] = state
with open(self.__config_file, "w") as handle:
json.dump(self.__config, handle)
self.reload_config()
def get_times(self):
times = None
if self.__config['water'][0]['times']:
times = self.__config['water'][0]['times']
return times
def set_times(self, times):
self.__config['water'][0]['times'] = times
with open(self.__config_file, "w") as handle:
json.dump(self.__config, handle)
self.reload_config()
green_ctrl = GreenControl()
green_ctrl.start()
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/sample', methods=['GET'])
def get_sample():
global green_ctrl
sample = {}
sample["id"] = str(1)
sample["state"] = green_ctrl.state()
return make_response(jsonify(sample), 200)
@app.route('/sample', methods=['PATCH'])
def patch_sample():
global green_ctrl
record = json.loads(request.data)
if "water" in record:
if record["water"]:
log.info("Switch water on by button: %s", datetime.datetime.now())
else:
log.info("Switch water off by button: %s", datetime.datetime.now())
return make_response("", 204)
@app.route('/config', methods=['GET'])
def get_config():
global green_ctrl
config = {}
config["autostate"] = green_ctrl.get_auto_state()
config["times"] = green_ctrl.get_times()
return make_response(jsonify(config), 200)
@app.route('/config', methods=['PATCH'])
def patch_config():
global green_ctrl
record = json.loads(request.data)
if "id" in record and record['id'] == '1':
if "autostate" in record:
green_ctrl.set_auto_state(record['autostate'])
if "times" in record:
green_ctrl.set_times(record['times'])
config = {}
config["autostate"] = green_ctrl.get_auto_state()
config["times"] = green_ctrl.get_times()
return make_response(jsonify(config), 200)
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=8000)

View File

@ -0,0 +1,20 @@
{
"water": [
{
"id": "1",
"autostate": false,
"pin": "22",
"times": [
{
"on_time": "7:00",
"off_time": "7:20"
},
{
"on_time": "19:00",
"off_time": "19:20"
}
]
}
]
}

View File

@ -0,0 +1,73 @@
html, body {
font-size: 22px !important;
font-family: arial, verdana, helvetica, sans-serif;
color: #b6b6b6;
background: #282929;
}
h1, h2, h3 {text-align: center;}
p {text-align: center;}
div {text-align: center;}
.center {
margin-left: auto;
margin-right: auto;
text-align: center;
}
input {
border: 1px #999999 solid;
background-color: #f6f6f6;
padding: 2px 4px 2px 4px;
margin: 0 0 0.5rem 0;
font-size: 1.0rem;
border-radius: 8px;
}
input[type="submit" i] {
color: #b6b6b6;
background-color: #282929;
padding: 10px;
margin: 10px 0;
border-color: #b6b6b6;
border-width: 3px;
border-radius: 18px;
font-size: 20px;
}
input[type="text"] {
border-radius: 8px;
}
td {
padding: 8px;
}
.table_left {
text-align: right;
}
footer {
position: fixed;
/* left: 50%; */
bottom: 20px;
/* transform: translate(-50%, -50%); */
margin: 0 auto;
}
a {
outline: none;
color: #b6b6b6;
text-decoration-line: none;
}
select {
color: #b6b6b6;
background-color: #282929;
padding: 10px;
margin: 10px 0;
border-color: #b6b6b6;
border-width: 3px;
border-radius: 16px;
font-size: 20px;
}

View File

@ -0,0 +1,113 @@
var on_switch_water = function() {
var state = true;
if(document.getElementById("water_switch").value == "ausschalten") {
state = false;
}
var json_str = JSON.stringify({"id": "1", "waterstate": state});
patch_sample(json_str);
}
var on_switch_auto_state = function() {
var state = true;
if(document.getElementById("auto_switch").value == "ausschalten") {
state = false;
}
var json_str = JSON.stringify({"id": "1", "autostate": state});
patch_config(json_str);
}
var on_change_config = function() {
var on_time_one = document.getElementById("water_on_one").value;
var off_time_one = document.getElementById("water_off_one").value;
var on_time_two = document.getElementById("water_on_two").value;
var off_time_two = document.getElementById("water_off_two").value;
var json_str = JSON.stringify({"id": "1", "times": [{"on_time": on_time_one, "off_time": off_time_one}, {"on_time": on_time_two, "off_time": off_time_two}]});
patch_config(json_str);
}
var http_patch_config = new XMLHttpRequest();
http_patch_config.onreadystatechange = function () {
if (http_patch_config.readyState === 4) {
var status = http_patch_config.status;
if (status === 0 || (status >= 200 && status < 400)) {
// The request has been completed successfully
parse_config(http_patch_config.responseText);
}
} else {
// request error
}
}
var patch_config = function(config) {
http_patch_config.abort();
http_patch_config.open("PATCH", "/config");
http_patch_config.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
http_patch_config.send(config);
}
var patch_http = new XMLHttpRequest();
var patch_sample = function(sample) {
patch_http.abort();
patch_http.open("PATCH", "/sample");
patch_http.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
patch_http.send(sample);
}
var parse_config = function (event) {
var config = JSON.parse(event)
var output = "einschalten"
var visibility = 'hidden'
if(config.autostate) {
output = "ausschalten"
visibility = "visible"
document.getElementById("water_on_one").value = config.times[0].on_time;
document.getElementById("water_off_one").value = config.times[0].off_time;
document.getElementById("water_on_two").value = config.times[1].on_time;
document.getElementById("water_off_two").value = config.times[1].off_time;
}
document.getElementById("auto_switch").value = output;
document.getElementById("water_times").style.visibility = visibility
}
var get_sample = function (event) {
var sample = JSON.parse(event)
output = "einschalten"
if(sample.state) {
output = "ausschalten"
}
document.getElementById("water_switch").value = output;
}
var http = new XMLHttpRequest();
http.onreadystatechange = function () {
if (http.readyState === 4) {
var status = http.status;
if (status === 0 || (status >= 200 && status < 400)) {
// The request has been completed successfully
get_sample(http.responseText);
setTimeout(function () {
http.open("GET", 'sample');
http.send();
}, 500);
}
} else {
// request error
}
}
http.open("GET", "sample");
http.send();
var http_get_config = new XMLHttpRequest();
http_get_config.onreadystatechange = function () {
if (http_get_config.readyState === 4) {
var status = http_get_config.status;
if (status === 0 || (status >= 200 && status < 400)) {
// The request has been completed successfully
parse_config(http_get_config.responseText);
}
} else {
// request error
}
}
http_get_config.open("GET", "config");
http_get_config.send();

View File

@ -0,0 +1,71 @@
<!DOCTYPE html>
<html>
<head>
<title>Hochbeet</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="/static/css/style.css" rel="stylesheet">
<script src="/static/scripts/index.js"></script>
</head>
<body>
<h1>Hochbeet</h1>
<table class="center">
<tr>
<td class="left">Bewässerung </td>
<td class="input">
<input id="water_switch" type="submit" value="" onclick="on_switch_water()"></input>
</td>
</tr>
<tr>
<td class="left">Zeigesteuerte Bewässerung </td>
<td class="input">
<input id="auto_switch" type="submit" value="einschalten" onclick="on_switch_auto_state()"></input>
</td>
</tr>
</table>
<div id="water_times" style="visibility:hidden;">
<table class="center">
<tr>
<td>Vormittag</td>
<td>
<table>
<tr>
<td>Einschaltzeit</td>
<td class="input">
<input type="text" id="water_on_one" style="width: 70px;" onchange="on_change_config()"></input>
</td>
</tr>
<tr>
<td>Ausschaltzeit</td>
<td class="input">
<input type="text" id="water_off_one" style="width: 70px;" onchange="on_change_config()"></input>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>Nachmittag</td>
<td>
<table>
<tr>
<td>Einschaltzeit</td>
<td class="input">
<input type="text" id="water_on_two" style="width: 70px;" onchange="on_change_config()"></input>
</td>
</tr>
<tr>
<td>Ausschaltzeit</td>
<td class="input">
<input type="text" id="water_off_two" style="width: 70px;" onchange="on_change_config()"></input>
</td>
</tr>
</table>
</td>
</tr>
</table>
</div>
</body>
</html>

53
setup.py Executable file
View File

@ -0,0 +1,53 @@
import os
import shutil
import stat
from setuptools import setup
from setuptools.command.install import install
NAME = 'Hochbeet'
VERSION = '1'
AUTHOR = 'Thomas Klaehn'
EMAIL = 'tkl@blackfinn.de'
PACKAGES = ['hochbeet']
REQUIRES = ['RPi.GPIO']
CONFIG_FILE = 'config.json'
PACKAGE_DATA = {
'hochbeet': [
'templates/*',
'static/css/*',
'static/scripts/*',
'config/config.json'
]
}
SERVICEDIR = "/lib/systemd/system"
DAEMON_START_SCRIPT = os.path.join(SERVICEDIR, 'hochbeet.service')
LOGFILE = "/var/log/hochbeet.log"
class Install(install):
def run(self):
install.run(self)
os.makedirs(SERVICEDIR, exist_ok=True)
shutil.copyfile('hochbeet.service', os.path.join(SERVICEDIR, DAEMON_START_SCRIPT))
os.chmod(DAEMON_START_SCRIPT, stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IROTH)
try:
open(LOGFILE, 'r')
except FileNotFoundError:
os.makedirs(os.path.dirname(LOGFILE), exist_ok=True)
open(LOGFILE, 'x')
os.chmod(LOGFILE,
stat.S_IRUSR |
stat.S_IWUSR |
stat.S_IRGRP |
stat.S_IWGRP |
stat.S_IROTH |
stat.S_IWOTH)
setup(name=NAME, version=VERSION, long_description=__doc__, author=AUTHOR, author_email=EMAIL,
packages=PACKAGES, include_package_data=True, package_data=PACKAGE_DATA, zip_safe=False,
install_requires=REQUIRES, cmdclass={'install': Install})