diff --git a/helper/poo_recorder_template.conf b/helper/home_automation_backend_template.conf similarity index 88% rename from helper/poo_recorder_template.conf rename to helper/home_automation_backend_template.conf index 8caa060..30f926c 100644 --- a/helper/poo_recorder_template.conf +++ b/helper/home_automation_backend_template.conf @@ -1,4 +1,4 @@ -[program:poo_recorder_backend] +[program:home_automation_backend] environment=PYTHONUNBUFFERED=1 command= directory= diff --git a/helper/install.sh b/helper/install.sh index 6a497f9..f608f3b 100755 --- a/helper/install.sh +++ b/helper/install.sh @@ -3,8 +3,8 @@ # Argument parsing if [[ $# -ne 1 ]]; then echo "Usage: $0 [--install|--uninstall|--help]" - echo " --install Install the poo recorder" - echo " --uninstall Uninstall the poo recorder" + echo " --install Install the automation backend" + echo " --uninstall Uninstall the automation backend" echo " --update Update the installation" echo " --help Show this help message" exit 0 @@ -22,9 +22,10 @@ case $key in UPDATE=true ;; --help) - echo "Usage: $0 [--install|--uninstall|--help]" - echo " --install Install the poo recorder" - echo " --uninstall Uninstall the poo recorder" + echo "Usage: $0 [--install|--uninstall|--update|--help]" + echo " --install Install the automation backend" + echo " --uninstall Uninstall the automation backend" + echo " --update Update the installation" echo " --help Show this help message" exit 0 ;; @@ -34,68 +35,69 @@ case $key in ;; esac -TARGET_DIR="$HOME/.local/poo-recorder" +TARGET_DIR="$HOME/.local/home-automation-backend" +SUPERVISOR_CFG_NAME="home_automation_backend" +SUPERVISOR_CFG="$SUPERVISOR_CFG_NAME.conf" BASEDIR=$(dirname "$0") # Install or uninstall based on arguments -install_poo_recorder() { +install_backend() { # Installation code here echo "Installing..." sudo apt update sudo apt install python3 python3-venv supervisor - sudo supervisorctl stop poo_recorder_backend + sudo supervisorctl stop $SUPERVISOR_CFG_NAME mkdir -p $TARGET_DIR - rm -rf $BASEDIR/../recorder/__pycache__ - cp -r $BASEDIR/../recorder $BASEDIR/../requirements.txt $TARGET_DIR - + rm -rf `find $BASEDIR/../src -type d -name __pycache__` + cp -r $BASEDIR/../src $BASEDIR/../requirements.txt $TARGET_DIR python3 -m venv "$TARGET_DIR/venv" $TARGET_DIR/venv/bin/pip install -r $TARGET_DIR/requirements.txt - cp $BASEDIR/poo_recorder_template.conf $BASEDIR/poo_recorder.conf + cp $BASEDIR/"$SUPERVISOR_CFG_NAME"_template.conf $BASEDIR/$SUPERVISOR_CFG - sed -i "s+command=+command=$TARGET_DIR/venv/bin/fastapi run $TARGET_DIR/recorder/main.py --port 8881+g" $BASEDIR/poo_recorder.conf - sed -i "s+directory=+directory=$TARGET_DIR+g" $BASEDIR/poo_recorder.conf - sed -i "s+user=+user=$USER+g" $BASEDIR/poo_recorder.conf - sed -i "s+group=+group=$USER+g" $BASEDIR/poo_recorder.conf + sed -i "s+command=+command=$TARGET_DIR/venv/bin/fastapi run $TARGET_DIR/src/main.py --port 8881+g" $BASEDIR/$SUPERVISOR_CFG + sed -i "s+directory=+directory=$TARGET_DIR+g" $BASEDIR/$SUPERVISOR_CFG + sed -i "s+user=+user=$USER+g" $BASEDIR/$SUPERVISOR_CFG + sed -i "s+group=+group=$USER+g" $BASEDIR/$SUPERVISOR_CFG - sudo mv $BASEDIR/poo_recorder.conf /etc/supervisor/conf.d/poo_recorder.conf + sudo mv $BASEDIR/$SUPERVISOR_CFG /etc/supervisor/conf.d/$SUPERVISOR_CFG sudo supervisorctl reread sudo supervisorctl update - sudo supervisorctl start poo_recorder_backend + sudo supervisorctl start $SUPERVISOR_CFG_NAME echo "Installation complete." } -uninstall_poo_recorder() { +uninstall_backend() { # Uninstallation code here echo "Uninstalling..." - sudo supervisorctl stop poo_recorder_backend + sudo supervisorctl stop $SUPERVISOR_CFG_NAME - sudo supervisorctl remove poo_recorder_backend + sudo supervisorctl remove $SUPERVISOR_CFG_NAME - sudo rm /etc/supervisor/conf.d/poo_recorder.conf + sudo rm /etc/supervisor/conf.d/$SUPERVISOR_CFG rm -rf $TARGET_DIR echo "Uninstallation complete." } -update_poo_recorder() { - uninstall_poo_recorder - install_poo_recorder +update_backend() { + uninstall_backend + install_backend } if [[ $INSTALL ]]; then - install_poo_recorder + install_backend elif [[ $UNINSTALL ]]; then - uninstall_poo_recorder + uninstall_backend elif [[ $UPDATE ]]; then - update_poo_recorder + update_backend else echo "Invalid argument: $key" exit 1 diff --git a/recorder/__init__.py b/src/__init__.py similarity index 100% rename from recorder/__init__.py rename to src/__init__.py diff --git a/src/cloud_util/__init__.py b/src/cloud_util/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/config.py b/src/config.py new file mode 100644 index 0000000..02ec31f --- /dev/null +++ b/src/config.py @@ -0,0 +1,7 @@ +import os + +from dotenv import load_dotenv + +load_dotenv() + +NOTION_TOKEN = os.getenv("NOTION_TOKEN") diff --git a/recorder/main.py b/src/main.py similarity index 90% rename from recorder/main.py rename to src/main.py index 152ccca..09caaba 100644 --- a/recorder/main.py +++ b/src/main.py @@ -2,7 +2,7 @@ from contextlib import asynccontextmanager from fastapi import FastAPI -from recorder.poo import PooRecorder +from src.recorder.poo import PooRecorder recorder = PooRecorder() diff --git a/src/recorder/__init__.py b/src/recorder/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/recorder/notion_handle.py b/src/recorder/notion_handle.py similarity index 89% rename from recorder/notion_handle.py rename to src/recorder/notion_handle.py index e586000..8d4943b 100644 --- a/recorder/notion_handle.py +++ b/src/recorder/notion_handle.py @@ -1,14 +1,13 @@ -import os from datetime import datetime -from dotenv import load_dotenv from notion_client import AsyncClient as Client +from src.config import NOTION_TOKEN + class NotionClient: def __init__(self) -> None: - load_dotenv() - self._notion = Client(auth=os.environ["NOTION_TOKEN"]) + self._notion = Client(auth=NOTION_TOKEN) self._page_id = "3cf594afd0754497ba0a93b94912b897" self._table_id = "9828b56c53de46c794673fe1d01ad522" diff --git a/recorder/poo.py b/src/recorder/poo.py similarity index 97% rename from recorder/poo.py rename to src/recorder/poo.py index 1b32e24..6a1d298 100644 --- a/recorder/poo.py +++ b/src/recorder/poo.py @@ -2,7 +2,7 @@ from datetime import datetime from fastapi_mqtt import FastMQTT, MQTTConfig -from recorder.notion_handle import NotionClient +from src.recorder.notion_handle import NotionClient class PooRecorder: @@ -60,7 +60,7 @@ class PooRecorder: "device": { "name": "Dog Poop Recorder", "model": "poop-recorder-backend", - "sw_version": "1.0", + "sw_version": "1.1", "identifiers": ["poo_recorder"], "manufacturer": "Studio TJ", },