Compare commits

...

No commits in common. "508d6aad50b60bfe9290267567a349a58e1e7d12" and "ae9633462e96416c3971e60c57872264883fb8d4" have entirely different histories.

2 changed files with 45 additions and 45 deletions

View File

@ -20,12 +20,14 @@ log_level = logging.INFO
LOG_FILE = "/var/log/hochbeet.log" LOG_FILE = "/var/log/hochbeet.log"
LOG_FORMAT = "%(asctime)s %(levelname)s %(message)s" 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, filename=LOG_FILE)
#logging.basicConfig(format=LOG_FORMAT, level=log_level) logging.basicConfig(format=LOG_FORMAT, level=log_level)
log = logging.getLogger('hochbeet') log = logging.getLogger('hochbeet')
class GreenControl(Thread): class GreenControl(Thread):
"""Green Control"""
def __init__(self): def __init__(self):
super(GreenControl, self).__init__() super(GreenControl, self).__init__()
self.__config_file = os.path.join(os.path.expanduser('~'), ".config/hochbeet/config.json") self.__config_file = os.path.join(os.path.expanduser('~'), ".config/hochbeet/config.json")
@ -41,14 +43,11 @@ class GreenControl(Thread):
except FileNotFoundError: except FileNotFoundError:
# create default config # create default config
os.makedirs(os.path.dirname(self.__config_file), exist_ok=True) os.makedirs(os.path.dirname(self.__config_file), exist_ok=True)
shutil.copyfile("hochbeet/config/config.json", self.__config_file) pwd = os.getcwd()
shutil.copyfile("config/config.json", self.__config_file)
with open(self.__config_file, "r") as handle: with open(self.__config_file, "r") as handle:
self.__config = json.load(handle) self.__config = json.load(handle)
def reload_config(self):
self.__trigger_read_config = True
def run(self): def run(self):
while self.__run_condition: while self.__run_condition:
if self.__trigger_read_config: if self.__trigger_read_config:
@ -60,20 +59,24 @@ class GreenControl(Thread):
GPIO.setwarnings(False) GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM) GPIO.setmode(GPIO.BCM)
GPIO.setup(self.__pin, GPIO.OUT) GPIO.setup(self.__pin, GPIO.OUT)
now = datetime.datetime.now() now = datetime.datetime.now()
# Check if auto-water is on
if self.__config['water'][0]['autostate']: if self.__config['water'][0]['autostate']:
index = 0 idx = 0
if int(now.hour) >= 12: if int(now.hour) >= 12:
index = 1 idx = 1
on_time_pattern = self.__config['water'][0]['times'][index]['on_time'] on_time_pattern = self.__config['water'][0]['times'][idx]['on_time']
on_time_pattern = on_time_pattern.split(':') 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) on_time = now.replace(hour=int(on_time_pattern[0]),
off_time_pattern = self.__config['water'][0]['times'][index]['off_time'] minute=int(on_time_pattern[1]),
second=0,
microsecond=0)
off_time_pattern = self.__config['water'][0]['times'][idx]['off_time']
off_time_pattern = off_time_pattern.split(':') 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) 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: if now > on_time and now <= off_time and not self.__water_state:
GPIO.output(self.__pin, 1) GPIO.output(self.__pin, 1)
@ -85,61 +88,60 @@ class GreenControl(Thread):
log.info("Switch water off by time") log.info("Switch water off by time")
sleep(1) sleep(1)
def state(self): def state(self):
"""Return water state"""
return self.__water_state return self.__water_state
def set_state(self, state: bool):
if state:
GPIO.output(self.__pin, 1)
self.__water_state = True
else:
GPIO.output(self.__pin, 0)
self.__water_state = False
def get_auto_state(self): def get_auto_state(self):
"""Return auto state"""
state = False state = False
if self.__config['water'][0]['autostate']: if self.__config['water'][0]['autostate']:
state = self.__config['water'][0]['autostate'] state = self.__config['water'][0]['autostate']
return state return state
def set_auto_state(self, state): def set_auto_state(self, state):
"""Set the auto state"""
self.__config['water'][0]['autostate'] = state self.__config['water'][0]['autostate'] = state
with open(self.__config_file, "w") as handle: with open(self.__config_file, "w") as handle:
json.dump(self.__config, handle) json.dump(self.__config, handle)
self.reload_config() self.__trigger_read_config = True
def get_times(self): def get_times(self):
"""Return the times for auto state"""
times = None times = None
if self.__config['water'][0]['times']: if self.__config['water'][0]['times']:
times = self.__config['water'][0]['times'] times = self.__config['water'][0]['times']
return times return times
def set_times(self, times): def set_times(self, times):
"""Set the times for auto state"""
self.__config['water'][0]['times'] = times self.__config['water'][0]['times'] = times
with open(self.__config_file, "w") as handle: with open(self.__config_file, "w") as handle:
json.dump(self.__config, handle) json.dump(self.__config, handle)
self.reload_config() self.__trigger_read_config = True
def switch_water(self, state):
"""Switch the water on/off"""
if state:
GPIO.output(self.__pin, 1)
else:
GPIO.output(self.__pin, 0)
green_ctrl = GreenControl() green_ctrl = GreenControl()
green_ctrl.start() green_ctrl.start()
app = Flask(__name__) app = Flask(__name__)
@app.route('/') @app.route('/')
def index(): def index():
"""Handle index.html"""
return render_template('index.html') return render_template('index.html')
@app.route('/sample', methods=['GET']) @app.route('/sample', methods=['GET'])
def get_sample(): def get_sample():
"""Handle GET request for /sample"""
global green_ctrl global green_ctrl
sample = {} sample = {}
@ -147,32 +149,32 @@ def get_sample():
sample["state"] = green_ctrl.state() sample["state"] = green_ctrl.state()
return make_response(jsonify(sample), 200) return make_response(jsonify(sample), 200)
@app.route('/sample', methods=['PATCH']) @app.route('/sample', methods=['PATCH'])
def patch_sample(): def patch_sample():
"""Handle PATCH request for sample"""
global green_ctrl global green_ctrl
record = json.loads(request.data) record = json.loads(request.data)
if "waterstate" in record: if "waterstate" in record:
if record["waterstate"]: if record["waterstate"]:
green_ctrl.set_state(True)
log.info("Switch water on by button: %s", datetime.datetime.now()) log.info("Switch water on by button: %s", datetime.datetime.now())
green_ctrl.switch_water(True)
else: else:
green_ctrl.set_state(False)
log.info("Switch water off by button: %s", datetime.datetime.now()) log.info("Switch water off by button: %s", datetime.datetime.now())
green_ctrl.switch_water(False)
return make_response("", 204) return make_response("", 204)
@app.route('/config', methods=['GET']) @app.route('/config', methods=['GET'])
def get_config(): def get_config():
"""Handle GER request for /config"""
global green_ctrl global green_ctrl
config = {} config = {}
config["autostate"] = green_ctrl.get_auto_state() config["autostate"] = green_ctrl.get_auto_state()
config["times"] = green_ctrl.get_times() config["times"] = green_ctrl.get_times()
return make_response(jsonify(config), 200) return make_response(jsonify(config), 200)
@app.route('/config', methods=['PATCH']) @app.route('/config', methods=['PATCH'])
def patch_config(): def patch_config():
"""Handle PATCH request for sample"""
global green_ctrl global green_ctrl
record = json.loads(request.data) record = json.loads(request.data)
if "id" in record and record['id'] == '1': if "id" in record and record['id'] == '1':
@ -185,6 +187,5 @@ def patch_config():
config["times"] = green_ctrl.get_times() config["times"] = green_ctrl.get_times()
return make_response(jsonify(config), 200) return make_response(jsonify(config), 200)
if __name__ == '__main__': if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=8000) app.run(debug=True, host='0.0.0.0', port=8000)

