From bf0e2e6cfe7158c4967d0f73796d974d57a41618 Mon Sep 17 00:00:00 2001 From: 1e9abhi1e10 <2311abhiptdr@gmail.com> Date: Fri, 7 Jun 2024 02:12:48 +0530 Subject: [PATCH] feat: Add update firmware for BFG miner --- pyasic/miners/backends/bfgminer.py | 51 ++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/pyasic/miners/backends/bfgminer.py b/pyasic/miners/backends/bfgminer.py index c45231f1..cc22caf9 100644 --- a/pyasic/miners/backends/bfgminer.py +++ b/pyasic/miners/backends/bfgminer.py @@ -15,6 +15,10 @@ # ------------------------------------------------------------------------------ from typing import List, Optional +import logging +import aiofiles +import base64 +from pathlib import Path from pyasic.config import MinerConfig from pyasic.data import AlgoHashRate, Fan, HashBoard, HashUnit @@ -228,4 +232,51 @@ class BFGMiner(StockFirmware): except LookupError: pass + async def upgrade_firmware(self, file: Path): + """ + Upgrade the firmware of the BFG miner device. + Args: + file (Path): The local file path of the firmware to be uploaded. + + Returns: + str: Confirmation message after upgrading the firmware. + """ + try: + logging.info("Starting firmware upgrade process for BFG miner.") + + if not file: + raise ValueError("File location must be provided for firmware upgrade.") + + # Read the firmware file contents + async with aiofiles.open(file, "rb") as f: + upgrade_contents = await f.read() + + # Encode the firmware contents in base64 + encoded_contents = base64.b64encode(upgrade_contents).decode("utf-8") + + # Upload the firmware file to the BFG miner device + logging.info(f"Uploading firmware file from {file} to the device.") + await self.ssh.send_command( + f"echo {encoded_contents} | base64 -d > /tmp/firmware.tar && sysupgrade /tmp/firmware.tar" + ) + + logging.info("Firmware upgrade process completed successfully for BFG miner.") + return "Firmware upgrade completed successfully." + 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 \ No newline at end of file