Made consistent API calls

This commit is contained in:
1e9abhi1e10
2024-06-15 17:44:20 +05:30
parent eefb055a3f
commit 999e8ef318
2 changed files with 59 additions and 25 deletions

View File

@@ -15,6 +15,8 @@
# ------------------------------------------------------------------------------
import logging
from enum import Enum
import httpx
import aiofiles
from typing import List, Optional
from pyasic.config import MinerConfig
@@ -193,6 +195,42 @@ class Auradine(StockFirmware):
for key in conf.keys():
await self.web.send_command(command=key, **conf[key])
async def upgrade_firmware(self, url: str = None, file_path: str = None, version: str = "latest") -> bool:
"""
Upgrade the firmware of the Auradine device.
Args:
url (str): The URL to download the firmware from.
file_path (str): The local file path of the firmware to be uploaded.
version (str): The version of the firmware to upgrade to.
Returns:
bool: True if the firmware upgrade was successful, False otherwise.
"""
try:
logging.info("Starting firmware upgrade process.")
if not url and not file_path:
raise ValueError("Either URL or file path must be provided for firmware upgrade.")
if url:
# Download the firmware file from the URL
async with httpx.AsyncClient() as client:
response = await client.get(url)
if response.status_code != 200:
raise ValueError(f"Failed to download firmware from URL: {url}")
upgrade_contents = response.content
else:
# read the fimware file contents from the local file path
async with aiofiles.open(file_path, "rb") as f:
upgrade_contents = await f.read()
logging.info("Firmware upgrade process completed successfully.")
return True
except Exception as e:
logging.error(f"An error occurred during the firmware upgrade process: {e}", exc_info=True)
return False
##################################################
### DATA GATHERING FUNCTIONS (get_{some_data}) ###
##################################################
@@ -385,28 +423,4 @@ class Auradine(StockFirmware):
try:
return rpc_summary["SUMMARY"][0]["Elapsed"]
except LookupError:
pass
async def upgrade_firmware(self, url: str = None, version: str = "latest") -> dict:
"""
Upgrade the firmware of the Auradine miner.
Args:
url (str, optional): The URL to download the firmware from.
version (str, optional): The version of the firmware to upgrade to, defaults to 'latest'.
Returns:
dict: A dictionary indicating the result of the firmware upgrade.
"""
try:
logging.info("Starting firmware upgrade process for Auradine miner.")
if url is not None:
return await self.web.firmware_upgrade(url=url)
return await self.web.firmware_upgrade(version=version)
except Exception as e:
logging.error(
f"An unexpected error occurred during the firmware upgrade process: {e}",
exc_info=True,
)
raise
pass

View File

@@ -16,6 +16,7 @@
import asyncio
import ipaddress
import warnings
import logging
from typing import List, Optional, Protocol, Tuple, Type, TypeVar, Union
from pyasic.config import MinerConfig
@@ -560,5 +561,24 @@ class BaseMiner(MinerProtocol):
if self._ssh_cls is not None:
self.ssh = self._ssh_cls(ip)
async def upgrade_firmware(self, url: str = None, version: str = "latest") -> bool:
"""Upgrade the firmware of the miner.
Parameters:
url: The URL to download the firmware from.
version: The version of the firmware to upgrade to.
Returns:
A boolean value of the success of the firmware upgrade.
"""
try:
if url is not None:
await self.web.send_command("firmware-upgrade", url=url)
else:
await self.web.send_command("firmware-upgrade", version=version)
return True
except Exception as e:
logging.error(f"Firmware upgrade failed: {e}")
return False
AnyMiner = TypeVar("AnyMiner", bound=BaseMiner)