improved miner types, added a generalized API to unknown

This commit is contained in:
UpstreamData
2021-10-08 13:41:27 -06:00
parent e0dd2ee1f3
commit 3fa822de3a
8 changed files with 132 additions and 63 deletions

73
API/unknown.py Normal file
View File

@@ -0,0 +1,73 @@
from API import BaseMinerAPI
class UnknownAPI(BaseMinerAPI):
def __init__(self, ip, port=4028):
super().__init__(ip, port)
async def asccount(self) -> dict:
return await self.send_command("asccount")
async def asc(self, n: int) -> dict:
return await self.send_command("asc", parameters=n)
async def devdetails(self) -> dict:
return await self.send_command("devdetails")
async def devs(self) -> dict:
return await self.send_command("devs")
async def edevs(self, old: bool = False) -> dict:
if old:
return await self.send_command("edevs", parameters="old")
else:
return await self.send_command("edevs")
async def pools(self) -> dict:
return await self.send_command("pools")
async def summary(self) -> dict:
return await self.send_command("summary")
async def stats(self) -> dict:
return await self.send_command("stats")
async def version(self) -> dict:
return await self.send_command("version")
async def estats(self) -> dict:
return await self.send_command("estats")
async def check(self) -> dict:
return await self.send_command("check")
async def coin(self) -> dict:
return await self.send_command("coin")
async def lcd(self) -> dict:
return await self.send_command("lcd")
async def switchpool(self, n: int) -> dict:
# BOS has not implemented this yet, they will in the future
return NotImplementedError
# return await self.send_command("switchpool", parameters=n)
async def enablepool(self, n: int) -> dict:
# BOS has not implemented this yet, they will in the future
return NotImplementedError
# return await self.send_command("enablepool", parameters=n)
async def disablepool(self, n: int) -> dict:
# BOS has not implemented this yet, they will in the future
return NotImplementedError
# return await self.send_command("disablepool", parameters=n)
async def addpool(self, url: str, username: str, password: str) -> dict:
# BOS has not implemented this yet, they will in the future
return NotImplementedError
# return await self.send_command("addpool", parameters=f"{url}, {username}, {password}")
async def removepool(self, n: int) -> dict:
# BOS has not implemented this yet, they will in the future
return NotImplementedError
# return await self.send_command("removepool", parameters=n)

View File

@@ -1,14 +1,11 @@
from API.bosminer import BOSMinerAPI
from API.bmminer import BMMinerAPI
from network import MinerNetwork
import asyncio
async def main():
miner_network = MinerNetwork("192.168.1.1")
miner_list = await miner_network.scan_network_for_miners()
print(miner_list)
miner_network = MinerNetwork('192.168.1.1')
data = await miner_network.scan_network_for_miners()
print(data)
if __name__ == '__main__':
asyncio.get_event_loop().run_until_complete(main())

View File

@@ -1,7 +1,5 @@
from miners import BaseMiner
from API.bosminer import BOSMinerAPI
from API.bmminer import BMMinerAPI
from API.cgminer import CGMinerAPI
from miners import BaseMiner
class BMMiner(BaseMiner):

View File

@@ -1,7 +1,5 @@
from miners import BaseMiner
from API.bosminer import BOSMinerAPI
from API.bmminer import BMMinerAPI
from API.cgminer import CGMinerAPI
from miners import BaseMiner
class CGMiner(BaseMiner):

View File

@@ -1,12 +1,13 @@
from miners.bosminer import BOSminer
from miners.bmminer import BMMiner
from miners.cgminer import CGMiner
from miners.unknown_miner import UnknownMiner
from miners.unknown import UnknownMiner
from API import APIError
import asyncio
import ipaddress
import json
class MinerFactory:
async def get_miner(self, ip: ipaddress.ip_address):
version_data = await self._get_version_data(ip)
@@ -23,51 +24,52 @@ class MinerFactory:
return UnknownMiner(str(ip))
async def _get_version_data(self, ip: ipaddress.ip_address):
try:
# get reader and writer streams
reader, writer = await asyncio.open_connection(str(ip), 4028)
for i in range(3):
try:
# get reader and writer streams
reader, writer = await asyncio.open_connection(str(ip), 4028)
# create the command
cmd = {"command": "version"}
# create the command
cmd = {"command": "version"}
# send the command
writer.write(json.dumps(cmd).encode('utf-8'))
await writer.drain()
# send the command
writer.write(json.dumps(cmd).encode('utf-8'))
await writer.drain()
# instantiate data
data = b""
# instantiate data
data = b""
# loop to receive all the data
while True:
d = await reader.read(4096)
if not d:
break
data += d
# loop to receive all the data
while True:
d = await reader.read(4096)
if not d:
break
data += d
data = json.loads(data.decode('utf-8')[:-1])
data = json.loads(data.decode('utf-8')[:-1])
# close the connection
writer.close()
await writer.wait_closed()
# close the connection
writer.close()
await writer.wait_closed()
# check if the data returned is correct or an error
# if status isn't a key, it is a multicommand
if "STATUS" not in data.keys():
for key in data.keys():
# make sure not to try to turn id into a dict
if not key == "id":
# make sure they succeeded
if data[key][0]["STATUS"][0]["STATUS"] not in ["S", "I"]:
# this is an error
raise APIError(data["STATUS"][0]["Msg"])
else:
# make sure the command succeeded
if data["STATUS"][0]["STATUS"] not in ("S", "I"):
# this is an error
raise APIError(data["STATUS"][0]["Msg"])
# check if the data returned is correct or an error
# if status isn't a key, it is a multicommand
if "STATUS" not in data.keys():
for key in data.keys():
# make sure not to try to turn id into a dict
if not key == "id":
# make sure they succeeded
if data[key][0]["STATUS"][0]["STATUS"] not in ["S", "I"]:
# this is an error
raise APIError(data["STATUS"][0]["Msg"])
else:
# make sure the command succeeded
if data["STATUS"][0]["STATUS"] not in ("S", "I"):
# this is an error
raise APIError(data["STATUS"][0]["Msg"])
# return the data
return data
except Exception as e:
print(e)
return None
# return the data
return data
except Exception as e:
print(ip, e)
return None

11
miners/unknown.py Normal file
View File

@@ -0,0 +1,11 @@
from API.unknown import UnknownAPI
from miners import BaseMiner
class UnknownMiner(BaseMiner):
def __init__(self, ip: str):
api = UnknownAPI(ip)
super().__init__(ip, api)
def __repr__(self):
return f"Unknown: {str(self.ip)}"

View File

@@ -1,9 +0,0 @@
import ipaddress
class UnknownMiner():
def __init__(self, ip: str):
self.ip = ipaddress.ip_address(ip)
def __repr__(self):
return f"Unknown: {str(self.ip)}"

View File

@@ -40,7 +40,6 @@ class MinerNetwork:
miners = await asyncio.gather(*create_miners_tasks)
return miners
async def ping_miner(self, ip: ipaddress.ip_address):
for i in range(PING_RETRIES):
connection_fut = asyncio.open_connection(str(ip), 4028)