feature: add support for whatsminer API v2.0.4, which moves the location of error codes to a new function (that happens to have very incorrect JSON)

This commit is contained in:
UpstreamData
2022-11-01 12:57:33 -06:00
parent 08d273c7c1
commit 833de3ab43
3 changed files with 55 additions and 10 deletions

View File

@@ -18,6 +18,7 @@ import ipaddress
import warnings
import logging
from typing import Union
import re
from pyasic.errors import APIError, APIWarning
@@ -223,6 +224,10 @@ If you are sure you want to use this command please use API.send_command("{comma
if not str_data.endswith("}"):
str_data = ",".join(str_data.split(",")[:-1]) + "}"
# fix a really nasty bug with whatsminer API v2.0.4 where they return a list structured like a dict
if re.search(r"\"error_code\":\[\".+\"\]", str_data):
str_data = str_data.replace("[", "{").replace("]", "}")
# parse the json
try:
parsed_data = json.loads(str_data)

View File

@@ -768,3 +768,17 @@ class BTMinerAPI(BaseMinerAPI):
</details>
"""
return await self.send_command("get_miner_info")
async def get_error_code(self) -> dict:
"""Get a list of error codes from the miner.
<details>
<summary>Expand</summary>
Get a list of error codes from the miner. Replaced `summary` as the location of error codes with API version 2.0.4.
Returns:
A list of error codes on the miner.
</details>
"""
return await self.send_command("get_error_code")

View File

@@ -158,17 +158,29 @@ class BTMiner(BaseMiner):
async def get_errors(self) -> List[MinerErrorData]:
data = []
summary_data = await self.api.summary()
if summary_data[0].get("Error Code Count"):
for i in range(summary_data[0]["Error Code Count"]):
if summary_data[0].get(f"Error Code {i}"):
if not summary_data[0][f"Error Code {i}"] == "":
data.append(
WhatsminerError(
error_code=summary_data[0][f"Error Code {i}"]
try:
err_data = await self.api.get_error_code()
if err_data:
if err_data.get("Msg"):
if err_data["Msg"].get("error_code"):
for err in err_data["Msg"]["error_code"]:
data.append(
WhatsminerError(
error_code=int(err)
)
)
)
except APIError:
summary_data = await self.api.summary()
if summary_data[0].get("Error Code Count"):
for i in range(summary_data[0]["Error Code Count"]):
if summary_data[0].get(f"Error Code {i}"):
if not summary_data[0][f"Error Code {i}"] == "":
data.append(
WhatsminerError(
error_code=summary_data[0][f"Error Code {i}"]
)
)
return data
async def reboot(self) -> bool:
@@ -278,6 +290,10 @@ class BTMiner(BaseMiner):
psu_data = await self.api.get_psu()
except APIError:
psu_data = None
try:
err_data = await self.api.get_error_code()
except APIError:
err_data = None
if summary:
summary_data = summary.get("SUMMARY")
@@ -330,6 +346,16 @@ class BTMiner(BaseMiner):
if psu.get("fan_speed"):
data.fan_psu = psu["fan_speed"]
if err_data:
if err_data.get("Msg"):
if err_data["Msg"].get("error_code"):
for err in err_data["Msg"]["error_code"]:
data.errors.append(
WhatsminerError(
error_code=int(err)
)
)
if devs:
temp_data = devs.get("DEVS")
if temp_data: