added connect delay after

This commit is contained in:
2025-12-19 21:47:41 +01:00
parent 2048464938
commit e9def3b6f3

View File

@@ -3,7 +3,6 @@ import json
import subprocess import subprocess
import logging import logging
from time import sleep from time import sleep
from random import randint
import paho.mqtt.client as mqtt import paho.mqtt.client as mqtt
import paho.mqtt.enums as mqtt_enums import paho.mqtt.enums as mqtt_enums
@@ -11,8 +10,8 @@ import paho.mqtt.enums as mqtt_enums
# Logger options # Logger options
LOGFILE = '/mnt/logs/UPSmon.log' LOGFILE = '/mnt/logs/UPSmon.log'
#CONSOLE_DEBUG = logging.DEBUG CONSOLE_DEBUG = logging.DEBUG
CONSOLE_DEBUG = logging.INFO #CONSOLE_DEBUG = logging.INFO
#FILE_DEBUG = logging.ERROR #FILE_DEBUG = logging.ERROR
#FILE_DEBUG = logging.WARNING #FILE_DEBUG = logging.WARNING
FILE_DEBUG = logging.INFO FILE_DEBUG = logging.INFO
@@ -23,7 +22,7 @@ LOG_TIME_FORMAT = ('%m-%d %H:%M:%S')
LOGGER: logging.Logger LOGGER: logging.Logger
# UPSmon Config # UPSmon Config
CMD: str = 'apcaccess' CMD: list[str] = ['apcaccess', '-u']
T_LONG: float = 15.0 T_LONG: float = 15.0
T_SHORT: float = 5.0 T_SHORT: float = 5.0
EXPORT_KEYS: list[str] = [ EXPORT_KEYS: list[str] = [
@@ -33,12 +32,11 @@ EXPORT_KEYS: list[str] = [
def clean_data(v: str) -> float | str: def clean_data(v: str) -> float | str:
if v.isalpha(): if v.isalpha():
return v return v
return float(v.split(' ')[0]) return float(v)
def main() -> int: def main() -> int:
i: int = 0 i: int = 0
client: mqtt.Client = mqtt.Client(callback_api_version=mqtt_enums.CallbackAPIVersion.VERSION2, client: mqtt.Client = mqtt.Client(callback_api_version=mqtt_enums.CallbackAPIVersion.VERSION2)
client_id=f"upsmon_{randint(1,100)}")
while client.connect(host='10.0.2.249', port=1883) != mqtt_enums.MQTTErrorCode.MQTT_ERR_SUCCESS: while client.connect(host='10.0.2.249', port=1883) != mqtt_enums.MQTTErrorCode.MQTT_ERR_SUCCESS:
LOGGER.warning("MQTT Client retry connect") LOGGER.warning("MQTT Client retry connect")
@@ -46,6 +44,7 @@ def main() -> int:
LOGGER.info("MQTT Client connected") LOGGER.info("MQTT Client connected")
client.loop_start() client.loop_start()
sleep(1.0)
while client.is_connected(): while client.is_connected():
try: try:
@@ -66,22 +65,26 @@ def main() -> int:
sleep(T_LONG if data.get("STATUS", "ONLINE") == "ONLINE" else T_SHORT) sleep(T_LONG if data.get("STATUS", "ONLINE") == "ONLINE" else T_SHORT)
i += 1 i += 1
except KeyboardInterrupt:
LOGGER.warning("Stopping...")
client.loop_stop()
client.disconnect()
return 0
except FileNotFoundError as f: except FileNotFoundError as f:
LOGGER.error(f"{CMD} executable not found: {f}") LOGGER.error(f"{CMD} executable not found: {f}")
return f.errno if f.errno else -1 return f.errno if f.errno else -1
except subprocess.CalledProcessError as e: except subprocess.CalledProcessError as e:
LOGGER.error(f"{CMD} failed with: {e}") LOGGER.error(f"{CMD} failed with: {e}")
return e.returncode return e.returncode
except KeyboardInterrupt: except subprocess.TimeoutExpired as t:
LOGGER.warning("Stopping...") LOGGER.warning(f"{CMD} timed out: {t}")
break return int(t.timeout)
except Exception as ee: # default case catch all except Exception as ee: # default case catch all
LOGGER.error(f"Unexpected Exception: {ee}") LOGGER.error(f"Unexpected Exception: {ee}")
return -2 return -3
LOGGER.info("MQTT Client Disconnected") LOGGER.warning("MQTT Client Disconnected")
client.disconnect() return 1
return 0
if __name__ == "__main__": if __name__ == "__main__":