Wake Display on Alarm (and launch montage view)

Add any particular hints or tricks you have found to help with your ZoneMinder experience.
Post Reply
vtdstein95
Posts: 2
Joined: Sun Jan 24, 2016 3:13 pm

Wake Display on Alarm (and launch montage view)

Post by vtdstein95 » Sun Jan 24, 2016 5:17 pm

Goal: Have a Display turn on when there is an alarm. Also save power and display backlight life by allowing the display to go to sleep when there are no alarms. I spent the last month working through this and think I have a fairly stable but more complex than expected solution.

System is Ubuntu 14.04 server w/GUI and ZM 1.28.1

1) Setting up a filter to run a command when there is a new alarm
There are many suggestions I found but the only one I could get to work was to use event archiving. I wish there was another way as this method eliminates the use of archiving to save important events (unless you have unlimited storage).
- Create a new filter. I named mine TurnDisplayOn
- Add triggers
-- Alarm Frames, Greater than or equal to, 5 (adjust as needed for your system)
-- and, Archive Status equal to Unarchived Only
- Sort by, Date/Time, Desc, Limit to first 10 (adjust as needed) results only
- Archive all matches, check
- Execute command on all matches:

Code: Select all

/bin/sh /home/www-data/zmDOA-ssh.sh
If you are running the PurgeWhenFull filter, you need to modify the filter and remove the 'Unarchived Only' trigger.

2) Getting zmDOA-ssh.sh to execute
This was a challenge. I could never get the www-data to have enough permission to get the display to turn on (lots of visudo experiments). In the end I found leaving the browser running for extended times on the montage view resulted in browser freezes, so needed more permission to close/restart the browsers anyway. Solution was creating a script which will kick off commands (via ssh) as another Ubuntu user.

I am sure there is a much more elegant solution. I also believe there is a significant security exposure with this method, however in my case the system use is local only with no internet access. So intrusion requires physical access at which point the hacker owns the system anyway. No data of value on this system anyway. They'd just take the hardware for the hardware value.

The ZoneMinder user on Ubuntu is www-data. This is not a full user, so getting commands to execute is challenging.
a) Create the www-data user directory, open a terminal

Code: Select all

$ sudo mkdir /home/www-data
b) Create the zmDOA-ssh.sh script

Code: Select all

sudo nano /home/www-data/zmDOA-ssh.sh
Paste this into and save. Replace yourUbuntuUser with your default Ubuntu user account. Note www-data is not your default user account. Also this could be used to wake the display on a remote system if so desired by replacing localhost with another system's address and altering a couple of the other instructions below.

Code: Select all

#!/bin/sh
/usr/bin/ssh yourUbuntuUser@localhost /bin/sh /home/yourUbuntuUser/zmDispOnAlarm.sh
c) Change ownership of everything to www-data

Code: Select all

$ sudo chown -R www-data:www-data /home/www-data
d) Allow www-data to ssh
This is where the security exposure comes in. There must be a secure way to do this, but I really did not need to find one.
- As your ubuntu default user ssh into your system.

Code: Select all

$ ssh localhost
- Accept the ECDSA key fingerprint and exit

Code: Select all

$ exit
- Copy the key to a location where the www-data user can find it. I had to put it in the /var/www-data directory.

Code: Select all

 $ sudo cp /home/yourUbuntuUser/.ssh /var/www 
- Change ownership of the .ssh directory and key

Code: Select all

 $ sudo chown -R www-data:www-data /var/www/.ssh
- Allow login with no password

Code: Select all

$ nano /home/yourUbuntuUser/.bash_profile
- Paste in and save
### START-Keychain ###
# Let re-use ssh-agent and/or gpg-agent between logins
/usr/bin/keychain $HOME/.ssh/id_rsa
source $HOME/.keychain/$HOSTNAME-sh
### End-Keychain ###
[/code]

3) Install some tools which will be needed.

Code: Select all

 $ sudo apt-get install xdotool
 $ sudo apt-get install wmctrl 
4) Create the script to wake the display. Note I use firefox as I have found it requires less CPU in the montage view. Chromium can be used as well but may require more adjustment than just replacing 'firefox' with 'chromium-browser'.

