Compare commits
No commits in common. "c157fffcc30e8d98215a7b985c2bde61679e8764" and "abdb3fe00c0d36290c47cc5570ef9407df6f6ace" have entirely different histories.
c157fffcc3
...
abdb3fe00c
@ -41,11 +41,14 @@ class GreenControl(Thread):
|
||||
except FileNotFoundError:
|
||||
# create default config
|
||||
os.makedirs(os.path.dirname(self.__config_file), exist_ok=True)
|
||||
pwd = os.getcwd()
|
||||
shutil.copyfile("config/config.json", self.__config_file)
|
||||
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:
|
||||
@ -57,24 +60,20 @@ class GreenControl(Thread):
|
||||
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']:
|
||||
idx = 0
|
||||
index = 0
|
||||
if int(now.hour) >= 12:
|
||||
idx = 1
|
||||
on_time_pattern = self.__config['water'][0]['times'][idx]['on_time']
|
||||
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'][idx]['off_time']
|
||||
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)
|
||||
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)
|
||||
@ -99,7 +98,8 @@ class GreenControl(Thread):
|
||||
self.__config['water'][0]['autostate'] = state
|
||||
with open(self.__config_file, "w") as handle:
|
||||
json.dump(self.__config, handle)
|
||||
self.__trigger_read_config = True
|
||||
self.reload_config()
|
||||
|
||||
|
||||
def get_times(self):
|
||||
times = None
|
||||
@ -107,22 +107,17 @@ class GreenControl(Thread):
|
||||
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.__trigger_read_config = True
|
||||
|
||||
def switch_water(self, state):
|
||||
if state:
|
||||
GPIO.output(self.__pin, 1)
|
||||
else:
|
||||
GPIO.output(self.__pin, 0)
|
||||
|
||||
self.reload_config()
|
||||
|
||||
|
||||
green_ctrl = GreenControl()
|
||||
green_ctrl.start()
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
|
||||
@ -130,6 +125,7 @@ app = Flask(__name__)
|
||||
def index():
|
||||
return render_template('index.html')
|
||||
|
||||
|
||||
@app.route('/sample', methods=['GET'])
|
||||
def get_sample():
|
||||
global green_ctrl
|
||||
@ -139,17 +135,19 @@ def get_sample():
|
||||
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 "waterstate" in record:
|
||||
if record["waterstate"]:
|
||||
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
|
||||
@ -158,6 +156,7 @@ def get_config():
|
||||
config["times"] = green_ctrl.get_times()
|
||||
return make_response(jsonify(config), 200)
|
||||
|
||||
|
||||
@app.route('/config', methods=['PATCH'])
|
||||
def patch_config():
|
||||
global green_ctrl
|
||||
@ -172,5 +171,6 @@ def patch_config():
|
||||
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)
|
||||
|
9
setup.py
9
setup.py
@ -10,7 +10,8 @@ VERSION = '1'
|
||||
AUTHOR = 'Thomas Klaehn'
|
||||
EMAIL = 'tkl@blackfinn.de'
|
||||
PACKAGES = ['hochbeet']
|
||||
REQUIRES = ['Flask', 'gunicorn', 'RPi.GPIO']
|
||||
REQUIRES = ['RPi.GPIO']
|
||||
|
||||
CONFIG_FILE = 'config.json'
|
||||
PACKAGE_DATA = {
|
||||
'hochbeet': [
|
||||
@ -22,8 +23,8 @@ PACKAGE_DATA = {
|
||||
}
|
||||
|
||||
SERVICEDIR = "/lib/systemd/system"
|
||||
START_SCRIPT = 'hochbeet.service'
|
||||
DAEMON_START_SCRIPT = os.path.join(SERVICEDIR, START_SCRIPT)
|
||||
DAEMON_START_SCRIPT = os.path.join(SERVICEDIR, 'hochbeet.service')
|
||||
|
||||
LOGFILE = "/var/log/hochbeet.log"
|
||||
|
||||
|
||||
@ -31,7 +32,7 @@ class Install(install):
|
||||
def run(self):
|
||||
install.run(self)
|
||||
os.makedirs(SERVICEDIR, exist_ok=True)
|
||||
shutil.copyfile(START_SCRIPT, DAEMON_START_SCRIPT)
|
||||
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:
|
||||
|
Loading…
Reference in New Issue
Block a user