webhook test
This commit is contained in:
parent
3f5bb31646
commit
7f5262575e
@ -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 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:
|
||||
def __init__(self, id, moistureThresholdUpper, moistureThresholdLower, tempThresholdUpper, tempThresholdLower):
|
||||
self.id = id
|
||||
@ -18,31 +24,17 @@ class Detector:
|
||||
self.moisture = 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):
|
||||
self.moisture += rnd.random() / 100
|
||||
self.temp += (rnd.random() - 0.5) / 100
|
||||
|
||||
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:
|
||||
for detector in detectors:
|
||||
detector.cycle()
|
||||
detector.sendData()
|
||||
time.sleep(1)
|
||||
t1 = threading.Thread(target=start_detector)
|
||||
|
||||
if __name__ =="__main__":
|
||||
t1.start()
|
||||
app.run(host='0.0.0.0', port=20001, debug=True)
|
||||
|
||||
|
@ -2,15 +2,15 @@ from kafka import KafkaProducer, KafkaConsumer
|
||||
import kafka
|
||||
import socket
|
||||
from json import dumps, loads
|
||||
from flask import Flask, request
|
||||
import time
|
||||
from enum import Enum
|
||||
from GreenhouseDetector.detector import Detector
|
||||
import threading
|
||||
|
||||
class Status(Enum):
|
||||
UNKNOWN = -1
|
||||
OFF = 0
|
||||
ON = 1
|
||||
app = Flask(__name__)
|
||||
|
||||
def start_manager():
|
||||
return
|
||||
|
||||
class Manager:
|
||||
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')
|
||||
)
|
||||
|
||||
# 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(
|
||||
'commands',
|
||||
bootstrap_servers=['localhost:9092'],
|
||||
@ -129,16 +120,17 @@ class Manager:
|
||||
self.command = message.value['command']
|
||||
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)
|
||||
|
||||
managers = [manager1]
|
||||
|
||||
t1 = threading.Thread(target=start_manager)
|
||||
|
||||
while True:
|
||||
time.sleep(5)
|
||||
manager1.sendData()
|
||||
for manager in managers:
|
||||
|
||||
manager.getCommand()
|
||||
if __name__ == "__main__":
|
||||
app.run(host="0.0.0.0", port=20002)
|
Loading…
Reference in New Issue
Block a user