Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
3 changes: 3 additions & 0 deletions backend/energy/energy_goal.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"goal_value": 190000.0
}
31 changes: 30 additions & 1 deletion backend/energy_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,33 @@ def get_energy_data(time_range: str):

def get_energy_data_pdf(time_range: str):
"""Fetch energy usage data from JSON files and convert to PDF."""
return # To be implemented
return # To be implemented


def set_energy_goal(goal_value: float):
"""Set a static energy goal with a specific value and save it to a JSON file."""

goal_data = {
"goal_value": goal_value,
}

goal_file_path = os.path.join(ENERGY_FOLDER, "energy_goal.json")

try:
with open(goal_file_path, "w") as file:
json.dump(goal_data, file, indent=4)
return {"success": f"Energy goal set to {goal_value} and saved to energy_goal.json"}
except Exception as e:
return {"error": f"Error saving energy goal: {str(e)}"}

def get_energy_goal():
"""Fetch the current energy goal from the JSON file."""
goal_file_path = os.path.join(ENERGY_FOLDER, "energy_goal.json")

try:
with open(goal_file_path, "r") as file:
return json.load(file)
except FileNotFoundError:
return {"error": "energy_goal.json not found."}
except json.JSONDecodeError:
return {"error": "Invalid JSON format in energy_goal.json."}
49 changes: 49 additions & 0 deletions backend/fastAPI.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import energy_json as ej
import automations as am
import groups as gr
import testPDF as pdf

import asyncio
import os
Expand All @@ -14,11 +15,24 @@
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from collections import deque
from fastapi.responses import FileResponse

# Serve user data (Only for testing purposes)
USER_DB_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "database", "users_db.json")
# print(USER_DB_PATH) # For testing purposes

print("Current Working Directory:", os.getcwd())
print("Graphs folder exists:", os.path.exists("graphs"))
print("Available files in graphs:", os.listdir("graphs"))

base_dir = os.path.dirname(os.path.abspath(__file__))
daily_graph_path = os.path.join(base_dir, "graphs", "daily_energy_graph.png")
monthly_graph_path = os.path.join(base_dir, "graphs", "monthly_energy_graph.png")

print("Daily graph path:", daily_graph_path, "| Exists:", os.path.exists(daily_graph_path))
print("Monthly graph path:", monthly_graph_path, "| Exists:", os.path.exists(monthly_graph_path))


app = FastAPI()

latest_updates = deque(maxlen=5)
Expand Down Expand Up @@ -52,6 +66,15 @@ class GroupRequestStatus(BaseModel):
class DeviceIdsRequest(BaseModel):
device_ids: List[int]


class EnergyReportRequest(BaseModel):
from_month: int
to_month: int
from_date: int
to_date: int
device_ids: List[int]
theme: str = "dark"

@app.on_event("startup")
async def startup_event():
"""Starts device updates when the FastAPI server starts."""
Expand Down Expand Up @@ -171,6 +194,16 @@ def fetch_energy_usage(time_range: str):
def fetch_energy_usage_pdf(time_range: str):
return ej.get_energy_data_pdf(time_range)

@app.get("/energy_goal")
def read_energy_goal():
"""Endpoint to read/fetch the current energy goal"""
return ej.get_energy_goal()

@app.post("/energy_goal/{goal_value}")
def set_energy_goal(goal_value: float):
"""Endpoint to set the energy goal"""
return ej.set_energy_goal(goal_value)

@app.get("/automations")
async def get_automations():
"""Returns the current automation rules"""
Expand Down Expand Up @@ -226,3 +259,19 @@ def update_group_status(group_id: int, status: str):
def delete_group(group_id: int):
"""Delete a group from the selected user"""
return gr.deleteGroup(group_id)

@app.post("/energy_report/")
def generate_energy_report(data: EnergyReportRequest):
print(f"FastAPI CWD: {os.getcwd()}")
print(f"Graphs folder exists: {os.path.exists(os.path.join(os.getcwd(), 'graphs'))}")
print(f"Daily graph exists: {os.path.exists(os.path.join(os.getcwd(), 'graphs', 'daily_energy_graph.png'))}")

pdf_path = pdf.generate_pdf(
from_month=data.from_month,
to_month=data.to_month,
from_day=data.from_date,
to_day=data.to_date,
device_ids=data.device_ids,
theme=data.theme
)
return FileResponse(pdf_path, filename="Energy_Report.pdf")
Binary file added backend/graphs/daily_energy_graph.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added backend/graphs/device_bar_chart.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added backend/graphs/energy_goal_chart.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added backend/graphs/gauge_chart.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added backend/graphs/monthly_energy_graph.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added backend/graphs/power_rating_bar_chart.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading