diff --git a/watering.py b/watering.py index 3b7fbb2..3bee1c8 100755 --- a/watering.py +++ b/watering.py @@ -8,6 +8,8 @@ import paho.mqtt.client as paho from paho import mqtt import json +from http.server import HTTPServer, BaseHTTPRequestHandler +from threading import Thread client = paho.Client(paho.CallbackAPIVersion.VERSION2) client.connect("stanley") @@ -47,6 +49,31 @@ a = WaterActor.start(client).proxy() +class SprinklerHTTPRequestHandler(BaseHTTPRequestHandler): + def do_GET(self): + current_state = a.state.get() + self.send_response(200) + self.send_header("Content-type", "text/plain") + self.end_headers() + self.flush_headers() + if self.path == '/on': + a.queue_update({ + 'state': 0, + 'intervals': current_state.delay_hours * 360 + 1 + }) + self.wfile.write(bytes("Switching pump on", "utf-8")) + elif self.path == '/off': + a.queue_update({ + 'state': 1, + 'intervals': 0, + 'wet': current_state.moisture + 1 + }) + self.wfile.write(bytes("Switching pump off", "utf-8")) + +httpd = HTTPServer(('', 80), SprinklerHTTPRequestHandler) + +Thread(target=httpd.serve_forever, daemon=True).start() + def on_radio_recv(payload): a.greenhouse(payload) @@ -64,4 +91,7 @@ client.on_message = on_mqtt_message client.subscribe("commands/greenhouse") -client.loop_forever(retry_first_connection=False) +#client.loop_forever(retry_first_connection=False) +Thread(target=client.loop_forever, + kwargs={"retry_first_connection": False}, + daemon=True).start()