webhook test

This commit is contained in:
the 2024-11-12 16:18:14 +04:00
parent 3f5bb31646
commit 7f5262575e
2 changed files with 32 additions and 48 deletions

View File

@ -1,13 +1,19 @@
from random import random
from turtledemo.penrose import start
from kafka import KafkaProducer, KafkaConsumer
import kafka
import socket
from json import dumps, loads
import time import time
import random as rnd import random as rnd
from flask import Flask, jsonify
import requests
import threading
app = Flask(__name__)
def start_detector():
while True:
for detector in detectors:
detector.cycle()
detector.sendData()
time.sleep(1)
class Detector: class Detector:
def __init__(self, id, moistureThresholdUpper, moistureThresholdLower, tempThresholdUpper, tempThresholdLower): def __init__(self, id, moistureThresholdUpper, moistureThresholdLower, tempThresholdUpper, tempThresholdLower):
self.id = id self.id = id
@ -18,31 +24,17 @@ class Detector:
self.moisture = 0 self.moisture = 0
self.temp = 0 self.temp = 0
self.producer = KafkaProducer(
bootstrap_servers=['localhost:9092'],
client_id=f'detector{self.id}',
value_serializer=lambda v: dumps(v).encode('utf-8')
)
def sendData(self):
message = {'id' : self.id,
'moisture': self.moisture,
'temperature' : self.temp }
self.producer.send('dataDetectors', message)
def cycle(self): def cycle(self):
self.moisture += rnd.random() / 100 self.moisture += rnd.random() / 100
self.temp += (rnd.random() - 0.5) / 100 self.temp += (rnd.random() - 0.5) / 100
detector1 = Detector(1, 0.6, 0.2, 40, 20) detector1 = Detector(1, 0.6, 0.2, 40, 20)
detector2 = Detector(2, 0.7, 0.3, 40, 20)
detector3 = Detector(3, 0.9, 0.6, 40, 20)
detectors = [detector1, detector2, detector3] detectors = [detector1]
while True: t1 = threading.Thread(target=start_detector)
for detector in detectors:
detector.cycle() if __name__ =="__main__":
detector.sendData() t1.start()
time.sleep(1) app.run(host='0.0.0.0', port=20001, debug=True)

View File

@ -2,15 +2,15 @@ from kafka import KafkaProducer, KafkaConsumer
import kafka import kafka
import socket import socket
from json import dumps, loads from json import dumps, loads
from flask import Flask, request
import time import time
from enum import Enum from enum import Enum
from GreenhouseDetector.detector import Detector import threading
class Status(Enum): app = Flask(__name__)
UNKNOWN = -1
OFF = 0
ON = 1
def start_manager():
return
class Manager: class Manager:
def __init__(self, id: int, moisture: float = 0, temp: float = 20, isAutoOn: bool = False, valve_state: str = "closed", def __init__(self, id: int, moisture: float = 0, temp: float = 20, isAutoOn: bool = False, valve_state: str = "closed",
@ -28,15 +28,6 @@ class Manager:
value_serializer=lambda v: dumps(v).encode('utf-8') value_serializer=lambda v: dumps(v).encode('utf-8')
) )
# self.detectorConsumer = KafkaConsumer(
# '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( self.controllerConsumer = KafkaConsumer(
'commands', 'commands',
bootstrap_servers=['localhost:9092'], bootstrap_servers=['localhost:9092'],
@ -129,16 +120,17 @@ class Manager:
self.command = message.value['command'] self.command = message.value['command']
self.toggle_device(self.command, self.request_id, self.greenhouse_id) self.toggle_device(self.command, self.request_id, self.greenhouse_id)
@app.route('/webhook', methods=['POST'])
def webhook():
if request.method == 'POST':
print("Data received from Webhook is")
return "Webhook received"
manager1 = Manager(id=1) manager1 = Manager(id=1)
managers = [manager1] managers = [manager1]
t1 = threading.Thread(target=start_manager)
while True: if __name__ == "__main__":
time.sleep(5) app.run(host="0.0.0.0", port=20002)
manager1.sendData()
for manager in managers:
manager.getCommand()