Merge pull request #163 from 1e9abhi1e10/update_firmware_4

This commit is contained in:
Brett Rowan
2024-07-11 09:59:15 -06:00
committed by GitHub
2 changed files with 46 additions and 0 deletions

View File

@@ -14,6 +14,7 @@
# limitations under the License. - # limitations under the License. -
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
from pathlib import Path
from typing import List, Optional from typing import List, Optional
from pyasic.config import MinerConfig from pyasic.config import MinerConfig
@@ -452,3 +453,17 @@ class ePIC(ePICFirmware):
return pool_data return pool_data
except LookupError: except LookupError:
pass pass
async def upgrade_firmware(self, file: Path | str, keep_settings: bool = True) -> bool:
"""
Upgrade the firmware of the ePIC miner device.
Args:
file (Path | str): The local file path of the firmware to be uploaded.
keep_settings (bool): Whether to keep the current settings after the update.
Returns:
bool: Whether the firmware update succeeded.
"""
return await self.web.system_update(file=file, keep_settings=keep_settings)

View File

@@ -19,6 +19,10 @@ import json
from typing import Any from typing import Any
import httpx import httpx
import aiofiles
import aiohttp
import hashlib
from pathlib import Path
from pyasic import settings from pyasic import settings
from pyasic.errors import APIError from pyasic.errors import APIError
@@ -46,6 +50,14 @@ class ePICWebAPI(BaseWebAPI):
async with httpx.AsyncClient(transport=settings.transport()) as client: async with httpx.AsyncClient(transport=settings.transport()) as client:
for retry_cnt in range(settings.get("get_data_retries", 1)): for retry_cnt in range(settings.get("get_data_retries", 1)):
try: try:
if parameters.get("form") is not None:
form_data = parameters["form"]
form_data.add_field('password', self.pwd)
response = await client.post(
f"http://{self.ip}:{self.port}/{command}",
timeout=5,
data=form_data,
)
if post: if post:
response = await client.post( response = await client.post(
f"http://{self.ip}:{self.port}/{command}", f"http://{self.ip}:{self.port}/{command}",
@@ -135,3 +147,22 @@ class ePICWebAPI(BaseWebAPI):
async def capabilities(self) -> dict: async def capabilities(self) -> dict:
return await self.send_command("capabilities") return await self.send_command("capabilities")
async def system_update(self, file: Path | str, keep_settings: bool = True):
"""Perform a system update by uploading a firmware file and sending a
command to initiate the update."""
# calculate the SHA256 checksum of the firmware file
sha256_hash = hashlib.sha256()
async with aiofiles.open(str(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(keep_settings).lower())
form_data.add_field('update.zip', open(file, 'rb'), filename='update.zip')
await self.send_command("systemupdate", form=form_data)