78 righe
2.4 KiB
Python
78 righe
2.4 KiB
Python
import config
|
|
import devdi.rooms as rooms
|
|
import devdi.topic
|
|
import mqtt
|
|
import report
|
|
import z_protocol
|
|
import tcp_socket
|
|
import time
|
|
|
|
logger = report.default_logging_config()
|
|
|
|
|
|
class devices(dict):
|
|
def __init__(self):
|
|
super().__init__()
|
|
|
|
def __key_by_value__(self, dictionary, value):
|
|
for key in dictionary:
|
|
if dictionary[key] == value:
|
|
return key
|
|
|
|
def add_room(self, room):
|
|
for key, value in room.__dict__.items():
|
|
inst = getattr(room, key)
|
|
try:
|
|
topic = inst.topic
|
|
except AttributeError:
|
|
pass # attribute is nor device
|
|
else:
|
|
STG, LOC, ROO, FUN = topic.split("/")[:4]
|
|
STG = self.__key_by_value__(devdi.topic.STG_TOPIC, STG)
|
|
LOC = self.__key_by_value__(devdi.topic.LOC_TOPIC, LOC)
|
|
ROO = self.__key_by_value__(devdi.topic.ROO_TOPIC, ROO)
|
|
FUN = self.__key_by_value__(devdi.topic.FUN_TOPIC, FUN)
|
|
self[f"{STG}.{LOC}.{ROO}.{FUN}"] = inst
|
|
|
|
def get_str(self, **kwargs):
|
|
STG = getattr(devdi.topic, kwargs.get("stg"))
|
|
LOC = getattr(devdi.topic, kwargs.get("loc"))
|
|
ROO = getattr(devdi.topic, kwargs.get("roo"))
|
|
FUN = getattr(devdi.topic, kwargs.get("fun"))
|
|
return self.get(f"{STG}.{LOC}.{ROO}.{FUN}")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
#
|
|
# MQTT Client
|
|
#
|
|
mc = mqtt.mqtt_client(host=config.MQTT_SERVER, port=config.MQTT_PORT, username=config.MQTT_USER,
|
|
password=config.MQTT_PASSWORD, name=config.APP_NAME)
|
|
#
|
|
# Smarthome physical Devices
|
|
#
|
|
pd = devices()
|
|
pd.add_room(rooms.ffe_diningroom(mc))
|
|
pd.add_room(rooms.ffe_floor(mc))
|
|
pd.add_room(rooms.ffe_kitchen(mc))
|
|
pd.add_room(rooms.ffe_livingroom(mc))
|
|
pd.add_room(rooms.ffe_sleep(mc))
|
|
pd.add_room(rooms.ffw_bath(mc))
|
|
pd.add_room(rooms.ffw_floor(mc))
|
|
pd.add_room(rooms.ffw_julian(mc))
|
|
pd.add_room(rooms.ffw_livingroom(mc))
|
|
pd.add_room(rooms.ffw_sleep(mc))
|
|
pd.add_room(rooms.gar_garden(mc))
|
|
pd.add_room(rooms.gfw_dirk(mc))
|
|
pd.add_room(rooms.gfw_floor(mc))
|
|
pd.add_room(rooms.gfw_marion(mc))
|
|
pd.add_room(rooms.stairway(mc))
|
|
|
|
#
|
|
# Socket Protocol
|
|
#
|
|
s = tcp_socket.tcp_server_stp('127.0.0.1', config.SOCK_PROT_PORT)
|
|
sp = z_protocol.server(s, channel_name='example_server', devices=pd)
|
|
while (True):
|
|
time.sleep(5)
|