diff --git a/.vscode/launch.json b/.vscode/launch.json index a08d8af..79b83e3 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -1,6 +1,7 @@ { "version": "0.2.0", "configurations": [ + { "name": "Python Debugger: Tikmon", "type": "debugpy", @@ -37,6 +38,25 @@ "LOG_FILE_LVL": "WARNING", "LOG_CLI_LVL": "DEBUG" } + }, + { + "name": "Python Debugger: PROJmon", + "type": "debugpy", + "request": "launch", + "program": "${file}", + "console": "integratedTerminal", + "env": { + "INTERVAL" : "5", + "INFLUXDB_TOKEN":"apiv3_tbEpA8JmIRTfr8Wbw9npD79BcMlFsnV4_jhdt-CdUP53Mos61KBODGaggl2g5oKZZvZrZu3e6mpob6zorhEdbg", + "INFLUXDB_URL" : "http://edelweiss-srv:8181", + "INFLUXDB_DATABASE" : "edelweiss30d", + "LOG_FILE": "/tmp/upsmon.log", + "LOG_FILE_LVL": "WARNING", + "LOG_CLI_LVL": "DEBUG", + "PROJECTOR_IP":"192.168.31.10", + "PROJECTOR_USER":"alberto", + "PROJECTOR_PASSWORD":"alberto" + } } ] } \ No newline at end of file diff --git a/projmon/projmon.py b/projmon/projmon.py index 919cd18..fc51480 100644 --- a/projmon/projmon.py +++ b/projmon/projmon.py @@ -3,10 +3,9 @@ import sys import time import logging import json -import requests -import xmltodict -from pyutils.utils import * +import projrequest as projrequest +from utils import * from influxdb_client_3 import InfluxDBClient3, Point # Get environment variables @@ -29,6 +28,32 @@ def main() -> int: 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__": diff --git a/projmon/projrequest.py b/projmon/projrequest.py new file mode 100644 index 0000000..2c74b54 --- /dev/null +++ b/projmon/projrequest.py @@ -0,0 +1,42 @@ +import requests +import xmltodict +from requests.auth import HTTPBasicAuth +from datetime import datetime + +from typing import Dict, List, Any + +class ProjectorConnection(): + ip: str + username: str + password: str + auth: HTTPBasicAuth + + def __init__(self, ip: str, username: str, password: str) -> None: + self.ip = ip + self.username = username + self.password = password + self.auth = HTTPBasicAuth(username=self.username, password=self.password) + pass + + def get(self, path: List[str], params: Dict[str, str] | None = None) -> Dict[str, Any] | None: + response: requests.Response = requests.get( + url=f"https://{self.ip}/{"/".join(path)}", + params=params, + headers={ + "Accept": "application/xml" + }, + auth=self.auth, + allow_redirects=False, + timeout=4, + verify=False + ) + if response.status_code == 200: # HTTP ok response + rv: dict = {} + content: dict = xmltodict.parse(response.text)["SMSMessage"] # Common containert for all messages + header: dict = content["MessageHeader"] + body: dict = content["MessageBody"] + rv['type'] = header.get("Type", None) + rv['timestamp'] = int(header.get("Timestamp", datetime.now())) + rv['body'] = body[rv["type"]] + return rv + return None diff --git a/projmon/utils.py b/projmon/utils.py new file mode 100644 index 0000000..d90ec35 --- /dev/null +++ b/projmon/utils.py @@ -0,0 +1,37 @@ +import signal +from logging import Logger +from influxdb_client_3 import Point + +class SignalHandler: + running: bool + logger: Logger + + def __init__(self, logger): + self.running: bool = True + self.logger: Logger = logger + signal.signal(signal.SIGINT, self._handle_sigint) + signal.signal(signal.SIGTERM, self._handle_sigint) + + def _handle_sigint(self, signum, frame): + self.logger.info(f"Received SIGNAL: {signal.strsignal(signum)}") + self.running = False + +def dict2Point(measurement: str, fields: dict, tags: dict | None = None) -> Point: + p = Point(measurement) + for k,v in fields.items(): + p.field(k,v) + if tags: + for k,v in tags.items(): + p.tag(k,v) + return p + +def convertInt(d: dict) -> dict: + for k,v in d.items(): + if str.isdecimal(v): + d[k] = int(v) + return d + +def convertIntList(l: list[dict]) -> list[dict]: + for n,d in enumerate(l): + l[n] = convertInt(d) + return l \ No newline at end of file diff --git a/projmon/xmlresponses/importjob.xml b/projmon/xmlresponses/importjob.xml deleted file mode 100644 index bd4f2ff..0000000 --- a/projmon/xmlresponses/importjob.xml +++ /dev/null @@ -1,27 +0,0 @@ - - - -1 - Command - 1770199074967 - LSM-100v2 - - - - AddImportJob - - - Folder - ftp://dcpftp:dcpftp@192.168.31.25:21/RicercatoriFon_PRO-1-30_F-178_IT-XX_20_2K_20251126_CIN_SMPTE_OV - - - Resume - true - - - Validate - true - - - - - \ No newline at end of file