From 99001e2e13706c58b81b0501b00d68e54b8c3f9c Mon Sep 17 00:00:00 2001 From: UpstreamData <75442874+UpstreamData@users.noreply.github.com> Date: Wed, 10 Aug 2022 16:21:47 -0600 Subject: [PATCH] added the ability to configure whatsminer via API --- pyasic/config/__init__.py | 34 ++++++++++++++++++++++++++++++ pyasic/miners/_backends/btminer.py | 21 ++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/pyasic/config/__init__.py b/pyasic/config/__init__.py index 4c73ef21..9c6e000e 100644 --- a/pyasic/config/__init__.py +++ b/pyasic/config/__init__.py @@ -52,6 +52,19 @@ class _Pool: self.password = data[key] return self + def as_wm(self, user_suffix: str = None) -> dict: + """Convert the data in this class to a dict usable by an Whatsminer device. + + Parameters: + user_suffix: The suffix to append to username. + """ + username = self.username + if user_suffix: + username = f"{username}{user_suffix}" + + pool = {"url": self.url, "user": username, "pass": self.password} + return pool + def as_x19(self, user_suffix: str = None) -> dict: """Convert the data in this class to a dict usable by an X19 device. @@ -141,6 +154,19 @@ class _PoolGroup: pools.append(pool.as_x19(user_suffix=user_suffix)) return pools + def as_wm(self, user_suffix: str = None) -> List[dict]: + """Convert the data in this class to a list usable by an Whatsminer device. + + Parameters: + user_suffix: The suffix to append to username. + """ + pools = [] + for pool in self.pools[:3]: + pools.append(pool.as_wm(user_suffix=user_suffix)) + while len(pools) < 3: + pools.append({"url": None, "user": None, "pass": None}) + return pools + def as_avalon(self, user_suffix: str = None) -> str: """Convert the data in this class to a dict usable by an Avalonminer device. @@ -327,6 +353,14 @@ class MinerConfig: """ return self.from_dict(yaml.load(data, Loader=yaml.SafeLoader)) + def as_wm(self, user_suffix: str = None) -> List[dict]: + """Convert the data in this class to a config usable by an Whatsminer device. + + Parameters: + user_suffix: The suffix to append to username. + """ + return self.pool_groups[0].as_x19(user_suffix=user_suffix) + def as_x19(self, user_suffix: str = None) -> str: """Convert the data in this class to a config usable by an X19 device. diff --git a/pyasic/miners/_backends/btminer.py b/pyasic/miners/_backends/btminer.py index 0d59fe35..55eddbfc 100644 --- a/pyasic/miners/_backends/btminer.py +++ b/pyasic/miners/_backends/btminer.py @@ -162,6 +162,27 @@ class BTMiner(BaseMiner): async def restart_backend(self) -> bool: return False + async def send_config(self, yaml_config, ip_user: bool = False): + if ip_user: + suffix = str(self.ip).split(".")[-1] + conf = MinerConfig().from_yaml(yaml_config).as_wm(user_suffix=suffix) + else: + conf = MinerConfig().from_yaml(yaml_config).as_wm() + + data = await self.api.update_pools( + conf[0]["url"], + conf[0]["user"], + conf[0]["pass"], + conf[1]["url"], + conf[1]["user"], + conf[1]["pass"], + conf[2]["url"], + conf[2]["user"], + conf[2]["pass"], + ) + + print(data) + async def get_config(self) -> MinerConfig: pools = None cfg = MinerConfig()