Менеджер

This commit is contained in:
dimazhelovanov 2024-10-30 11:10:39 +04:00
parent 1c38c61fbc
commit 3f5bb31646
2 changed files with 89 additions and 14 deletions

View File

@ -43,5 +43,6 @@ class ManageController:
'valve_state': self.valve_state, 'valve_state': self.valve_state,
'heater_state': self.heater_state 'heater_state': self.heater_state
} }
self.producer.send(self.topic, value=status)
print(f"Sent device status: {status}") print(f"Sent device status: {status}")
return status

View File

@ -4,20 +4,23 @@ import socket
from json import dumps, loads from json import dumps, loads
import time import time
from enum import Enum from enum import Enum
from GreenhouseDetector.detector import Detector
class Status(Enum): class Status(Enum):
UNKNOWN = -1 UNKNOWN = -1
OFF = 0 OFF = 0
ON = 1 ON = 1
class Manager: class Manager:
def __init__(self, id : int, moisture : float = 0, temp : float = 20, valveStatus : Status = Status.UNKNOWN, heaterStatus : Status = Status.UNKNOWN, isAutoOn : bool = False): def __init__(self, id: int, moisture: float = 0, temp: float = 20, isAutoOn: bool = False, valve_state: str = "closed",
heater_state: str = "off"):
self.id = id self.id = id
self.moisture = moisture self.moisture = moisture
self.temp = temp self.temp = temp
self.valveStatus = valveStatus
self.heaterStatus = heaterStatus
self.isAutoOn = isAutoOn self.isAutoOn = isAutoOn
self.valve_state = valve_state
self.heater_state = heater_state
self.dataPublisher = KafkaProducer( self.dataPublisher = KafkaProducer(
bootstrap_servers=['localhost:9092'], bootstrap_servers=['localhost:9092'],
@ -25,19 +28,33 @@ class Manager:
value_serializer=lambda v: dumps(v).encode('utf-8') value_serializer=lambda v: dumps(v).encode('utf-8')
) )
self.detectorConsumer = KafkaConsumer( # self.detectorConsumer = KafkaConsumer(
'dataDetectors', # 'dataDetectors',
# bootstrap_servers=['localhost:9092'],
# auto_offset_reset='earliest',
# enable_auto_commit=True,
# consumer_timeout_ms=1000,
#group_id=f'manager{id}',
# value_deserializer=lambda x: loads(x.decode('utf-8'))
#)
self.controllerConsumer = KafkaConsumer(
'commands',
bootstrap_servers=['localhost:9092'], bootstrap_servers=['localhost:9092'],
auto_offset_reset='earliest', auto_offset_reset='earliest',
enable_auto_commit=True, enable_auto_commit=True,
consumer_timeout_ms = 1000, consumer_timeout_ms=2000,
group_id=f'manager{id}', group_id=f'manager{id}',
value_deserializer=lambda x: loads(x.decode('utf-8')) value_deserializer=lambda x: loads(x.decode('utf-8'))
) )
self.controllerConsumerResponse = KafkaProducer(
bootstrap_servers=['localhost:9092'],
client_id=f'manager{id}_producer',
value_serializer=lambda v: dumps(v).encode('utf-8')
)
def update(self): def update(self):
for message in self.detectorConsumer: for message in self.detectorConsumer:
if message.value['id'] == self.id:
print(f"Manager {self.id} received message: ") print(f"Manager {self.id} received message: ")
print(message.value) print(message.value)
self.moisture = message.value['moisture'] self.moisture = message.value['moisture']
@ -52,19 +69,76 @@ class Manager:
'id': self.id, 'id': self.id,
'moisture': self.moisture, 'moisture': self.moisture,
'temp': self.temp, 'temp': self.temp,
'valveStatus': str(self.valveStatus), 'valveStatus': str(self.valve_state),
'heaterStatus': str(self.heaterStatus), 'heaterStatus': str(self.heater_state),
'isAutoOn': self.isAutoOn 'isAutoOn': self.isAutoOn
} }
print(message) print(message)
self.dataPublisher.send('data', message) self.dataPublisher.send('data', message)
manager1 = Manager(id = 1) 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.sendDataCommand(status)
print("Updating info...\n")
def sendDataCommand(self, message):
print("sending data...")
self.dataPublisher.send('response', message)
def getCommand(self):
messages = self.controllerConsumer.poll(timeout_ms=1000)
# Проверяем, есть ли сообщения
for tp, msgs in messages.items():
for message in msgs:
print(f"Manager {self.id} received message: ")
print(message.value)
self.request_id = message.value['request_id']
self.greenhouse_id = message.value['greenhouse_id']
self.command = message.value['command']
self.toggle_device(self.command, self.request_id, self.greenhouse_id)
manager1 = Manager(id=1)
managers = [manager1] managers = [manager1]
while True: while True:
time.sleep(1) time.sleep(5)
manager1.sendData()
for manager in managers: for manager in managers:
manager.update()
manager.getCommand()