diff --git a/garage/garage.py b/garage/garage.py index 2d9b35b..3833015 100644 --- a/garage/garage.py +++ b/garage/garage.py @@ -4,7 +4,7 @@ import logging.handlers as handlers import time from threading import Thread import cherrypy # https://docs.cherrypy.org/en/latest/tutorials.html#tutorial-1-a-basic-web-application -import urllib.request, json +import requests, json cherrypy.config.update({ 'server.socket_host' : '192.168.178.49', @@ -15,7 +15,7 @@ logger = logging.getLogger('Rotating Log') logger.setLevel(logging.INFO) logHandler = handlers.RotatingFileHandler('garage.log', maxBytes=1000000, backupCount=2) logHandler.setLevel(logging.INFO) -logHandler.setFormatter(logging.Formatter('%(asctime)s %(levelname)s - %(message)s')) +logHandler.setFormatter(logging.Formatter('%(asctime)s %(levelname)s - %(message)s', '%Y-%m-%d %H:%M:%S')) logger.addHandler(logHandler) @@ -41,6 +41,33 @@ GPIO.setup(PIN_SENSOR_GATE, GPIO.IN, GPIO.PUD_UP) +# move robomower into safe zone +def mower_safety(): + try: + #response = requests.get("http://192.168.178.54/json?user=lewe&pass=nFdcX3sMD115WAap&cmd=status") + jsonstr = '{"name": "Rasi", "id": "C7697E", "status": {"status": 17, "distance": 0, "stopped": false, "duration": 4290, "mode": 2, "battery": 100, "hours": 90}, "timer": {"status": 0}, "wlan": {"signal": -86}, "health": {"temperature": 23, "humidity": 25}, "clock": {"date": "2021-05-13", "time": "14:23:27", "unix": 1620915807}, "successful": true}' + except requests.exceptions.RequestException as e: + #raise SystemExit(e) + logger.warning("couldn't reach mower") + return None + #status = json.loads(response.text) + status = json.loads(jsonstr) + logger.info(status["status"]["status"]) + if (status["status"]["status"] in [2, 3, 5]): + logger.info("mower, run for your life") + else: + logger.info("lock mower in garage") + +# robomower back to auto mode after gate has been closed +def mower_auto(): + logger.info("set mower back to auto mode") + try: + response = requests.get("http://192.168.178.54/json?cmd=mode&mode=auto&user=lewe&pass=nFdcX3sMD115WAap&cmd=status") + except requests.exceptions.RequestException as e: + logger.warning("couldn't reach mower") + return None + + def garage_button(channel): time.sleep(0.01) # el cheapo relay module creates lots of interference, so we check again after a waiting period (see https://electronics.stackexchange.com/a/361793/253533) if channel == 0 or GPIO.input(PIN_GARAGE_BUTTON) == False: @@ -62,9 +89,13 @@ def gate_button(channel): time.sleep(0.4) GPIO.output(PIN_GATE, GPIO.LOW) GPIO.output(PIN_LED, GPIO.LOW) + if GPIO.input(PIN_SENSOR_GATE) == 0: + mower_auto() + else: + mower_safety() else: logger.info("flare (remote button)") - + GPIO.add_event_detect(PIN_GARAGE_BUTTON, GPIO.FALLING, callback=garage_button, bouncetime=250) GPIO.add_event_detect(PIN_GATE_BUTTON, GPIO.FALLING, callback=gate_button, bouncetime=250)