85 lines
4.0 KiB
Python
85 lines
4.0 KiB
Python
# Simuliert den ESP8266: schickt alle 2 s Fantasiewerte an den Server (Entwicklung ohne Hardware).
|
|
# Start: python simulator.py [Server-URL] [--szenario NAME] z. B. http://localhost:3000 --szenario hitze
|
|
#
|
|
# Szenarien (fuer Vorfuehrungen; das Backend entscheidet, Webinterface und Unity-Zwilling zeigen es):
|
|
# wechsel (Standard) Temperatur und CO2 schwanken, CO2 ueberschreitet zeitweise 1000 ppm
|
|
# normal angenehmes Raumklima (22 °C, ca. 650 ppm, ca. 45 dB)
|
|
# hitze Temperatur steigt langsam auf ca. 30 °C -> Fenster oeffnen
|
|
# stickig CO2 steigt langsam auf ca. 1600 ppm -> Lueften (Fenster oder Lueftungsanlage)
|
|
# kalt Temperatur faellt auf ca. 17 °C -> Heizung
|
|
# laerm CO2 hoch und Lautstaerke ca. 80 dB -> Laerm-Veto: Fenster zu, Lueftungsanlage (ausser Mehrheit will lueften)
|
|
# dunkel Helligkeit faellt auf ca. 10 % -> Licht heller
|
|
# demo laeuft nacheinander normal -> stickig -> laerm -> normal -> hitze -> dunkel -> kalt durch (je ca. 1 min)
|
|
import argparse
|
|
import json
|
|
import math
|
|
import time
|
|
import urllib.error
|
|
import urllib.request
|
|
|
|
parser = argparse.ArgumentParser(description="Fake-Messwerte wie vom ESP8266")
|
|
parser.add_argument("basis", nargs="?", default="http://localhost:3000", help="Server-URL")
|
|
parser.add_argument("--szenario", default="wechsel",
|
|
choices=["wechsel", "normal", "hitze", "stickig", "kalt", "laerm", "dunkel", "demo"])
|
|
args = parser.parse_args()
|
|
|
|
ZIELE = { # Zielwerte (Temperatur °C, eCO2 ppm, Lautstaerke dB, Helligkeit %), auf die sich die Werte langsam zubewegen
|
|
"normal": (22.0, 650, 45.0, 60),
|
|
"hitze": (30.0, 750, 48.0, 85),
|
|
"stickig": (23.0, 1600, 52.0, 60),
|
|
"kalt": (17.0, 600, 42.0, 35),
|
|
"laerm": (23.0, 1400, 80.0, 60),
|
|
"dunkel": (22.0, 700, 44.0, 10),
|
|
}
|
|
DEMO_ABLAUF = ["normal", "stickig", "laerm", "normal", "hitze", "dunkel", "kalt"]
|
|
DEMO_SCHRITTE = 30 # 30 x 2 s = 1 min je Abschnitt
|
|
|
|
|
|
def naeher(wert, ziel, schritt):
|
|
return wert + max(-schritt, min(schritt, ziel - wert))
|
|
|
|
|
|
temperatur, eco2, lautstaerke, helligkeit = 22.0, 650.0, 45.0, 60.0
|
|
schritt = 0
|
|
|
|
while True:
|
|
schritt += 1
|
|
if args.szenario == "wechsel":
|
|
temperatur = 22 + 2 * math.sin(schritt / 20)
|
|
eco2 = 800 + 400 * math.sin(schritt / 25) # ueberschreitet zeitweise 1000 ppm
|
|
lautstaerke = 50 + 10 * math.sin(schritt / 7)
|
|
helligkeit = 55 + 30 * math.sin(schritt / 30)
|
|
aktiv = "wechsel"
|
|
else:
|
|
aktiv = args.szenario
|
|
if aktiv == "demo":
|
|
aktiv = DEMO_ABLAUF[(schritt // DEMO_SCHRITTE) % len(DEMO_ABLAUF)]
|
|
ziel_t, ziel_co2, ziel_laut, ziel_licht = ZIELE[aktiv]
|
|
temperatur = naeher(temperatur, ziel_t, 0.4)
|
|
eco2 = naeher(eco2, ziel_co2, 60.0)
|
|
lautstaerke = naeher(lautstaerke, ziel_laut, 4.0)
|
|
helligkeit = naeher(helligkeit, ziel_licht, 6.0)
|
|
|
|
messwert = {
|
|
"temperatur": round(temperatur + 0.1 * math.sin(schritt), 1),
|
|
"luftfeuchte": round(45 + 5 * math.sin(schritt / 15), 1),
|
|
"eco2": round(eco2 + 10 * math.sin(schritt / 3)),
|
|
"tvoc": round(70 + 30 * math.sin(schritt / 10) + max(0.0, eco2 - 800) / 10),
|
|
"lautstaerke": round(lautstaerke + 1.5 * math.sin(schritt / 2), 1),
|
|
"helligkeit": max(0, min(100, round(helligkeit + 2 * math.sin(schritt / 4)))),
|
|
}
|
|
anfrage = urllib.request.Request(args.basis + "/api/messwerte", data=json.dumps(messwert).encode(),
|
|
headers={"Content-Type": "application/json"})
|
|
try:
|
|
urllib.request.urlopen(anfrage, timeout=3).read()
|
|
print(f"gesendet [{aktiv}]", json.dumps(messwert), flush=True)
|
|
except urllib.error.HTTPError as fehler: # z. B. 409: der echte Arduino sendet gerade
|
|
try:
|
|
grund = json.loads(fehler.read()).get("fehler", fehler.reason)
|
|
except ValueError:
|
|
grund = fehler.reason
|
|
print("Nicht angenommen:", grund, flush=True)
|
|
except OSError as fehler:
|
|
print("Server nicht erreichbar:", fehler, flush=True)
|
|
time.sleep(2)
|