Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upload camera team items #3

Open
wants to merge 41 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
fe21cbf
Add files via upload
Nov 18, 2019
3b9b542
Update and rename 581-iot-script.sh to DVR-to-Pi.sh
Nov 18, 2019
f71e65b
Update DVR-to-Pi.sh
Nov 20, 2019
c94c8f4
Update DVR-to-Pi.sh
cdog97 Nov 20, 2019
753682d
Added Date items; needs restructuring
Nov 20, 2019
8a450ab
Added inotify timeout time
Nov 21, 2019
007b6ac
Updated inotify
Nov 21, 2019
1ec0644
Supports multiple cameras automagically
Nov 21, 2019
6e96712
Corrected variables; edited inotify to give the file which was created
Nov 21, 2019
e73615f
Update DVR-to-Pi.sh
Nov 28, 2019
08dcb6e
Code validation checked; not tested; need to work out when motion done
Nov 28, 2019
9da77cc
Logic checks out; ready to test
Nov 29, 2019
2ed5b1b
Comments added
Nov 29, 2019
a8c941d
Moved comments on their own lines as crontabs don't support this
Dec 9, 2019
eea836a
Update FTP details
Dec 10, 2019
e11d3f5
Fixed FTP
Dec 10, 2019
5110c5c
Added if statement to see if script is running and Date motion dir
Dec 10, 2019
039d072
Testing Wave1
Dec 11, 2019
41f8877
Bug fixing
Dec 11, 2019
a5c73bb
FTP working
Dec 11, 2019
cf68c01
FTP script for each FTP command
Dec 11, 2019
935cc0c
Reworked inotifywait
Dec 11, 2019
ae5c684
Fixed grep directories for cut
Dec 11, 2019
a010015
More debugging
Dec 11, 2019
610df0b
Renamed scripts, added Edge->Web script
Dec 11, 2019
5a1a30e
Fixed ps & updated SMS path
Dec 11, 2019
1f4e322
Avoided Edge
Dec 11, 2019
8d60081
Removed FTP function in Pi
Dec 11, 2019
3d6ec02
Added live backup
Dec 12, 2019
787524b
Fixed live backups regex
Dec 13, 2019
4b4a065
Fixed script's current working directory
Dec 13, 2019
65c0462
Fixed FTP to Edge directories
Dec 13, 2019
f26dece
Optimized and commented.
Dec 13, 2019
cbf16b7
Fixed loop error
Dec 13, 2019
f1f7521
Fixed more bugs
Dec 13, 2019
ab3acdb
More errors and syntax
Dec 13, 2019
0aa97fe
Converted all binary variables to true binary variables
Dec 13, 2019
3ee770d
Reverted changes to less optimized method; functioning now
Dec 19, 2019
a5222fd
Delete pseudo-dvr-code.txt
Dec 19, 2019
91a50a9
Created Camera Directory
Dec 19, 2019
85e7da4
Merge branch 'master' of https://github.com/Zamanry/2019Fall_CNIT581_IOT
Dec 19, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
187 changes: 187 additions & 0 deletions Camera/DVR-Motion-Detection.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
#!/bin/bash
#----------------Notes-----------------------------
# Supports additional cameras automatically
# Privileged access is permitted
# FTP only ever worked in its own script; not built-in to this one.
#----------------Installation-&-Running-Steps-on-DVR-----
# 1. SSH into DVR
# 2. sudo apt-get inotify-tools ftp git screen -y --force-yes
# 3. sudo vi ~/.netrc
# machine 172.16.2.12 login Frank password <password>
# 4. sudo chmod 600 ~/.netrc
# 5. git clone https://github.com/Zamanry/2019Fall_CNIT581_IOT.git
# 6. sudo chown root:root 2019Fall_CNIT581_IOT/DVR-Motion-Detection.sh
# 7. sudo chmod +x 2019Fall_CNIT581_IOT/*.sh
# 8. screen
# 9. ./2019Fall_CNIT581_IOT/DVR-Motion-Detection.sh
# 10. CTRL-A
# 11. D
#----------------motionRecording-Overall-Steps---------------------------
# 1. Detect a new JSON was created per each camera on current UTC date
# 2. Determine what event in JSON occured (motionRecording or fullTimeRecording)
# 3. SendNotification
# 4. Get startTime from JSON
# 5. While motionRecording is in progress, filter for related motion videos, FTP them, and rename them with *_FTPed.mp4. _FTPed is added to prevent files from being FTPed twice.
# 6. When motionRecording is done, get endTime from JSON
# 7. Filter for any final related motion videos, FTP them, and rename them with *_FTPed.mp4. _FTPed is added to prevent files from being FTPed twice.
# 8. Repeat
#----------------fullTimeRecording-Overall-Steps---------------------------
# 1. Detect a new JSON was created per each camera on current UTC date
# 2. Determine what event in JSON occured (motionRecording or fullTimeRecording)
# 4. Get startTime from JSON
# 4. Get endTime from JSON
# 7. Filter for any related videos, FTP them, and rename them with *_FTPed.mp4. _FTPed is added to prevent files from being FTPed twice.
# 8. Repeat
#----------------Static-Variables------------------------
T=true
# Run script forever
UniFiDir="/srv/unifi-video/videos"
# Where the videos are stored at
WebServer="172.16.1.2"
# Web server's IP address
WebNotificationsDir="/var/www/html/notifications"
# Directory on the web server where notifications will be FTPed to
WebMotionDir="/var/www/html/camera"
# Directory on the web server where motion videos will be FTPed to
WebLiveBackupDir="/var/www/html/LiveBackups"
# Directory on the web server where live video backups will be FTPed to
ScriptLocation="/root/2019Fall_CNIT581_IOT"
# The parent directory of the script
Date=""
# Leave blank; Establish $Date as a global variable
EventFound=false
# Leave blank; Establish $EventFound as a global variable
#----------------Functions------------------------------
FTPtoPi() {
echo "FTPing $1 to $WebServer:$2"
$ScriptLocation/FTP.sh "$WebServer" "$2" "$Date" "$1"
# FTP the video file to the web server
if [ "$2" = "$WebMotionDir" ]; then
# Only rename motion videos, not backup videos
EditedName=$(echo "${1/.mp4/_FTPed.mp4}")
# Add _FTPed.mp4 to the end of the FTPed motion video
mv "$1" "$EditedName"
# Rename it
fi
}
JSONCheck() {
JSON=$(inotifywait -t 15 -e create --exclude '\.(jpg|png)' --format '%f' "$1/meta/")
# Check for new JSON files and not JPG or PNG. Output the name of the create file. Will wait here till event is found.
if [ -n "$JSON" ]; then
# Run only if JSON is not null
echo "Event detected."
cd "$1" || exit
# Change directory to camera's UTC date
eventType=$(grep -Po "\{\"eventType\"\:\"(.*?)\"" "meta/$JSON" | sed 's/"$//' | sed -n 's/.*"//p')
# Obtain eventType (fullTimeRecording or motionRecording)
echo "eventType = $eventType."
Date=$(date +%m-%d-%Y-%H:%M)
# Get current timezone date in a nice format (ex. 11-20-2020-17:14)-
if [ "$eventType" = "motionRecording" ]; then
# If eventType is a motionRecording, then
echo "Motion JSON = $1/meta/$JSON."
SendNotification
# Since a new motionRecording was found, send a notification to web server
StartTime=$(grep -Po 'startTime":(.*?),' "meta/$JSON" | sed -n 's/.*://p' | sed 's/,$//')
# Obtain motionRecording's startTime from the JSON
echo "startTime = $StartTime."
echo "Making motionRecording directory at $WebServer:$WebMotionDir/$Date."
$ScriptLocation/FTP-Mkdir.sh "$WebServer" "$WebMotionDir" "$Date"
# Make a directory with the timestamp in the web server's motion directory
echo "Done."
while [ "$(grep -Po 'inProgress":(.*?),' "meta/$JSON" | sed -n 's/.*://p' | sed 's/,$//')" = true ]; do
# Detect if motion is still in progress according to the JSON
Videos=$(ls -p | grep -Ev "_FTPed|/|.txt")
# Grab all items that do not contain _FTPed, a .txt, or are a directory for Video in $Videos; do
if [[ $(echo "$Video" | cut -f1 -d '_') -ge "$StartTime" ]]; then
# Video name format is *_*_*_*.mp4.
# We take the first * and see if it is greater than or equal to the motionRecording's startTime.
FTPtoPi "$Video" "$WebMotionDir"
# If true (video is within the time of the motionRecording), FTP it
fi
done
echo "Motion is no longer in progress."
EndTime=$(grep -Po 'endTime":(.*?),' "meta/$JSON" | sed -n 's/.*://p' | sed 's/,$//')
# Obtain motionRecording's endTime from JSON
echo "endTime = $EndTime."
Videos=$(ls -p | grep -Ev "_FTPed|/|.txt")
# Grab all items that do not contain _FTPed, a .txt, or are a directory
for Video in $Videos; do
if [[ $(echo "$Video" | cut -f1 -d '_') -ge "$StartTime" ]] && [[ $(echo "$Video" | cut -f2 -d '_') -le "$EndTime" ]]; then
# Video name format is *_*_*_*.mp4.
# We take the first * and see if it is greater than or equal to the motionRecording's startTime.
# Then take the second * and see if it less than or equal to the motionRecording's endTime.
FTPtoPi "$Video" "$WebMotionDir"
# If true (video is within the time of the motionRecording), FTP it
fi
done
echo "Sent all motionRecordings."
elif [ "$eventType" = "fullTimeRecording" ]; then
# If eventType is a fullTimeRecording, then
echo "Live backup JSON = $1/meta/$JSON."
StartTime=$(grep -Po 'startTime":(.*?),' "meta/$JSON" | sed -n 's/.*://p' | sed 's/,$//')
# Obtain fullTimeRecording's startTime from the JSON
echo "startTime = $StartTime."
EndTime=$(grep -Po 'endTime":(.*?),' "meta/$JSON" | sed -n 's/.*://p' | sed 's/,$//')
# Obtain fullTimeRecording's endTime from JSON
echo "endTime = $EndTime."
echo "Making fullTimeRecording directory at $WebServer:$WebLiveBackupDir/$Date."
$ScriptLocation/FTP-Mkdir.sh "$WebServer" "$WebLiveBackupDir" "$Date"
# Make a directory with the timestamp in the web server's backups directory
Videos=$(ls -p | grep -Ev "/|.txt")
# Grab all items that are not a .txt or directory
for Video in $Videos; do
if [[ $(echo "$Video" | cut -f1 -d '_') -ge "$StartTime" ]] && [[ $(echo "$Video" | cut -f2 -d '_') -le "$EndTime" ]]; then
# Video name format is *_*_*_*.mp4.
# We take the first * and see if it is greater than or equal to the fullTimeRecording's startTime.
# Then take the second * and see if it less than or equal to the fullTimeRecording's endTime.
FTPtoPi "$Video" "$WebLiveBackupDir"
# If true (video is within the time of the fullTimeRecording), FTP it
fi
done
echo "Sent all fullTimeRecordings."
fi
EventFound=true
fi
}
SendNotification() {
echo "" >> "$Date.txt"
# Create notification message (ex. 11-20-2020-17:14.txt)
echo "Sending motionRecording notification to $WebServer:$WebNotificationsDir/$Date."
$ScriptLocation/DVR-Notify-FTP-Pi.sh "$WebServer" "$WebNotificationsDir" "$Date"
# FTP a notification of motion to the web server to trigger a notification to the farmer
echo "Done."
rm "$Date.txt"
# Remove the local notification
}

#----------------Main-----------------------------------
cd "$UniFiDir" || exit
# Change script's working directory to the camera directory
while "$T" = true; do
# Run forever
Year=$(date +%Y -u)
# Get UTC year (ex. 2019)
Month=$(date +%m -u)
# Get UTC month (ex. 11)
Day=$(date +%d -u)
# Get UTC day (ex. 27)
Cameras=$(ls "$UniFiDir" | grep '^d')
# Get each cameras' directory and assign it to $Cameras
ctr='0'
for Camera in $Cameras; do
CameraPath[$ctr]="$UniFiDir/$Camera/$Year/$Month/$Day"
# Add cameras' path to array $CameraPath
JSONCheck "${CameraPath[$ctr]}" &
# Check for newly created JSON files per each camera and run in the background
ctr=$((ctr + 1))
done
sleep 15
# while "$EventFound" = false; do
# # This loop was setup to prevent more than one inotifywait process running at a time. As soon as an event is found, inotifywait will FTP it and mark $EventFound as true.
# # This loop will see this and stop sleeping as to allow a new inotifywait process to begin.
# sleep 1
# done
# EventFound=false
# # Reset $EventFound variable
done
9 changes: 9 additions & 0 deletions Camera/DVR-Notify-FTP-Pi.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash
# 1 = EdgeServer
# 2 = EdgeNotificationsDir
# 3 = Date
ftp -4 "$1" <<EOD
cd "$2"
put "$3.txt"
bye
EOD
9 changes: 9 additions & 0 deletions Camera/FTP-Mkdir.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash
# 1 = Server
# 2 = MotionDir
# 3 = Date
ftp -4 "$1" <<EOD
cd "$2"
mkdir "$3"
bye
EOD
10 changes: 10 additions & 0 deletions Camera/FTP.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash
# 1 = Server
# 2 = MotionDir
# 3 = Date
# 4 = Video
ftp -4 "$1" <<EOD
cd "$2/$3"
put "$4
bye
EOD
10 changes: 10 additions & 0 deletions Camera/Pi-FTP-Web
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash
# 1 = WebServer
# 2 = WebMotionDir
# 3 = Date
# 4 = Video
ftp -4 "$1" <<EOD
cd "$2/$3"
put "$4
bye
EOD
79 changes: 79 additions & 0 deletions Camera/Pi-Motion-Detection.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#!/bin/bash

#----------------Random----------------------------------
# vi ~/.netrc
# machine 172.16.2.12 login Frank password <password>
# chmod 600 ~/.netrc

#----------------Installation-Steps----------------------
# 1. sudo apt-get inotify-tools ftp vsftpd git -y --force-yes
# 2. git clone https://github.com/Zamanry/2019Fall_CNIT581_IOT.git
# 3. crontab -e
# 4. SHELL=/bin/bash
# 5. [email protected]
# 6. PATH=/root
# 7. 0 * * * * /root/2019Fall_CNIT581_IOT/Pi-Motion-Detection.sh
# 8. chown root:root 2019Fall_CNIT581_IOT/Pi-Motion-Detection.sh
# 9. chmod +x 2019Fall_CNIT581_IOT/*.sh
# 10. mkdir /home/Frank/

#----------------Overall-Steps---------------------------
# 1. Detect a new JSON was created per each camera on current date
# 2. SendNotification
# 3. Get StartTime from JSON
# 4. While motion is in progress, filter for current motion videos, FTP them, and rename them with *_FTPed.mp4
# 5. When motion is done, get EndTime from JSON
# 6. Filter for current motion videos, FTP them, and rename them with *_FTPed.mp4
# 7. Restart process
#----------------Static-Variables------------------------
T=true
# Run script forever
LocalFTPLocationDir="/var/www/html"
# Parent directory of all FTP directories
LocalFTPBackupsDir="LiveBackups"
# Directory of fullTimeRecordings
LocalFTPMotionDir="camera"
# Directory of motionRecordings
LocalFTPNotificationDir="notifications"
# Directory of motionRecording notifications
ScriptLocationDir="/root/2019Fall_CNIT581_IOT"
# The parent directory of the script
SendSMSLocation="/root/send_sms.py"
# Location of send SMS Python script
Date=""
# Leave blank; Establish $Date as a global variable
EventFound=""
# Leave blank; Establish $EventFound as a global variable
NotificationCheck() {
NewNotification=$(inotifywait -t 15 -e create --format '%f' "$1")
# Check for new txt files. Output the name of the create file. Will wait here till event is found.
Date=$(echo "${NewNotification%%.*}")
# Get motionRecording's timestamp.
echo "Motion was detected at $Date."
echo "Sending SMS message..."
python3 "$SendSMSLocation"
echo "Done."
EventFound=true
}
#----------------Main-----------------------------------
cd "$LocalFTPLocationDir" || exit
# Change script's working directory to the parent directory of all FTP directories
while $T = true; do
NotificationCheck "$LocalFTPLocationDir/$LocalFTPNotificationDir" &
#MotionCheck "$LocalFTPLocation/$LocalFTPMotionDir" &
#while "$EventFound" = false; do
# This loop was setup to prevent more than one inotifywait process running at a time. As soon as an event is found, inotifywait will FTP it and mark $EventFound as true.
# This loop will see this and stop sleeping as to allow a new inotifywait process to begin.
sleep 15
if [ "$(find "$LocalFTPLocationDir/$LocalFTPBackupsDir" -mtime +3)" -ne "$null" ]; then
# Made to not constantly be deleting files. Only when time is older than three days.
echo "Deleting any camera archives older than three days."
find "$LocalFTPLocationDir/$LocalFTPMotionDir" -mtime +3 -exec rm -f {} \;
find "$LocalFTPLocationDir/$LocalFTPBackupsDir" -mtime +3 -exec rm -f {} \;
find "$LocalFTPLocationDir/$LocalFTPNotificationDir" -mtime +3 -exec rm -f {} \;
echo "Done."
fi
# done
# EventFound=false
# Reset $EventFound variable
done
27 changes: 0 additions & 27 deletions pseudo-dvr-code.txt

This file was deleted.