finished adding settings page

This commit is contained in:
UpstreamData
2022-03-07 12:38:56 -07:00
parent 2bd25c3f35
commit 244dac76af
5 changed files with 70 additions and 26 deletions

Binary file not shown.

View File

@@ -13,7 +13,7 @@ from fastapi.templating import Jinja2Templates
from network import MinerNetwork from network import MinerNetwork
from tools.web_monitor.miner_factory import miner_factory from tools.web_monitor.miner_factory import miner_factory
from tools.web_monitor.web_settings import MINER_DATA_TIMEOUT, MINER_IDENTIFY_TIMEOUT, GRAPH_SLEEP_TIME from tools.web_monitor.web_settings import get_current_settings, update_settings
app = FastAPI() app = FastAPI()
@@ -37,6 +37,7 @@ def dashboard(request: Request):
@app.websocket("/dashboard/ws") @app.websocket("/dashboard/ws")
async def dashboard_websocket(websocket: WebSocket): async def dashboard_websocket(websocket: WebSocket):
await websocket.accept() await websocket.accept()
graph_sleep_time = get_current_settings()["graph_data_sleep_time"]
try: try:
while True: while True:
miners = get_current_miner_list() miners = get_current_miner_list()
@@ -50,7 +51,7 @@ async def dashboard_websocket(websocket: WebSocket):
await websocket.send_json( await websocket.send_json(
{"datetime": datetime.datetime.now().isoformat(), {"datetime": datetime.datetime.now().isoformat(),
"miners": all_miner_data}) "miners": all_miner_data})
await asyncio.sleep(GRAPH_SLEEP_TIME) await asyncio.sleep(graph_sleep_time)
except WebSocketDisconnect: except WebSocketDisconnect:
print("Websocket disconnected.") print("Websocket disconnected.")
pass pass
@@ -60,9 +61,13 @@ async def dashboard_websocket(websocket: WebSocket):
async def get_miner_data_dashboard(miner_ip): async def get_miner_data_dashboard(miner_ip):
try: try:
miner_ip = await asyncio.wait_for(miner_factory.get_miner(miner_ip), MINER_IDENTIFY_TIMEOUT) settings = get_current_settings()
miner_identify_timeout = settings["miner_identify_timeout"]
miner_data_timeout = settings["miner_data_timeout"]
miner_summary = await asyncio.wait_for(miner_ip.api.summary(), MINER_DATA_TIMEOUT) miner_ip = await asyncio.wait_for(miner_factory.get_miner(miner_ip), miner_identify_timeout)
miner_summary = await asyncio.wait_for(miner_ip.api.summary(), miner_data_timeout)
if miner_summary: if miner_summary:
if 'MHS av' in miner_summary['SUMMARY'][0].keys(): if 'MHS av' in miner_summary['SUMMARY'][0].keys():
hashrate = format( hashrate = format(
@@ -103,14 +108,18 @@ def miner(_request: Request, _miner_ip):
@app.websocket("/miner/{miner_ip}/ws") @app.websocket("/miner/{miner_ip}/ws")
async def miner_websocket(websocket: WebSocket, miner_ip): async def miner_websocket(websocket: WebSocket, miner_ip):
await websocket.accept() await websocket.accept()
settings = get_current_settings()
miner_identify_timeout = settings["miner_identify_timeout"]
miner_data_timeout = settings["miner_data_timeout"]
try: try:
while True: while True:
try: try:
cur_miner = await asyncio.wait_for( cur_miner = await asyncio.wait_for(
miner_factory.get_miner(str(miner_ip)), MINER_IDENTIFY_TIMEOUT) miner_factory.get_miner(str(miner_ip)), miner_identify_timeout)
data = await asyncio.wait_for( data = await asyncio.wait_for(
cur_miner.api.multicommand("summary", "fans", "stats"), MINER_DATA_TIMEOUT) cur_miner.api.multicommand("summary", "fans", "stats"), miner_data_timeout)
miner_model = await cur_miner.get_model() miner_model = await cur_miner.get_model()
@@ -167,7 +176,7 @@ async def miner_websocket(websocket: WebSocket, miner_ip):
"datetime": datetime.datetime.now().isoformat(), "datetime": datetime.datetime.now().isoformat(),
"model": miner_model} "model": miner_model}
await websocket.send_json(data) await websocket.send_json(data)
await asyncio.sleep(GRAPH_SLEEP_TIME) await asyncio.sleep(settings["graph_sleep_time"])
except asyncio.exceptions.TimeoutError: except asyncio.exceptions.TimeoutError:
data = {"error": "The miner is not responding."} data = {"error": "The miner is not responding."}
await websocket.send_json(data) await websocket.send_json(data)
@@ -214,13 +223,32 @@ def get_current_miner_list():
return cur_miners return cur_miners
@app.get("/settings") @app.route("/settings", methods=["GET", "POST"])
async def settings(request: Request): async def settings(request: Request):
return templates.TemplateResponse("settings.html", { return templates.TemplateResponse("settings.html", {
"request": request, "request": request,
"cur_miners": get_current_miner_list(), "cur_miners": get_current_miner_list(),
"settings": get_current_settings()
}) })
@app.post("/settings/update")
async def update_settings_page(request: Request):
data = await request.form()
graph_data_sleep_time = data.get('graph_data_sleep_time')
miner_data_timeout = data.get('miner_data_timeout')
miner_identify_timeout = data.get('miner_identify_timeout')
new_settings = {
"graph_data_sleep_time": int(graph_data_sleep_time),
"miner_data_timeout": int(miner_data_timeout),
"miner_identify_timeout": int(miner_identify_timeout),
}
update_settings(new_settings)
return RedirectResponse(request.url_for("settings"))
@app.get("/remove_all_miners") @app.get("/remove_all_miners")
async def remove_all_miners(request: Request): async def remove_all_miners(request: Request):
file = open("miner_list.txt", "w") file = open("miner_list.txt", "w")

