[WIP] Myth Suspend Alarm
#1
I wanted to post this now as I lost the script once when my hard drive crashed unexpectedly (I know, I know...)

Script:
Myth Suspend Alarm

Download: https://github.com/elParaguayo/xbmc-myth...all/master

What it does: Sets an ACPI alarm based on the next scheduled MythTV recording and suspends the machine. I don't use the MythTV frontend, nor do I use MythWelcome. I needed a script that not only checked what MythTV was doing, but also whether XBMC was active.

Who might want to use it: Anyone running a MythTV backend on the same machine as the XBMC frontend (may just be me...).

Legal bit: I make no guarantees that this script won't harm your system, so if it does, don't come running to me as I accept no liability. It works for me. That's all I can guarantee.

Screenshot:
Image

How it works:
The script performs a series of tests and, if all are passed, sets an ACPI and suspends/hibernates the machine.

The tests are as follows:
  • Get MythTV backend status and check if recording/transcoding/running user jobs etc
  • Check if XBMC has any active players
  • Check if there are any active sessions e.g. ssh

Tested on: Dharma

How to use:
(I'll be honest, there's a bit of effort needed on the user's part! But the idea is once it's working, you can forget all about it.)

Unzip the script folder to your addons folder.

There are 2 bash scripts which need to be configured so the script can use them:

