From 4adb7dc92c13f102dd2e214fe0da031bdbf2961b Mon Sep 17 00:00:00 2001 From: 1e9abhi1e10 <2311abhiptdr@gmail.com> Date: Sat, 22 Jun 2024 08:06:44 +0530 Subject: [PATCH] feat: Add update firmware for ePIC miner --- pyasic/miners/backends/epic.py | 72 ++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/pyasic/miners/backends/epic.py b/pyasic/miners/backends/epic.py index a3c60fdc..bfbf73a7 100644 --- a/pyasic/miners/backends/epic.py +++ b/pyasic/miners/backends/epic.py @@ -14,6 +14,11 @@ # limitations under the License. - # ------------------------------------------------------------------------------ +import logging +import aiofiles +import hashlib +import aiohttp +from pathlib import Path from typing import List, Optional from pyasic.config import MinerConfig @@ -450,3 +455,70 @@ class ePIC(ePICFirmware): return pool_data except LookupError: pass + + async def upgrade_firmware(self, file: Path, keepsettings: bool, password: str): + + """ + Upgrade the firmware of the ePIC miner device. + + Args: + file (Path): The local file path of the firmware to be uploaded. + keepsettings (bool): Whether to keep the current settings after the update. + password (str): The password for authentication. + + Returns: + str: Confirmation message after upgrading the firmware. + """ + try: + logging.info("Starting firmware upgrade process for ePIC miner.") + + if not file: + raise ValueError("File location must be provided for firmware upgrade.") + + # calculate the SHA256 checksum of the firmware file + sha256_hash = hashlib.sha256() + async with aiofiles.open(file, "rb") as f: + while chunk := await f.read(8192): + sha256_hash.update(chunk) + checksum = sha256_hash.hexdigest() + + # prepare the multipart/form-data request + form_data = aiohttp.FormData() + form_data.add_field('checksum', checksum) + form_data.add_field('keepsettings', str(keepsettings).lower()) + form_data.add_field('password', password) + form_data.add_field('update.zip', open(file, 'rb'), filename='update.zip') + + # Send the POST request to the ePIC miner device + async with aiohttp.ClientSession() as session: + async with session.post(f"http://{self.ip}:{self.port}/systemupdate", data=form_data) as response: + if response.status == 200: + result = await response.json() + if result.get("result"): + logging.info("Firmware upgrade process completed successfully for ePIC miner.") + return "Firmware upgrade completed successfully." + else: + error = result.get("error", "Unknown error") + logging.error(f"Firmware upgrade failed: {error}") + raise Exception(f"Firmware upgrade failed: {error}") + else: + logging.error(f"Firmware upgrade failed with status code: {response.status}") + raise Exception(f"Firmware upgrade failed with status code: {response.status}") + + 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