feature: add is_mining for btminer, and fix some minor bugs.

This commit is contained in:
UpstreamData
2023-06-23 13:38:45 -06:00
parent d06cb19da3
commit 5db52c46f3
4 changed files with 28 additions and 3 deletions

View File

@@ -73,6 +73,11 @@ class BaseMinerAPI:
# send the command
data = await self._send_bytes(json.dumps(cmd).encode("utf-8"))
if data == b"Socket connect failed: Connection refused\n":
if not ignore_errors:
raise APIError(data.decode("utf-8"))
return {}
data = self._load_api_data(data)
# check for if the user wants to allow errors to return

View File

@@ -616,3 +616,22 @@ class BTMiner(BaseMiner):
async def set_hostname(self, hostname: str):
await self.api.set_hostname(hostname)
async def is_mining(self, api_status: dict = None) -> Optional[bool]:
if not api_status:
try:
api_status = await self.api.status()
except APIError:
pass
if api_status:
try:
if api_status["Msg"].get("btmineroff"):
try:
await self.api.devdetails()
except APIError:
return False
return True
return True if api_status["Msg"]["mineroff"] == "false" else False
except LookupError:
pass

View File

@@ -346,7 +346,7 @@ class BaseMiner(ABC):
pass
async def is_mining(self, *args, **kwargs) -> Optional[bool]:
"""Check whether the miner is mining
"""Check whether the miner is mining.
Returns:
A boolean value representing if the miner is mining.

View File

@@ -453,7 +453,6 @@ class MinerFactory:
text, resp = await concurrent_get_first_result(
tasks, lambda x: x[0] is not None
)
if text is not None:
return self._parse_web_type(text, resp)
@@ -462,7 +461,7 @@ class MinerFactory:
session: aiohttp.ClientSession, url: str
) -> Tuple[Optional[str], Optional[aiohttp.ClientResponse]]:
try:
resp = await session.get(url)
resp = await session.get(url, allow_redirects=False)
return await resp.text(), resp
except (aiohttp.ClientError, asyncio.TimeoutError):
pass
@@ -620,6 +619,8 @@ class MinerFactory:
return
except (ConnectionError, OSError):
return
if data == b"Socket connect failed: Connection refused\n":
return
data = await self._fix_api_data(data)