Many
MythTV users are concerned with saving power. There are lots of ways to reduce the total power consumption of your
MythTV backend server(s), including
ACPI Wakeup,
MythWelcome and others. The trouble is,
MythWeb requires a connection to the
MythTV master backend server to function properly.
Rather than choosing to run a
MythTV master backend server 24/7 for access to
MythWeb, I put together the MythWeb WOL/Redirector that is meant to be run from a web server that is more likely to already be on 24/7.
The Process
- When called from any web browser, the PHP page attempts to make a basic TCP connection to your MythWeb URL.
- If that TCP connection fails, a WOL MagicPacket(tm) is sent to your MythTV master backend server.
- After a short delay, only as long as you define for your MythTV master backend server to start up the webserver process, you are redirected to the "real" MythWeb URL.
Source
<?php
/* MythTV - MythWeb WOL/Redirector Configuration Parameters
*
* $url: the complete URL to your MythWeb host/location
* 'https://example.com/mythweb"'
* $mac: the MAC address of the MythWeb host
* 'XX:XX:XX:XX:XX:XX'
* $bcast: the IP broadcast address to which to send the Wake
* On LAN MagicPacket(tm)
* '192.168.1.255'
* $refresh: the number of seconds it takes for your MythWeb
* host to wakeup and start the webserver process after
* receiving a Wake on LAN packet
* '120' (2 mintes)
*/
$url = 'https://example.com/mythweb';
$mac = 'XX:XX:XX:XX:XX:XX';
$bcast = '192.168.1.255';
$refresh = '120';
function Connect($url) {
if (strpos($url, 'https', 0) === 0) {
$port = '443';
} else {
$port = '80';
}
$host = parse_url($url, PHP_URL_HOST);
$nic = @fsockopen('tcp://'.$host, $port, $errno, $errstr, 5);
if ($nic == FALSE) {
return "DOWN";
}
fclose($nic);
return "UP";
}
function WakeOnLan($mac, $bcast) {
$packet = '';
$mac_addr = '';
for($i = 0; $i < 6; $i++) $packet .= chr(255);
$mac_byte = explode(':', $mac);
for($i = 0; $i < 6; $i++) $mac_addr .= chr(hexdec($mac_byte[$i]));
for($i = 0; $i < 16; $i++) $packet .= $mac_addr;
$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
if ($sock == FALSE) {
return FALSE;
} else {
socket_set_option($sock, SOL_SOCKET, SO_BROADCAST, TRUE);
socket_sendto($sock, $packet, strlen($packet), 0, $bcast, 40000);
socket_close($sock);
return TRUE;
}
}
if (Connect($url) == "UP") {
header("Location: $url");
exit();
} else {
?>
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>MythTV - MythWeb WOL/Redirector</title>
<meta http-equiv="content-type" content="application/xhtml+xml; charset=utf-8" />
<?php echo " <meta http-equiv=\"refresh\" content=\"$refresh\" />\n" ?>
</head>
<body>
<?php
if (WakeOnLan($mac, $bcast) == TRUE) {
echo "<p>Please wait while I wake the MythWeb server at <a href=\"$url\">$url</a>. You will be redirected in $refresh seconds.</p>\n";
} else {
echo "<p>There was a problem attempting to wake the MythWeb Server at <a href=\"$url\">$url</a>.</p>\n";
}
?>
</body>