test get class
This commit is contained in:
20
.vscode/launch.json
vendored
20
.vscode/launch.json
vendored
@@ -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"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -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__":
|
||||
|
||||
42
projmon/projrequest.py
Normal file
42
projmon/projrequest.py
Normal file
@@ -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
|
||||
37
projmon/utils.py
Normal file
37
projmon/utils.py
Normal file
@@ -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
|
||||
@@ -1,27 +0,0 @@
|
||||
<SMSMessage>
|
||||
<MessageHeader>
|
||||
<Id>-1</Id>
|
||||
<Type>Command</Type>
|
||||
<Timestamp>1770199074967</Timestamp>
|
||||
<Source>LSM-100v2</Source>
|
||||
</MessageHeader>
|
||||
<MessageBody>
|
||||
<Command>
|
||||
<Operation>AddImportJob</Operation>
|
||||
<ParameterList>
|
||||
<Parameter>
|
||||
<Name>Folder</Name>
|
||||
<Value>ftp://dcpftp:dcpftp@192.168.31.25:21/RicercatoriFon_PRO-1-30_F-178_IT-XX_20_2K_20251126_CIN_SMPTE_OV</Value>
|
||||
</Parameter>
|
||||
<Parameter>
|
||||
<Name>Resume</Name>
|
||||
<Value>true</Value>
|
||||
</Parameter>
|
||||
<Parameter>
|
||||
<Name>Validate</Name>
|
||||
<Value>true</Value>
|
||||
</Parameter>
|
||||
</ParameterList>
|
||||
</Command>
|
||||
</MessageBody>
|
||||
</SMSMessage>
|
||||
Reference in New Issue
Block a user