Kodi Community Forum

Full Version: Preventing suspend when client connected tvheadend
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have xbmc which runs tvheadend set to suspend when not active for 30 mins or so, it seems to be recording aware ie it wont suspend if something is recording however it does not appear to be client aware , so if I am watchin tv on another machine the the tvh/xbmc machine suspends anyway and I get cut off?

Any one dealt with this issue?

I see there is an addon that will send continous wol packet but I would prefer it could be something native.
Not an elegant solution, but perhaps you can disable the Shutdown function timer on your XBMC system before accessing it from your client system? Maybe write a script that toggles the Shutdown function timer and assign it to a remote control button or have the client system launch a script that sends the command to toggle off the timer when accessing the XBMC system to play a recording and, when done playing recordings, send another command to restore the timer setting?
Much for my own future reference as well as an answer, I created a script 94_pvr-activity.sh in /etc/pm/sleep.d/ using various source and example found online.

It checks if a current recording is taking place, if any clients are connected to the web interface and checks for any none localhost clients connected.
Works great on XBMC and tvheadend running on one machine with other XBMC clients on other machines.[/code]

Code:
#!/bin/bash
#
# modyfy if different location for tvheadend dvr/log path
cd ~hts/.hts/tvheadend/dvr/log

######################

start_date=0
stop_date=0
recording=0
pingcount=0
webclients=0
clients=0
# wait for at least 60 seconds to let the calling recording expire
#sleep 60

current_date=`date +%s`
for i in $( ls ); do
tmp_start=`cat $i | grep '"start":' | cut -f 2 -d " " | cut -f 1 -d ","`
tmp_stop=`cat $i | grep '"stop":' | cut -f 2 -d " " | cut -f 1 -d ","`

# check for any current recording
if [ $((tmp_stop)) -gt $((current_date)) -a $((tmp_start)) -lt $((current_date)) ]; then
recording=1
fi
done

#if [ $((recording)) -ne 0 ]; then
# echo "Recording in progress"
#fi

established='ESTABLISHED'
webclients=$(netstat -n -A inet | grep ':9981' | awk -F '     ' '{print $5}' | grep --count $established)

#echo $webclients
#if [ $webclients -ne 0 ]; then
# echo 'Web clients connected'
#fi

clients=$(netstat -n -A inet | grep :9982 | awk -F ' ' '{print $4}' | awk -F ':' '{print $1}' | grep -v -c '127.0.0.1')

#if [ $clients -ne 0 ]; then
# echo 'Clients connected'
#fi

# do the final check, no pingcount and no recording means we can suspend
#
if [ $((pingcount)) -eq 0 -a $((recording)) -eq 0 -a $((webclients)) -eq 0 -a $((clients)) -eq 0 ]; then
echo "No Clients and NO recording in progress, so going can SUSPEND"
exit 0
fi
#pvr is busy, cancel suspend
exit 1
Good stuff, I found similar online resources see my earlier thread:
http://forum.xbmc.org/showthread.php?tid=144091
but I am running this as a cron job, does the suspend abort for you when running yours as a suspend script in sleep.d, and does xbmc shutdown timer then reset?

I am running this suspend sleep.d script so that my machine wakes up to record scheduled recordings.
https://www.lonelycoder.com/redmine/proj...iki/Wakeup

I hope to do away with my cronjob with the good work cube is doing here:

http://forum.xbmc.org/showthread.php?tid=143774
(2012-12-12, 17:15)bilbonvidia Wrote: [ -> ]does the suspend abort for you when running yours as a suspend script in sleep.d

Yes, the suspend aborts, I would assume the timer would restart. I get a brief flick on the xbmc display when it tries to suspend and it is aborted.

The plugin looks interesting, might have a look at modifying it. I have since found out about http://{server}:9981/status.xml where {server} is your tvheadend ip or host.

<currentload>
<systemload>1.780000,1.530000,1.560000</systemload>
<recordings></recordings>
<subscriptions>1</subscriptions>
</currentload>

so should be a simple task to check the xml file for recordings/subs.
(2012-12-13, 13:53)jamiejones85 Wrote: [ -> ]The plugin looks interesting, might have a look at modifying it. I have since found out about http://{server}:9981/status.xml where {server} is your tvheadend ip or host.

<currentload>
<systemload>1.780000,1.530000,1.560000</systemload>
<recordings></recordings>
<subscriptions>1</subscriptions>
</currentload>

so should be a simple task to check the xml file for recordings/subs.

It's all done. Look here.

_BJ1

I am trying it out as a suspend script now and it seems to work however I noticed there is a delay on resume until the system becomes responsive which is down to the netstat command running so I have now included the susend | hibernate resume | thaw tags so it only does the netstat on the suspend and not the resume. So far so good.

AFAIK xbmc is recording aware so will not sleep / suspend while there is an active recording (unless you tell it to with the remote). This script is checking for 3 or more connection on port 9982, the only way so far I could get it to tell if there was a network client connected to tvheadend other than the local machine, if no client it then checks the hts dvr log for scheduled recordings and sets an RTC wake alarm accordingly an the system will wake itself to do the recording. and then of course xbmc will suspend after the recording is over and xbmc is idle hopefully. This is far better than a cron job I had. Hopefully it will work okay.

Code:
#!/bin/bash
#
# set ACPI Wakeup alarm
# safe_margin - minutes to start up system before the earliest timer
# script does not check if recording is in progress
#
#
case "$1" in
suspend|hibernate)
if [ `netstat -t | grep -c ":9982"` -ge 3 ]
then
    exit 1
fi
echo 1 > /timer

# bootup system 60 sec. before timer
safe_margin=60

# modyfy if different location for tvheadend dvr/log path
cd ~billy/.hts/tvheadend/dvr/log

######################

start_date=0
stop_date=0

current_date=`date +%s`

for i in $( ls ); do
tmp_start=`cat $i | grep '"start":' | cut -f 2 -d " " | cut -f 1 -d ","`
tmp_stop=`cat $i | grep '"stop":' | cut -f 2 -d " " | cut -f 1 -d ","`

# check for outdated timer
if [ $((tmp_stop)) -gt $((current_date)) -a $((tmp_start)) -gt $((current_date)) ]; then

# take lower value (tmp_start or start_date)
if [ $((start_date)) -eq 0 -o $((tmp_start)) -lt $((start_date)) ]; then
start_date=$tmp_start
stop_date=$tmp_stop
fi
fi
done

wake_date=$((start_date-safe_margin))

echo $start_date >> /timer
echo $wake_date >> /timer

# set up waleup alarm
if [ $((start_date)) -ne 0 ]; then
echo 2 >> /timer
echo 0 > /sys/class/rtc/rtc0/wakealarm
echo $wake_date > /sys/class/rtc/rtc0/wakealarm
fi
;;
resume|thaw)
;;
esac

sticking this in would abort a manual suspend if there is a current recording:

for i in $( ls ); do
tmp_start=`cat $i | grep '"start":' | cut -f 2 -d " " | cut -f 1 -d ","`
tmp_stop=`cat $i | grep '"stop":' | cut -f 2 -d " " | cut -f 1 -d ","`

# check for any current recording
if [ $((tmp_stop)) -gt $((current_date)) -a $((tmp_start)) -lt $((current_date)) ]; then
exit 1

but no nice osd like BJ1's cool add on.

I guess a variation of the above could be used as a shutdown script as well, not sure where to put it in xbmcbuntu tho.