Skip to content

Commit

Permalink
first working draft
Browse files Browse the repository at this point in the history
  • Loading branch information
isaric committed Dec 2, 2023
1 parent 3f84a4e commit a8cc6fc
Show file tree
Hide file tree
Showing 19 changed files with 142 additions and 29 deletions.
17 changes: 3 additions & 14 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,21 +1,10 @@
install:
## check if vars are set
source ./check_vars.sh GOPATH
## check for sudo privileges
source ./check_sudo.sh
bash ./check_vars.sh GOPATH
## run go install
go install ./...
## create system-wide symlink to executables
source ./create_symlinks.sh motion-poll set-preset goto-preset set-time
bash ./create_symlinks.sh motion-poll set-preset goto-preset set-time
## create config folder
echo "Path for configuration folder [~/.config/onvif-cam-poll]"
read path
if [ -z "$path" ]
then path="~/.config/onvif-cam-poll"
mkdir -p $path
## create service templates
cp ./services/* $path/.
## overwrite service files - environment file, user, group, execstart
## copy service files to system service folder
## run systemctl daemon-reload
bash ./create_service_templates.sh

8 changes: 4 additions & 4 deletions check_vars.sh
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
function check_var() {
#!/bin/bash
function check_var {
declare -n var_ref=$1
declare -p "$var_ref" &>/dev/null
declared=$?
if ["$declared" != 0 ]
if [ "$declared" != 0 ]
then
echo "$var_ref must be set! Exiting!"
exit
exit 1
fi
}

Expand Down
17 changes: 17 additions & 0 deletions create_service_templates.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash

read -r -p "Path for configuration folder [~/.config/onvif-cam-poll]" $CONFIG_PATH
export CONFIG_PATH=${config_path:-~/.config/onvif-cam-poll}
echo "$CONFIG_PATH"
mkdir --p "$CONFIG_PATH"
export CONFIG_PATH
## create service templates
cp ./services/* "$CONFIG_PATH"/.
envsubst < "$CONFIG_PATH"/motion@.service > "$CONFIG_PATH"/motion@.service.temp && mv "$CONFIG_PATH"/motion@.service.temp "$CONFIG_PATH"/motion@.service
envsubst < "$CONFIG_PATH"/time@.service > "$CONFIG_PATH"/time@.service.temp && mv "$CONFIG_PATH"/time@.service.temp "$CONFIG_PATH"/time@.service
sudo cp "$CONFIG_PATH"/*.service /lib/systemd/system/.
sudo systemctl daemon-reload
echo "To track a new camera create an .env file for it in $CONFIG_PATH"
echo "Fill in all the variables that are listed in the example.env file"
echo "Afterwards, run systemctl start motion@camera_envfile_name"
echo "Don't forget to enable the service if you want it to run after a restart"
3 changes: 2 additions & 1 deletion create_symlinks.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
function create_symlink() {
ln -s "$GOPATH/$1" "/usr/bin/$1"
sudo rm "/usr/bin/$1"
sudo ln -s "$GOPATH/$1" "/usr/bin/$1"
}

for var in "$@"
Expand Down
16 changes: 16 additions & 0 deletions motion@.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[Unit]
DefaultDependencies=no
After=network.target

[Service]
Type=simple
EnvironmentFile=/home/isaric/.config/onvif-cam-poll/%i.env
Restart=always
RestartSec=3
User=isaric
Group=isaric
ExecStart=/bin/bash /home/isaric/.config/onvif-cam-poll/motion.sh
TimeoutStartSec=0

[Install]
WantedBy=default.target
6 changes: 6 additions & 0 deletions onfig_path/example.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CAMERA_ADDRESS=bla.bla.bla:8899
CAMERA_USER=admin
CAMERA_PASSWORD=xxxxxx
CAMERA_NAME=garage
SLACK_BOT_TOKEN=xoxb-xxxxxx-xxxxxx-xxxxxx
SLACK_CHANNEL_ID=xxxxxx
1 change: 1 addition & 0 deletions onfig_path/motion.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
motion-poll -a ${CAMERA_ADDRESS} -u ${CAMERA_USER} -p ${CAMERA_PASSWORD} -n ${CAMERA_NAME} -s ${SLACK_WEBHOOK} -t 30 -b ${SLACK_BOT_TOKEN} -c ${SLACK_CHANNEL_ID}
16 changes: 16 additions & 0 deletions onfig_path/motion@.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[Unit]
DefaultDependencies=no
After=network.target

[Service]
Type=simple
EnvironmentFile=$path/%i.env
Restart=always
RestartSec=3
User=$SUDO_USER
Group=$SUDO_USER
ExecStart=/bin/bash $path/motion.sh
TimeoutStartSec=0

[Install]
WantedBy=default.target
1 change: 1 addition & 0 deletions onfig_path/time.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
set-time -a ${CAMERA_ADDRESS} -u admin -p ${CAMERA_PASSWORD}
16 changes: 16 additions & 0 deletions onfig_path/time@.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[Unit]
DefaultDependencies=no
After=network.target

[Service]
Type=simple
EnvironmentFile=$path/%i.env
Restart=always
RestartSec=3
User=$SUDO_USER
Group=$SUDO_USER
ExecStart=/bin/bash $path/time.sh
TimeoutStartSec=0

[Install]
WantedBy=default.target
16 changes: 16 additions & 0 deletions services/goto-preset@.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[Unit]
DefaultDependencies=no
After=network.target

[Service]
Type=simple
EnvironmentFile=${CONFIG_PATH}/%i.env
Restart=always
RestartSec=3
User=${USER}
Group=${USER}
ExecStart=/bin/bash ${CONFIG_PATH}/goto.sh
TimeoutStartSec=0

[Install]
WantedBy=default.target
1 change: 1 addition & 0 deletions services/goto.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
goto-preset -a ${CAMERA_ADDRESS} -u ${CAMERA_USER} -p ${CAMERA_PASSWORD} -t 30 -l 001
2 changes: 1 addition & 1 deletion services/motion.sh
Original file line number Diff line number Diff line change
@@ -1 +1 @@
/path/to/motion-poll -a ${CAMERA_ADDRESS} -u ${CAMERA_USER} -p ${CAMERA_PASSWORD} -n ${CAMERA_NAME} -s ${SLACK_WEBHOOK} -t 30 -b ${SLACK_BOT_TOKEN} -c ${SLACK_CHANNEL_ID}
motion-poll -a ${CAMERA_ADDRESS} -u ${CAMERA_USER} -p ${CAMERA_PASSWORD} -n ${CAMERA_NAME} -s ${SLACK_WEBHOOK} -t 30 -b ${SLACK_BOT_TOKEN} -c ${SLACK_CHANNEL_ID}
8 changes: 4 additions & 4 deletions services/motion@.service
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ After=network.target

[Service]
Type=simple
EnvironmentFile=/home/your-user/motion-poll-services/%i.env
EnvironmentFile=${CONFIG_PATH}/%i.env
Restart=always
RestartSec=3
User=isaric
Group=isaric
ExecStart=/bin/bash /home/your-user/motion-poll-services/motion.sh
User=${USER}
Group=${USER}
ExecStart=/bin/bash ${CONFIG_PATH}/motion.sh
TimeoutStartSec=0

[Install]
Expand Down
1 change: 1 addition & 0 deletions services/set-preset.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
set-preset -a ${CAMERA_ADDRESS} -u ${CAMERA_USER} -p ${CAMERA_PASSWORD} -l 001
16 changes: 16 additions & 0 deletions services/set-preset@.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[Unit]
DefaultDependencies=no
After=network.target

[Service]
Type=simple
EnvironmentFile=${CONFIG_PATH}/%i.env
Restart=always
RestartSec=3
User=${USER}
Group=${USER}
ExecStart=/bin/bash ${CONFIG_PATH}/set-preset.sh
TimeoutStartSec=0

[Install]
WantedBy=default.target
2 changes: 1 addition & 1 deletion services/time.sh
Original file line number Diff line number Diff line change
@@ -1 +1 @@
/path/to/set-time -a ${CAMERA_ADDRESS} -u admin -p ${CAMERA_PASSWORD}
set-time -a ${CAMERA_ADDRESS} -u admin -p ${CAMERA_PASSWORD}
8 changes: 4 additions & 4 deletions services/time@.service
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ After=network.target

[Service]
Type=simple
EnvironmentFile=/home/your-user/motion-poll-services/%i.env
EnvironmentFile=${CONFIG_PATH}/%i.env
Restart=always
RestartSec=3
User=isaric
Group=isaric
ExecStart=/bin/bash /home/your-user/motion-poll-services/time.sh
User=${USER}
Group={$USER}
ExecStart=/bin/bash ${CONFIG_PATH}/time.sh
TimeoutStartSec=0

[Install]
Expand Down
16 changes: 16 additions & 0 deletions time@.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[Unit]
DefaultDependencies=no
After=network.target

[Service]
Type=simple
EnvironmentFile=/home/isaric/.config/onvif-cam-poll/%i.env
Restart=always
RestartSec=3
User=isaric
Group=isaric
ExecStart=/bin/bash /home/isaric/.config/onvif-cam-poll/time.sh
TimeoutStartSec=0

[Install]
WantedBy=default.target

0 comments on commit a8cc6fc

Please sign in to comment.