WIP frigate config syncing improvements

pull/3/head
riley 1 year ago
parent 1bf23bf356
commit 8b997a53bf

@ -4,6 +4,7 @@ APP_NAME="frigate"
ACTION="${1}"
shift 1
set -x
fetch_and_validate_uci () {
local config_name=$1
@ -280,67 +281,91 @@ write_camera_to_yml() {
fi
}
update_camera_in_yml() {
local name=$1
# Deletes the camera from the YML file and writes it again
yq eval "del(.cameras.$name)" -i /opt/docker2/compose/frigate/config.yml
write_camera_to_yml $name
get_camera_index_by_name() {
local name=$1
local camera_index=0
while : ; do
local current_name=$(get_uci_camera_value $camera_index)
[[ "$current_name" == "$name" ]] && echo $camera_index && break
[[ -z "$current_name" ]] && break
camera_index=$((camera_index+1))
done
}
update_camera_in_yml() {
local name=$1
# Get the index of the camera with name "$name"
local camera_index=$(get_camera_index_by_name $name)
# Fetch and Update the specific settings for this camera from UCI -> YAML
local path=$(get_uci_camera_value $camera_index "path")
set_yml_value $name ".ffmpeg.inputs[0].path" $path
local roles=$(get_uci_camera_value $camera_index "roles")
set_yml_value $name ".ffmpeg.inputs[0].roles" $roles
sync_camera_config() {
echo "Starting sync_camera_config..."
local record=$(get_uci_camera_value $camera_index "record")
set_yml_value $name ".record.enabled" $record
# Fetching all cameras from UCI
local uci_cameras=""
local camera_index=0
while : ; do
local name=$(get_uci_camera_value $camera_index)
# Exit loop if no more cameras are found
[[ -z "$name" ]] && break
local snapshots=$(get_uci_camera_value $camera_index "snapshots")
set_yml_value $name ".snapshots.enabled" $snapshots
uci_cameras="$uci_cameras $name"
camera_index=$((camera_index+1))
done
local mask=$(get_uci_camera_value $camera_index "mask")
if [ -n "$mask" ]; then
set_yml_value $name ".motion.mask" $mask
else
echo "Mask is empty, ignoring."
fi
}
echo "Cameras fetched from UCI: $uci_cameras"
sync_camera_config() {
echo "Starting sync_camera_config..."
# Fetching all cameras from Frigate YML
local yml_cameras=$(yq eval '.cameras | keys' /opt/docker2/compose/frigate/config.yml | sed 's/- //g' | xargs)
# Initializing the camera index
camera_index=0
# Fetching all cameras from frigate YML
yml_cameras=$(yq eval '.cameras | keys' /opt/docker2/compose/frigate/config.yml | sed 's/- //g' | xargs)
echo "Cameras fetched from YML: $yml_cameras"
# Checking for cameras in UCI that are not in YML and adding them
for camera_name in $uci_cameras; do
if ! echo "$yml_cameras" | grep -q "$camera_name"; then
# Loop through all camera configs in UCI using index
while true; do
# Try to fetch camera configuration
local camera_name=$(get_uci_camera_value $camera_index)
# Exit loop if no more cameras are found
[ -z "$camera_name" ] && break
if ! echo "$yml_cameras" | grep -q -w "$camera_name"; then
echo "Camera $camera_name not found in YML, adding..."
# Retrieves camera settings from UCI configuration and adds them to the YML file.
write_camera_to_yml $camera_name
else
echo "Camera $camera_name found in YML, updating..."
# Retrieves camera settings from UCI configuration and updates them in the YML file.
update_camera_in_yml $camera_name
fi
done
# Checking for cameras in YML that are not in UCI and deleting them
if [[ $yml_cameras != '[]' ]]; then # Add this condition
for camera_name in $yml_cameras; do
if ! echo "$uci_cameras" | grep -q "$camera_name"; then
echo "Camera $camera_name not found in UCI, removing from YML..."
yq eval "del(.cameras.$camera_name)" -i /opt/docker2/compose/frigate/config.yml
fi
done
fi # Close the condition
echo "sync_camera_config completed!"
}
# Add to uci_cameras
uci_cameras="${uci_cameras} ${camera_name}"
# Increment camera index
camera_index=$((camera_index+1))
done
# Crosscheck and remove any old cameras in YML not present in UCI config
for yml_camera in $yml_cameras; do
# Fix for word splitting
set -f
if ! echo $uci_cameras | grep -q -w "$yml_camera"; then
echo "Camera $yml_camera found in YML, but not in UCI, removing..."
yq eval "del(.cameras.$yml_camera)" -i /opt/docker2/compose/frigate/config.yml
fi
set +f
done
echo "sync_camera_config completed!"
}
case "${ACTION}" in
"install" | "upgrade")
@ -362,6 +387,7 @@ case "${ACTION}" in
CONTAINER_IDS=$(docker ps -a --filter "name=${APP_NAME}" --format '{{.ID}}')
for ID in $CONTAINER_IDS; do
docker "${ACTION}" "${ID}"
sync_camera_config
done
;;
"status")
@ -381,7 +407,4 @@ case "${ACTION}" in
# Use the UCI port to filter the docker ps output and return only the external port
docker ps --all -f "name=${CONTAINER_NAME}" --format '{{.Ports}}' | grep -o "0.0.0.0:$uci_port->[0-9]*/tcp" | sed "s/0.0.0.0://;s/->[0-9]*\/tcp//"
;;
"sync")
sync_camera_config
;;
esac
Loading…
Cancel
Save