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