Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement door lock-out feature #24

Merged
merged 18 commits into from
Dec 20, 2024
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: 12 additions & 0 deletions app/api/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from .routes import accessLog, camera, card, user
import os
import multiprocessing
from classes.RFID_Reader import door_controller
import traceback

os.environ["FLASK_RUN_FROM_CLI"] = "false"

Expand Down Expand Up @@ -65,3 +67,13 @@ def destroy_api():
@app.route("/")
def hello_world():
return jsonify("Hello World"), 200

@app.route("/lockout", methods=["POST"])
def set_lockout():
try:
_, locked_out_state = door_controller.get_state()
door_controller.set_lockout(not locked_out_state)

return jsonify({"locked_out": str(not locked_out_state)}), 200
except Exception as e:
return jsonify({"error": str(e), "stack": traceback.format_exc()}), 400
1 change: 1 addition & 0 deletions app/aws/S3.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
aws_access_key_id=os.getenv("AWS_ACCESS_KEY_ID"),
aws_secret_access_key=os.getenv("AWS_SECRET_ACCESS_KEY"),
aws_session_token=os.getenv("AWS_SESSION_TOKEN"),
region_name=os.getenv("AWS_REGION")
)


Expand Down
32 changes: 25 additions & 7 deletions app/classes/DoorControl.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,42 +8,60 @@

sysfs_path = "/sys/kernel/led_toggle/led_toggle"


class DoorState(Enum):
LOCKED = "0" # Light Off [LOW]
UNLOCKED = "1" # Light On [HIGH]

class DoorController:
def __init__(self):
self.locked_out = False
if not path.exists(sysfs_path):
raise FileNotFoundError(f"Sysfs File Not Found. Expected Location: {sysfs_path}")
raise FileNotFoundError(
f"Sysfs File Not Found. Expected Location: {sysfs_path}"
)

def get_state(self):
with open(sysfs_path, "r") as file:
state = file.read().strip()
return int(state)

is_active = state == "1"
return is_active, self.locked_out
def get_locked_out(self):
return self.locked_out

def lock(self):
thread_logger.info("LOCKING DOOR")
if self.locked_out:
return False

self._write_state(DoorState.LOCKED)

def unlock(self, seconds: int):
thread_logger.info("UNLOCKING DOOR")
if self.locked_out:
return False

self._write_state(DoorState.UNLOCKED)
if seconds:
sleep(seconds)
self.lock()

def toggle_lock(self):
if self.get_state() == DoorState.LOCKED:
state, locked_out = self.get_state()
if locked_out:
return False

if state:
self.unlock()
else:
self.lock()

def set_lockout(self, shouldLockout: bool):
self.locked_out = shouldLockout

def _write_state(self, state: DoorState):
thread_logger.info(f"Attempting to write state {str(state.value)}")

try:
subprocess.run(['sudo', 'sh', '-c', f'echo {state.value} > {sysfs_path}'], check=True)
thread_logger.info(f"Successfully wrote state {str(state.value)} to {sysfs_path}")
except subprocess.CalledProcessError as e:
thread_logger.error(f"Failed to write state to sysfs: {e}")
thread_logger.error(f"Failed to write state to sysfs: {e}")
2 changes: 1 addition & 1 deletion app/classes/RFID_Reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def _start_reader(self):
f"*{'VALID' if is_valid else 'INVALID'} TAG READ* | ID: {id} | Text: '{text}'"
)

if is_valid:
if is_valid and not door_controller.get_locked_out():
door_controller.unlock(3)
self.logger.info("Attempting upload to bucket.")
bucket, file_object = self.camera.record_and_upload(
Expand Down
Empty file modified app/load_python.sh
100644 → 100755
Empty file.
1 change: 0 additions & 1 deletion app/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
boto3
python-dotenv
picamera
smbus2
mfrc522
Expand Down
3 changes: 3 additions & 0 deletions kernel/load_module.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#!/bin/bash
# Compile LKM
echo "Cleaning up......"
make clean

echo "Compiling Linux Kernel Module (led_toggle)..."
make

Expand Down
7 changes: 6 additions & 1 deletion startup.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
#!/bin/bash
if [ "$(id -u)" -ne 0 ]; then
echo "This script must executed as root (OR with the sudo command)"
exit 1
fi

echo "Setting up Linux Kernel Module..."
cd kernel/
sudo ./load_module.sh
Expand All @@ -7,4 +12,4 @@ cd ..

echo "Setting up Python Application..."
cd app
./load_python.sh
sudo ./load_python.sh