detector webhooks
This commit is contained in:
parent
b7f4aa3f9f
commit
f82c8daa92
@ -1,19 +1,12 @@
|
||||
import time
|
||||
import random as rnd
|
||||
|
||||
from flask import Flask, jsonify
|
||||
from flask import Flask
|
||||
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
|
||||
@ -28,13 +21,19 @@ class Detector:
|
||||
self.moisture += rnd.random() / 100
|
||||
self.temp += (rnd.random() - 0.5) / 100
|
||||
|
||||
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)
|
||||
|
||||
detector1 = Detector(1, 0.6, 0.2, 40, 20)
|
||||
|
||||
detectors = [detector1]
|
||||
|
||||
t1 = threading.Thread(target=start_detector)
|
||||
|
||||
if __name__ =="__main__":
|
||||
t1.start()
|
||||
app.run(host='0.0.0.0', port=20001, debug=True)
|
||||
while True:
|
||||
for detector in detectors:
|
||||
detector.cycle()
|
||||
detector.sendData()
|
||||
time.sleep(1)
|
||||
|
||||
|
@ -13,9 +13,9 @@ def start_manager():
|
||||
return
|
||||
|
||||
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",
|
||||
heater_state: str = "off"):
|
||||
self.id = id
|
||||
self._id = _id
|
||||
self.moisture = moisture
|
||||
self.temp = temp
|
||||
self.isAutoOn = isAutoOn
|
||||
@ -24,7 +24,7 @@ class Manager:
|
||||
|
||||
self.dataPublisher = KafkaProducer(
|
||||
bootstrap_servers=['localhost:9092'],
|
||||
client_id=f'manager{id}_producer',
|
||||
client_id=f'manager{self._id}_producer',
|
||||
value_serializer=lambda v: dumps(v).encode('utf-8')
|
||||
)
|
||||
|
||||
@ -34,30 +34,19 @@ class Manager:
|
||||
auto_offset_reset='earliest',
|
||||
enable_auto_commit=True,
|
||||
consumer_timeout_ms=2000,
|
||||
group_id=f'manager{id}',
|
||||
group_id=f'manager{self._id}',
|
||||
value_deserializer=lambda x: loads(x.decode('utf-8'))
|
||||
)
|
||||
self.controllerConsumerResponse = KafkaProducer(
|
||||
bootstrap_servers=['localhost:9092'],
|
||||
client_id=f'manager{id}_producer',
|
||||
client_id=f'manager{self._id}_producer',
|
||||
value_serializer=lambda v: dumps(v).encode('utf-8')
|
||||
)
|
||||
|
||||
def update(self):
|
||||
for message in self.detectorConsumer:
|
||||
|
||||
print(f"Manager {self.id} received message: ")
|
||||
print(message.value)
|
||||
self.moisture = message.value['moisture']
|
||||
self.temp = message.value['temperature']
|
||||
print("Updating info...\n")
|
||||
|
||||
self.sendData()
|
||||
|
||||
def sendData(self):
|
||||
print("sending data...")
|
||||
message = {
|
||||
'id': self.id,
|
||||
'id': self._id,
|
||||
'moisture': self.moisture,
|
||||
'temp': self.temp,
|
||||
'valveStatus': str(self.valve_state),
|
||||
@ -113,21 +102,27 @@ class Manager:
|
||||
|
||||
for tp, msgs in messages.items():
|
||||
for message in msgs:
|
||||
print(f"Manager {self.id} received message: ")
|
||||
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)
|
||||
|
||||
@app.route('/webhook', methods=['POST'])
|
||||
@app.route(f'/webhook', methods=['POST'])
|
||||
def webhook():
|
||||
if request.method == 'POST':
|
||||
print("Data received from Webhook is", request.json)
|
||||
return "Webhook received"
|
||||
for manager in managers:
|
||||
if request.args.get('id') == manager._id and request.method == 'POST':
|
||||
print("Data received from Webhook is", request.json)
|
||||
|
||||
manager1 = Manager(id=1)
|
||||
body = request.json
|
||||
for key, value in body.items():
|
||||
setattr(manager, key, value)
|
||||
|
||||
return f"Webhook received for manager {manager._id}"
|
||||
return "Webhook ignored"
|
||||
|
||||
manager1 = Manager(_id=1)
|
||||
managers = [manager1]
|
||||
|
||||
t1 = threading.Thread(target=start_manager)
|
||||
|
Loading…
Reference in New Issue
Block a user