From cdedc126ebe012dc37b1c3dbfd26bfd60a43c366 Mon Sep 17 00:00:00 2001 From: Dirk Alders Date: Sat, 13 Sep 2025 16:30:07 +0200 Subject: [PATCH] Module update + adaptions due to module update --- socket_protocol | 2 +- stringtools | 2 +- task | 2 +- tcp_socket | 2 +- z_server/.venv_required | 0 z_server/Makefile | 133 ++++++++++++++++++++++++++++++ z_server/config_example/config.j2 | 13 ++- z_server/devdi | 2 +- z_server/devices/__init__.py | 33 +++++++- z_server/mqtt | 2 +- z_server/report | 2 +- z_server/z_server.py | 65 +++++++++++---- 12 files changed, 233 insertions(+), 25 deletions(-) create mode 100644 z_server/.venv_required create mode 100644 z_server/Makefile diff --git a/socket_protocol b/socket_protocol index 8ab382d..877ba5a 160000 --- a/socket_protocol +++ b/socket_protocol @@ -1 +1 @@ -Subproject commit 8ab382dd63bd8b8f3819bebf5be4a3089abbeaff +Subproject commit 877ba5a47b605067e134ad7c51c378f14dc76a35 diff --git a/stringtools b/stringtools index e1f76d9..6142649 160000 --- a/stringtools +++ b/stringtools @@ -1 +1 @@ -Subproject commit e1f76d96312e540544b2328d0937b0aa41126aa9 +Subproject commit 6142649b3de313f2a1ef65441ae14423c0cf8cd0 diff --git a/task b/task index af35e83..bb6f7ea 160000 --- a/task +++ b/task @@ -1 +1 @@ -Subproject commit af35e83d1f07fd4cb9070bdb77cf1f3bdda3a463 +Subproject commit bb6f7ea26f24481cba7218256e47572fa84db478 diff --git a/tcp_socket b/tcp_socket index ffe6b2b..c7aced7 160000 --- a/tcp_socket +++ b/tcp_socket @@ -1 +1 @@ -Subproject commit ffe6b2bf3ba5829361349ff318cb721cb6f42266 +Subproject commit c7aced7c3f708e81ccbb17163267f96642dd5381 diff --git a/z_server/.venv_required b/z_server/.venv_required new file mode 100644 index 0000000..e69de29 diff --git a/z_server/Makefile b/z_server/Makefile new file mode 100644 index 0000000..1503a9f --- /dev/null +++ b/z_server/Makefile @@ -0,0 +1,133 @@ +# git helper Makefile: Version 2.5 (2025-08-11) +default: help + +.ONESHELL: +SHELL = /usr/bin/bash +MAKEFLAGS += --no-print-directory +.SILENT: + +-include __make.d__/*.mk + +GIT_FLAG = ./.git +VENV_FLAG = ./.venv_required +VENV_FOLDER = ./venv + +localhelp: + +help: + echo "Possible common options are:" + echo " - init - Initialise the repository and all folders below with a Makefile" + echo " - giti - Get the git status of all submodules including their submodules" + echo " - update_submodules - Set all submodules to remote master" + echo " - venv_flag - Set the venev flag for the base folder. A venv will be generated" + echo " - clean - clean up" + echo " - deepclean - clean up this and all Makefiles below" + $(MAKE) localhelp + echo "You are able to create files make.d/*.mk and add local rules there. " + echo " - localinit: print_head - Will be executed as last step in the init process." + echo " - localhelp: print_head - Will be executed in th middle of the help text generation" + echo " - localclean:print_head - Will be executed before the clean rule" + +localinit: + +init: print_head + # Init git repo + if [[ -e $(GIT_FLAG) ]]; then + echo -e "\033[1;33mInitialising git submodules...\e[0m" + git submodule init + git submodule update + echo + fi + # Init submodules + SUBDIRS=$$(find . -maxdepth 2 -mindepth 2 -name Makefile | sort) + for subdir in $$SUBDIRS; do + $(MAKE) --no-print-directory -C $$(dirname $$subdir) init + done + if [[ $$SUBDIRS = *[![:space:]]* ]]; then + $(MAKE) print_head + fi + # Create venv if needed + if [[ -e $(VENV_FLAG) ]]; then + BASEPATH=$$(pwd -P) + # + # Create venv + # + if [ ! -e venv ];then + echo -e "\033[1;33mCreating venv in $$BASEPATH...\e[0m" + python3 -m venv $$BASEPATH/venv > /dev/null 2>&1 + else + echo -e "\033[1;33mVirtualenv already exists in $$BASEPATH...\e[0m" + fi + echo + + # + # Install modules + # + echo -e "\033[1;33mInstalling modules to venv in $$BASEPATH...\e[0m" + for req_file in $$(find -L $$BASEPATH -name requirements.txt 2> /dev/null); do + # echo " $$req_file" + while read req_mod; do + if [[ $$req_mod = *[![:space:]]* ]]; then + # req_mod is not empty + OUT=$$($$BASEPATH/venv/bin/pip install -U $$req_mod 2>&1 ) + if [[ $$OUT =~ "Successfully installed" ]]; then + echo -e " * \033[1;32m$$req_mod installed.\e[0m" + elif [[ $$OUT =~ "already satisfied" ]]; then + echo -e " * \033[1;36m$$req_mod already installed.\e[0m" + else + echo -e " * \033[1;31m$$req_mod installation FAILED!\e[0m" + #echo $$OUT + fi + fi + done < $$req_file + done + echo + fi + # Start local init + echo -e "\033[1;33mDoing localinit...\e[0m" + $(MAKE) localinit + +update_submodules: + git submodule foreach "git fetch && git checkout master && git pull && git submodule init && git submodule update" + +giti_this: print_head + giti + echo " Submodules:" + git submodule --quiet foreach "echo -n ' ' && giti" + +giti: + git submodule --quiet foreach "[ -e Makefile ] && make --no-print-directory giti_this || :" + +localclean: + +clean: localclean + if [[ ! -e $(VENV_FLAG) ]]; then + if [[ -d $(VENV_FOLDER) ]]; then + rm -rf $(VENV_FOLDER) + fi + fi + +cleanall: clean + for subdir in $$(find . -maxdepth 2 -mindepth 2 -name Makefile | sort); do + $(MAKE) --no-print-directory -C $$(dirname $$subdir) cleanall + done + + +venv_flag: + if [[ ! -e $(VENV_FLAG) ]]; then + touch $(VENV_FLAG) + if [[ -e $(GIT_FLAG) ]]; then + git add $(VENV_FLAG) + fi + fi + +print_head: + DIRNAME=$$(basename $$(pwd)) + DIRLENGTH=$${#DIRNAME} + echo -ne "\n\n\033[1;34m╔═" + for i in $$(seq 1 $$DIRLENGTH); do echo -n "═"; done + echo -e "═╗" + echo -e "║ $$DIRNAME ║" + echo -ne "╚═" + for i in $$(seq 1 $$DIRLENGTH); do echo -n "═"; done + echo -e "═╝\033[00m" diff --git a/z_server/config_example/config.j2 b/z_server/config_example/config.j2 index 5a8ccf4..926f8c8 100644 --- a/z_server/config_example/config.j2 +++ b/z_server/config_example/config.j2 @@ -1,9 +1,15 @@ import logging -DEBUG = False # False: logging to stdout with given LOGLEVEL - True: logging all to localhost:19996 and warnings or higher to stdout -LOGLEVEL = logging.INFO - +DEBUG = False +# +# Logging +# APP_NAME = "z_monitor" +LOG_HOSTNAME = "localhost" # When DEBUG is True +LOG_LEVEL = logging.INFO # STDOUT logging + +CHRISTMAS = False + MQTT_SERVER = "{{ server_nagios_hostname }}" MQTT_PORT = 1883 @@ -11,3 +17,4 @@ MQTT_USER = "{{ server_nagios_username }}" MQTT_PASSWORD = "{{ server_nagios_password }}" SOCK_PROT_PORT = 8380 + diff --git a/z_server/devdi b/z_server/devdi index 8f8ace1..5a5679b 160000 --- a/z_server/devdi +++ b/z_server/devdi @@ -1 +1 @@ -Subproject commit 8f8ace13c131abf9c4dfb79087fa1c13c76805d2 +Subproject commit 5a5679b0baa9ba978f75d8581cb0dc7c13158e34 diff --git a/z_server/devices/__init__.py b/z_server/devices/__init__.py index 2537a59..5343000 100644 --- a/z_server/devices/__init__.py +++ b/z_server/devices/__init__.py @@ -68,6 +68,9 @@ class base(object): if message.topic == self.topic: self.last_device_msg = time.time() + def set_ch_name(self, key, name): + pass + def target(self, key, value): tm_t, value_t = self.__target_storage__.get(key, (0, None)) if value != value_t: @@ -188,6 +191,10 @@ class tradfri_sw_br_ct(base): pass +class hue_sw_br_ct(base): + pass + + class tradfri_button(base): LAST_MSG_WARNING = 24 * 60 * 60 LAST_MSG_ERROR = 48 * 60 * 60 @@ -238,7 +245,10 @@ class silvercrest_motion_sensor(base): class my_powerplug(base): - pass + KEY_OUTPUT_0 = None + KEY_OUTPUT_1 = None + KEY_OUTPUT_2 = None + KEY_OUTPUT_3 = None class audio_status(base): @@ -301,3 +311,24 @@ class my_ambient(base): return self.__nagios_return__(DID_HEARTBEAT, status, msg) else: return super().status(key) + +class videv_sw(base): + pass +class videv_sw_br(base): + pass +class videv_sw_br_ct(base): + pass +class videv_sw_tm(base): + pass +class videv_sw_mo(base): + pass +class videv_hea(base): + pass +class videv_pure_switch(base): + pass +class videv_multistate(base): + pass +class videv_audio_player(base): + pass +class videv_all_off(base): + pass \ No newline at end of file diff --git a/z_server/mqtt b/z_server/mqtt index 14e56cc..c2b3285 160000 --- a/z_server/mqtt +++ b/z_server/mqtt @@ -1 +1 @@ -Subproject commit 14e56ccdbf6594f699b4afcfb4acafe9b899e914 +Subproject commit c2b32852127bc1a3aca078a2dad3993f46cb81c2 diff --git a/z_server/report b/z_server/report index 7003c13..1526900 160000 --- a/z_server/report +++ b/z_server/report @@ -1 +1 @@ -Subproject commit 7003c13ef8c7e7c3a55a545cbbad4039cc024a9f +Subproject commit 152690007a3b87ee0047bcad78b2673111ff1928 diff --git a/z_server/z_server.py b/z_server/z_server.py index 166f858..c121d84 100644 --- a/z_server/z_server.py +++ b/z_server/z_server.py @@ -1,26 +1,48 @@ import config -import devdi.devices as devices -import logging +import devdi.rooms as rooms +import devdi.topic import mqtt import report import z_protocol -import socket_protocol import tcp_socket import time -logger = logging.getLogger(config.APP_NAME) +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__": - # - # Logging - # - if config.DEBUG: - report.appLoggingConfigure(None, 'stdout', ((config.APP_NAME, logging.DEBUG), ), - target_level=logging.WARNING, fmt=report.SHORT_FMT, host='localhost', port=19996) - else: - report.stdoutLoggingConfigure(((config.APP_NAME, config.LOGLEVEL), ), report.SHORT_FMT) - # # MQTT Client # @@ -29,7 +51,22 @@ if __name__ == "__main__": # # Smarthome physical Devices # - pd = devices.physical_devices(mc) + 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