40 lines
1.0 KiB
Python
40 lines
1.0 KiB
Python
import time
|
|
import random as rnd
|
|
|
|
from flask import Flask
|
|
import requests
|
|
import threading
|
|
|
|
app = Flask(__name__)
|
|
|
|
class Detector:
|
|
def __init__(self, id, moistureThresholdUpper, moistureThresholdLower, tempThresholdUpper, tempThresholdLower):
|
|
self.id = id
|
|
self.moistureThresholdUpper = moistureThresholdUpper
|
|
self.moistureThresholdLower = moistureThresholdLower
|
|
self.tempThresholdUpper = tempThresholdUpper
|
|
self.tempThresholdLower = tempThresholdLower
|
|
self.moisture = 0
|
|
self.temp = 0
|
|
|
|
def cycle(self):
|
|
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]
|
|
|
|
if __name__ =="__main__":
|
|
while True:
|
|
for detector in detectors:
|
|
detector.cycle()
|
|
detector.sendData()
|
|
time.sleep(1)
|
|
|