setwakealarm: this sets the ACPI alarm. It needs to be executable and, more importantly, able to run as sudo without a password (so you'll need to edit the sudoers file to do this).

mythshutdownstatus: this script outputs the status code (to stdout) of the "mythshutdown -s" command. Again, make sure this script is executable.

Ok, that's the easy bit.

You now need to figure out what your motherboard needs to do to use the ACPI alarm. This is out of the scope of my plugin, but I found the steps on this page http://www.mythtv.org/wiki/ACPI_Wakeup very helpful.

The key thing for the script is to know whether the ACPI alarm needed is a UTC time, or an incremental time (e.g. +600 for an alarm 10 minutes from now). You may need to experiment on the command line first.

NB: you should also test whether your backend is able to record after waking from suspend (see here for my fix: http://ubuntuforums.org/showpost.php?p=1...stcount=18)

You may also want to consider setting up your system to wake from your remote.

Once you've got ACPI suspend/wake working outside of xbmc you can configure the script.

Restart xbmc. The script should appear in addons menu.

The first time you use it, you must configure the script first - the important settings are the paths to the two scripts above and the mythtv database settings.

Once that's done you can try running the script.


Advanced use:


The script can be run as a cron job e.g. by putting the following line in your crontab file:
Code:
0 * * * * xbmc-send -a "RunScript(script.mythsuspendalarm, cron)"
Image

This is version 0.1 so there are bound to be bugs, so all comments bug reports are welcome. However, I'm not a python developer (this is the first python code I ever wrote) so I don't guarantee I can fix the problems!!
BBC Live Football Scores: Football scores notifications service.
script.squeezeinfo: shows what's playing on your Logitech Media Server
Reply
#2
To do:
  • Tidy up skin settings
  • Add exception handling
  • Make logo!
  • Save recording details as strings callable by the skin
  • Add "Lock/Unlock" button to Mythweb
  • Put source on github DONE
BBC Live Football Scores: Football scores notifications service.
script.squeezeinfo: shows what's playing on your Logitech Media Server
Reply
#3
MythTV supports the ability to automatically run a script to check whether it is OK to automatically shutdown. Below is the script I have configured MythTV to use on my HTPC that runs XBMC Media Center. No cron job needed because mythbackend deals with running the script. The mythshutdown script provided by MythTV does all the hard work with some other checks for other things running that should prevent the box from shutting down. I have configured XMBC Media Center to automatically quit after 20 minutes of inactivity. XBMC is started up when our bluetooth Nintendo Wii remote connects to the Event Cient Server.

Code:
#!/bin/bash
mythshutdown --check > /dev/null
if [ "$?" -ne "0" ]; then
        mythshutdown --status > /dev/null
        echo "Cannot shutdown as mythbackend is not idle: status = '`echo $?`'"
# TODO: List out the appropriate error message.
#-s/--status         (returns a code indicating the current status)
#         0 - Idle
#         1 - Transcoding
#         2 - Commercial Flagging
#         4 - Grabbing EPG data
#         8 - Recording - only valid if flag is 1
#        16 - Locked
#        32 - Jobs running or pending
#        48 - Possibly 16 + 32
#        64 - In a daily wakeup/shutdown period
#       128 - Less than 15 minutes to next wakeup period
#       255 - Setup is running
        exit 1
fi

if [ "`pidof -s xbmc.bin`" ]; then
        echo "Cannot shutdown as XBMC Media Center is running"
        exit 1
fi

if [ "`pidof -s firefox-bin`" ]; then
        echo "Cannot shutdown as Firefox is running"
        exit 1
fi

if [ "`pidof -s mousepad`" ]; then
        echo "Cannot shutdown as Mousepad is running"
        exit 1
fi

if [ "`pidof -s transmission`" ]; then
        echo "Cannot shutdown as Transmission is running"
        exit 1
fi

if [ "`pidof -s xfce4-terminal`" ]; then
        echo "Cannot shutdown as Terminal is running"
        exit 1
fi

if [ "`pidof -s synaptic`" ]; then
        echo "Cannot shutdown as Synaptic package manager is running"
        exit 1
fi

if [ "`pidof -s xfs_fsr`" ]; then
        echo "Cannot shutdown as the XFS defragmentation tool is running"
        exit 1
fi

if [ "`pidof -s -x rdiff-backup`" ]; then
        echo "Cannot shutdown as rdiff-backup is running"
        exit 1
fi

if [ "`users`" != "xbmc" ]; then
        echo "Cannot shutdown the machine as there are users other than xbmc: '`users`'"
        exit 1
fi

if [ "`net status shares | grep movies`" ]; then
        echo "Cannot shutdown as a samba share is being used"
        exit 1
fi

if [ "`netstat | grep nfs`" ]; then
        echo "Cannot shutdown as an NFS share is being used"
        exit 1
fi

exit 0

Below is the script I use to set the wakealarm. Took ages to sort this out due to some weird motherboard behavior. This script will be different depending on what your motherboard and BIOS support. This script also called automatically by MythTV once it is OK to automatically shut down the machine.

Code:
#!/bin/sh
# The first argument must be the startup time in seconds since the epoch.
# This is defined in mythtv-setup using "time_t" as the format.

echo "Setting wakealarm to: `date -d "@$1"`"
NOW=`date +%s`
SECS=$(( $1 - $NOW ))                           # Subtract the current time from the needed startup time

echo 0 > /sys/class/rtc/rtc0/wakealarm          # Clears the alarm
echo +$SECS > /sys/class/rtc/rtc0/wakealarm     # Writes the alarm using the relative format
cat /proc/driver/rtc                            # Log the information after setting the wakealarm to help with debugging
Use MythTV for recording TV? Try the integrated MythTV support in XBMC Media Center. Now with commercial skip support built-in and integration with the Movie database!
Reply
#4
That's quite a bit neater than my solution!

However, I think I still need to use the JSON RPC call to check active players. My machine boots straight into XBMC session so xbmc.bin will always be running, but it may not be active.

Having this as a script in XBMC means I can also run it whenever I want with a click of the remote.

May not be pretty, but a good learning experience for me if nothing else!
BBC Live Football Scores: Football scores notifications service.
script.squeezeinfo: shows what's playing on your Logitech Media Server
Reply
#5
el_Paraguayo Wrote:That's quite a bit neater than my solution!

However, I think I still need to use the JSON RPC call to check active players. My machine boots straight into XBMC session so xbmc.bin will always be running, but it may not be active.

Having this as a script in XBMC means I can also run it whenever I want with a click of the remote.

May not be pretty, but a good learning experience for me if nothing else!

My machine opens XBMC once it boots as well. I found using the existing functionality in XBMC that closes XBMC when it's been idle for a period the easiest way to get everything to work. I had to do some tweaks to the WiiRemote event client to re-launch XBMC if the Wii Remote connected again.

Didn't know you could check to see if there was an active player using JSON.
Use MythTV for recording TV? Try the integrated MythTV support in XBMC Media Center. Now with commercial skip support built-in and integration with the Movie database!
Reply

Logout Mark Read Team Forum Stats Members Help
[WIP] Myth Suspend Alarm0