reformatted and clarified some code

This commit is contained in:
UpstreamData
2022-04-21 10:09:30 -06:00
parent 0724a376ea
commit 59ab6e6c8a
3 changed files with 91 additions and 13 deletions

View File

@@ -19,23 +19,29 @@ class BOSMiner(BaseMiner):
def __repr__(self) -> str:
return f"BOSminer: {str(self.ip)}"
async def send_ssh_command(self, cmd: str):
"""Sends SSH command to miner."""
# creates result variable
async def send_ssh_command(self, cmd: str) -> str or None:
"""Send a command to the miner over ssh.
:return: Result of the command or None.
"""
result = None
# runs the command on the miner
# open an ssh connection
async with (await self._get_ssh_connection()) as conn:
# attempt to run command up to 3 times
# 3 retries
for i in range(3):
try:
# save result of the command
# run the command and get the result
result = await conn.run(cmd)
except Exception as e:
# if the command fails, log it
logging.warning(f"{self} command {cmd} error: {e}")
# on the 3rd retry, return None
if i == 3:
return
continue
# return the result, either command output or None
return result
async def fault_light_on(self) -> None:
@@ -52,7 +58,7 @@ class BOSMiner(BaseMiner):
await self.send_ssh_command("miner fault_light off")
logging.debug(f"{self}: fault_light off command completed.")
async def restart_backend(self):
async def restart_backend(self) -> None:
await self.restart_bosminer()
async def restart_bosminer(self) -> None:
@@ -80,7 +86,10 @@ class BOSMiner(BaseMiner):
self.config = cfg
async def get_hostname(self) -> str:
"""Attempts to get hostname from miner."""
"""Get miner hostname.
:return: The hostname of the miner as a string or "?"
"""
try:
async with (await self._get_ssh_connection()) as conn:
if conn is not None:
@@ -95,30 +104,53 @@ class BOSMiner(BaseMiner):
logging.warning(f"Failed to get hostname for miner: {self}")
return "?"
async def get_model(self):
async def get_model(self) -> str or None:
"""Get miner model.
:return: Miner model or None.
"""
# check if model is cached
if self.model:
logging.debug(f"Found model for {self.ip}: {self.model} (BOS)")
return self.model + " (BOS)"
# get devdetails data
version_data = await self.api.devdetails()
# if we get data back, parse it for model
if version_data:
if not version_data["DEVDETAILS"] == []:
# handle Antminer BOSMiner as a base
self.model = version_data["DEVDETAILS"][0]["Model"].replace(
"Antminer ", ""
)
logging.debug(f"Found model for {self.ip}: {self.model} (BOS)")
return self.model + " (BOS)"
# if we don't get devdetails, log a failed attempt
logging.warning(f"Failed to get model for miner: {self}")
return None
async def get_version(self):
"""Get miner firmware version.
:return: Miner firmware version or None.
"""
# check if version is cached
if self.version:
logging.debug(f"Found version for {self.ip}: {self.version}")
return self.version
# get output of bos version file
version_data = await self.send_ssh_command("cat /etc/bos_version")
# if we get the version data, parse it
if version_data:
self.version = version_data.stdout.split("-")[5]
logging.debug(f"Found version for {self.ip}: {self.version}")
return self.version
# if we fail to get version, log a failed attempt
logging.warning(f"Failed to get model for miner: {self}")
return None