Code: Select all

 $ cd ~
 $ nano zmDispOnAlarm.sh
- Paste this in and save

Code: Select all

#!/bin/sh
# Check if firefox is already running (could be due to a recent alarm) if not start firefox
if ps ax |grep -v grep | grep firefox > /dev/null
then
echo firefox already running > /dev/null
else
env DISPLAY=:0 firefox &
fi
# Wake the display. I have found that the export command will wake the display all by itself in some cases.
export DISPLAY=:0
# To make sure the display is awake, move the mouse a pixel and back
/usr/bin/xdotool mousemove_relative 1 1
sleep 1
#/usr/bin/xdotool mousemove 0 480
/usr/bin/xdotool mousemove_relative -1 -1
# Uncomment items below if you wish to log when this sript is called
#TIMESTAMP=`/bin/date "+%Y-%m-%d %H:%M:%S"`
#MONITOR=`/usr/bin/xset -display :0 -q|grep "Monitor is"`
#echo "$TIMESTAMP: $MONITOR"  >> /~/zmDispOnAlarm.log
5) Create the firefox timeout script to gracefully close firefox when the display is off. I found this was required as running the browser for extended time in montage mode would result in a browser freeze (same for chromium). Aslo by closing the browser when the monitor is off a little more power is saved as the CPU is not working as much.

Code: Select all

 $ cd ~
 $ nano ff_to.sh
-Paste this in and save

Code: Select all

#!/bin/sh
#grab the current display status
MONITOR=`/usr/bin/xset -display :0 -q|grep "Monitor is"`

#If display is off close firefox
case  $MONITOR in *Off*)
 if ps ax |grep -v grep | grep firefox > /dev/null
 then
        env DISPLAY=:0 wmctrl -c firefox
 fi
esac
6) Have the ff_to.sh script run every 5 minutes. I left the Ubuntu defaults for Display turn off. This way if there is no alarm for 5 minutes the Display will turn off and within 5 minutes after the display turns off the browser will be closed.

Code: Select all

$ crontab -e
-Paste this in at the last line, replace yourUbuntuUser as appropriate, save

Code: Select all

0,5,10,15,20,25,30,35,40,45,50,55 * * * * sh /home/yourUbuntuUser/ff_to.sh
7) Configure firefox to launch the montage page when started.
- In preferences set show my homepage when firefox starts
- Use the following as your homepage replacing zmuser and zmpassw0rd as appropriate

Code: Select all

http://localhost/zm/?action=login&view=montage&group=0&username=zmuser&password=zmpassw0rd
Good luck, hope it works as well for you as it does for me.

GDixon
Posts: 9
Joined: Sun Jan 31, 2016 11:40 pm

Re: Wake Display on Alarm (and launch montage view)

Post by GDixon » Sun Jan 31, 2016 11:44 pm

Hi,

another simple way to turn on a sleeping monitor.

Code: Select all

xset dpms force on
I like what you have done.

I have a similar goal, I want to force the monitor on but have the montage pop up in front of the lock screen but also have it so no one can use the browser except to see the montage as it is in alarm or recording and then all go back to sleep after.

I don't want anyone to have desktop access or browser access.

EDIT: to force a screen back to sleep

Code: Select all

xset dpms force off

mrLitter
Posts: 5
Joined: Sat May 14, 2016 7:56 pm

Re: Wake Display on Alarm (and launch montage view)

Post by mrLitter » Mon Nov 06, 2017 11:07 pm

Hi,

I have a similar scenario working on raspberrypi (Raspbian Jessie) using midori as a browser.
For that it was sufficient to
- sudo chmod u+s /bin/fgconsole and sudo chmod u+s /bin/chvt to make both files always to be executed as root
- modify /home/pi/.config/lxsession/LXDE-pi/autostart with a line
@xhost +

Then I can use this script from https://gist.github.com/AGWA/9874925 to turn on and off the hdmi screen.
Maybe that also works on Ubuntu.

However I would like to use luakit as a browser now and this requires far more permissions than midori - at least that's what I understand. So I'm facing the same problem as you. I'm just curious: Did you find another solution rather than ssh-ing into the localhost?

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests