Skip to content

Commit

Permalink
Change navigation logic & restructure
Browse files Browse the repository at this point in the history
  • Loading branch information
nickdelis committed Dec 23, 2024
1 parent 762e7a2 commit e37e01f
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 49 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
.idea/
.venv/
.venv/
build/
dist/
Broadlink_Reader.egg-info/
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,17 @@ them to a file for future use.

To get started with Broadlink Reader, clone the repository from GitHub:

```
```sh
git clone https://github.com/JexSrs/broadlink-reader.git
cd broadlink-reader
```

python -m venv .venv
source .venv/bin/activate
and install the project:
```sh
python3 setup.py install
```
or
```sh
pip install -r requirements.txt
```

Expand All @@ -29,7 +34,7 @@ pip install -r requirements.txt
You can run the Broadlink Reader using the `main.py` script. The following options are available to customize your
interaction with the Broadlink device:

```
```sh
python main.py --ip 192.168.1.100 # Remote's IP address
```

Expand Down
8 changes: 5 additions & 3 deletions utils.py → cli_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,12 @@ def print_keys(actions: dict, max_keys=10):
max_length = -1
for index, action in enumerate(keys):
color = get_action_color(actions, action)
display_text = color + f"{index}: {action}{' (select to expand)' if isinstance(actions[action], dict) else ''}"
display_text = color + f"{index}: {action}"
# Find the largest string when keys >= 10
if len(keys) >= max_keys and len(keys) >= max_length:
max_length = len(display_text)
if len(keys) >= max_keys:
if len(display_text) >= max_length:
max_length = len(display_text)

display_texts.append(display_text)
else:
print(display_text)
Expand Down
61 changes: 20 additions & 41 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
from broadlink import Device
from colorama import Fore, init

import bdlk
from utils import print_keys
import remote
from cli_utils import print_keys

init(autoreset=True)

Expand All @@ -16,65 +16,46 @@


def navigate_actions(device: Device, actions):
"""Navigate through the nested actions."""
global read_timeout

current_actions = actions
path = []
display_path = []

current_path = []
while True:
if display_path:
print(Fore.LIGHTBLUE_EX + f'\nCurrent path: {' > '.join(display_path)}')
if current_path:
print(Fore.LIGHTBLUE_EX + f'\nCurrent path: {' > '.join(current_path)}')
else:
print(Fore.LIGHTBLUE_EX + '\nHome Page')
print(Fore.LIGHTBLUE_EX + '\nCurrent path: Home')
print("Choose an action by index to register:")
print(Fore.WHITE + "-1: " + ("Go back" if path else "Exit"))
action_keys = print_keys(current_actions)
print(Fore.WHITE + "-1: " + ("Go back" if current_path else "Exit"))

curr_actions = actions
for path in current_path:
curr_actions = curr_actions[path]
action_keys = print_keys(curr_actions)

choice = -1
try:
choice = int(input("> "))
except ValueError:
print(Fore.RED + "Please enter a valid number.")
continue

if choice == -1:
if path:
if current_path:
# Go back one level
current_actions = path.pop()

# Fix "Go back" bug
for action in reversed(display_path):
if action in current_actions:
if not isinstance(current_actions[action], dict) and path:
current_actions = path.pop()
display_path.pop()

display_path.pop()
current_path.pop()
else:
break
elif 0 <= choice < len(action_keys):
# Save current level
selected_action = action_keys[choice]
if selected_action not in display_path:
display_path.append(selected_action)
path.append(current_actions)

# Go deeper into the next level
if isinstance(current_actions[selected_action], dict):
current_actions = current_actions[selected_action]
if isinstance(curr_actions[selected_action], dict):
current_path.append(selected_action)
else:
# If it's a leaf and read the packet
try:
packet = bdlk.handle_action(device, read_timeout)
packet = remote.handle_action(device, read_timeout)
except Exception as e:
print(Fore.RED + 'Failed to read from remote: ' + str(e))
continue

# Save the packet in the nested structure
parent_action = path[-1] if path else actions
parent_action[selected_action] = packet
curr_actions[selected_action] = packet

# Save the updated data back to the output file
with open(mFile, 'w') as file:
Expand All @@ -99,10 +80,8 @@ def main():
args = parser.parse_args()

read_timeout = args.read
data = {}

# Load existing data from the input file if it exists
mFile = args.file
# Load existing data from the input file if it exists
if os.path.exists(mFile):
with open(mFile, 'r') as f:
try:
Expand All @@ -111,7 +90,7 @@ def main():
print(Fore.WHITE + "Could not read input file. Starting with empty data.")

# Setup code
device = bdlk.get_device(ip_address=args.ip, port=args.port, timeout=args.timeout)
device = remote.get_device(ip_address=args.ip, port=args.port, timeout=args.timeout)
if not device:
exit(1)

Expand Down
File renamed without changes.
16 changes: 16 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from setuptools import setup, find_packages

with open("requirements.txt") as f:
requirements = f.read().splitlines()

setup(
name="Broadlink Reader",
version="0.1.0",
description="A Python CLI tool for learning and capturing IR commands from Broadlink devices, with options for customizable connection settings and command saving.",
long_description=open("README.md").read(),
long_description_content_type="text/markdown",
url="https://github.com/JexSrs/broadlink-reader",
packages=find_packages(),
install_requires=requirements,
python_requires=">=3.11",
)

0 comments on commit e37e01f

Please sign in to comment.