From 020558ed4d4339bd2209f17c71f689ffab1608c1 Mon Sep 17 00:00:00 2001 From: UpstreamData Date: Mon, 13 Feb 2023 12:38:03 -0700 Subject: [PATCH] Add the ability to set static IP and hostname on X19. --- pyasic/API/btminer.py | 14 +++++-- pyasic/miners/antminer/bmminer/X19/X19.py | 50 +++++++++++++++++++++++ 2 files changed, 61 insertions(+), 3 deletions(-) diff --git a/pyasic/API/btminer.py b/pyasic/API/btminer.py index c4b22c05..3b774e95 100644 --- a/pyasic/API/btminer.py +++ b/pyasic/API/btminer.py @@ -245,17 +245,25 @@ class BTMinerAPI(BaseMinerAPI): try: data = await self._send_bytes(enc_command, timeout) except (asyncio.CancelledError, asyncio.TimeoutError) as e: - if command["cmd"] in ["reboot", "restart", "power_on", "power_off"]: + if command["cmd"] in ["reboot", "restart_btminer", "power_on", "power_off"]: logging.info( - f"{self} - (reboot/restart/power_on/power_off) - Whatsminers currently break this. " + f"{self} - (reboot/restart_btminer/power_on/power_off) - Whatsminers currently break this. " f"Ignoring exception. Command probably worked." ) # FAKING IT HERE - data = b'{"STATUS": "S", "When": 1670966423, "Code": 131, "Msg": "API command OK", "Description": "Reboot/restart/power_on"}' + data = ( + b'{"STATUS": "S", "When": 1670966423, "Code": 131, "Msg": "API command OK", "Description": "' + + command["cmd"].encode("utf-8") + + b'"}' + ) else: + if ignore_errors: + return {} raise APIError("No data was returned from the API.") if not data: + if ignore_errors: + return {} raise APIError("No data was returned from the API.") data = self._load_api_data(data) diff --git a/pyasic/miners/antminer/bmminer/X19/X19.py b/pyasic/miners/antminer/bmminer/X19/X19.py index 45dea712..e6d5d223 100644 --- a/pyasic/miners/antminer/bmminer/X19/X19.py +++ b/pyasic/miners/antminer/bmminer/X19/X19.py @@ -172,3 +172,53 @@ class BMMinerX19(BMMiner): return round(ideal_rate, 2) except (KeyError, IndexError): pass + + async def set_static_ip( + self, + ip: str, + dns: str, + gateway: str, + subnet_mask: str = "255.255.255.0", + hostname: str = None, + ): + if not hostname: + hostname = await self.get_hostname() + payload = { + "ipAddress": ip, + "ipDns": dns, + "ipGateway": gateway, + "ipHost": hostname, + "ipPro": 2, # static + "ipSub": subnet_mask, + } + await self.send_web_command("set_network_conf", params=payload) + + async def set_dhcp(self, hostname: str = None): + if not hostname: + hostname = await self.get_hostname() + payload = { + "ipAddress": "", + "ipDns": "", + "ipGateway": "", + "ipHost": hostname, + "ipPro": 1, # DHCP + "ipSub": "", + } + await self.send_web_command("set_network_conf", params=payload) + + async def set_hostname(self, hostname: str): + cfg = await self.send_web_command("get_network_info") + dns = cfg["conf_dnsservers"] + gateway = cfg["conf_gateway"] + ip = cfg["conf_ipaddress"] + subnet_mask = cfg["conf_netmask"] + protocol = 1 if cfg["conf_nettype"] == "DHCP" else 2 + payload = { + "ipAddress": ip, + "ipDns": dns, + "ipGateway": gateway, + "ipHost": hostname, + "ipPro": protocol, + "ipSub": subnet_mask, + } + await self.send_web_command("set_network_conf", params=payload)