Added comments in YML for clarity, handle YML camera creation

pull/3/head
riley 12 months ago
parent 7bc9c3d2b6
commit d7f316f26b

@ -20,21 +20,6 @@ config camera_config
option roles 'detect'
option record '1'
option snapshots '1'
option mask '0,461,3,0,1919,0,1919,843,1699,492,1344,458,1346,336,973,317,869,375,866,432'
config camera_config
option name 'upstairs_bedroom'
option path 'rtsp://admin:password@192.168.70.173:554/cam/realmonitor?channel=1&subtype=0&authbasic=64'
option roles 'detect'
option record '1'
option snapshots '1'
option mask '0,461,3,0,1919,0,1919,843,1699,492,1344,458,1346,336,973,317,869,375,866,432'
config camera_config
option name 'kitchen'
option path 'rtsp://admin:password@192.168.70.173:554/cam/realmonitor?channel=1&subtype=0&authbasic=64'
option roles 'detect'
option record '1'
option snapshots '1'
option mask '0,461,3,0,1919,0,1919,843,1699,492,1344,458,1346,336,973,317,869,375,866,432'
option mask ''
option overwrite_cfg '1'

@ -103,6 +103,7 @@ EOF
# yq eval ".usbcoral = \"$usbcoral\"" -i /opt/docker2/compose/frigate/config.yml
# yq eval ".hwaccel = \"$hwaccel\"" -i /opt/docker2/compose/frigate/config.yml
# yq eval ".media.storage = \"$storage\"" -i /opt/docker2/compose/frigate/config.yml
yq eval '. head_comment="WARNING: Any values marked as OVERWRITTEN_BY_ROUTER are managed by Private Router. Manual changes will overwritten unless you uncheck "Overwrite Frigate Config" in the frigate app camera settings."' -i /opt/docker2/compose/frigate/config.yml
yq eval ".mqtt.enabled = $mqtt" -i /opt/docker2/compose/frigate/config.yml
yq eval ".mqtt.host = \"$host\"" -i /opt/docker2/compose/frigate/config.yml
yq eval ".detectors.coral.type = \"$type\"" -i /opt/docker2/compose/frigate/config.yml
@ -216,8 +217,9 @@ write_camera_to_yml() {
# Add path and roles to the inputs in YML
yq eval ".cameras.$name.ffmpeg.inputs[0].path = \"$path\"" -i /opt/docker2/compose/frigate/config.yml
overwrite_warning_comment $name "ffmpeg.inputs[0].path"
yq eval ".cameras.$name.ffmpeg.inputs[0].roles = [\"detect\"]" -i /opt/docker2/compose/frigate/config.yml
overwrite_warning_comment $name "ffmpeg.inputs[0].roles"
# Add other camera settings to YML
yq eval ".cameras.$name.detect = {}" -i /opt/docker2/compose/frigate/config.yml
yq eval ".cameras.$name.detect.width = 1280" -i /opt/docker2/compose/frigate/config.yml
@ -225,18 +227,21 @@ write_camera_to_yml() {
yq eval ".cameras.$name.record = {}" -i /opt/docker2/compose/frigate/config.yml
yq eval ".cameras.$name.record.enabled = $record" -i /opt/docker2/compose/frigate/config.yml
overwrite_warning_comment $name "record.enabled"
yq eval ".cameras.$name.snapshots = {}" -i /opt/docker2/compose/frigate/config.yml
yq eval ".cameras.$name.snapshots.enabled = $snapshots" -i /opt/docker2/compose/frigate/config.yml
overwrite_warning_comment $name "snapshots.enabled"
# Check if the mask is not empty
if [ -n "$mask" ]; then
# Run the yq commands to update the YAML file
yq eval ".cameras.$name.motion = {}" -i /opt/docker2/compose/frigate/config.yml
yq eval ".cameras.$name.motion.mask = [\"$mask\"]" -i /opt/docker2/compose/frigate/config.yml
overwrite_warning_comment $name "motion.mask"
else
echo "Mask is empty, ignoring."
fi
yq eval ".cameras.$name.origin = \"privaterouter\"" -i /opt/docker2/compose/frigate/config.yml
yq eval ".cameras.$name.origin line_comment=\"DO NOT EDIT THIS LINE\"" -i /opt/docker2/compose/frigate/config.yml
}
get_camera_index_by_name() {
@ -251,20 +256,28 @@ get_camera_index_by_name() {
}
# Toggles the line comment for a specific camera attribute in the YML
toggle_override() {
overwrite_warning_comment() {
local name=$1
local attribute=$2
local current_comment=$(yq eval ".cameras.$name.$attribute | line_comment" /opt/docker2/compose/frigate/config.yml)
if [ "$current_comment" = "OVERWRITTEN_BY_UCI" ]; then
# If the current comment is "OVERWRITTEN_BY_UCI", remove it by setting it to nothing
yq eval ".cameras.$name.$attribute line_comment=\"\"" -i /opt/docker2/compose/frigate/config.yml
else
# Otherwise, set the comment to "OVERWRITTEN_BY_UCI"
yq eval ".cameras.$name.$attribute line_comment=\"OVERWRITTEN_BY_UCI\"" -i /opt/docker2/compose/frigate/config.yml
fi
yq eval ".cameras.$name.$attribute line_comment=\"OVERWRITTEN_BY_ROUTER\"" -i /opt/docker2/compose/frigate/config.yml
}
remove_warning_comments() {
local name=$1
# Attribute list to remove warning comments
local attributes=(
"ffmpeg.inputs[0].path"
"ffmpeg.inputs[0].roles"
"record.enabled"
"snapshots.enabled"
"motion.mask"
)
for attribute in "${attributes[@]}"; do
yq '.cameras.'$name'.'$attribute' line_comment=""' -i /opt/docker2/compose/frigate/config.yml
done
}
update_camera_in_yml() {
local name=$1
@ -272,21 +285,28 @@ update_camera_in_yml() {
local camera_index=$(get_camera_index_by_name $name)
# Fetch and Update the specific settings for this camera from UCI -> YAML
# For each attribute you update, apply warning comment
local path=$(get_uci_camera_value $camera_index "path")
set_yml_value $name ".ffmpeg.inputs[0].path" "\"$path\""
overwrite_warning_comment $name "ffmpeg.inputs[0].path"
local roles=$(get_uci_camera_value $camera_index "roles")
set_yml_value $name ".ffmpeg.inputs[0].roles" $roles
overwrite_warning_comment $name "ffmpeg.inputs[0].roles"
local record=$(get_uci_camera_value $camera_index "record")
set_yml_value $name ".record.enabled" $record
overwrite_warning_comment $name "record.enabled"
local snapshots=$(get_uci_camera_value $camera_index "snapshots")
set_yml_value $name ".snapshots.enabled" $snapshots
overwrite_warning_comment $name "snapshots.enabled"
local mask=$(get_uci_camera_value $camera_index "mask")
if [ -n "$mask" ]; then
set_yml_value $name ".motion.mask" "[\"$mask\"]"
overwrite_warning_comment $name "motion.mask"
else
yq eval "del(.cameras.$name.motion)" -i /opt/docker2/compose/frigate/config.yml
fi
@ -307,10 +327,14 @@ sync_camera_config() {
while true; do
# Try to fetch camera configuration
local camera_name=$(get_uci_camera_value $camera_index)
local overwrite_cfg=$(get_uci_camera_value $camera_index "overwrite_cfg")
# Exit loop if no more cameras are found
[ -z "$camera_name" ] && break
# Only proceed if overwrite_cfg is enabled (set to 1)
if [ "$overwrite_cfg" -eq 1 ]
then
if ! echo "$yml_cameras" | grep -q -w "$camera_name"; then
echo "UCI Camera $camera_name not found in YML, adding..."
write_camera_to_yml $camera_name
@ -318,6 +342,10 @@ sync_camera_config() {
echo "UCI Camera $camera_name found in YML, updating..."
update_camera_in_yml $camera_name
fi
else
echo "Skipping camera $camera_name, overwrite is set to false."
remove_warning_comment $camera_name
fi
# Add to uci_cameras
uci_cameras="${uci_cameras} ${camera_name}"
@ -326,18 +354,26 @@ sync_camera_config() {
camera_index=$((camera_index+1))
done
# Crosscheck and remove any old cameras in YML not present in UCI config
# Crosschecking and removing orphaned entry in YML not present in UCI config
for yml_camera in $yml_cameras; do
# Get the origin of this camera
local origin=$(get_yml_value $yml_camera ".origin")
# 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..."
# If the origin is either non-existent or not equal to "privaterouter", keep it
if [ -z "$origin" ] || [ "$origin" != "privaterouter" ]; then
echo "Manual camera $yml_camera found in YML. Keeping it since it's manually added."
else
echo "Auto generated 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
fi
set +f
done
echo "sync_camera_config completed!"
echo "Camera config syncing completed!"
}
case "${ACTION}" in

Loading…
Cancel
Save