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
12 changes: 2 additions & 10 deletions backend/automations.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,9 @@
},
{
"id": 3,
"name": "Test3",
"device_id": 1,
"triggers": "08:23",
"enabled": true,
"status": "off"
},
{
"id": 4,
"name": "Test4",
"device_id": 8,
"triggers": "23:00",
"device_id": 1,
"triggers": "21:45",
"enabled": true,
"status": "off"
}
Expand Down
3 changes: 2 additions & 1 deletion backend/automations.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,11 @@ async def automation_scheduler():
for automation in automations:
automation_device_id = int(automation["device_id"])
trigger_time = automation["triggers"]
device_status = automation["status"]

if automation["enabled"] and trigger_time == current_time:
print(f"Triggering automation: {automation['name']} at {current_time}")
changeDeviceStatus(automation_device_id)
changeDeviceStatus(automation_device_id, device_status)

now = datetime.now()
next_minute = (now + timedelta(minutes=1)).replace(second=0, microsecond=0)
Expand Down
5 changes: 5 additions & 0 deletions backend/fastAPI.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ def add_new_user(user: UserRequest):
"""Adds a new user with the given name, password, and optional allocated devices."""
return u.add_user(user.user_name, user.user_password, user.allocated_devices or [])

@app.put("/rename_user/{new_name}")
def rename_user(new_name: str):
"""API endpoint to rename the selected user."""
return u.rename_selected_user(new_name)

@app.delete("/delete_user/{user_name}/{user_password}")
def delete_user(user_name: str, user_password: str):
"""Deletes a user with the given name and password."""
Expand Down
30 changes: 30 additions & 0 deletions backend/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,36 @@ def add_user(user_name: str, user_password: str, allocated_device_ids: list = No

return {"success": message, "user": new_user}

def rename_selected_user(new_name: str):
"""Rename the currently selected user and update both selected_user.json and users_db.json."""
selected_user_data = get_selected_user()
current_name = selected_user_data.get("selected_user")

if not current_name:
return {"error": "No user selected."}

users = load_users()

if any(user["user_name"] == new_name for user in users):
return {"error": f"Username '{new_name}' is already taken."}

user = next((u for u in users if u["user_name"] == current_name), None)

if not user:
return {"error": f"User {current_name} not found in database."}

user["user_name"] = new_name
save_users(users)

with open(SELECTED_USER_FILE, "w") as f:
json.dump({"selected_user": new_name, "user_role": user["user_role"]}, f)

message = f"User {current_name} renamed to {new_name}."
updates.append(message)

return {"success": message, "selected_user": new_name, "user_role": user["user_role"]}



def delete_user(user_name: str, user_password: str):
"""Deletes a user from the system if the given password matches. If deleting the super user, assign the position to the next user.
Expand Down
28 changes: 12 additions & 16 deletions database/users_db.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"users": [
{
"user_id": 1,
"user_name": "Aditya",
"user_name": "Aditya S",
"user_password": "7777",
"allocated_devices": [
"1",
Expand Down Expand Up @@ -57,25 +57,12 @@
6,
5
]
},
{
"id": 5,
"name": "Idk what this is",
"status": "on",
"devices": [
1,
2,
3,
4,
7,
8
]
}
]
},
{
"user_id": 2,
"user_name": "David F",
"user_name": "David Farid",
"user_password": "1415",
"allocated_devices": [
"1",
Expand All @@ -89,11 +76,20 @@
{
"id": 2,
"name": "Bedroom Devices",
"status": "off",
"status": "on",
"devices": [
2,
7
]
},
{
"id": 3,
"name": "Kitchen Devices",
"status": "on",
"devices": [
7,
8
]
}
]
},
Expand Down
42 changes: 34 additions & 8 deletions frontend/src/app/ui/dashboard/accountMenu.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import DevicesIcon from "@mui/icons-material/Devices";

import AddUserDialog from "../newUserDialogue";
import AllocateDevicesDialog from "../allocateDevices";
import UserSettingsDialog from "../userSettingsDialogue";

import { signOut } from "firebase/auth";
import { auth } from "@/app/firebase/config";
Expand All @@ -28,13 +29,15 @@ export default function AccountMenu() {
const [isSuperUser, setIsSuperUser] = React.useState(false);
const [openUserDialog, setOpenUserDialog] = React.useState(false);
const [openAllocateDialog, setOpenAllocateDialog] = React.useState(false);
const [openUserSettingsDialog, setOpenUserSettingsDialog] =
React.useState(false);

const open = Boolean(anchorEl);

const handleClick = (event) => {
setAnchorEl(event.currentTarget);
};

const handleClose = () => {
setAnchorEl(null);
};
Expand Down Expand Up @@ -84,7 +87,11 @@ export default function AccountMenu() {
aria-haspopup="true"
aria-expanded={open ? "true" : undefined}
>
<Avatar sx={{ width: 32, height: 32, fontFamily: "Jetbrains Mono" }}>{selectedUser.charAt(0)}</Avatar>
<Avatar
sx={{ width: 32, height: 32, fontFamily: "Jetbrains Mono" }}
>
{selectedUser.charAt(0)}
</Avatar>
</IconButton>
</Tooltip>
</Box>
Expand Down Expand Up @@ -133,26 +140,38 @@ export default function AccountMenu() {
color: "primary.main",
}}
>
<Avatar sx={{ fontFamily: "Jetbrains Mono" }}>{selectedUser.charAt(0)}</Avatar> {selectedUser}
<Avatar sx={{ fontFamily: "Jetbrains Mono" }}>
{selectedUser.charAt(0)}
</Avatar>{" "}
{selectedUser}
</MenuItem>
<Divider />
{isSuperUser && (
<MenuItem onClick={() => setOpenAllocateDialog(true)} sx={{ fontFamily: "JetBrains Mono" }}>
<MenuItem
onClick={() => setOpenAllocateDialog(true)}
sx={{ fontFamily: "JetBrains Mono" }}
>
<ListItemIcon>
<DevicesIcon fontSize="small" sx={{ color: "primary.main" }} />
</ListItemIcon>
Allocate devices
</MenuItem>
)}
{isSuperUser && (
<MenuItem onClick={() => setOpenUserDialog(true)} sx={{ fontFamily: "JetBrains Mono" }}>
<MenuItem
onClick={() => setOpenUserDialog(true)}
sx={{ fontFamily: "JetBrains Mono" }}
>
<ListItemIcon>
<PersonAdd fontSize="small" sx={{ color: "primary.main" }} />
</ListItemIcon>
Add another account
</MenuItem>
)}
<MenuItem onClick={handleClose} sx={{ fontFamily: "JetBrains Mono" }}>
<MenuItem
onClick={() => setOpenUserSettingsDialog(true)}
sx={{ fontFamily: "JetBrains Mono" }}
>
<ListItemIcon>
<Settings fontSize="small" sx={{ color: "primary.main" }} />
</ListItemIcon>
Expand All @@ -171,7 +190,14 @@ export default function AccountMenu() {
onClose={() => setOpenUserDialog(false)}
// onSave={handleSaveUser}
/>
<AllocateDevicesDialog open={openAllocateDialog} onClose={() => setOpenAllocateDialog(false)} />
<AllocateDevicesDialog
open={openAllocateDialog}
onClose={() => setOpenAllocateDialog(false)}
/>
<UserSettingsDialog
open={openUserSettingsDialog}
onClose={() => setOpenUserSettingsDialog(false)}
/>
</React.Fragment>
);
}
Loading