import requests import json from flask import Flask, jsonify, request app = Flask(__name__) with open("carls.conf", "r") as config_file: config = json.load(config_file) url_path = "/" + config["service_info"]["url_path"] # Ensure no leading or trailing slashes @app.route(url_path + '/usage', methods=['GET']) def get_cpu_data(): url = "http://localhost:8085/data.json" try: response = requests.get(url) data = response.json() cpu_temps = { } cpu_usage = { } def extract_temperatures(node): print(node['Text'], node.keys( )) if 'Children' in node: for child in node['Children']: extract_temperatures(child) if node['Text'] == 'Temperatures': for child in node['Children']: if child['Text'][:-1] == 'CPU Core #': cpu_temps[child['Text']] = child['Value'] def extract_usage(node): if 'Children' in node: for child in node['Children']: extract_usage(child) if node['Text'] == 'Load': for child in node['Children']: if child['Text'][:-1] == 'CPU Core #': cpu_usage[child['Text']] = child['Value'] for child in data['Children']: extract_temperatures(data['Children'][0]) extract_usage(data['Children'][0]) return jsonify({"Temperatures" : cpu_temps, "Loads" : cpu_usage}) except Exception as e: print(f"Error: {e}") return None # temps, loads = get_highest_cpu_temp_lhm() # if temps is not None: # for temp in temps: # print(f"CPU temperature [{temp}]: {temps[temp]} °C") # print(f"CPU Load [{temp}]: {loads[temp]} %") # else: # print("Could not read CPU temperature.") if __name__ == '__main__': app.run(port=1098)