mirror of
https://github.com/suchmememanyskill/CYD-Klipper.git
synced 2026-03-21 05:33:24 +00:00
Lower cpu usage
This commit is contained in:
@@ -105,58 +105,57 @@ def write(text : str, write : bool):
|
|||||||
def main():
|
def main():
|
||||||
while True:
|
while True:
|
||||||
# Read a line from the serial port
|
# Read a line from the serial port
|
||||||
if ser.in_waiting > 0:
|
line = ser.readline().decode('utf-8').strip()
|
||||||
line = ser.readline().decode('utf-8').strip()
|
if line.startswith("HTTP_REQUEST") or line.startswith("HTTP_BINARY"):
|
||||||
if line.startswith("HTTP_REQUEST") or line.startswith("HTTP_BINARY"):
|
print(f">>> {line}")
|
||||||
print(f">>> {line}")
|
# Parse the parameters
|
||||||
# Parse the parameters
|
try:
|
||||||
try:
|
parts = line.split(' ', 3)
|
||||||
parts = line.split(' ', 3)
|
timeout_ms, request_type, url_path = int(parts[1]), parts[2], parts[3]
|
||||||
timeout_ms, request_type, url_path = int(parts[1]), parts[2], parts[3]
|
|
||||||
|
|
||||||
ignore_timeout = timeout_ms <= 0
|
ignore_timeout = timeout_ms <= 0
|
||||||
binary = line.startswith("HTTP_BINARY")
|
binary = line.startswith("HTTP_BINARY")
|
||||||
|
|
||||||
if ignore_timeout:
|
if ignore_timeout:
|
||||||
timeout_ms = 1000;
|
timeout_ms = 1000;
|
||||||
|
|
||||||
# Construct the full URL
|
# Construct the full URL
|
||||||
full_url = f"{PROTOCOL}://{HOSTNAME}:{PORT}{url_path}"
|
full_url = f"{PROTOCOL}://{HOSTNAME}:{PORT}{url_path}"
|
||||||
|
|
||||||
# Make the HTTP request based on the type
|
# Make the HTTP request based on the type
|
||||||
response = None
|
response = None
|
||||||
if request_type.upper() == "GET":
|
if request_type.upper() == "GET":
|
||||||
response = requests.get(full_url, timeout=timeout_ms / 1000)
|
response = requests.get(full_url, timeout=timeout_ms / 1000)
|
||||||
elif request_type.upper() == "POST":
|
elif request_type.upper() == "POST":
|
||||||
response = requests.post(full_url, timeout=timeout_ms / 1000)
|
response = requests.post(full_url, timeout=timeout_ms / 1000)
|
||||||
else:
|
else:
|
||||||
write("400 Unsupported request type", not ignore_timeout)
|
write("400 Unsupported request type", not ignore_timeout)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# Send response back over serial
|
# Send response back over serial
|
||||||
if response != None:
|
if response != None:
|
||||||
if binary:
|
if binary:
|
||||||
if response.status_code != 200:
|
if response.status_code != 200:
|
||||||
write("00000000", not ignore_timeout)
|
write("00000000", not ignore_timeout)
|
||||||
else:
|
|
||||||
length = len(response.content)
|
|
||||||
ser.write(f"{length:>08}".encode('utf-8'))
|
|
||||||
ser.write(response.content)
|
|
||||||
print(f"<<< (Binary data of {length} bytes)")
|
|
||||||
else:
|
else:
|
||||||
status_code = response.status_code
|
length = len(response.content)
|
||||||
body = response.text.replace('\n', ' ') # Trim and sanitize body for serial
|
ser.write(f"{length:>08}".encode('utf-8'))
|
||||||
message = f"{status_code} {body}"
|
ser.write(response.content)
|
||||||
write(message, not ignore_timeout)
|
print(f"<<< (Binary data of {length} bytes)")
|
||||||
except (IndexError, ValueError) as e:
|
else:
|
||||||
write(f"400 Malformed request {str(e)}", not ignore_timeout)
|
status_code = response.status_code
|
||||||
except requests.exceptions.ReadTimeout as e:
|
body = response.text.replace('\n', ' ') # Trim and sanitize body for serial
|
||||||
print("Request timed out.")
|
message = f"{status_code} {body}"
|
||||||
pass
|
write(message, not ignore_timeout)
|
||||||
except requests.exceptions.RequestException as e:
|
except (IndexError, ValueError) as e:
|
||||||
write("500 Request failed", not ignore_timeout)
|
write(f"400 Malformed request {str(e)}", not ignore_timeout)
|
||||||
else:
|
except requests.exceptions.ReadTimeout as e:
|
||||||
print(f"[LOG] {line}")
|
print("Request timed out.")
|
||||||
|
pass
|
||||||
|
except requests.exceptions.RequestException as e:
|
||||||
|
write("500 Request failed", not ignore_timeout)
|
||||||
|
else:
|
||||||
|
print(f"[LOG] {line}")
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
while True:
|
while True:
|
||||||
|
|||||||
Reference in New Issue
Block a user