soakforjesus/rpiserver/bosminer.py

42 lines
936 B
Python

import asyncio
import json
async def bosminer_cmd(host, command, param=None):
reader, writer = await asyncio.open_connection(host, 4028)
payload = dict(command=command)
if param:
payload['parameter'] = param
message = json.dumps(payload)
writer.write(message.encode())
await writer.drain()
data = await reader.readuntil(separator=b'\00')
writer.close()
await writer.wait_closed()
return data[:-1].decode()
async def test():
import sys
if len(sys.argv) == 3:
cmd = sys.argv[1]
param = sys.argv[2]
if len(sys.argv) == 2:
cmd = sys.argv[1]
param = None
else:
cmd = 'temps'
param = None
res = await bosminer_cmd('192.168.69.4', cmd, param)
j = json.loads(res)
print(json.dumps(j, indent=4))
if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.run_until_complete(test())
loop.close()