I was wondering whether anyone had a script that would indicate whether the station was online or offline. I have tried writing my own based on comparing the current time with the time tag in the clientraw file, but my php coding is not good enough. I know its possible, just don’t know how.
Hi Paul,
I check the file modified time of my clientraw file and if it is over a certain amount of time (5 minutes) I display an appropriate “no data since etc etc” message where the time of last update is on my webpage.
e.g.
Have just created a page www.paulwilman.com/includes/online_status.php to record whether station is on or offline. Tried to include it in my footer but didn’t work for some reason. Not one to solve on a Friday night!
I use a script based off of the clientraw that says this after 5 minutes of no activity… We are currently fixing technical difficulties, or the power is out. Past data will be updated once Live updates resume.
But I do also like the button showing online/offline.
I use a script by Mike Challis which runs off the clientraw file and displays: We are currently experiencing technical difficulties.
Live updates will be restored as soon as possible.
Create a new file called “live-update.php” and add the below script into it.
Then add a standard php include where you want the text to appear.
<?php include("live-update.php"); ?>
<?php
// version 1.3 - 14-Dec-2008 added make sure full clientraw.txt is read
// version 1.2 - 14-Dec-2008 added setting for non US date format in clientraw.txt
// version 1.1 - 14-Dec-2008 added better format of offline time
// Live Update Error script for WD
// Mike Challis (http://www.642weather.com/) and
// Ken True (http://saratoga-weather.org/)
//
global $SITE;
// begin settings for Live Updates Error
// uncomment only one $lu_crfile setting below
$lu_crfile = './clientraw.txt'; // to set manually (set to relative file path)
//
$lu_refreshSecs = 240; // set to max seconds of age for clientraw.txt
$lu_test_the_warning = false; // true or false
$lu_check_filedata = true; // check the file internal timestamp
$lu_non_us_date = true; // clientraw.txt with non en_US date format (d/m/y)
//
$lu_timezone = 'Europe/London'; // set to YOUR timezone
//
// end settings Live Updates Error
//
// --- begin overrides from Settings.php if availablSettings.php if availablee
if (isset($SITE['tz'])) {$lu_timezone = $SITE['tz'];}
if (isset($site['clientrawfile'])) {$lu_crfile = $SITE['clientrawfile']; }
// --- end overrides from Settings.php
if (isset($_REQUEST['sce']) && ( strtolower($_REQUEST['sce']) == 'view' or
strtolower($_REQUEST['sce']) == 'show') ) {
//--self downloader --
$filenameReal = __FILE__;
$download_size = filesize($filenameReal);
header('Pragma: public');
header('Cache-Control: private');
header('Cache-Control: no-cache, must-revalidate');
header("Content-type: text/plain");
header("Accept-Ranges: bytes");
header("Content-Length: $download_size");
header('Connection: close');
readfile($filenameReal);
exit;
}
if(isset($_REQUEST['testwarn'])) {
$lu_test_the_warning = true;
}
$lu_error = 0;
if (isset($lu_crfile)) {
if (!file_exists($lu_crfile) ) { // missing CR
print "<p class=\"advisoryBox2\">Live Updates Error: Data file not found.\n";
print "<!-- $lu_crfile -->\n";
$lu_error = 1;
} else {
$lu_now = time();
$lu_crfile_time = filemtime($lu_crfile);
$lu_crfile_age = $lu_now - $lu_crfile_time;
$crage = 0;
if ($lu_check_filedata) {
putenv("TZ=$lu_timezone");
$crdata = explode(' ',file_get_contents($lu_crfile)); // fetch clientraw.txt
// make sure full clientraw.txt is read by looking for '12345' at start and '!!' at end of record
if (isset($crdata[0]) && $crdata[0] == '12345' && isset($crdata[174])) {
$crtime = explode('-',$crdata[32]); // get time from last field in name-time entry
$crtime = $crtime[count($crtime)-1]; // just the last entry
$crtime = preg_replace('|_|is',' ',$crtime);
$crdate = $crdata[74]; // updated date in CR
if ($lu_non_us_date) {
$cr_date = explode("/",$crdate);
$crdate = strftime("%m/%d/%Y",mktime(0,0,0,$cr_date[1],$cr_date[0],$cr_date[2]));
}
$crtimezone = date("T",time());
$crdatetime = strtotime("$crdate $crtime $crtimezone");
$crage = abs($lu_now - $crdatetime);
print "<!-- Data file '$crdate $crtime $crtimezone' is $crage secs old -->\n";
} else {
print "<!-- Data file was blank or not complete -->\n";
}
}
print "<!-- $lu_crfile file age = $lu_crfile_age secs. -->\n";
$lu_crfile_age = max($lu_crfile_age,$crage); // use the older of the two
if ($lu_test_the_warning) { // show test message
print "<p class=\"advisoryBox2\">Showing Test Live Updates Error because \$lu_test_the_warning is turned on.\n";
$lu_error = 1;
}
else if ($lu_crfile_age > $lu_refreshSecs) { // stale CR
print '<p class="advisoryBox2">Live Updates Error: Data file has not been updated for '.time_offline($lu_crfile_age) .".\n";
$lu_error = 1;
}
}
}else{
print "<p class=\"advisoryBox2\">Live Updates Config Error: \$lu_crfile not properly configured.\n";
$lu_error = 1;
}
$lu_error and print "
We are currently experiencing technical difficulties.
Live updates will be restored as soon as possible.</p>\n";
function time_offline ($time) {
// added by mchallis 642weather.com
// takes a time diff like 6740 secs and formats to '1 hour, 52 minutes'
$hrs = (int) intval($time / 3600);
$time = (int) intval($time - (3600 * $hrs));
$mns = (int) intval($time / 60);
$time = (int) intval($time - (60 * $mns));
$secs = (int) intval($time / 1);
$string = '';
$hrs == 1 and $string .= "$hrs hour, ";
$hrs > 1 and $string .= "$hrs hours, ";
$string .= sprintf("%01d minutes", $mns);
return $string;
}
?>
What Budgie said…it works beautifully with no intervention. My test site on this computer hasn’t had updated data files in a long time, and I’m used to seeing the error message there. When it appears on my web site I know there’s a problem somewhere.