Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add update firmware for Avalon miner #181

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions pyasic/miners/backends/avalonminer.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

import re
from typing import List, Optional
import httpx
import aiofiles
import logging

from pyasic.data import AlgoHashRate, Fan, HashBoard, HashUnit
from pyasic.errors import APIError
Expand Down Expand Up @@ -108,6 +111,58 @@ async def reboot(self) -> bool:
return False
return False

async def upgrade_firmware(self, ip: str, port: int, file: str) -> str:
"""
Upgrade the firmware of an Avalon Miner.

Parameters:
ip (str): The IP address of the Avalon Miner.
port (int): The port number of the Avalon Miner's web interface.
file (str): Path to the firmware file to be uploaded.

Returns:
str: Result of the upgrade process.
"""
if not file:
raise ValueError("File location must be provided for firmware upgrade.")

async with aiofiles.open(file, "rb") as f:
upgrade_contents = await f.read()

url = f"http://{ip}:{port}/cgi-bin/upgrade"
data = {'version': self.get_fw_ver()}

try:
async with httpx.AsyncClient() as client:
1e9abhi1e10 marked this conversation as resolved.
Show resolved Hide resolved
response = await client.post(
url,
files={'firmware': upgrade_contents},
data=data,
auth=('root', 'root'),
timeout=60
)

response_text = response.text

if 'Upgrade success' in response_text:
logging.info("Firmware upgrade process completed successfully for Avalon Miner.")
return "Firmware upgrade to version {} successful.".format(self.get_fw_ver())
else:
return f"Firmware upgrade failed. Response: {response_text}"

except FileNotFoundError as e:
logging.error(f"File not found during the firmware upgrade process: {e}")
raise
except ValueError as e:
logging.error(f"Validation error occurred during the firmware upgrade process: {e}")
raise
except OSError as e:
logging.error(f"OS error occurred during the firmware upgrade process: {e}")
raise
except Exception as e:
logging.error(f"An unexpected error occurred during the firmware upgrade process: {e}", exc_info=True)
raise

@staticmethod
def parse_stats(stats):
_stats_items = re.findall(".+?\\[*?]", stats)
Expand Down