commit 4c3d41e7c98fb4a2e3b271c1d6e71afd40bc757f Author: ben Date: Tue Sep 12 03:30:35 2023 -0400 first commit diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..ff14318 --- /dev/null +++ b/Makefile @@ -0,0 +1,16 @@ + + +include $(TOPDIR)/rules.mk + + +LUCI_TITLE:=LuCI support for frigate +LUCI_PKGARCH:=all +LUCI_DEPENDS:=+lsblk +docker +luci-lib-taskd +yq + +define Package/luci-app-frigate/conffiles +/etc/config/frigate +endef + +include $(TOPDIR)/feeds/luci/luci.mk + +# call BuildPackage - OpenWrt buildroot signature diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/luasrc/controller/frigate.lua b/luasrc/controller/frigate.lua new file mode 100644 index 0000000..ef89352 --- /dev/null +++ b/luasrc/controller/frigate.lua @@ -0,0 +1,15 @@ + +module("luci.controller.frigate", package.seeall) + +function index() + entry({"admin", "apps"}, firstchild(), _("Apps") , 45).dependent = false + if not nixio.fs.access("/etc/config/frigate") then + return + end + + local page = entry({"admin", "apps", "frigate"}, cbi("frigate"), _("frigate")) + page.order = 10 + page.dependent = true + page.acl_depends = { "luci-app-frigate" } + entry({"admin","apps","frigate","status"},call("act_status")).leaf=true +end diff --git a/luasrc/model/cbi/frigate.lua b/luasrc/model/cbi/frigate.lua new file mode 100644 index 0000000..a526cc6 --- /dev/null +++ b/luasrc/model/cbi/frigate.lua @@ -0,0 +1,75 @@ +--[[ +LuCI - Lua Configuration Interface +]]-- + +local taskd = require "luci.model.tasks" +local frigate_model = require "luci.model.frigate" +local m, s, o + +m = taskd.docker_map("frigate", "Frigate NVR", "/usr/libexec/apps/frigate/frigate.sh", + translate("Frigate"), + translate("Frigate is an open-source NVR built around real-time AI object detection. All processing is performed locally on your own hardware.") + .. translate("Official website:") .. ' https://frigate.video/') + +s = m:section(SimpleSection, translate("Service Status"), translate("frigate status:")) +s:append(Template("frigate/status")) + +-- General Frigate Docker setup +s = m:section(TypedSection, "frigate_config", "Docker Configuration", "General settings for Docker") +s.addremove = false +s.anonymous = true + +o = s:option(Value, "port", "Port") +o.default = "5000" + +o = s:option(Value, "image", "Image") +o.default = "ghcr.io/blakeblackshear/frigate:stable" + +o = s:option(Value, "usbcoral", "USB Coral") +o.default = "/dev/bus/usb" + +o = s:option(Value, "hwaccel", "Hardware Acceleration") +o.default = "/dev/crypto" + +o = s:option(Value, "storage", "Storage Path") +o.default = "./frigate/storage" + +-- MQTT Configuration +s = m:section(TypedSection, "frigate_config", "MQTT Configuration", "Settings for MQTT") +s.addremove = false +s.anonymous = true + +o = s:option(Flag, "mqtt", "Enable MQTT") +o.default = "0" + +o = s:option(Value, "host", "MQTT Host") +o.default = "mqtt.server.com" + +-- Detectors Configuration +s = m:section(TypedSection, "frigate_config", "Detectors Configuration", "Settings for Detectors") +s.addremove = false +s.anonymous = true + +o = s:option(Flag, "coral", "Enable Coral") +o.default = "1" + +o = s:option(Value, "type", "Type") +o.default = "edgetpu" + +o = s:option(Value, "device", "Device") +o.default = "usb" + +-- Camera Configuration +s = m:section(TypedSection, "camera_config", "Camera Configuration", "Settings for Cameras") +s.addremove = true +s.anonymous = true +s.novaluetext = "There are no cameras configured yet." + +o = s:option(Value, "name", "Camera Name") +o = s:option(Value, "path", "RTSP Path") +o = s:option(Value, "roles", "Roles") +o = s:option(Flag, "record", "Enable Recording") +o = s:option(Flag, "snapshots", "Enable Snapshots") +o = s:option(Value, "mask", "Mask") + +return m \ No newline at end of file diff --git a/luasrc/model/frigate.lua b/luasrc/model/frigate.lua new file mode 100644 index 0000000..f4d501d --- /dev/null +++ b/luasrc/model/frigate.lua @@ -0,0 +1,55 @@ +local util = require "luci.util" +local jsonc = require "luci.jsonc" + +local frigate = {} + +frigate.blocks = function() + local f = io.popen("lsblk -s -f -b -o NAME,FSSIZE,MOUNTPOINT --json", "r") + local vals = {} + if f then + local ret = f:read("*all") + f:close() + local obj = jsonc.parse(ret) + for _, val in pairs(obj["blockdevices"]) do + local fsize = val["fssize"] + if fsize ~= nil and string.len(fsize) > 10 and val["mountpoint"] then + -- fsize > 1G + vals[#vals+1] = val["mountpoint"] + end + end + end + return vals +end + +frigatered.home = function() + local uci = require "luci.model.uci".cursor() + local home_dirs = {} + home_dirs["main_dir"] = uci:get_first("quickstart", "main", "main_dir", "/opt/docker2/compose") + home_dirs["Configs"] = uci:get_first("quickstart", "main", "conf_dir", home_dirs["main_dir"].."/frigate/Configs") + home_dirs["Public"] = uci:get_first("quickstart", "main", "pub_dir", home_dirs["main_dir"].."/Public") + home_dirs["Downloads"] = uci:get_first("quickstart", "main", "dl_dir", home_dirs["Public"].."/Downloads") + home_dirs["Caches"] = uci:get_first("quickstart", "main", "tmp_dir", home_dirs["main_dir"].."/frigate/Caches") + return home_dirs +end + +frigate.find_paths = function(blocks, home_dirs, path_name) + local default_path = '' + local configs = {} + + default_path = home_dirs[path_name] .. "/frigate" + if #blocks == 0 then + table.insert(configs, default_path) + else + for _, val in pairs(blocks) do + table.insert(configs, val .. "/" .. path_name .. "/frigate") + end + local without_conf_dir = "/root/" .. path_name .. "/frigate" + if default_path == without_conf_dir then + default_path = configs[1] + end + end + + return configs, default_path +end + +return frigate diff --git a/luasrc/view/frigate/status.htm b/luasrc/view/frigate/status.htm new file mode 100644 index 0000000..26740c0 --- /dev/null +++ b/luasrc/view/frigate/status.htm @@ -0,0 +1,31 @@ +<% +local util = require "luci.util" +local container_status = util.trim(util.exec("/usr/libexec/apps/frigate/frigate.sh status")) +local container_running = string.find(string.lower(container_status), "up") and string.find(string.lower(container_status), "(healthy)") +-%> + +
+ +
+ <% if container_running then %> + + <% else %> + + <% end %> +
+
+ +<% +if container_running then + local port=util.trim(util.exec("/usr/libexec/apps/frigate/frigate.sh port")) + if port == "" then + port="1880" + end +-%> +
+ +
+ +
+
+<% end %> diff --git a/root/etc/config/frigate b/root/etc/config/frigate new file mode 100644 index 0000000..4ed3026 --- /dev/null +++ b/root/etc/config/frigate @@ -0,0 +1,40 @@ +config frigate_config 'docker' + option port '5000' + option image 'ghcr.io/blakeblackshear/frigate:stable' + option usbcoral '/dev/bus/usb' + option hwaccel '/dev/crypto' + option storage './frigate/storage' + +config frigate_config 'mqtt' + option mqtt 'False' + option host 'mqtt.server.com + +config frigate_config 'detectors' + option coral 'True' + option type 'edgetpu' + option device 'usb' + +config camera_config 'cam1' + option name 'living_room' + option path 'rtsp://admin:password@192.168.70.173:554/cam/realmonitor?channel=1&subtype=0&authbasic=64' + option roles 'detect' + option record 'true' + option snapshots 'true' + 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 'cam2' + 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 'true' + option snapshots 'true' + 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 'cam3' + 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 'true' + option snapshots 'true' + option mask '0,461,3,0,1919,0,1919,843,1699,492,1344,458,1346,336,973,317,869,375,866,432' + diff --git a/root/etc/uci-defaults/luci-app-frigate b/root/etc/uci-defaults/luci-app-frigate new file mode 100644 index 0000000..8e6bea6 --- /dev/null +++ b/root/etc/uci-defaults/luci-app-frigate @@ -0,0 +1,14 @@ +#!/bin/sh + +config_dir=`uci -q get frigate.@frigate[0].config_path` + +data_dir=`docker inspect --format '{{.Mounts}}' frigate | grep -Eom1 '[^ ]+/_data /var/www/html local true ' | cut -d' ' -f1` + +if [ -n "$data_dir" -a "$data_dir" != "$config_dir" ]; then + uci -q batch <<-EOF >/dev/null + set frigate.@frigate[0].config_path="$data_dir" + commit frigate +EOF +fi + +exit 0 diff --git a/root/usr/libexec/apps/frigate/frigate.sh b/root/usr/libexec/apps/frigate/frigate.sh new file mode 100644 index 0000000..cd5f3c5 --- /dev/null +++ b/root/usr/libexec/apps/frigate/frigate.sh @@ -0,0 +1,192 @@ +#!/bin/sh + +APP_NAME="frigate" + +ACTION="${1}" +shift 1 + +get_image() { + do_install_detail +} + +do_install_detail() { + local config port IMAGE_NAME username password LAN_IP + local usbcoral hwaccel storage mqtt host coral type device + + config=$(uci get frigate.@frigate_config[0].config_path 2>/dev/null) + port=$(uci get frigate.@frigate_config[0].port 2>/dev/null) + IMAGE_NAME=$(uci get frigate.@frigate_config[0].image 2>/dev/null) + usbcoral=$(uci get frigate.@frigate_config[0].usbcoral 2>/dev/null) + hwaccel=$(uci get frigate.@frigate_config[0].hwaccel 2>/dev/null) + storage=$(uci get frigate.@frigate_config[0].storage 2>/dev/null) + + mqtt=$(uci get frigate.@frigate_config[0].mqtt 2>/dev/null) + host=$(uci get frigate.@frigate_config[0].host 2>/dev/null) + + coral=$(uci get frigate.@frigate_config[0].coral 2>/dev/null) + type=$(uci get frigate.@frigate_config[0].type 2>/dev/null) + device=$(uci get frigate.@frigate_config[0].device 2>/dev/null) + + username=$(uci get frigate.@frigate_config[0].username 2>/dev/null) + password=$(uci get frigate.@frigate_config[0].password 2>/dev/null) + APP_NAME="frigate" + + GEN_PASS=$(< /dev/urandom tr -dc A-Za-z0-9 2>/dev/null | head -c 14; echo) + GEN_PASS2=$(< /dev/urandom tr -dc A-Za-z0-9 2>/dev/null | head -c 14; echo) + + LAN_IP=$(uci get network.lan.ipaddr) + LAN_IP="${LAN_IP%/*}" + + if [ -z "$config" ]; then + echo "config path is empty!" + exit 1 + fi + + [ -z "$port" ] && port=1880 + [ -z "$IMAGE_NAME" ] && IMAGE_NAME="ghcr.io/blakeblackshear/frigate:stable" + + # Initialize counter + local i=0 +mkdir -p /opt/docker2/compose/frigate +touch /opt/docker2/compose/frigate/config.yml + + # Write global settings to config.yml using yq + # Replace these uci get commands with actual commands to get these variables + local usbcoral=$(uci get frigate.@frigate_config[0].usbcoral) + local hwaccel=$(uci get frigate.@frigate_config[0].hwaccel) + local storage=$(uci get frigate.@frigate_config[0].storage) + local mqtt=$(uci get frigate.@frigate_config[0].mqtt) + local host=$(uci get frigate.@frigate_config[0].host) + local coral=$(uci get frigate.@frigate_config[0].coral) + local type=$(uci get frigate.@frigate_config[0].type) + + yq eval ".usbcoral = \"$usbcoral\"" -i /opt/docker2/compose/frigate/config.yml + yq eval ".hwaccel = \"$hwaccel\"" -i /opt/docker2/compose/frigate/config.yml + yq eval ".storage = \"$storage\"" -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 + yq eval ".detectors.coral.device = \"$coral\"" -i /opt/docker2/compose/frigate/config.yml + + # Write each camera's configuration to config.yml + local camera_index=0 + while : ; do + # Try to fetch camera configuration + local name=$(uci get frigate.@camera_config[$camera_index].name 2>/dev/null) + # Exit loop if no more cameras are found + [[ -z "$name" ]] && break + + # Retrieve the rest of the camera settings here... + local path=$(uci get frigate.@camera_config[$camera_index].path) + local roles=$(uci get frigate.@camera_config[$camera_index].roles) + local record=$(uci get frigate.@camera_config[$camera_index].record) + local snapshots=$(uci get frigate.@camera_config[$camera_index].snapshots) + local mask=$(uci get frigate.@camera_config[$camera_index].mask) + + # Write to config.yml using yq + yq eval ".cameras.$name.ffmpeg.inputs[0].path = \"$path\"" -i /opt/docker2/compose/frigate/config.yml + yq eval ".cameras.$name.ffmpeg.inputs[0].roles[0] = \"detect\"" -i /opt/docker2/compose/frigate/config.yml + yq eval ".cameras.$name.detect.width = 1280" -i /opt/docker2/compose/frigate/config.yml + yq eval ".cameras.$name.detect.height = 720" -i /opt/docker2/compose/frigate/config.yml + yq eval ".cameras.$name.record.enabled = $record" -i /opt/docker2/compose/frigate/config.yml + yq eval ".cameras.$name.snapshots.enabled = $snapshots" -i /opt/docker2/compose/frigate/config.yml + yq eval ".cameras.$name.motion.mask[0] = \"$mask\"" -i /opt/docker2/compose/frigate/config.yml + + camera_index=$((camera_index+1)) + done +} + + + #rm -r /opt/docker2/compose/frigate 2>/dev/null + #mkdir -p /opt/docker2/compose/frigate + + #touch /opt/docker2/compose/frigate/config.yml + + #touch /opt/docker2/compose/frigate/docker-compose.yml + cat > /opt/docker2/compose/frigate/docker-compose.yml </dev/null) + [ -z "$IMAGE_NAME" ] && IMAGE_NAME="ghcr.io/blakeblackshear/frigate:stable" + CONTAINER_IDS=$(docker ps -a --filter "ancestor=${IMAGE_NAME}" --format '{{.ID}}') + echo "Stopping and removing containers..." + for ID in $CONTAINER_IDS; do + docker stop "$ID" + docker rm "$ID" + done + docker rmi -f "$IMAGE_NAME" + rm -r /opt/docker2/compose/frigate 2>/dev/null + ;; + "start" | "stop" | "restart") + APP_NAME="frigate" + CONTAINER_IDS=$(docker ps -a --filter "ancestor=${APP_NAME}" --format '{{.ID}}') + for ID in $CONTAINER_IDS; do + docker "${ACTION}" "${ID}" + done + ;; + "status") + APP_NAME="frigate" + CONTAINER_NAMES=$(docker ps -a --filter "name=${APP_NAME}" --format '{{.Names}}') + docker ps --all -f "name=${CONTAINER_NAMES}" --format '{{.Status}}' + ;; + "port") + APP_NAME="frigate" + CONTAINER_NAMES=$(docker ps -a --filter "ancestor=${APP_NAME}" --format '{{.Names}}') + docker ps --all -f "name=${CONTAINER_NAMES}" --format '{{.Ports}}' | grep -o -m1 '0.0.0.0:[0-9]*' | sed 's/0.0.0.0://' + ;; + *) + usage + exit 1 + ;; +esac \ No newline at end of file diff --git a/root/usr/share/rpcd/acl.d/luci-app-frigate.json b/root/usr/share/rpcd/acl.d/luci-app-frigate.json new file mode 100644 index 0000000..3b3ffae --- /dev/null +++ b/root/usr/share/rpcd/acl.d/luci-app-frigate.json @@ -0,0 +1,11 @@ +{ + "luci-app-frigate": { + "description": "Grant UCI access for luci-app-frigate", + "read": { + "uci": [ "frigate" ] + }, + "write": { + "uci": [ "frigate" ] + } + } +}