Created
May 17, 2019 16:18
-
-
Save prankousky/ebf8cf4409b66999294a0466fcf393c6 to your computer and use it in GitHub Desktop.
python mqtt jspn
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# mqtt | |
import paho.mqtt.client as mqtt | |
# clear screen | |
import os | |
# farbige Ausgabe | |
from termcolor import colored | |
# json importieren | |
import json | |
# -- Variabeln {{{ | |
myip = | |
myport = | |
myuser = | |
mypw = | |
# -- }}} | |
# -- farbige Ausgabe {{{ | |
def gruen(g): | |
print(colored(g, "green")) | |
def gelb(gl): | |
print(colored(gl, "yellow")) | |
def rot(r): | |
print(colored(r, "red")) | |
def blau(b): | |
print(colored(b, "blue")) | |
offen = (colored("geöffnet", "red")) | |
zu = (colored("geschlosen", "green")) | |
# --}}} | |
# -- externe Dateie(en) holen {{{ | |
# with open('alle.json') as data_file: | |
# data = json.loads(data_file.read()) | |
# -- }}} | |
# -- JSON wird später von externer Datei eingelesen {{{ | |
status = { | |
"OBEN": { | |
"Schlafzimmer": { | |
"Fenster": { | |
"topic": "home/oben/schlafzimmer/open/fenster/open", | |
"status": None, | |
"einheit": "" | |
}, | |
"Tür": { | |
"topic": "home/oben/schlafzimmer/open/tuer/open", | |
"status": None, | |
"einheit": "" | |
} | |
}, | |
"Zwischenzimmer": { | |
"Fenster": { | |
"topic": "home/oben/zwischenzimmer/open/fenster/open", | |
"status": None, | |
"einheit": "" | |
}, | |
"Tür": { | |
"topic": "home/oben/zwischenzimmer/open/tuer/open", | |
"status": None, | |
"einheit": "" | |
} | |
}, | |
"Badezimmer": { | |
"Fenster": { | |
"topic": "home/oben/bad_oben/open/fenster/open", | |
"status": None, | |
"einheit": "" | |
}, | |
"Tür": { | |
"topic": "home/oben/bad_oben/open/tuer/open", | |
"status": None, | |
"einheit": "" | |
}, | |
"Temperatur": { | |
"topic": "home/oben/bad_oben/messwerte/temp", | |
"status": None, | |
"einheit": " °C" | |
}, | |
}, | |
"Jonna": { | |
"Fenster": { | |
"topic": "home/oben/jonna/open/fenster/open", | |
"status": None, | |
"einheit": "" | |
}, | |
"Tür": { | |
"topic": "home/oben/jonna/open/tuer/open", | |
"status": None, | |
"einheit": "" | |
} | |
}, | |
}, | |
"UNTEN": { | |
"Sally": { | |
"Batterie": { "topic": "home/misc/vacuum/sally/stat/battery", | |
"status": None, | |
"einheit": " %"}, | |
}, | |
"Badezimmer": { | |
"Fenster": { | |
"topic": "home/unten/bad_unten/open/fenster/open", | |
"status": None, | |
"einheit": "" | |
}, | |
"Tür": { | |
"topic": "home/unten/bad_unten/open/tuer/open", | |
"status": None, | |
"einheit": "" | |
} | |
}, | |
"Wohnzimmer": { | |
"Tür": { | |
"topic": "home/unten/wohnzimmer/open/tuer/open", | |
"status": None, | |
"einheit": "" | |
}, | |
"Fenster Vorne": { | |
"topic": "home/unten/wohnzimmer_vorne/open/fenster/open", | |
"status": None, | |
"einheit": "" | |
}, | |
"Fenster Mitte": { | |
"topic": "home/unten/wohnzimmer_mitte/open/fenster/open", | |
"status": None, | |
"einheit": "" | |
}, | |
"Fenster Straße": { | |
"topic": "home/unten/wohnzimmer_strasse/open/fenster/open", | |
"status": None, | |
"einheit": "" | |
} | |
}, | |
"Küche": { | |
"Tür": { | |
"topic": "home/unten/kueche/open/tuer/open", | |
"status": None, | |
"einheit": "" | |
}, | |
"Fenster": { | |
"topic": "home/unten/kueche/open/fenster/open", | |
"status": None, | |
"einheit": "" | |
} | |
} | |
} | |
} | |
# -- }}} | |
# -- | |
def on_connect(client, userdata, flags, rc): | |
for stockwerk,zimmer in status.items(): | |
for zimmer,objekt in zimmer.items(): | |
for name,info in objekt.items(): | |
# print(f'Subscribing to {stockwerk}.{zimmer}.{name}: {info["topic"]}') | |
client.subscribe(info['topic'], 1) | |
def on_message(client, userdata, msg): | |
payload = msg.payload.decode("utf-8") | |
for stockwerk,zimmer in status.items(): | |
for zimmer,objekt in zimmer.items(): | |
for name,info in objekt.items(): | |
if info['topic'] == msg.topic: | |
info['status'] = payload | |
# print(f'Updated {stockwerk}.{zimmer}.{name}: {info["status"]}') | |
display_info() | |
return | |
def display_info(): | |
# Uncomment the next line if it's working | |
os.system('clear') | |
for stockwerk,zimmer in status.items(): | |
print("==================================") | |
print(stockwerk) | |
print("==================================") | |
for zimmer,objekt in zimmer.items(): | |
print(f'{zimmer}') | |
for name,info in objekt.items(): | |
if (name == "Fenster") or (name == "Tür") or (name == "Fenster Mitte") or (name == "Fenster Vorne") or (name == "Fenster Straße"): | |
if info["status"] == "true": | |
print(f' {name} => %s' % (offen)) | |
elif info["status"] == "false": | |
print(f' {name} => %s' %(zu)) | |
if (name == "Batterie"): | |
# print(f' {name} => {info["status"]}{info["einheit"]}') | |
print(f' {name} => {info["status"]}{info["einheit"]}') | |
if (name == "Temperatur"): | |
print(f' {name} => {info["status"]}{info["einheit"]}') | |
# -- }}} | |
# -- MQTT ausführen {{{ | |
client = mqtt.Client() | |
client.on_connect = on_connect | |
client.on_message = on_message | |
client.username_pw_set(myuser, mypw) | |
client.connect(myip, myport, 60) | |
# -- }}} | |
client.loop_forever() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"OBEN": { | |
"Schlafzimmer": { | |
"Fenster": { | |
"topic": "home/oben/schlafzimmer/open/fenster/open", | |
"status": "", | |
"einheit": "" | |
}, | |
"Tür": { | |
"topic": "home/oben/schlafzimmer/open/tuer/open", | |
"status": "", | |
"einheit": "" | |
} | |
}, | |
"Zwischenzimmer": { | |
"Fenster": { | |
"topic": "home/oben/zwischenzimmer/open/fenster/open", | |
"status": "", | |
"einheit": "" | |
}, | |
"Tür": { | |
"topic": "home/oben/zwischenzimmer/open/tuer/open", | |
"status": "", | |
"einheit": "" | |
} | |
}, | |
"Badezimmer": { | |
"Fenster": { | |
"topic": "home/oben/bad_oben/open/fenster/open", | |
"status": "", | |
"einheit": "" | |
}, | |
"Tür": { | |
"topic": "home/oben/bad_oben/open/tuer/open", | |
"status": "", | |
"einheit": "" | |
}, | |
"Temperatur": { | |
"topic": "home/oben/bad_oben/messwerte/temp", | |
"status": "", | |
"einheit": "° C" | |
} | |
}, | |
"Jonna": { | |
"Fenster": { | |
"topic": "home/oben/jonna/open/fenster/open", | |
"status": "", | |
"einheit": "" | |
}, | |
"Tür": { | |
"topic": "home/oben/jonna/open/tuer/open", | |
"status": "", | |
"einheit": "" | |
} | |
} | |
}, | |
"UNTEN": { | |
"Sally": { | |
"Batterie": { "topic": "home/misc/vacuum/sally/stat/battery", | |
"status": "", | |
"einheit": " %"} | |
}, | |
"Badezimmer": { | |
"Fenster": { | |
"topic": "home/unten/bad_unten/open/fenster/open", | |
"status": "", | |
"einheit": "" | |
}, | |
"Tür": { | |
"topic": "home/unten/bad_unten/open/tuer/open", | |
"status": "", | |
"einheit": "" | |
} | |
}, | |
"Wohnzimmer": { | |
"Tür": { | |
"topic": "home/unten/wohnzimmer/open/tuer/open", | |
"status": "", | |
"einheit": "" | |
}, | |
"Fenster Vorne": { | |
"topic": "home/unten/wohnzimmer_vorne/open/fenster/open", | |
"status": "", | |
"einheit": "" | |
}, | |
"Fenster Mitte": { | |
"topic": "home/unten/wohnzimmer_mitte/open/fenster/open", | |
"status": "", | |
"einheit": "" | |
}, | |
"Fenster Straße": { | |
"topic": "home/unten/wohnzimmer_strasse/open/fenster/open", | |
"status": "", | |
"einheit": "" | |
} | |
}, | |
"Küche": { | |
"Tür": { | |
"topic": "home/unten/kueche/open/tuer/open", | |
"status": "", | |
"einheit": "" | |
}, | |
"Fenster": { | |
"topic": "home/unten/kueche/open/fenster/open", | |
"status": "", | |
"einheit": "" | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment