completed bmminer api and moved APIs to package
This commit is contained in:
59
API/__init__.py
Normal file
59
API/__init__.py
Normal file
@@ -0,0 +1,59 @@
|
||||
import asyncio
|
||||
import json
|
||||
|
||||
|
||||
class APIError(Exception):
|
||||
def __init__(self, *args):
|
||||
if args:
|
||||
self.message = args[0]
|
||||
else:
|
||||
self.message = None
|
||||
|
||||
def __str__(self):
|
||||
if self.message:
|
||||
return f"{self.message}"
|
||||
else:
|
||||
return "Incorrect API parameters."
|
||||
|
||||
|
||||
class BaseMinerAPI:
|
||||
def __init__(self, ip, port):
|
||||
self.port = port
|
||||
self.ip = ip
|
||||
|
||||
async def send_command(self, command, parameters=None):
|
||||
# get reader and writer streams
|
||||
reader, writer = await asyncio.open_connection(self.ip, self.port)
|
||||
|
||||
# create the command
|
||||
cmd = {"command": command}
|
||||
if parameters:
|
||||
cmd["parameters"] = parameters
|
||||
|
||||
# send the command
|
||||
writer.write(json.dumps(cmd).encode('utf-8'))
|
||||
await writer.drain()
|
||||
|
||||
# instantiate data
|
||||
data = b""
|
||||
|
||||
# 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])
|
||||
|
||||
# close the connection
|
||||
writer.close()
|
||||
await writer.wait_closed()
|
||||
|
||||
# check if the data returned is correct or an error
|
||||
if not data["STATUS"][0]["STATUS"] in ("S", "I"):
|
||||
# this is an error
|
||||
raise APIError(data["STATUS"][0]["Msg"])
|
||||
|
||||
# return the data
|
||||
return data
|
||||
150
API/bmminer.py
Normal file
150
API/bmminer.py
Normal file
@@ -0,0 +1,150 @@
|
||||
from API import BaseMinerAPI
|
||||
|
||||
|
||||
class BMMinerAPI(BaseMinerAPI):
|
||||
def __init__(self, ip, port=4028):
|
||||
super().__init__(ip, port)
|
||||
|
||||
async def version(self):
|
||||
return await self.send_command("version")
|
||||
|
||||
async def config(self):
|
||||
return await self.send_command("config")
|
||||
|
||||
async def summary(self):
|
||||
return await self.send_command("summary")
|
||||
|
||||
async def pools(self):
|
||||
return await self.send_command("pools")
|
||||
|
||||
async def devs(self):
|
||||
return await self.send_command("devs")
|
||||
|
||||
async def edevs(self, old: bool = False):
|
||||
if old:
|
||||
return await self.send_command("edevs", parameters="old")
|
||||
else:
|
||||
return await self.send_command("edevs")
|
||||
|
||||
async def pga(self, n: int):
|
||||
return await self.send_command("pga", parameters=n)
|
||||
|
||||
async def pgacount(self):
|
||||
return await self.send_command("pgacount")
|
||||
|
||||
async def switchpool(self, n: int):
|
||||
return await self.send_command("switchpool", parameters=n)
|
||||
|
||||
async def enablepool(self, n: int):
|
||||
return await self.send_command("enablepool", parameters=n)
|
||||
|
||||
async def addpool(self, url: str, username: str, password: str):
|
||||
return await self.send_command("enablepool", parameters=f"{url}, {username}, {password}")
|
||||
|
||||
async def poolpriority(self, *n: int):
|
||||
return await self.send_command("poolpriority", parameters=f"{','.join(n)}")
|
||||
|
||||
async def poolquota(self, n: int, q: int):
|
||||
return await self.send_command("poolquota", parameters=f"{n}, {q}")
|
||||
|
||||
async def disablepool(self, n: int):
|
||||
return await self.send_command("disablepool", parameters=n)
|
||||
|
||||
async def removepool(self, n: int):
|
||||
return await self.send_command("removepool", parameters=n)
|
||||
|
||||
async def save(self, filename: str = None):
|
||||
if filename:
|
||||
return await self.send_command("save", parameters=filename)
|
||||
else:
|
||||
return await self.send_command("save")
|
||||
|
||||
async def quit(self):
|
||||
return await self.send_command("quit")
|
||||
|
||||
async def notify(self):
|
||||
return await self.send_command("notify")
|
||||
|
||||
async def privileged(self):
|
||||
return await self.send_command("privileged")
|
||||
|
||||
async def pgaenable(self, n: int):
|
||||
return await self.send_command("pgaenable", parameters=n)
|
||||
|
||||
async def pgadisable(self, n: int):
|
||||
return await self.send_command("pgadisable", parameters=n)
|
||||
|
||||
async def pgaidentify(self, n: int):
|
||||
return await self.send_command("pgaidentify", parameters=n)
|
||||
|
||||
async def devdetails(self):
|
||||
return await self.send_command("devdetails")
|
||||
|
||||
async def restart(self):
|
||||
return await self.send_command("restart")
|
||||
|
||||
async def stats(self):
|
||||
return await self.send_command("stats")
|
||||
|
||||
async def estats(self, old=False):
|
||||
if old:
|
||||
return await self.send_command("estats", parameters="old")
|
||||
else:
|
||||
return await self.send_command("estats")
|
||||
|
||||
async def check(self, command):
|
||||
return await self.send_command("check", parameters=command)
|
||||
|
||||
async def failover_only(self, failover: bool):
|
||||
return self.send_command("failover-only", parameters=failover)
|
||||
|
||||
async def coin(self):
|
||||
return await self.send_command("coin")
|
||||
|
||||
async def debug(self, setting: str):
|
||||
return await self.send_command("debug", parameters=setting)
|
||||
|
||||
async def setconfig(self, name, n):
|
||||
return await self.send_command("setconfig", parameters=f"{name}, {n}")
|
||||
|
||||
async def usbstats(self):
|
||||
return await self.send_command("usbstats")
|
||||
|
||||
async def pgaset(self, n, opt, val=None):
|
||||
if val:
|
||||
return await self.send_command("pgaset", parameters=f"{n}, {opt}, {val}")
|
||||
else:
|
||||
return await self.send_command("pgaset", parameters=f"{n}, {opt}")
|
||||
|
||||
async def zero(self, which: str, value: bool):
|
||||
return await self.send_command("zero", parameters=f"{which}, {value}")
|
||||
|
||||
async def hotplug(self, n: int):
|
||||
return await self.send_command("hotplug", parameters=n)
|
||||
|
||||
async def asc(self, n: int):
|
||||
return await self.send_command("asc", parameters=n)
|
||||
|
||||
async def ascenable(self, n: int):
|
||||
return await self.send_command("ascenable", parameters=n)
|
||||
|
||||
async def ascdisable(self, n: int):
|
||||
return await self.send_command("ascdisable", parameters=n)
|
||||
|
||||
async def ascidentify(self, n: int):
|
||||
return await self.send_command("ascidentify", parameters=n)
|
||||
|
||||
async def asccount(self):
|
||||
return await self.send_command("asccount")
|
||||
|
||||
async def ascset(self, n, opt, val=None):
|
||||
if val:
|
||||
return await self.send_command("ascset", parameters=f"{n}, {opt}, {val}")
|
||||
else:
|
||||
return await self.send_command("ascset", parameters=f"{n}, {opt}")
|
||||
|
||||
async def lcd(self):
|
||||
return await self.send_command("lcd")
|
||||
|
||||
async def lockstats(self):
|
||||
return await self.send_command("lockstats")
|
||||
54
API/bosminer.py
Normal file
54
API/bosminer.py
Normal file
@@ -0,0 +1,54 @@
|
||||
from API import BaseMinerAPI
|
||||
|
||||
|
||||
class BOSMinerAPI(BaseMinerAPI):
|
||||
def __init__(self, ip, port=4028):
|
||||
super().__init__(ip, port)
|
||||
|
||||
async def asccount(self):
|
||||
return await self.send_command("asccount")
|
||||
|
||||
async def devdetails(self):
|
||||
return await self.send_command("devdetails")
|
||||
|
||||
async def devs(self):
|
||||
return await self.send_command("devs")
|
||||
|
||||
async def edevs(self):
|
||||
return await self.send_command("edevs")
|
||||
|
||||
async def pools(self):
|
||||
return await self.send_command("pools")
|
||||
|
||||
async def summary(self):
|
||||
return await self.send_command("summary")
|
||||
|
||||
async def stats(self):
|
||||
return await self.send_command("stats")
|
||||
|
||||
async def version(self):
|
||||
return await self.send_command("version")
|
||||
|
||||
async def estats(self):
|
||||
return await self.send_command("estats")
|
||||
|
||||
async def check(self):
|
||||
return await self.send_command("check")
|
||||
|
||||
async def coin(self):
|
||||
return await self.send_command("coin")
|
||||
|
||||
async def lcd(self):
|
||||
return await self.send_command("lcd")
|
||||
|
||||
async def fans(self):
|
||||
return await self.send_command("fans")
|
||||
|
||||
async def tempctrl(self):
|
||||
return await self.send_command("tempctrl")
|
||||
|
||||
async def temps(self):
|
||||
return await self.send_command("temps")
|
||||
|
||||
async def tunerstatus(self):
|
||||
return await self.send_command("tunerstatus")
|
||||
7
API/cgminer.py
Normal file
7
API/cgminer.py
Normal file
@@ -0,0 +1,7 @@
|
||||
from API import BaseMinerAPI
|
||||
|
||||
|
||||
class CGMinerAPI(BaseMinerAPI):
|
||||
def __init__(self, ip, port=4028):
|
||||
super().__init__(ip, port)
|
||||
|
||||
164
main.py
164
main.py
@@ -1,169 +1,9 @@
|
||||
from API.bosminer import BOSMinerAPI
|
||||
import asyncio
|
||||
import json
|
||||
|
||||
|
||||
class APIError(Exception):
|
||||
def __init__(self, *args):
|
||||
if args:
|
||||
self.message = args[0]
|
||||
else:
|
||||
self.message = None
|
||||
|
||||
def __str__(self):
|
||||
if self.message:
|
||||
return f"{self.message}"
|
||||
else:
|
||||
return "Incorrect API parameters."
|
||||
|
||||
|
||||
class API:
|
||||
def __init__(self, ip, port):
|
||||
self.port = port
|
||||
self.ip = ip
|
||||
|
||||
async def send_command(self, command):
|
||||
# get reader and writer streams
|
||||
reader, writer = await asyncio.open_connection(self.ip, self.port)
|
||||
|
||||
# send the command
|
||||
writer.write(json.dumps({"command": command}).encode('utf-8'))
|
||||
await writer.drain()
|
||||
|
||||
# instantiate data
|
||||
data = b""
|
||||
|
||||
# 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])
|
||||
|
||||
# close the connection
|
||||
writer.close()
|
||||
await writer.wait_closed()
|
||||
|
||||
# check if the data returned is correct or an error
|
||||
if not data["STATUS"][0]["STATUS"] == "S":
|
||||
# this is an error
|
||||
raise APIError(data["STATUS"][0]["Msg"])
|
||||
|
||||
# return the data
|
||||
return data
|
||||
|
||||
|
||||
class CGMiner(API):
|
||||
def __init__(self, ip, port=4028):
|
||||
super().__init__(ip, port)
|
||||
|
||||
|
||||
class BMMiner(API):
|
||||
def __init__(self, ip, port=4028):
|
||||
super().__init__(ip, port)
|
||||
|
||||
async def version(self):
|
||||
return await self.send_command("version")
|
||||
|
||||
async def config(self):
|
||||
return await self.send_command("config")
|
||||
|
||||
async def summary(self):
|
||||
return await self.send_command("summary")
|
||||
|
||||
async def pools(self):
|
||||
return await self.send_command("pools")
|
||||
|
||||
async def devs(self):
|
||||
return await self.send_command("devs")
|
||||
|
||||
async def edevs(self):
|
||||
return await self.send_command("edevs")
|
||||
|
||||
async def pgacount(self):
|
||||
return await self.send_command("pgacount")
|
||||
|
||||
async def notify(self):
|
||||
return await self.send_command("notify")
|
||||
|
||||
async def devdetails(self):
|
||||
return await self.send_command("devdetails")
|
||||
|
||||
async def stats(self):
|
||||
return await self.send_command("stats")
|
||||
|
||||
async def estats(self):
|
||||
return await self.send_command("estats")
|
||||
|
||||
async def check(self):
|
||||
return await self.send_command("check")
|
||||
|
||||
async def coin(self):
|
||||
return await self.send_command("coin")
|
||||
|
||||
async def asccount(self):
|
||||
return await self.send_command("asccount")
|
||||
|
||||
async def lcd(self):
|
||||
return await self.send_command("lcd")
|
||||
|
||||
|
||||
class BOSMiner(API):
|
||||
def __init__(self, ip, port=4028):
|
||||
super().__init__(ip, port)
|
||||
|
||||
async def asccount(self):
|
||||
return await self.send_command("asccount")
|
||||
|
||||
async def devdetails(self):
|
||||
return await self.send_command("devdetails")
|
||||
|
||||
async def devs(self):
|
||||
return await self.send_command("devs")
|
||||
|
||||
async def edevs(self):
|
||||
return await self.send_command("edevs")
|
||||
|
||||
async def pools(self):
|
||||
return await self.send_command("pools")
|
||||
|
||||
async def summary(self):
|
||||
return await self.send_command("summary")
|
||||
|
||||
async def stats(self):
|
||||
return await self.send_command("stats")
|
||||
|
||||
async def version(self):
|
||||
return await self.send_command("version")
|
||||
|
||||
async def estats(self):
|
||||
return await self.send_command("estats")
|
||||
|
||||
async def check(self):
|
||||
return await self.send_command("check")
|
||||
|
||||
async def coin(self):
|
||||
return await self.send_command("coin")
|
||||
|
||||
async def lcd(self):
|
||||
return await self.send_command("lcd")
|
||||
|
||||
async def fans(self):
|
||||
return await self.send_command("fans")
|
||||
|
||||
async def tempctrl(self):
|
||||
return await self.send_command("tempctrl")
|
||||
|
||||
async def temps(self):
|
||||
return await self.send_command("temps")
|
||||
|
||||
async def tunerstatus(self):
|
||||
return await self.send_command("tunerstatus")
|
||||
|
||||
|
||||
async def main():
|
||||
bosminer = BOSMiner("172.16.1.199")
|
||||
bosminer = BOSMinerAPI("172.16.1.199")
|
||||
data = await bosminer.stats()
|
||||
print(data)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user