exception and retry
All checks were successful
Deploy powerbar.ti Frontend Portal / Deploy-Tinance2-Frontend-Portal-Production (push) Successful in 37s

This commit is contained in:
Matthew Frost 2024-02-02 12:29:55 +01:00
parent 1cd70d5e5e
commit 2799621420
2 changed files with 31 additions and 34 deletions

File diff suppressed because one or more lines are too long

View file

@ -1,6 +1,7 @@
import telnetlib
import re
from app.settings import powerbars, groups
import time
def check_input_error(tn):
tn.write("/\r\n".encode('ascii'))
@ -18,45 +19,41 @@ def get_telnet_connection(powerbar):
def run_telnet_command(powerbar, command):
try:
tn = get_telnet_connection(powerbar)
if check_input_error(tn):
retries = 5
for _ in range(retries):
try:
tn = get_telnet_connection(powerbar)
tn.write(f"{command}\r\n".encode('ascii'))
tn.close()
return True
tn.close()
return False
except Exception as e:
print(f"Run Telnet Command Rrror: {e}")
return False
except Exception as e:
print(f"Run Telnet Command Error: {e}")
time.sleep(1) # Wait for 1 second before retrying
pass
def get_baytech_status_outlet_telnet(powerbar):
try:
# Create a Telnet object and connect to the host
tn = telnetlib.Telnet(powerbars[powerbar]['host'], powerbars[powerbar]['port'])
# Send the command you want to execute
command = "status"
if check_input_error(tn):
retries = 5
for _ in range(retries):
try:
# Create a Telnet object and connect to the host
tn = telnetlib.Telnet(powerbars[powerbar]['host'], powerbars[powerbar]['port'])
# Send the command you want to execute
command = "status"
tn.write(command.encode('ascii') + b"\r\n")
else:
# Read the output until you receive the prompt or a timeout occurs
output = tn.read_until(b"<prompt>", timeout=5).decode('ascii')
# Close the Telnet connection
tn.close()
return None
# Read the output until you receive the prompt or a timeout occurs
output = tn.read_until(b"<prompt>", timeout=5).decode('ascii')
# Close the Telnet connection
tn.close()
pattern = r"(\d+)\)\.\.\.Outlet\s+(\d+)\s+:\s+(\w+)"
outlets = {}
matches = re.findall(pattern, output)
for match in matches:
number = int(match[1])
status = match[2]
outlets[number] = status
pattern = r"(\d+)\)\.\.\.Outlet\s+(\d+)\s+:\s+(\w+)"
outlets = {}
matches = re.findall(pattern, output)
for match in matches:
number = int(match[1])
status = match[2]
outlets[number] = status
return outlets
except:
return None
return outlets
except:
pass
return None