Files
edelweiss/projmon/projmon.py
2026-02-05 13:01:16 +01:00

89 lines
2.7 KiB
Python

import os
import sys
import time
import logging
import json
import projrequest as projrequest
from utils import *
from influxdb_client_3 import InfluxDBClient3, Point
# Get environment variables
env = dict(os.environ)
LOGGER: logging.Logger
##################
###### MAIN ######
##################
def main() -> int:
INTERVAL = int(env['INTERVAL'])
try:
# Init InfluxDB-v3 Client
write_client = InfluxDBClient3(host=env['INFLUXDB_URL'],
token=env['INFLUXDB_TOKEN'],
database=env['INFLUXDB_DATABASE'])
except Exception as e:
LOGGER.error(e)
return 1
finally:
LOGGER.info(f"InfluxDB Connected: [{env['INFLUXDB_URL']}/{env['INFLUXDB_DATABASE']}]")
##############################
########## MAIN LOOP #########
##############################
last: float = 0
handler: SignalHandler = SignalHandler(LOGGER)
projector: projrequest.ProjectorConnection = projrequest.ProjectorConnection(
ip=env['PROJECTOR_IP'],
username=env['PROJECTOR_USER'],
password=env['PROJECTOR_PASSWORD']
)
while handler.running:
try:
now:float = time.time()
resp = projector.get(path=['status', 'storage', 'info'], params={"area":"DCP"})
if resp is not None:
print(json.dumps(resp, indent=2))
last: float = time.time()
cycle_time: float = last - now
LOGGER.debug(f"Cycle Time: {cycle_time:4.3f}")
time.sleep(INTERVAL-cycle_time)
time.sleep(INTERVAL-cycle_time)
except Exception as e:
print(f"Unexpected exception: [{e}]")
return 1
pass
return 0
if __name__ == "__main__":
# Logger Constants
LOG_FORMAT = '%(asctime)s| %(levelname)-7s|%(funcName)-10s|%(lineno)-3d: %(message)-50s'
# Enabling Logger
LOGGER = logging.getLogger(__name__)
LOGGER.setLevel(logging.DEBUG)
LOGGER.propagate = False
formatter = logging.Formatter(LOG_FORMAT, None)
levels = logging.getLevelNamesMapping()
# File logging
log_name = os.path.abspath(env['LOG_FILE'])
fh = logging.FileHandler(log_name)
fh.setLevel(levels[env['LOG_FILE_LVL']])
fh.setFormatter(formatter)
LOGGER.addHandler(fh)
# Console logging
cl = logging.StreamHandler(sys.stdout)
cl.setLevel(levels[env['LOG_CLI_LVL']])
cl.setFormatter(formatter)
LOGGER.addHandler(cl)
LOGGER.warning(f"Routermon started on: {time.asctime()}")
LOGGER.info(f"Routermon BUILD: {env.get("VER", "Test")}")
while main():
LOGGER.error("Main thread exited unexpectedly")
time.sleep(15)
sys.exit(0)