From 091dcbd3a3bbda0af7b0b00ca439483c69e10476 Mon Sep 17 00:00:00 2001 From: dimazhelovanov Date: Tue, 29 Oct 2024 19:49:33 +0400 Subject: [PATCH] =?UTF-8?q?=D0=A3=D0=BF=D1=80=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D0=B2=D0=B5=D0=BD=D1=82=D0=B8=D0=BB=D1=8F?= =?UTF-8?q?=D0=BC=D0=B8/=D0=BD=D0=B0=D0=B3=D1=80=D0=B5=D0=B2=D0=B0=D1=82?= =?UTF-8?q?=D0=B5=D0=BB=D1=8F=D0=BC=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- GreenhouseController/ManageController.py | 47 ++++++++++++++++++++++++ GreenhouseManager/manager.py | 3 +- 2 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 GreenhouseController/ManageController.py diff --git a/GreenhouseController/ManageController.py b/GreenhouseController/ManageController.py new file mode 100644 index 0000000..37f60b5 --- /dev/null +++ b/GreenhouseController/ManageController.py @@ -0,0 +1,47 @@ +from json import dumps + + + +from json import dumps + +class ManageController: + def __init__(self, producer, topic='commands'): + self.valve_state = "closed" + self.heater_state = "off" + self.producer = producer + self.topic = topic + + def toggle_device(self, device, request_id, greenhouse_id): + + + if device == 'valve': + + if self.valve_state == 'closed': + self.valve_state = 'open' + print("Valve opened") + else: + self.valve_state = 'closed' + print("Valve closed") + + elif device == 'heater': + + if self.heater_state == 'off': + self.heater_state = 'on' + print("Heater turned on") + else: + self.heater_state = 'off' + print("Heater turned off") + + + self.send_status(request_id, greenhouse_id) + + def send_status(self, request_id, greenhouse_id): + + status = { + 'request_id': request_id, + 'greenhouse_id': greenhouse_id, + 'valve_state': self.valve_state, + 'heater_state': self.heater_state + } + self.producer.send(self.topic, value=status) + print(f"Sent device status: {status}") \ No newline at end of file diff --git a/GreenhouseManager/manager.py b/GreenhouseManager/manager.py index 74fed03..9e96558 100644 --- a/GreenhouseManager/manager.py +++ b/GreenhouseManager/manager.py @@ -3,7 +3,8 @@ import kafka import socket from json import dumps, loads import time - +from ManageController import ManagerController +greenhouse_controller = GreenhouseController(producer) consumer = KafkaConsumer( 'commands', bootstrap_servers=['localhost:9092'],