View File

@@ -85,7 +85,7 @@
<li class="border-top my-3"></li> <li class="border-top my-3"></li>
<li class="nav-item mb-1 mx-2"> <li class="nav-item mb-1 mx-2">
<a href="/settings" class="nav-link text-white"> <a href="/settings" class="nav-link {% if request.path == '/settings' %}active{% else %}text-white{% endif %}">
<svg class="bi me-2" width="16" height="16"><use xlink:href="#settings"></use></svg> <svg class="bi me-2" width="16" height="16"><use xlink:href="#settings"></use></svg>
Settings Settings
</a> </a>

View File

@@ -1,6 +1,6 @@
{% extends 'navbar.html'%} {% extends 'navbar.html'%}
{% block content %} {% block content %}
<div class="row mt-2"> <div class="row my-2">
<div class="col"> <div class="col">
<div class="d-flex flex-row-reverse"> <div class="d-flex flex-row-reverse">
<button type="button" class="btn btn-outline-danger mx-1" data-bs-toggle="modal" data-bs-target="#removeModal"> <button type="button" class="btn btn-outline-danger mx-1" data-bs-toggle="modal" data-bs-target="#removeModal">
@@ -28,10 +28,19 @@
</div> </div>
</div> </div>
</div> </div>
<form method="post" action="/settings/update">
<script> <div class="input-group mb-3">
window.post = function(url, data) { <span class="input-group-text">Graph Data Sleep Time</span>
return fetch(url, {method: "POST", headers: {'Content-Type': 'application/json'}, body: JSON.stringify(data)}); <input type="number" class="form-control" value="{{settings['graph_data_sleep_time']}}" name="graph_data_sleep_time" id="graph_data_sleep_time">
} </div>
</script> <div class="input-group mb-3">
<span class="input-group-text">Miner Data Timeout</span>
<input type="number" class="form-control" value="{{settings['miner_data_timeout']}}" name="miner_data_timeout" id="miner_data_timeout">
</div>
<div class="input-group mb-3">
<span class="input-group-text">Miner Identification Timeout</span>
<input type="number" class="form-control" value="{{settings['miner_identify_timeout']}}" name="miner_identify_timeout" id=" ">
</div>
<button type="submit" class="btn btn-primary w-100">Submit</button>
</form>
{% endblock content %} {% endblock content %}

View File

@@ -1,13 +1,20 @@
import toml import toml
import os import os
try:
with open(os.path.join(os.getcwd(), "web_settings.toml"), "r") as settings_file: def get_current_settings():
settings = toml.loads(settings_file.read()) try:
GRAPH_SLEEP_TIME: int = settings["graph_data_sleep_time"] with open(os.path.join(os.getcwd(), "web_settings.toml"), "r") as settings_file:
MINER_DATA_TIMEOUT: int = settings["miner_data_timeout"] settings = toml.loads(settings_file.read())
MINER_IDENTIFY_TIMEOUT: int = settings["miner_identify_timeout"] except:
except: settings = {
GRAPH_SLEEP_TIME: int = 1 "graph_data_sleep_time": 1,
MINER_DATA_TIMEOUT: int = 5 "miner_data_timeout": 5,
MINER_IDENTIFY_TIMEOUT: int = 5 "miner_identify_timeout": 5,
}
return settings
def update_settings(settings):
with open(os.path.join(os.getcwd(), "web_settings.toml"), "w") as settings_file:
settings_file.write(toml.dumps(settings))