autocommit

This commit is contained in:
ALIHAN DIKEL
2024-10-29 21:46:31 +03:00
parent 104da61427
commit 57f12e0ab4
2 changed files with 51 additions and 41 deletions

View File

@@ -1,46 +1,63 @@
import os
import re
import subprocess
import time
from loguru import logger
import re
import nmcli
from pythonping import ping
# nmcli connection show djituad2_plus
tello_ssid = os.environ["TELLO_SSID_NAME"]
conns = nmcli.connection()
def renew_ip_wlan1(timeout=15):
tello_ssid = os.environ["TELLO_SSID_NAME"]
def check_dhcp_ip(log):
address = os.environ["TELLO_STATION_IP"]
# Regex to find the specific IP address in a successful DHCPACK or bound message
pattern = rf"(DHCPACK of {address}|bound to {address})"
match = re.search(pattern, log)
return bool(match)
def release_and_renew(timeout=20):
result_info = {
"status": None,
"release_stderr": None,
"renew_stderr": None,
"renew_status": None
}
try:
# Release the IP address on wlan1
print("releasing")
release_cmd = ["sudo", "dhclient", "-r", "wlan1"]
release_process = subprocess.run(release_cmd, capture_output=True, text=True, timeout=timeout)
# Renew the IP address on wlan1
print("renewing")
renew_cmd = ["sudo", "timeout", str(timeout), "dhclient", "-v", "wlan1"]
renew_process = subprocess.run(renew_cmd, capture_output=True, text=True, timeout=timeout)
result_info = {
"release_stdout": release_process.stdout,
"release_stderr": release_process.stderr,
"renew_stdout": renew_process.stdout,
"renew_stderr": renew_process.stderr,
"renew_status": renew_process.returncode
}
result_info["release_stderr"] = release_process.stderr
result_info["renew_stderr"] = renew_process.stderr
result_info["renew_status"] = renew_process.returncode
result_info["status"] = "done"
return result_info
except subprocess.TimeoutExpired:
return {"error": "timeout"}
result_info["status"] = "timeout"
return result_info
except Exception as e:
return {"error": str(e)}
result_info["status"] = str(e)
return result_info
if __name__ == "__main__":
info = renew_ip_wlan1()
print(info)
data = {'release_stdout': '', 'release_stderr': '', 'renew_stdout': '', 'renew_stderr': 'Internet Systems Consortium DHCP Client 4.4.3-P1\nCopyright 2004-2022 Internet Systems Consortium.\nAll rights reserved.\nFor info, please visit https://www.isc.org/software/dhcp/\n\nListening on LPF/wlan1/b0:48:7a:93:3e:0d\nSending on LPF/wlan1/b0:48:7a:93:3e:0d\nSending on Socket/fallback\nDHCPDISCOVER on wlan1 to 255.255.255.255 port 67 interval 6\nDHCPDISCOVER on wlan1 to 255.255.255.255 port 67 interval 13\nDHCPDISCOVER on wlan1 to 255.255.255.255 port 67 interval 18\nDHCPOFFER of 192.168.10.4 from 192.168.10.1\nDHCPREQUEST for 192.168.10.4 on wlan1 to 255.255.255.255 port 67\nDHCPACK of 192.168.10.4 from 192.168.10.1\nError: ipv4: Address already assigned.\nbound to 192.168.10.4 -- renewal in 1689 seconds.\n', 'renew_status': 0}
while True:
time.sleep(1)
info = release_and_renew()
print(info)

View File

@@ -8,7 +8,8 @@ from djitellopy import TelloException
from dotenv import load_dotenv
from loguru import logger
from nmcli import TimeoutExpiredException
from pythonping import ping
from services.connections import release_and_renew, check_dhcp_ip
load_dotenv("../../env")
@@ -19,23 +20,22 @@ import asyncio
import os
async def maintain_connection_to_tello(state):
tello_ssid = os.environ["TELLO_SSID_NAME"]
state["connection"] = "NOK"
conn_attempt_timeout = 30
while True:
try:
if state["connection"] == "NOK":
await asyncio.to_thread(nmcli.connection.reload)
await asyncio.to_thread(nmcli.connection.up, tello_ssid, conn_attempt_timeout)
state["connection"] = "OK"
logger.success("Connected to repeater: *_plus")
except TimeoutExpiredException as tee:
logger.warning(f"Failed to connect to Tello, timeout duration was {conn_attempt_timeout}s")
state["connection"] = "NOK"
# state["stats"] = "No stats available" # Leave this commented out to see the final stats state
if state["connection"] == "NOK":
await asyncio.to_thread(nmcli.connection.reload)
resp = await asyncio.to_thread(release_and_renew, 20)
if resp["status"] == "done":
if await asyncio.to_thread(check_dhcp_ip, resp["renew_stderr"]):
state["connection"] = "OK"
logger.success("Connected to Tello")
elif resp["status"] == "timeout":
logger.warning(f"No tello exist")
state["connection"] = "NOK"
else:
logger.error(f'There is an error: {resp["status"]}')
# state["stats"] = "No stats available" # Leave this commented out to see the final stats state
async def collect_flight_stats(state):
tello = state["tello"]
@@ -68,15 +68,8 @@ async def collect_flight_stats(state):
else:
if counter % 10 == 0:
logger.warning("missing connection, pinging tello")
resp_cmd = await asyncio.to_thread(tello.send_command_with_return, "command", 3)
if "Aborting command" in resp_cmd:
logger.warning("no tello responded")
else:
bat = await asyncio.to_thread(tello.get_battery)
logger.success(f"tello exists: {bat}")
counter += 1
await asyncio.sleep(1.001)
await asyncio.sleep(1)
async def forward_video_stream(state):