-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3c12aa4
commit df6161e
Showing
5 changed files
with
40 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,35 @@ | ||
import requests | ||
|
||
wled_ip = "1.2.3.4" # Replace with the actual IP address or hostname of your WLED device | ||
|
||
|
||
class Backend: | ||
|
||
def __init__(self, wled_base_url): | ||
def __init__(self, wled_base_url="1.2.3.4"): | ||
self.wled_base_url = wled_base_url | ||
self.led_count = self.get_led_count() | ||
|
||
def get_led_count(self): | ||
# Construct the WLED info API endpoint | ||
info_endpoint = f"http://{wled_ip}/json/info" | ||
|
||
try: | ||
# Send the HTTP GET request to WLED info API | ||
response = requests.get(info_endpoint) | ||
|
||
# Get the LED Count straight from the WLED Device :D | ||
if response.status_code == 200: | ||
info_data = response.json() | ||
return info_data['leds']['count'] | ||
else: | ||
print(f"Failed to retrieve LED count. Status code: {response.status_code}") | ||
return 0 | ||
except Exception as e: | ||
print(f"Error: {e}") | ||
return 0 | ||
info_endpoint = f"http://{self.wled_base_url}/json/info" | ||
|
||
def set_led(self, led_index: int, on: bool): | ||
# Construct the WLED API endpoint | ||
endpoint = f"http://{wled_ip}/json/state" | ||
# Send the HTTP GET request to WLED info API | ||
response = requests.get(info_endpoint) | ||
|
||
# Not the cleanest, but its honest work :3c | ||
if on is True: | ||
payload = {"seg": {"i": [led_index, "FFFFFF"]}} | ||
else: | ||
payload = {"seg": {"i": [led_index, "000000"]}} | ||
# Get the LED Count straight from the WLED Device :D | ||
if response.status_code != 200: | ||
raise ConnectionError(f"Failed to retrieve LED count. Status code: {response.status_code}") | ||
|
||
try: | ||
# Send the HTTP POST request to WLED API | ||
response = requests.post(endpoint, json=payload) | ||
info_data = response.json() | ||
return info_data['leds']['count'] | ||
|
||
def set_led(self, led_index: int, on: bool): | ||
# Construct the WLED API endpoint | ||
endpoint = f"http://{self.wled_base_url}/json/state" | ||
|
||
# Check if the request was successful (HTTP status code 200) | ||
if response.status_code == 200: | ||
print(f"LED at index {led_index} {'turned on' if on else 'turned off'} successfully.") | ||
else: | ||
print(f"Failed to set LED state. Status code: {response.status_code}") | ||
except Exception as e: | ||
print(f"Error: {e}") | ||
payload = {"seg": {"i": [led_index, "FFFFFF" if on else "000000"]}} | ||
|
||
# Send the HTTP POST request to WLED API | ||
response = requests.post(endpoint, json=payload) | ||
|
||
backend = Backend(wled_base_url=wled_ip) | ||
print(f"LED count: {backend.led_count}") | ||
# Check if the request was successful (HTTP status code 200) | ||
if response.status_code != 200: | ||
raise ConnectionError(f"WLED Backend failed to set LED state. Status code: {response.status_code}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters