zm_auto: uno script bash per automatizzare completamente Zoneminder

Questo è uno script bash con diverse funzioni per automatizzare completamente Zoneminder ed è anche presente un servizio da installare per avviare automaticamente questo script all'avvio.

Con questo script è possibile:

  • cambiare automaticamente gli stati Giorno e Notte in base all'ora
  • impostare automaticamente lo stato Armato se un dispositivo bluetooth non è più individuato
  • muovere automaticamente le telecamere ptz in due differenti preset quando si cambia stato
  • riavviare automaticamente Zoneminder se va nello stato Inattivo

Di seguito il codice di zm_auto.sh:

#!/bin/bash
# License: GPL
#
# Author: Mentor <[email protected]>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
zmdbuser=`cat /etc/zm/zm.conf | grep ZM_DB_USER | awk -F= '{print $2}' | tr -d ' '`
zmdbpass=`cat /etc/zm/zm.conf | grep ZM_DB_PASS | awk -F= '{print $2}' | tr -d ' '`

#echo $runstate # for debug only

armed_state='Armed'
day_state='Day'
night_state='Night'

calibrate='Calibrate'
reset='Reboot'

hour_day_start='80000'
hour_day_end='220000'

bt_mac=()

monitor_id=()
monitor_ptz=()
armed_preset=''
disarmed_preset=''

automatic_interval_check='10s'

if ! ([ ! -z "$armed_state" ] && [ ! -z `mysql -N -B -u$zmdbuser -p$zmdbpass zm  -e "select Name from States where Name Like '$armed_state' LIMIT 1;"` ] && [ ! -z "$day_state" ]  && [ ! -z `mysql -N -B -u$zmdbuser -p$zmdbpass zm  -e "select Name from States where Name Like '$day_state' LIMIT 1;"` ] && [ ! -z "$night_state" ] && [ ! -z `mysql -N -B -u$zmdbuser -p$zmdbpass zm  -e "select Name from States where Name Like '$night_state' LIMIT 1;"` ]); then
	exit
fi

function zm_status {

if ! [ -f /run/zm/zm.pid ]; then
        /bin/systemctl start zoneminder
fi

}

function auto_zm_status {

        if [ ! -z "$automatic_interval_check" ]; then
		while true; do
			zm_status
			sleep $automatic_interval_check
		done
	fi
}

function state {
	
	runstate=`mysql -N -B -u$zmdbuser -p$zmdbpass zm -e "select Name  from States where isActive=1 LIMIT 1;"`

	if [ "$runstate" != "$armed_state" ]; then
		/usr/bin/zmpkg.pl ${1}
	fi

}

function check_state {

	runstate=`mysql -N -B -u$zmdbuser -p$zmdbpass zm -e "select Name  from States where isActive=1 LIMIT 1;"`
	hour=$(date  +"%H%M%S" | bc)
	
	if [ "$runstate" != "$armed_state" ]; then
		if [ $hour -lt  "$hour_day_end" ] && [ $hour -gt "$hour_day_start" ]; then
			if [ "$runstate" != "$day_state" ]; then
				/usr/bin/zmpkg.pl $day_state
			fi
		else
			if [ "$runstate" != "$night_state" ]; then
				/usr/bin/zmpkg.pl $night_state
			fi
		fi
	fi

}

function auto_check_state {

	if [ ! -z "$automatic_interval_check" ]; then
		while true; do
			zm_status
			check_state
			sleep $automatic_interval_check
		done
	fi

}

function check_state_bt {

	if ! [ ${#bt_mac[@]} -eq 0 ]; then
		runstate=`mysql -N -B -u$zmdbuser -p$zmdbpass zm -e "select Name  from States where isActive=1 LIMIT 1;"`
		hour=$(date  +"%H%M%S" | bc)
		bt_devices=()
		for i in ${bt_mac[@]}; do
			bt_devices+=($(hcitool name $i))
		done
		#echo ${bt_devices[*]} # for debug only
		if [ ${#bt_devices[@]} -eq 0 ]; then
			if [ "$runstate" != "$armed_state" ]; then
				/usr/bin/zmpkg.pl $armed_state
			fi
		else
			if [ $hour -lt  "$hour_day_end" ] && [ $hour -gt "$hour_day_start" ]; then
				if [ "$runstate" != "$day_state" ]; then
					/usr/bin/zmpkg.pl $day_state
				fi
			else
				if [ "$runstate" != "$night_state" ]; then
					/usr/bin/zmpkg.pl $night_state
				fi
			fi
		fi
	fi

}

function auto_check_state_bt {

	if [ ! -z "$automatic_interval_check" ]; then
		while true; do
			zm_status
			check_state_bt
			sleep $automatic_interval_check
		done
	fi

}

function change_preset {

	runstate=`mysql -N -B -u$zmdbuser -p$zmdbpass zm -e "select Name  from States where isActive=1 LIMIT 1;"`

	if ! [ ${#monitor_ptz[@]} -eq 0 ] && [ ! -z "$armed_preset" ] && [ ! -z "$disarmed_preset" ]; then
		if [ "$runstate" == "$day_state" ]; then
			for i in ${monitor_ptz[@]}; do /usr/bin/zmcontrol.pl --id=$i --command=presetGoto --preset=$disarmed_preset; done
		elif [ "$runstate" == "$night_state" ]; then
			for i in ${monitor_ptz[@]}; do /usr/bin/zmcontrol.pl --id=$i --command=presetGoto --preset=$disarmed_preset; done
		elif [ "$runstate" == "$armed_state" ]; then
			for i in ${monitor_ptz[@]}; do /usr/bin/zmcontrol.pl --id=$i --command=presetGoto --preset=$armed_preset; done
		fi
	fi
}

function calibrate {
	
	if ! [ ${#monitor_id[@]} -eq 0 ]; then
		for i in ${monitor_id[@]}; do /usr/bin/zmcontrol.pl --id=$i --command=presetHome; done
	fi

}

function reset {

	if ! [ ${#monitor_id[@]} -eq 0 ]; then
		for i in ${monitor_id[@]}; do /usr/bin/zmcontrol.pl --id=$i --command=reset; done
	fi
	
}


[email protected]

Di seguito il codice del suo servizio, zm_auto.service:

[Unit]
After=network.target mysql.service zoneminder.service
Wants=mysql.service zoneminder.service

[Service]
Type=simple
ExecStart=/usr/bin/zm_auto.sh auto_check_state_bt

[Install]
WantedBy=multi-user.target

Qui i passi da seguire per installare zm_auto.sh ed il suo servizio:

nano /usr/bin/zm_auto.sh
chmod +x zm_auto.sh
nano /etc/systemd/system/zm_auto.service
systemctl daemon-reload
systemctl enable zm_auto.service
systemctl start zm_auto.service

Per muovere automaticamente le telecamere ptz al cambio di stato è necessario editare zmpkg.pl:

nano /usr/bin/zmpkg.pl

aggiungendo:

system("bash zm_auto.sh change_preset");

prima della riga:

$command = "restart";

Section: