diff --git a/watering.py b/watering.py index 3bee1c8..f9f1061 100755 --- a/watering.py +++ b/watering.py @@ -14,60 +14,56 @@ client = paho.Client(paho.CallbackAPIVersion.VERSION2) client.connect("stanley") -Watering = namedtuple('Watering', 'voltage moisture intervals dry wet state delay_hours max_time_mins pump_speed pump_running errors') -WateringStruct = ' 0: + time.sleep(0.5) + for cmd in self.cmds: + print("Sending: " + str(cmd)) + try: + reply = pack(CommandStruct, *cmd) + print(reply) + lora.send(reply, 2) + except Exception as e: + print(e) + self.cmds.clear() lora.set_mode_rx() - def queue_update(self, update): - self.updates.append(update) + def queue_command(self, command): + self.cmds.append(command) 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 - }) + a.queue_command(Command(1, 248)) 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 - }) + a.queue_command(Command(0, 0)) self.wfile.write(bytes("Switching pump off", "utf-8")) httpd = HTTPServer(('', 80), SprinklerHTTPRequestHandler)