fHDHR_XUMO/fHDHR/http/pages/cluster_html.py
deathbybandaid f0138a9b61 first
2020-12-06 08:41:24 -05:00

51 lines
2.1 KiB
Python

from flask import request, render_template
import urllib.parse
class Cluster_HTML():
endpoints = ["/cluster", "/cluster.html"]
endpoint_name = "page_cluster_html"
def __init__(self, fhdhr):
self.fhdhr = fhdhr
self.location_dict = {
"name": self.fhdhr.config.dict["fhdhr"]["friendlyname"],
"location": self.fhdhr.device.cluster.location,
"joined": "N/A",
"url_query": self.fhdhr.device.cluster.location_url
}
def __call__(self, *args):
return self.get(*args)
def get(self, *args):
locations_list = []
if self.fhdhr.config.dict["fhdhr"]["discovery_address"]:
locations_list.append(self.location_dict)
fhdhr_list = self.fhdhr.device.cluster.get_list()
for location in list(fhdhr_list.keys()):
if location in list(self.fhdhr.device.cluster.cluster().keys()):
location_name = self.fhdhr.device.cluster.cluster()[location]["name"]
else:
try:
location_info_url = location + "/discover.json"
location_info_req = self.fhdhr.web.session.get(location_info_url)
location_info = location_info_req.json()
location_name = location_info["FriendlyName"]
except self.fhdhr.web.exceptions.ConnectionError:
self.fhdhr.logger.error("Unreachable: " + location)
location_dict = {
"name": location_name,
"location": location,
"joined": str(fhdhr_list[location]["Joined"]),
"url_query": urllib.parse.quote(location)
}
locations_list.append(location_dict)
return render_template('cluster.html', request=request, fhdhr=self.fhdhr, locations_list=locations_list)