View File

@ -6,12 +6,11 @@ from setuptools import setup
from setuptools.command.install import install from setuptools.command.install import install
NAME = 'Hochbeet' NAME = 'Hochbeet'
VERSION = '2' VERSION = '1'
AUTHOR = 'Thomas Klaehn' AUTHOR = 'Thomas Klaehn'
EMAIL = 'tkl@blackfinn.de' EMAIL = 'tkl@blackfinn.de'
PACKAGES = ['hochbeet'] PACKAGES = ['hochbeet']
REQUIRES = ['RPi.GPIO'] REQUIRES = ['Flask', 'gunicorn', 'RPi.GPIO']
CONFIG_FILE = 'config.json' CONFIG_FILE = 'config.json'
PACKAGE_DATA = { PACKAGE_DATA = {
'hochbeet': [ 'hochbeet': [
@ -23,8 +22,8 @@ PACKAGE_DATA = {
} }
SERVICEDIR = "/lib/systemd/system" SERVICEDIR = "/lib/systemd/system"
DAEMON_START_SCRIPT = os.path.join(SERVICEDIR, 'hochbeet.service') START_SCRIPT = 'hochbeet.service'
DAEMON_START_SCRIPT = os.path.join(SERVICEDIR, START_SCRIPT)
LOGFILE = "/var/log/hochbeet.log" LOGFILE = "/var/log/hochbeet.log"
@ -32,7 +31,7 @@ class Install(install):
def run(self): def run(self):
install.run(self) install.run(self)
os.makedirs(SERVICEDIR, exist_ok=True) os.makedirs(SERVICEDIR, exist_ok=True)
shutil.copyfile('hochbeet.service', os.path.join(SERVICEDIR, DAEMON_START_SCRIPT)) shutil.copyfile(START_SCRIPT, DAEMON_START_SCRIPT)
os.chmod(DAEMON_START_SCRIPT, stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IROTH) os.chmod(DAEMON_START_SCRIPT, stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IROTH)
try: try: