simple code refactoring

This commit is contained in:
pavel.muhortov 2023-03-11 22:20:57 +03:00
parent a241aa5977
commit d470736839
5 changed files with 1002 additions and 685 deletions

View File

@ -1,18 +1,26 @@
# cctv-scheduler
PTZ IP-Camera management
![cctv-scheduler](info/images/cctv-scheduler-0.1.png)
## `Installation`
### `Requirements`
Cameras settings:
>
> - Configuration -> System -> Security -> Authentication -> RTSP Authentication: digest/basic
> - Configuration -> System -> Security -> Authentication -> WEB Authentication: digest/basic
> - Configuration -> Network -> Advanced Settings -> Integration Protocol -> Enable Hikvision-CGI: Enabled
> - Configuration -> Network -> Advanced Settings -> Integration Protocol -> Hikvision-CGI Authentication: digest/basic
Look at the description of dependencies and install the necessary.
### `Downloading`
Download scripts and configs.
```bash
wget https://git.hmp.today/pavel.muhortov/cctv-scheduler/raw/branch/master/sequences.sh -O /home/user/cctv-scheduler/sequences.sh
wget https://git.hmp.today/pavel.muhortov/cctv-scheduler/raw/branch/master/sequences.conf -O /home/user/cctv-scheduler/sequences.conf
@ -23,66 +31,84 @@ wget https://git.hmp.today/pavel.muhortov/cctv-scheduler/raw/branch/master/publi
wget https://git.hmp.today/pavel.muhortov/cctv-scheduler/raw/branch/master/publisher-template-page-1007.xml -O /home/user/cctv-scheduler/publisher-template-page-1007.xml
wget https://git.hmp.today/pavel.muhortov/cctv-scheduler/raw/branch/master/streaming.py -O /home/user/cctv-scheduler/streaming.py
```
### `Configuration`
Edit configs.
```bash
nano /home/user/cctv-scheduler/sequences.conf
nano /home/user/cctv-scheduler/converter.conf
nano /home/user/cctv-scheduler/publisher.conf
```
### `Scheduler`
Look at examples and edit scheduler tasks:
```bash
crontab -e
```
____
* [`sequences.sh`](https://git.hmp.today/pavel.muhortov/cctv-scheduler#sequences-sh)
* [`converter.sh`](https://git.hmp.today/pavel.muhortov/cctv-scheduler#converter-sh)
* [`publisher.sh`](https://git.hmp.today/pavel.muhortov/cctv-scheduler#publisher-sh)
* [`streaming.py`](https://git.hmp.today/pavel.muhortov/cctv-scheduler#streaming-py)
____
- [`sequences.sh`](https://git.hmp.today/pavel.muhortov/cctv-scheduler#sequences-sh)
- [`converter.sh`](https://git.hmp.today/pavel.muhortov/cctv-scheduler#converter-sh)
- [`publisher.sh`](https://git.hmp.today/pavel.muhortov/cctv-scheduler#publisher-sh)
- [`streaming.py`](https://git.hmp.today/pavel.muhortov/cctv-scheduler#streaming-py)
____
## `sequences`.sh
**Description:**
> [Hikvision](https://git.hmp.today/pavel.muhortov/cctv-scheduler/src/branch/master/info/hikvision/manual/isapi.pdf) PTZ-camera sequences.
> Additionally:
>
> - getting temperature from DS18B20 over SSH,
> - saving pictures to FTP.
>
> This is only a local "proof of conept" for testing and debugging.
**Dependencies:**
>
> - [bash](https://www.gnu.org/software/bash/) (tested version 5.1.4 on [Debian GNU/Linux 11](http://ftp.debian.org/debian/dists/bullseye/))
> - [curl](https://curl.se/download.html) (tested version 7.74 on [Debian GNU/Linux 11](http://ftp.debian.org/debian/dists/bullseye/))
> - [sshpass](https://www.cyberciti.biz/faq/noninteractive-shell-script-ssh-password-provider/) (tested version 1.09 on [Debian GNU/Linux 11](http://ftp.debian.org/debian/dists/bullseye/))
| POSITION | PARAMETERS | DESCRIPTION | DEFAULT |
|-----------|--------------|------------------------|---------------|
| 1 | **[qn]** |execution without pauses||
| 2 | **[/path/to/conf]** |path to config| ./sequences.conf |
Example usage in terminal with bash:
```bash
bash ./sequences.sh - ./sequences.conf
```
Example usage with cron:
```bash
# crontab -e
0 * * * * bash /home/user/cctv-scheduler/sequences.sh qn
```
____
## `converter`.sh
**Description:**
> JPEG to MP4 converter.
>
> This is only a local "proof of conept" for testing and debugging.
**Dependencies:**
>
> - [bash](https://www.gnu.org/software/bash/) (tested version 5.1.4 on [Debian GNU/Linux 11](http://ftp.debian.org/debian/dists/bullseye/))
> - [ffmpeg](https://ffmpeg.org/download.html) (tested version 4.3.4 on [Debian GNU/Linux 11](http://ftp.debian.org/debian/dists/bullseye/))
> - filesystem organization:
>
>```bash
> # filesystem organisation example
>/root/
@ -109,12 +135,14 @@ ____
| 2 | **[/path/to/conf]** |path to config| ./converter.conf |
| 3 | **[-d\|-w\|-m\|-y]** |periods: '' - today \| '-d' - yesterday \| '-w' - last week \| '-m' - last month \| '-y' - last year||
Example usage in terminal with bash for today's MP4 making:
```bash
bash ./converter.sh - ./converter.conf
```
Example usage with cron:
```bash
# crontab -e
1 0 * * * bash /home/user/cctv-scheduler/converter.sh qn - -d
@ -122,17 +150,22 @@ Example usage with cron:
30 0 1 * * bash /home/user/cctv-scheduler/converter.sh qn - -m
36 0 1 1 * bash /home/user/cctv-scheduler/converter.sh qn - -y
```
____
## `publisher`.sh
**Description:**
> Uploading MP4 to [Wordpress](https://wordpress.com/) and [Telegram](https://web.telegram.org/).
> Additionally:
>
> - editing [Wordpress](https://codex.wordpress.org/XML-RPC_WordPress_API) page from template
> - recompressing video if size [over 50MB](https://core.telegram.org/bots/api#sendvideo)
>
> This is only a local "proof of conept" for testing and debugging.
**Dependencies:**
>
> - [bash](https://www.gnu.org/software/bash/) (tested version 5.1.4 on [Debian GNU/Linux 11](http://ftp.debian.org/debian/dists/bullseye/))
> - [curl](https://curl.se/download.html) (tested version 7.74 on [Debian GNU/Linux 11](http://ftp.debian.org/debian/dists/bullseye/))
> - [ffmpeg](https://ffmpeg.org/download.html) (tested version 4.3.4 on [Debian GNU/Linux 11](http://ftp.debian.org/debian/dists/bullseye/))
@ -148,12 +181,14 @@ ____
| 4 | **[1\|2\|3..XXX]** |multiplier for period: '' - 1 day\|week\|month\|year|1|
| 5 | **[--onlytg\|--onlywp]** |'--onlytg' - only publish to Telegram \|'--onlywp' - only publish to Wordpress||
Example usage in terminal with bash for publish to Telegram today's MP4:
Example usage in terminal with bash for publish today's MP4 maked:
```bash
bash ./publisher.sh - ./publisher.conf - --onlytg
bash ./publisher.sh - ./publisher.conf - - - - --onlytg
```
Example usage with cron:
```bash
# crontab -e
1 1 * * * bash /home/user/cctv-scheduler/publisher.sh qn - -d
@ -161,12 +196,16 @@ Example usage with cron:
30 1 1 * * bash /home/user/cctv-scheduler/publisher.sh qn - -m
36 1 1 1 * bash /home/user/cctv-scheduler/publisher.sh qn - -y
```
____
## `streaming`.py
**Description:**
> FFmpeg management from Python
**Dependencies:**
>
> - [Python 3](https://www.python.org/downloads/) (tested version 3.9.5 on [Debian GNU/Linux 11](http://ftp.debian.org/debian/dists/bullseye/))
> - [ffmpeg](https://ffmpeg.org/download.html) (tested version 4.3.4 on [Debian GNU/Linux 11](http://ftp.debian.org/debian/dists/bullseye/))
@ -183,17 +222,22 @@ ____
|**[--mono]**|detect ffmpeg running copy and terminate||
Example usage in terminal with make the script executable:
```bash
chmod u+x ./streaming.py
./streaming.py -s rtsp://user:pass@host:554/Streaming/Channels/101 --dst rtp://239.0.0.1:5554
```
Example usage with cron:
```bash
# crontab -e
* * * * * /usr/bin/python3 /home/user/cctv-scheduler/streaming.py -s rtsp://user:pass@host:554/Streaming/Channels/video,http://Streaming/Channels/audio --dst rtmp://a.rtmp.youtube.com/live2/YOUKEY --mono --watchdog --sec 30 >> /dev/null 2>&1
* * * * * /usr/bin/python3 /home/user/cctv-scheduler/streaming.py -s ~/media.mp4 --dst rtmp://b.rtmp.youtube.com/live2?backup=1/YOUKEY --mono >> /dev/null 2>&1
```
Example usage in Python:
```Python
from streaming import FFmpeg

View File

@ -1,165 +1,208 @@
#! /bin/bash
# DESCRIPTION:
# Converting JPEG collection to MP4.
# This is only a local "proof of conept" for testing and debugging.
#
## DESCRIPTION:
# JPEG to MP4 converter.
# This is only a local "proof of conept" for testing and debugging.
#
#
## DEPENDENCIES: sudo apt|yum install -y ffmpeg
#
#
## FUNCTIONS
# DEPENDENCIES:
# - ffmpeg
#
# PARAMETERS:
# 1: "qn" - execution without pauses
# 2: custom configuration file path
# 3: periods: '' - today | '-d' - yesterday | '-w' - last week | '-m' - last month | '-y' - last year
#
# FUNCTIONS:
#
#######################################
# Print message and add to log.
# Globals:
# logs
# Arguments:
# 1: message to print and logging
#######################################
addtologs() {
echo "$(date +'%Y.%m.%d-%H:%M:%S') $1" | tee -a $logs
echo "$(date +'%Y.%m.%d-%H:%M:%S') $1" | tee -a "${logs}"
}
#######################################
# Waiting for press [ENTER].
# Globals:
# None
# Arguments:
# None
#######################################
execpause() {
read -p "Press [ENTER] to continue... "
read -r -p "Press [ENTER] to continue... "
}
#######################################
# Exit procedure.
# Globals:
# show
# Arguments:
# None
#######################################
execquite() {
addtologs "execution time is $(($(date +%s)-$time)) seconds, exit"
if [ "${show}" != "qn" ];then
execpause
fi
exit
addtologs "execution time is $(($(date +%s)-time)) seconds, exit"
if [ "${show}" != "qn" ]; then
execpause
fi
exit
}
#######################################
# Error exit procedure with Telegram notification.
# Globals:
# telegramapiurl
# telegramchatid
# Arguments:
# 1: message to print and logging
#######################################
execerror() {
addtologs "error: $1"
curl -s -X POST $telegramapiurl/sendMessage -d chat_id=$telegramchatid -d text="$(basename -s .sh $0) error: $1" >> /dev/null 2>&1
execquite
addtologs "error: $1"
curl -s -X POST "${telegramapiurl}/sendMessage" \
-d "chat_id=${telegramchatid}" \
-d "text=$(basename -s .sh "$0") error: $1" \
>> /dev/null 2>&1
execquite
}
#######################################
# Parsing config file and creating global vars.
# Globals:
# None
# Arguments:
# None
#######################################
getconfig() {
logs=$(cat $conf | grep "logs=" | cut -d= -f2)
list=$(cat $conf | grep "list=" | cut -d= -f2)
imgroot=$(cat $conf | grep "imgroot=" | cut -d= -f2)
imgnames=($(cat $conf | grep "imgnames=" | cut -d= -f2))
xscale=$(cat $conf | grep "xscale=" | cut -d= -f2)
yscale=$(cat $conf | grep "yscale=" | cut -d= -f2)
mp4fps=$(cat $conf | grep "mp4fps=" | cut -d= -f2)
telegramapiurl=$(cat $conf | grep "telegramapiurl=" | cut -d= -f2)
telegramchatid=$(cat $conf | grep "telegramchatid=" | cut -d= -f2)
logs=$(grep "logs=" "${conf}" | cut -d= -f2)
list=$(grep "list=" "${conf}" | cut -d= -f2)
imgroot=$(grep "imgroot=" "${conf}" | cut -d= -f2)
IFS=" " read -r -a imgnames <<< "$(grep "imgnames=" "${conf}" | cut -d= -f2)"
xscale=$(grep "xscale=" "${conf}" | cut -d= -f2)
yscale=$(grep "yscale=" "${conf}" | cut -d= -f2)
mp4fps=$(grep "mp4fps=" "${conf}" | cut -d= -f2)
telegramapiurl=$(grep "telegramapiurl=" "${conf}" | cut -d= -f2)
telegramchatid=$(grep "telegramchatid=" "${conf}" | cut -d= -f2)
}
#
## PARAMETERS
## VARIABLES:
#
show=${1}
conf=${2}
if [ -z "${conf}" ] || [ "${conf}" == "-" ];then
conf="$(dirname $(realpath "$0"))/$(basename -s .sh $0).conf"
show=$1
conf=$2
if [ -z "${conf}" ] || [ "${conf}" == "-" ]; then
conf="$(dirname "$(realpath "$0")")/$(basename -s .sh "$0").conf"
fi
when=${3}
if [ -z "$when" ]; then
d=$(date +"%d")
w=$(date +"%V")
m=$(date +"%m")
y=$(date +"%Y")
duration=1
imgpath=$y/$m/$w/$d
imgname=$y.$m.$d
when=$3
if [ -z "${when}" ]; then
d=$(date +"%d")
w=$(date +"%V")
m=$(date +"%m")
y=$(date +"%Y")
duration=1
imgpath="${y}/${m}/${w}/${d}"
imgname="${y}.${m}.${d}"
fi
if [ "$when" == "-d" ]; then
d=$(date -d "-1 day" +"%d")
m=$(date +"%m")
if [ "$(date -d '-1 day' +'%m')" != "$(date +'%m')" ]; then
m=$(date -d '-1 day' +'%m')
fi
y=$(date +"%Y")
if [ "$(date -d '-1 day' +'%Y')" != "$(date +'%Y')" ]; then
y=$(date -d '-1 day' +'%Y')
fi
w=$(date +"%V")
if [ "$(date +'%w')" == "1" ]; then
w=$(date -d "-1 week" +"%V")
fi
duration=1
imgpath=$y/$m/$w/$d
imgname=$y.$m.$d
if [ "${when}" == "-d" ]; then
d=$(date -d "-1 day" +"%d")
m=$(date +"%m")
if [ "$(date -d '-1 day' +'%m')" != "$(date +'%m')" ]; then
m=$(date -d '-1 day' +'%m')
fi
y=$(date +"%Y")
if [ "$(date -d '-1 day' +'%Y')" != "$(date +'%Y')" ]; then
y=$(date -d '-1 day' +'%Y')
fi
w=$(date +"%V")
if [ "$(date +'%w')" == "1" ]; then
w=$(date -d "-1 week" +"%V")
fi
duration=1
imgpath="${y}/${m}/${w}/${d}"
imgname="${y}.${m}.${d}"
fi
if [ "$when" == "-w" ]; then
w=$(date -d "-1 week" +"%V")
m=$(date +"%m")
if [ "$(date -d '-1 week' +'%m')" != "$(date +'%m')" ]; then
m=$(date -d '-1 week' +'%m')
fi
y=$(date +"%Y")
if [ "$(date -d '-1 week' +'%Y')" != "$(date +'%Y')" ]; then
y=$(date -d '-1 week' +'%Y')
fi
duration=7
imgpath=$y/*/$w
imgname=$y-w$w
if [ "${when}" == "-w" ]; then
w=$(date -d "-1 week" +"%V")
m=$(date +"%m")
if [ "$(date -d '-1 week' +'%m')" != "$(date +'%m')" ]; then
m=$(date -d '-1 week' +'%m')
fi
y=$(date +"%Y")
if [ "$(date -d '-1 week' +'%Y')" != "$(date +'%Y')" ]; then
y=$(date -d '-1 week' +'%Y')
fi
duration=7
imgpath="${y}/*/${w}"
imgname="${y}-w${w}"
fi
if [ "$when" == "-m" ]; then
m=$(date -d "-1 month" +"%m")
y=$(date +"%Y")
if [ "$(date -d '-1 month' +'%Y')" != "$(date +'%Y')" ]; then
y=$(date -d '-1 month' +'%Y')
fi
duration=30
imgpath=$y/$m
imgname=$y.$m
if [ "${when}" == "-m" ]; then
m=$(date -d "-1 month" +"%m")
y=$(date +"%Y")
if [ "$(date -d '-1 month' +'%Y')" != "$(date +'%Y')" ]; then
y=$(date -d '-1 month' +'%Y')
fi
duration=30
imgpath="${y}/${m}"
imgname="${y}.${m}"
fi
if [ "$when" == "-y" ]; then
y=$(date -d "-1 year" +"%Y")
duration=360
imgpath=$y
imgname=$y
if [ "${when}" == "-y" ]; then
y=$(date -d "-1 year" +"%Y")
duration=360
imgpath="${y}"
imgname="${y}"
fi
#
## VARIABLES
#
time=$(date +%s)
cd "$(dirname "$(realpath "$0")")"
if [ ! -e ${conf} ];then
execerror "Not found config file: ${conf}"
cd "$(dirname "$(realpath "$0")")" || execerror
if [ ! -e "${conf}" ]; then
execerror "Not found config file: ${conf}"
else
getconfig
getconfig
fi
if [ -z "${logs}" ];then
logs=/dev/null
elif [ ! -e ${logs} ];then
touch ${logs}
logs=/dev/null
elif [ ! -e "${logs}" ];then
touch "${logs}"
fi
if [ -z "${list}" ];then
list="$(dirname $(realpath "$0"))/$(basename -s .sh $0).list"
list="$(dirname "$(realpath "$0")")/$(basename -s .sh "$0").list"
fi
if [ ! -e ${list} ];then
touch ${list}
if [ ! -e "${list}" ];then
touch "${list}"
fi
if ! command -v ffmpeg &> /dev/null; then
execerror "Not found dependencies"
execerror "Not found dependencies"
fi
#
## RUN
# MAIN:
#
for name in ${imgnames[*]}; do
imgmatch="*$name*.jpeg"
imgarray=()
while read FILE; do
imgarray+=("${FILE}")
done < <(find $imgroot/$imgpath -name "$imgmatch" | sort)
imgcount=${#imgarray[*]}
for name in "${imgnames[@]}"; do
imgmatch="*${name}*.jpeg"
imgarray=()
while read -r FILE; do
imgarray+=("${FILE}")
done < <(find "${imgroot}/${imgpath}" -name "${imgmatch}" | sort)
imgcount=${#imgarray[*]}
echo '' > $list
for item in ${imgarray[*]}; do
echo file \'$item\' >> $list
done
echo '' > "${list}"
for item in "${imgarray[@]}"; do
echo file \'"${item}"\' >> "${list}"
done
imgdest=$imgroot/$name\_$imgname.mp4
echo $imgdest
#ffmpeg -r $imgcount/$duration -f concat -safe 0 -i $list -f lavfi -i anullsrc=channel_layout=stereo:sample_rate=44100 -t $duration -c:v libx264 -vf "scale=$xscale:$yscale,fps=$mp4fps,format=yuv420p" -c:a aac $imgdest -y
ffmpeg -r $imgcount/$duration -f concat -safe 0 -i $list -c:v libx264 -vf "scale=$xscale:$yscale,fps=$mp4fps,format=yuv420p" $imgdest -y \
&& addtologs "converted $imgcount images to $imgdest with duration $duration" \
|| execerror "converted $imgcount images to $imgdest with duration $duration"
imgdest="${imgroot}/${name}_${imgname}.mp4"
echo "${imgdest}"
if ffmpeg -r "${imgcount}/${duration}" -f concat -safe 0 -i "${list}" \
-c:v libx264 -vf "scale=${xscale}:${yscale},fps=${mp4fps},format=yuv420p" \
"${imgdest}" -y; then
addtologs "converted ${imgcount} images to ${imgdest} with duration ${duration}"
else
execerror "converted ${imgcount} images to ${imgdest} with duration ${duration}"
fi
done
execquite

File diff suppressed because it is too large Load Diff

View File

@ -9,6 +9,7 @@ ftpuser=user
ftppass=pass
ftpaddr=192.168.254.254
ftpport=21
ftppath=Records/cam.test.local
tmpuser=user
tmppass=pass

View File

@ -1,137 +1,291 @@
#! /bin/bash
# DESCRIPTION:
# Handling Hikvision PTZ-cameras sequences.
# Additionally:
# - get ds18b20 sensor value over SSH,
# - saving pictures to FTP.
# This is only a local "proof of conept" for testing and debugging.
#
## DESCRIPTION:
# Hikvision PTZ-camera sequences.
# Additionally:
# - getting temperature from ds-18b20 over SSH,
# - saving pictures to FTP.
# This is only a local "proof of conept" for testing and debugging.
#
#
## DEPENDENCIES: sudo apt|yum install -y curl sshpass
#
#
## FUNCTIONS
# DEPENDENCIES:
# - curl
# - sshpass
#
# PARAMETERS:
# 1: "qn" - execution without pauses
# 2: custom configuration file path
#
# FUNCTIONS:
#
#######################################
# Print message and add to log.
# Globals:
# logs
# Arguments:
# 1: message to print and logging
#######################################
addtologs() {
echo "$(date +'%Y.%m.%d-%H:%M:%S') $1" | tee -a $logs
echo "$(date +'%Y.%m.%d-%H:%M:%S') $1" | tee -a "${logs}"
}
#######################################
# Waiting for press [ENTER].
# Globals:
# None
# Arguments:
# None
#######################################
execpause() {
read -p "Press [ENTER] to continue... "
read -r -p "Press [ENTER] to continue... "
}
#######################################
# Exit procedure.
# Globals:
# show
# Arguments:
# None
#######################################
execquite() {
addtologs "execution time is $(($(date +%s)-$time)) seconds, exit"
if [ "${show}" != "qn" ];then
execpause
fi
exit
addtologs "execution time is $(($(date +%s)-time)) seconds, exit"
if [ "${show}" != "qn" ]; then
execpause
fi
exit
}
#######################################
# Error exit procedure with Telegram notification.
# Globals:
# telegramapiurl
# telegramchatid
# Arguments:
# 1: message to print and logging
#######################################
execerror() {
addtologs "error: $1"
curl -s -X POST $telegramapiurl/sendMessage -d chat_id=$telegramchatid -d text="$(basename -s .sh $0) error: $1" >> /dev/null 2>&1
execquite
addtologs "error: $1"
curl -s -X POST "${telegramapiurl}/sendMessage" \
-d "chat_id=${telegramchatid}" \
-d "text=$(basename -s .sh "$0") error: $1" \
>> /dev/null 2>&1
execquite
}
#######################################
# Parsing config file and creating global vars.
# Globals:
# None
# Arguments:
# None
#######################################
getconfig() {
logs=$(cat $conf | grep "logs=" | cut -d= -f2)
camuser=$(cat $conf | grep "camuser=" | cut -d= -f2)
campass=$(cat $conf | grep "campass=" | cut -d= -f2)
camaddr=$(cat $conf | grep "camaddr=" | cut -d= -f2)
camport=$(cat $conf | grep "camport=" | cut -d= -f2)
ftpuser=$(cat $conf | grep "ftpuser=" | cut -d= -f2)
ftppass=$(cat $conf | grep "ftppass=" | cut -d= -f2)
ftpaddr=$(cat $conf | grep "ftpaddr=" | cut -d= -f2)
ftpport=$(cat $conf | grep "ftpport=" | cut -d= -f2)
tmpuser=$(cat $conf | grep "tmpuser=" | cut -d= -f2)
tmppass=$(cat $conf | grep "tmppass=" | cut -d= -f2)
tmpaddr=$(cat $conf | grep "tmpaddr=" | cut -d= -f2)
tmpport=$(cat $conf | grep "tmpport=" | cut -d= -f2)
tmpnode=$(cat $conf | grep "tmpnode=" | cut -d= -f2)
telegramapiurl=$(cat $conf | grep "telegramapiurl=" | cut -d= -f2)
telegramchatid=$(cat $conf | grep "telegramchatid=" | cut -d= -f2)
logs=$(grep "logs=" "${conf}" | cut -d= -f2)
camuser=$(grep "camuser=" "${conf}" | cut -d= -f2)
campass=$(grep "campass=" "${conf}" | cut -d= -f2)
camaddr=$(grep "camaddr=" "${conf}" | cut -d= -f2)
camport=$(grep "camport=" "${conf}" | cut -d= -f2)
ftpuser=$(grep "ftpuser=" "${conf}" | cut -d= -f2)
ftppass=$(grep "ftppass=" "${conf}" | cut -d= -f2)
ftpaddr=$(grep "ftpaddr=" "${conf}" | cut -d= -f2)
ftpport=$(grep "ftpport=" "${conf}" | cut -d= -f2)
ftppath=$(grep "ftppath=" "${conf}" | cut -d= -f2)
tmpuser=$(grep "tmpuser=" "${conf}" | cut -d= -f2)
tmppass=$(grep "tmppass=" "${conf}" | cut -d= -f2)
tmpaddr=$(grep "tmpaddr=" "${conf}" | cut -d= -f2)
tmpport=$(grep "tmpport=" "${conf}" | cut -d= -f2)
tmpnode=$(grep "tmpnode=" "${conf}" | cut -d= -f2)
telegramapiurl=$(grep "telegramapiurl=" "${conf}" | cut -d= -f2)
telegramchatid=$(grep "telegramchatid=" "${conf}" | cut -d= -f2)
}
#######################################
# Get ds18b20 sensor value over SSH.
# Globals:
# tmpaddr
# tmpport
# tmpuser
# tmppass
# tmpnode
# Arguments:
# None
# return:
# temperature in Celsius
#######################################
getairtmp() {
local temp=$( \
sshpass -p $tmppass ssh $tmpuser@$tmpaddr -p $tmpport -o StrictHostKeyChecking=no -o ConnectTimeout=10 \
"cat /sys/bus/w1/devices/$tmpnode/temperature" \
) && temp=$(($temp / 1000))
if [ ! -z "${temp}" ]; then
addtologs "air temperature now is $temp'C" > /dev/null
echo $temp
else
execerror "air temperature now is $temp'C"
fi
temp=$(sshpass -p "${tmppass}" \
ssh "${tmpuser}@${tmpaddr}" -p "${tmpport}" \
-o StrictHostKeyChecking=no -o ConnectTimeout=10 \
"cat /sys/bus/w1/devices/${tmpnode}/temperature" \
) && temp=$((temp / 1000))
if [ -n "${temp}" ]; then
addtologs "getairtmp: air temperature now is ${temp}'C" > /dev/null
echo "${temp}"
else
execerror "getairtmp: air temperature now is ${temp}'C"
fi
}
#######################################
# Get static picture from camera and upload to FTP.
# Globals:
# camaddr
# camport
# camuser
# campass
# ftpaddr
# ftpport
# ftppath
# ftpuser
# ftppass
# Arguments:
# 1: filename prefix
#######################################
getcamimg() {
local name=$1
local file="$ftpaddr:$ftpport/Records/cam-street.home.local/$(date +"%Y")/$(date +"%m")/$(date +"%V")/$(date +"%d")/${name}_$(date +"%Y.%m.%d_%H-%M-%S").jpeg"
url='/Streaming/channels/101/picture?snapShotImageType=JPEG&videoResolutionWidth=1920&videoResolutionHeight=1080'
curl -vs "http://$camuser:$campass@$camaddr:$camport/$url" -o tmp \
&& curl -T tmp "ftp://$ftpuser:$ftppass@$file" --ftp-create-dirs \
&& rm tmp && addtologs "save photo to ftp://$file" || execerror "save photo to ftp://$file"
local name
name=$1
path="${ftppath}/$(date +"%Y")/$(date +"%m")/$(date +"%V")/$(date +"%d")"
file="${ftpaddr}:${ftpport}/${path}/${name}_$(date +"%Y.%m.%d_%H-%M-%S").jpeg"
url='Streaming/channels/101/picture?snapShotImageType=JPEG&videoResolutionWidth=1920&videoResolutionHeight=1080'
if curl -vs "http://$camuser:$campass@$camaddr:$camport/$url" -o tmp \
&& curl -T tmp "ftp://$ftpuser:$ftppass@$file" --ftp-create-dirs; then
addtologs "getcamimg: save photo to ftp://${file}"
rm tmp
else
execerror "getcamimg: save photo to ftp://${file}"
fi
}
#######################################
# Set message as video overlay text.
# Globals:
# camaddr
# camport
# camuser
# campass
# Arguments:
# 1: overlay text content
#######################################
setcamtxt() {
local message=$1
url='/Video/inputs/channels/101/overlays/text'
xml='<TextOverlayList><TextOverlay><id>1</id><enabled>true</enabled><posX>0</posX><posY>0</posY><message>'"${message}"'</message></TextOverlay></TextOverlayList>'
curl -d "$xml" -X PUT "http://$camuser:$campass@$camaddr:$camport/$url" && addtologs "text on video is $message" && sleep 5 || execerror "text on video is $message"
local message=$1
url='ISAPI/System/Video/inputs/channels/101/overlays/text'
xml='<TextOverlayList>
<TextOverlay>
<id>1</id>
<enabled>true</enabled>
<posX>0</posX><posY>0</posY>
<displayText>'"${message}"'</displayText>
</TextOverlay>
</TextOverlayList>'
if curl -d "${xml}" -X PUT "http://$camuser:$campass@$camaddr:$camport/$url" \
| grep -q '<statusString>OK</statusString>'; then
addtologs "setcamtxt: ${message}"
sleep 5
else
execerror "setcamtxt: ${message}"
fi
}
#######################################
# Set camera moving to absolute position.
# Globals:
# camaddr
# camport
# camuser
# campass
# Arguments:
# 1: horisontal camera position from 0 to 3600
# 2: vertical camera position from -900 to 2700
# 3: zoom camera position from 0 to 1000
# 4: time to wait
# 5: message to print and logging
#######################################
setcampos() {
local x=$1
local y=$2
local z=$3
local t=$4
local message=$5
url='/ISAPI/PTZCtrl/channels/101/absolute'
xml='<PTZData><AbsoluteHigh><elevation>'"${y}"'</elevation><azimuth>'"${x}"'</azimuth><absoluteZoom>'"${z}"'</absoluteZoom></AbsoluteHigh></PTZData>'
curl -d "$xml" -X PUT "http://$camuser:$campass@$camaddr:$camport/$url" && addtologs "$message" && sleep $t || execerror "$message"
local x=$1
local y=$2
local z=$3
local t=$4
local message=$5
url='ISAPI/PTZCtrl/channels/101/absolute'
xml='<PTZData>
<AbsoluteHigh>
<elevation>'"${y}"'</elevation>
<azimuth>'"${x}"'</azimuth>
<absoluteZoom>'"${z}"'</absoluteZoom>
</AbsoluteHigh>
</PTZData>'
if curl -d "${xml}" -X PUT "http://$camuser:$campass@$camaddr:$camport/$url" \
| grep -q '<statusString>OK</statusString>'; then
addtologs "setcampos: ${message}"
sleep "${t}"
else
execerror "setcampos: ${message}"
fi
}
#######################################
# Set camera moving to direction.
# Globals:
# camaddr
# camport
# camuser
# campass
# Arguments:
# 1: acceleration of horizontal camera movement from -100 to 100
# 2: acceleration of vertical camera movement from -100 to 100
# 3: acceleration of zoom camera movement from -100 to 100
# 4: time to wait
# 5: message to print and logging
#######################################
setcammov() {
local x=$1
local y=$2
local z=$3
local t=$4
local message=$5
url='/ISAPI/PTZCtrl/channels/101/continuous'
xml='<PTZData><pan>'"${x}"'</pan><tilt>'"${y}"'</tilt><zoom>'"${z}"'</zoom></PTZData>'
curl -d "$xml" -X PUT "http://$camuser:$campass@$camaddr:$camport/$url" && addtologs "$message" && sleep $t || execerror "$message"
local x=$1
local y=$2
local z=$3
local t=$4
local message=$5
url='ISAPI/PTZCtrl/channels/101/continuous'
xml='<PTZData>
<pan>'"${x}"'</pan>
<tilt>'"${y}"'</tilt>
<zoom>'"${z}"'</zoom>
</PTZData>'
if curl -d "${xml}" -X PUT "http://$camuser:$campass@$camaddr:$camport/$url" \
| grep -q '<statusString>OK</statusString>'; then
addtologs "setcammov: ${message}"
sleep "${t}"
else
execerror "setcammov: ${message}"
fi
}
#
## PARAMETERS
## VARIABLES:
#
show=${1}
conf=${2}
if [ -z "${conf}" ] || [ "${conf}" == "-" ];then
conf="$(dirname $(realpath "$0"))/$(basename -s .sh $0).conf"
show=$1
conf=$2
if [ -z "${conf}" ] || [ "${conf}" == "-" ]; then
conf="$(dirname "$(realpath "$0")")/$(basename -s .sh "$0").conf"
fi
#
## VARIABLES
#
time=$(date +%s)
cd "$(dirname "$(realpath "$0")")"
if [ ! -e ${conf} ];then
execerror "Not found config file: ${conf}"
cd "$(dirname "$(realpath "$0")")" || execerror
if [ ! -e "${conf}" ]; then
execerror "Not found config file: ${conf}"
else
getconfig
getconfig
fi
if [ -z "${logs}" ];then
logs=/dev/null
elif [ ! -e ${logs} ];then
touch ${logs}
logs=/dev/null
elif [ ! -e "${logs}" ];then
touch "${logs}"
fi
if ! command -v curl &> /dev/null || ! command -v sshpass &> /dev/null; then
execerror "Not found dependencies"
execerror "Not found dependencies"
fi
#
## RUN
# MAIN:
#
setcamtxt "$(getairtmp)'C"
@ -162,12 +316,13 @@ sleep 4m
setcammov 0 -44 33 1.50s '08 >> 09 Точка - Стадион 8x'
setcampos 2860 -170 80 5.00s '08 >| 09 Точка - Стадион 8x'
sleep 1m
setcammov 0 -99 0 7.50s '09 >> 10 Точка - Въезд во двор 8x'
setcampos 2805 200 80 5.00s '09 >| 10 Точка - Въезд во двор 8x'
sleep 5m
setcammov -88 99 0 5.00s '10 >> 11 Точка - Бассейн 8x'
setcammov -99 88 0 2.00s '10 >> 11 Точка - Бассейн 8x'
setcammov -44 99 0 4.25s '10 >> 11 Точка - Бассейн 8x'
setcampos 2510 -100 80 5.00s '10 >| 11 Точка - Бассейн 8x'
setcampos 2510 -20 80 5.00s '10 >| 11 Точка - Бассейн 8x'
sleep 1m
setcammov -88 99 33 2.50s '11 >> 12 Точка - Общежитие дальнее 16x'
setcampos 2415 -200 160 5.00s '11 >| 12 Точка - Общежитие дальнее 16x'
@ -176,7 +331,7 @@ setcammov 66 0 0 22.5s '12 >> 13 Точка - Общ
setcampos 2640 -200 160 5.00s '12 >| 13 Точка - Общежите ближнее 16x'
sleep 1m
setcammov 66 0 0 90.5s '13 >> 14 Точка - Крыша магазина 4x'
setcammov 99 0 -33 3.25s '13 >> 14 Точка - Крыша магазина 4x'
setcammov 99 0 -44 3.25s '13 >> 14 Точка - Крыша магазина 4x'
setcampos 335 -200 40 5.00s '13 >| 14 Точка - Крыша магазина 4x'
sleep 1m
setcamtxt '' && getcamimg 'point-11' && setcamtxt "$(getairtmp)'C"