fix: хз, вроде бы работает, но почему-то не подключается

This commit is contained in:
mfnefd 2024-12-04 23:20:09 +04:00
parent 71bc31d0bd
commit aace337052
5 changed files with 100 additions and 85 deletions

View File

@ -1,4 +1,4 @@
FROM python:latest
FROM python:3.9-slim
WORKDIR /app

View File

@ -1,3 +1,4 @@
import os
import time
import random as rnd
@ -5,10 +6,13 @@ from flask import Flask
import requests
import threading
app = Flask(__name__)
class Detector:
def __init__(self, id, moistureThresholdUpper, moistureThresholdLower, tempThresholdUpper, tempThresholdLower):
self.MANAGER_URL = os.environ.get('MANAGER_URL')
print("MANAGER_URL=", self.MANAGER_URL)
self.id = id
self.moistureThresholdUpper = moistureThresholdUpper
self.moistureThresholdLower = moistureThresholdLower
@ -24,7 +28,7 @@ class Detector:
def sendData(self):
data = {"moisture": self.moisture,
"temp": self.temp}
requests.post(f"http://127.0.0.1:20002/webhook?id={self.id}", json=data)
requests.post(f"{self.MANAGER_URL}/webhook?id={self.id}", json=data)
detector1 = Detector(1, 0.6, 0.2, 40, 20)

View File

@ -1,4 +1,4 @@
FROM python:latest
FROM python:3.9-slim
WORKDIR /app

View File

@ -1,3 +1,4 @@
import os
from kafka import KafkaProducer, KafkaConsumer
import kafka
import socket
@ -7,6 +8,7 @@ import time
from enum import Enum
import threading
app = Flask(__name__)
def start_manager():
@ -15,6 +17,8 @@ def start_manager():
class Manager:
def __init__(self, _id: int, moisture: float = 0, temp: float = 20, isAutoOn: bool = False, valve_state: str = "closed",
heater_state: str = "off"):
KAFKA_URL = os.environ.get('KAFKA_URL')
print("KAFKA_URL=", KAFKA_URL)
self._id = _id
self.moisture = moisture
self.temp = temp
@ -23,14 +27,14 @@ class Manager:
self.heater_state = heater_state
self.dataPublisher = KafkaProducer(
bootstrap_servers=['localhost:9092'],
bootstrap_servers=[KAFKA_URL],
client_id=f'manager{self._id}_producer',
value_serializer=lambda v: dumps(v).encode('utf-8')
)
self.controllerConsumer = KafkaConsumer(
'commands',
bootstrap_servers=['localhost:9092'],
bootstrap_servers=[KAFKA_URL],
auto_offset_reset='earliest',
enable_auto_commit=True,
consumer_timeout_ms=2000,
@ -38,7 +42,7 @@ class Manager:
value_deserializer=lambda x: loads(x.decode('utf-8'))
)
self.controllerConsumerResponse = KafkaProducer(
bootstrap_servers=['localhost:9092'],
bootstrap_servers=[KAFKA_URL],
client_id=f'manager{self._id}_producer',
value_serializer=lambda v: dumps(v).encode('utf-8')
)

View File

@ -79,20 +79,27 @@ services:
KAFKA_CLUSTERS_0_BOOTSTRAPSERVERS: kafka:29092
KAFKA_CLUSTERS_0_METRICS_PORT: 9997
#manager_py_service:
# container_name: manager_py
# build:
# context: .
# dockerfile: ./GreenhouseManager/Dockerfile
# depends_on:
# - kafka
manager:
networks:
- vpn
build:
context: .
dockerfile: ./GreenhouseManager/Dockerfile
environment:
KAFKA_URL: kafka:29092
depends_on:
- kafka
expose:
- 20002
detector:
networks:
- vpn
build:
context: .
dockerfile: ./GreenhouseDetector/Dockerfile
environment:
MANAGER_URL: http://manager:20002
depends_on:
- manager
#detector_py_service:
# container_name: detector_py
# build:
# context: .
# dockerfile: ./GreenhouseDetector/Dockerfile
# depends_on:
# - kafka
# expose:
# - 20002