parent
1cd70d5e5e
commit
2799621420
2 changed files with 31 additions and 34 deletions
app
File diff suppressed because one or more lines are too long
63
app/utils.py
63
app/utils.py
|
@ -1,6 +1,7 @@
|
||||||
import telnetlib
|
import telnetlib
|
||||||
import re
|
import re
|
||||||
from app.settings import powerbars, groups
|
from app.settings import powerbars, groups
|
||||||
|
import time
|
||||||
|
|
||||||
def check_input_error(tn):
|
def check_input_error(tn):
|
||||||
tn.write("/\r\n".encode('ascii'))
|
tn.write("/\r\n".encode('ascii'))
|
||||||
|
@ -18,45 +19,41 @@ def get_telnet_connection(powerbar):
|
||||||
|
|
||||||
|
|
||||||
def run_telnet_command(powerbar, command):
|
def run_telnet_command(powerbar, command):
|
||||||
try:
|
retries = 5
|
||||||
tn = get_telnet_connection(powerbar)
|
for _ in range(retries):
|
||||||
|
try:
|
||||||
if check_input_error(tn):
|
tn = get_telnet_connection(powerbar)
|
||||||
tn.write(f"{command}\r\n".encode('ascii'))
|
tn.write(f"{command}\r\n".encode('ascii'))
|
||||||
tn.close()
|
tn.close()
|
||||||
return True
|
return True
|
||||||
tn.close()
|
except Exception as e:
|
||||||
return False
|
print(f"Run Telnet Command Error: {e}")
|
||||||
except Exception as e:
|
time.sleep(1) # Wait for 1 second before retrying
|
||||||
print(f"Run Telnet Command Rrror: {e}")
|
pass
|
||||||
return False
|
|
||||||
|
|
||||||
|
|
||||||
def get_baytech_status_outlet_telnet(powerbar):
|
def get_baytech_status_outlet_telnet(powerbar):
|
||||||
try:
|
retries = 5
|
||||||
# Create a Telnet object and connect to the host
|
for _ in range(retries):
|
||||||
tn = telnetlib.Telnet(powerbars[powerbar]['host'], powerbars[powerbar]['port'])
|
try:
|
||||||
# Send the command you want to execute
|
# Create a Telnet object and connect to the host
|
||||||
command = "status"
|
tn = telnetlib.Telnet(powerbars[powerbar]['host'], powerbars[powerbar]['port'])
|
||||||
|
# Send the command you want to execute
|
||||||
if check_input_error(tn):
|
command = "status"
|
||||||
tn.write(command.encode('ascii') + b"\r\n")
|
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()
|
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+)"
|
pattern = r"(\d+)\)\.\.\.Outlet\s+(\d+)\s+:\s+(\w+)"
|
||||||
outlets = {}
|
outlets = {}
|
||||||
matches = re.findall(pattern, output)
|
matches = re.findall(pattern, output)
|
||||||
for match in matches:
|
for match in matches:
|
||||||
number = int(match[1])
|
number = int(match[1])
|
||||||
status = match[2]
|
status = match[2]
|
||||||
outlets[number] = status
|
outlets[number] = status
|
||||||
|
|
||||||
return outlets
|
return outlets
|
||||||
except:
|
except:
|
||||||
return None
|
pass
|
||||||
|
return None
|
||||||
|
|
Loading…
Reference in a new issue